Search code examples
node.jsreactjswebpackwebpack-dev-serverwebpack-4

npm start is giving webpack cli error with React.js


I have just started learning react.js from this site tutorailspoint and React.js.

After following all the setup information when I run this command npm start this give the error below:

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)

After installing

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

My package.json after installing:

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

Solution

  • The error you get tells you that "The CLI moved into a separate package: webpack-cli.". It seems like the tutorial you are following is from when the webpack package came with the CLI as well. You have to install that separately with newer versions.

    npm i -D webpack-cli
    

    You second error comes from that Webpack used to have the loaders defined in configuration.module.loaders, but it has since changed to 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;