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

时间:2017-09-07 20:36:34

标签: reactjs import create-react-app

如何从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 $ 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 个答案:

答案 0 :(得分: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/>
    );
  }
}