webpack Cannot find module 'webpack/schemas/WebpackOptions.json'

After installing the new version of webpack4 recently, execute webpack to report an error, as shown in the figure


At that time, I was stunned. According to the directory, I also found the webpack/schemas/WebpackOptions.json file.

Later, I looked at the documentation of the webpack4 version: as follows

Create project

Just create a new folder webpacklx on a disk to store our project. 
Then on the command line, switch to webpacklx

npm init

Here, a lot of options are required to be set, which can be configured according to the project situation or simply press Enter without filling in. When it was done, we found that there were files added to the package.jsonfolder, which were used to hold information about the project.

Install webpack-cli globally

webpack4 version, you need to install webpack-cli globally

npm install -g webpack-cli

set mode

There are two modes of production and development that can be set by default, so we try to set to development mode and enter on the command line
webpack --mode development

Create entry file

webpack4.x uses the project root directory './src'as the entry to create srca folder, in fact, webpack4.xas './src/index.js'the entry, so we are creating the index.js file, and then executing

webpack --mode development

This makes it possible to './src/index.js'package into './dist/main.js'

However, it is very troublesome to enter this command every time. We package.jsonadd scriptstwo members to:

"dev":"webpack --mode development",
 "build":"webpack --mode production"

In the future, we only need to execute on the command line , npm run devwhich is equivalent to execution webpack --mode development, and execution npm run buildis equivalent to execution webpack --mode production.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324820057&siteId=291194637