0
我剛剛開始學習React,並使用Webpack實現它。Webpack未能編譯
下面是我package.json
的devDependencies
:
"devDependencies": {
"babel-core": "^6.25.0",
"babel-loader": "^7.1.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"react-hot-loader": "^1.3.1",
"react-scripts": "1.0.7",
"serve": "^5.2.4",
"webpack": "^3.0.0",
"webpack-dev-server": "^2.5.0"
},
這裏是我的webpack.config.js
:
module.exports = {
context: __dirname + "/src",
entry: './index.js',
output: {
filename: "index.js",
path: __dirname + '/dist'
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loaders: ["react-hot-loader", "babel-loader"],
}
],
},
}
下面是.babelrc
{
"presets": [
"es2015",
"react"
]
}
現在,當我運行npm start
它提出這個電子rror
ERROR in ./src/index.css
Module parse failed: /home/tps/ReactApps/login-app/src/index.css Unexpected token (1:5)
You may need an appropriate loader to handle this file type.
| body {
| margin: 0;
| padding: 0;
@ ./src/index.js 21:0-22
@ multi ./node_modules/webpack-dev-server/client?http://localhost:8081 webpack/hot/dev-server ./index.js
ERROR in ./src/App.js
Module not found: Error: Can't resolve './notFound' in '/home/tps/ReactApps/login-app/src'
@ ./src/App.js 33:16-37
@ ./src/index.js
@ multi ./node_modules/webpack-dev-server/client?http://localhost:8081 webpack/hot/dev-server ./index.js
ERROR in ./src/App.css
Module parse failed: /home/tps/ReactApps/login-app/src/App.css Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
| .App {
| text-align: center;
| }
@ ./src/App.js 19:0-20
@ ./src/index.js
@ multi ./node_modules/webpack-dev-server/client?http://localhost:8081 webpack/hot/dev-server ./index.js
請幫我解決這個問題。
編輯1:
webpack.config.js
module.exports = {
context: __dirname + "/src",
entry: './index.js',
output: {
filename: "index.js",
path: __dirname + '/dist'
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loaders: ["react-hot-loader", "babel-loader"],
}
],
rules: [
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
}
]
},
}
package.json
"devDependencies": {
"babel-core": "^6.25.0",
"babel-loader": "^7.1.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"css-loader": "^0.28.4",
"react-hot-loader": "^1.3.1",
"react-scripts": "1.0.7",
"serve": "^5.2.4",
"style-loader": "^0.18.2",
"webpack": "^3.0.0",
"webpack-dev-server": "^2.5.0"
},
給我錯誤 '''ERROR在./src/index.js 模塊解析失敗:/家庭/ TPS/ReactApps /login-app/src/index.js意外的令牌(7:16) 您可能需要一個合適的加載程序來處理此文件類型。 |導入'./index.css'; | | ReactDOM.render( ,document.getElementById('root')); | registerServiceWorker(); | @ multi./node_modules/webpack-dev-server/client?http://localhost:8081 webpack/hot/dev-server ./index.js ''' –
ashvin
在index.js裏面你正在導入一個css文件並且webpack無法處理它,這就是錯誤的含義。 –
你安裝了css loader嗎?用新的webpack.config.js和新的package.json更新你的問題 –