Sumon Bappi
Sumon Bappi

Reputation: 2019

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

> [email protected] 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

> [email protected] 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"
  }
}

Upvotes: 2

Views: 1609

Answers (3)

Tholle
Tholle

Reputation: 112787

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;

Upvotes: 1

enapupe
enapupe

Reputation: 17019

Because you are based on an old article that didn't specify the webpack version, it seems you are running a newer version of webpack against an old codebase with previous version webpack config. You have 2 choices:

  • rollback to the correct webpack version, the one which will work with your webpack config and not ask you to install the CLI.
  • use latest webpack version, install the CLI and anything else that's necessary AND update your webpack configuration to their latest API.

I also suggest you look for a better updated post.

Upvotes: 1

AhmedMoawad
AhmedMoawad

Reputation: 91

I think you should install webpack-cli: npm i -D webpack-cli

Upvotes: 0

Related Questions