从 node_modules (create-react-app) 导入module

IT技术 reactjs import create-react-app
2021-05-02 13:30:42

如何从外部 src 目录导入module?

./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 $ from 'jquery';在旁边import mui...,它工作正常。更重要的是,如果我创建一个新项目并添加 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
1个回答

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

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/>
    );
  }
}