gpt4 book ai didi

reactjs - 从node_modules导入模块(create-react-app)

转载 作者:行者123 更新时间:2023-12-03 13:50:56 25 4
gpt4 key购买 nike

如何从 src 目录外部导入模块?

./src/App.js
Module not found: Can't resolve 'material-ui' in '/Users/suvo/GitHub/portfolio/src'

根据react-material-icons页面,我应该按如下方式导入:

import mui from 'material-ui';

这个错误很奇怪。我在 import mui... 旁边得到了 import $ from 'jquery'; 并且它工作正常。更重要的是,如果我创建一个新项目并添加React-material-icons,npm 无法启动,显示错误:

> myapp@0.1.0 start /Users/suvo/GitHub/myapp
> react-scripts start

sh: react-scripts: command not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! myapp@0.1.0 start: `react-scripts start`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the myapp@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /Users/suvo/.npm/_logs/2017-09-11T11_40_15_791Z-debug.log

最佳答案

只需删除包名称前面的.即可。

import mui from 'material-ui';

使用 npm installyarn install 安装的包将保存到 node_modules 目录中。您只需指定包名称即可导入它们。

import React from 'react';
import mui from 'material-ui';

import AlarmIcon from 'react-material-icons/icons/action/alarm';

export default class Alarm extends React.Component {
render() {
return (
<AlarmIcon/>
);
}
}

关于reactjs - 从node_modules导入模块(create-react-app),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46104720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com