将Webpack 4用于React项目时模块解析失败

时间:2018-11-28 14:34:20

标签: reactjs webpack babel webpack-4 babel-loader

我无法在Webpack 4中配置我的react项目。

我正在这里打印文件内容:

package.json(脚本部分):

    {
  "name": "todo",
  "version": "1.0.0",
  "description": "a ToDo app built using react and webpack",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack --mode development ./src/index.js --output ./dist/main.js",
    "build": "webpack --mode production ./src/index.js --output ./dist/main.js"
  },
  "author": "themaskedbit",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^8.0.4",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "webpack": "^4.26.1",
    "webpack-cli": "^3.1.2"
  },
  "dependencies": {
    "react": "^16.6.3",
    "react-dom": "^16.6.3"
  }
}

babelrc:

{
    "presets": [
        "@babel/preset-env", "@babel/preset-react"
    ]
}

webpack.config.js:

module.exports = {
    module: {
      rules: [
        {
          test: /\.js$/,
          exclude: /node_modules/,
          use: {
            loader: "babel-loader"
          }
        }
      ]
    }
  };

index.js:

import data from "./data/items";

import React from 'react';
import {render} from 'react-dom';

import ListItem from "./components/listItem";

render(
    <ListItem items={data}/>,
    document.getElementById("app")
);

在使用上述配置的同时,我试图使用开发脚本运行webpack。我收到以下错误:

$ npm run dev

> todo@1.0.0 dev C:\Users\rejir\Documents\HP works\ToDo
> webpack --mode development ./src/index.js --output ./dist/main.js

Hash: 0fac7774f9aa4bb8aeed
Version: webpack 4.26.1
Time: 234ms
Built at: 2018-11-28 19:50:35
  Asset      Size  Chunks             Chunk Names
main.js  4.01 KiB    main  [emitted]  main
Entrypoint main = main.js
[./src/index.js] 227 bytes {main} [built] [failed] [1 error]

ERROR in ./src/index.js 9:4
Module parse failed: Unexpected token (9:4)
You may need an appropriate loader to handle this file type.
|
| render(
>     <ListItem items={data}/>,
|     document.getElementById("app")
| );
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! todo@1.0.0 dev: `webpack --mode development ./src/index.js --output ./dist/main.js`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the todo@1.0.0 dev 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!     C:\Users\rejir\AppData\Roaming\npm-cache\_logs\2018-11-28T14_20_35_305Z-debug.log

请帮助我进行webpack配置。 我的理解是我以错误的方式包括了babel-loader。搜索stackoverflow答案并没有为我提供解决问题的线索。如果有其他开发人员可以帮助我解决此问题,对我来说将是有益的。

谢谢。

1 个答案:

答案 0 :(得分:1)

您需要在运行webpack命令时传递配置文件

"dev": "webpack --mode development ./src/index.js --output ./dist/main.js --config webpack.config.js",
"build": "webpack --mode production ./src/index.js --output ./dist/main.js --config webpack.config.js"

此处有更多信息-use-custom-configuration-file