从node_modules导入模块(create-react-app) [英] Import module from node_modules (create-react-app)

查看:386
本文介绍了从node_modules导入模块(create-react-app)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I import modules from outside src directory?

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

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

According to react-material-icons page, I was supposed to import as follows:

import mui from 'material-ui';

错误很奇怪。我从 jquery中获取了 import $; 紧挨 import mui ... ,而且效果很好。此外,如果我创建一个新项目并添加
react-material-icons,则npm将不会启动,并显示错误:

the error's strange. i got import $ from 'jquery'; just next to import mui... and it works fine. What's more, if i create a new project and add react-material-icons, the npm won't start, showing an error:

> 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


推荐答案

只需删除软件包名称前面的

Just remove the . in front of the package name.

import mui from 'material-ui';

使用 npm install 纱线安装保存到 node_modules 目录。您只需指定软件包名称即可导入它们。

Packages you install using npm install or yarn install are saved to node_modules directory. You can import them by just specifying package name.

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

这篇关于从node_modules导入模块(create-react-app)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆