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.json
folder, 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 linewebpack --mode development
Create entry file
webpack4.x uses the project root directory './src'
as the entry to create src
a folder, in fact, webpack4.x
as './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.json
add scripts
two members to:
"dev":"webpack --mode development",
"build":"webpack --mode production"
In the future, we only need to execute on the command line , npm run dev
which is equivalent to execution webpack --mode development
, and execution npm run build
is equivalent to execution webpack --mode production
.