I have just started to learn React and I'm implementing it with Webpack.
Here are the devDependencies
from my 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",
"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"
},
Here is my 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"],
}
],
},
}
Here is .babelrc
{
"presets": [
"es2015",
"react"
]
}
Now when I run npm start
it raises this error
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
Please help me to solve this.
Edit 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"
},
You need to add a css-loader to your webpack config in order to be able to import css files.
npm install --save-dev css-loader style-loader
and add the following to theloaders
array in your webpack config:
{
test: /\.css$/,
loader: 'style-loader!css-loader'
}
Check this for more info: https://github.com/webpack-contrib/css-loader.
Edit: You're using both loaders and rules. They're basically the same thing. Check this for more info: https://webpack.js.org/guides/migrating/#module-loaders-is-now-module-rules
Try this webpack config:
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"],
},
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
}
]
},
}