npm start通过react js给webpack cli错误

时间:2018-08-07 19:01:35

标签: node.js reactjs webpack webpack-dev-server webpack-4

我刚刚开始从该网站tutorailspoint + react.js学习react.js 在我运行此命令npm start后遵循所有设置信息后,将出现以下错误。我已经为之扫了一眼,但束手无策。有人可以帮忙吗?错误如下:

C:\Users\USER\Desktop\reactapp>npm start

> reactapp@0.0.1 start C:\Users\USER\Desktop\reactapp
> webpack-dev-server.cmd --content-base app

The CLI moved into a separate package: webpack-cli.
Please install 'webpack-cli' in addition to webpack itself to use the CLI.
-> When using npm: npm install webpack-cli -D
-> When using yarn: yarn add webpack-cli -D
module.js:442
    throw err;
    ^

Error: Cannot find module 'webpack-cli/bin/config-yargs'
    at Function.Module._resolveFilename (module.js:440:15)
    at Function.Module._load (module.js:388:25)
    at Module.require (module.js:468:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\Users\USER\Desktop\reactapp\node_modules\webpack-dev-server\bin\webpack-dev-server.js:65:1)

安装后

C:\Users\USER\Desktop\reactapp>npm start

> reactapp@0.0.1 start C:\Users\USER\Desktop\reactapp
> webpack-dev-server --hot

× 「wds」: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration.module has an unknown property 'loaders'. These properties are valid:
   object { exprContextCritical?, exprContextRecursive?, exprContextRegExp?, exprContextRequest?, noParse?, rules?, defaultRules?, unknownContextCritical?, unknownContextRecursive?, unknownContextRegExp?, unknownContextRequest?, unsafeCache?, wrappedContextCritical?, wrappedContextRecursive?, wrappedContextRegExp?, strictExportPresence?, strictThisContextOnImports? }
   -> Options affecting the normal modules (`NormalModuleFactory`).

npm ERR! Windows_NT 10.0.17134

我的package.json安装后>>

 {
  "name": "reactapp",
  "version": "0.0.1",
  "description": "first test",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --hot"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^16.4.2",
    "react-dom": "^16.4.2",
    "webpack": "^4.16.5",
    "webpack-dev-server": "^3.1.5"
  },
  "devDependencies": {
    "webpack-cli": "^3.1.0"
  }
}

3 个答案:

答案 0 :(得分:1)

由于您所基于的是未指定Webpack版本的旧文章,因此您似乎正在使用旧版本的webpack配置在旧代码库上运行新版本的webpack。 您有2个选择:

  • 回滚到正确的Webpack版本,该版本将与您的Webpack配置一起使用,而不要求您安装CLI。
  • 使用最新版的webpack,安装CLI和其他必要的工具,并将您的webpack配置更新为最新的API。

我还建议您寻找更新的帖子。

答案 1 :(得分:1)

您收到的错误告诉您“ CLI移至单独的程序包:webpack-cli。”。看来您正在遵循的教程也是webpack软件包也随CLI一起提供的。您必须单独安装较新的版本。

npm i -D webpack-cli

您的第二个错误是由于该Webpack曾经在configuration.module.loaders中定义了加载程序,但此后已更改为configuration.module.rules

webpack.config.js

var config = {
  // ...
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: "babel-loader",
        query: {
          presets: ["es2015", "react"]
        }
      }
    ]
  }
};

module.exports = config;

答案 2 :(得分:0)

我认为您应该安装webpack-cli: npm i -D webpack-cli