1. ホーム
  2. reactjs

[解決済み] Webpack のコンパイルに失敗した

2022-02-11 01:59:01

質問

Reactを学び始めたばかりで、Webpackで実装しています。

以下は devDependencies から、私の 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"
},

以下は私の 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 このようなエラーが発生します。

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"
},

解決方法は?

cssファイルをインポートできるようにするために、webpackの設定にcss-loaderを追加する必要があります。

npm install --save-dev css-loader style-loader

に以下を追加してください。 loaders の配列を、webpack の設定に追加します。

{
  test: /\.css$/,
  loader: 'style-loader!css-loader'
}

詳しくはこちらをご確認ください。 https://github.com/webpack-contrib/css-loader .

編集部:ローダーとルールを使い分けているんですね。これらは基本的に同じものです。 詳しくはこちらをご覧ください。 https://webpack.js.org/guides/migrating/#module-loaders-is-now-module-rules

このwebpackの設定を試してみてください。

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' ]
      }
    ]
  },
}