2017-09-07 85 views
0

如何從src目錄之外導入模塊?從node_modules導入模塊(create-react-app)

./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...旁邊,它工作正常。更重要的是,如果我創建一個新項目,並添加 反應材料,圖標,故宮將無法啓動,顯示錯誤:

> [email protected] 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! [email protected] start: `react-scripts start` 
npm ERR! spawn ENOENT 
npm ERR! 
npm ERR! Failed at the [email protected] 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/> 
    ); 
    } 
} 
+0

還是我得到錯誤: 模塊未發現:在「/用戶/ suvo/GitHub上/組合/ src目錄」 – DuchSuvaa

+0

有不知道在那裏我有這點從無法解析「材料UI」,我更新與實際錯誤 – DuchSuvaa

+0

問題你可以嘗試'紗安裝react-material-icons'或'npm install --save react-material-icons'再試一次嗎? –