In the previous article, we talked about some common fields in package.json
the file , so in this article, we will learn about the fieldspackage.json
in the file and other knowledge, and go directly to the topic.script
npm
npm script guide
npm
Scripts are a set of built-in scripts and custom scripts defined package.json
in , whose templates provide a simple command to perform repetitive or complex tasks, such as:
- start a
react
project ; - Use
webpack
to package the project; - etc;
Each script command has a life cycle, what to do before execution, what to do during execution, and what to do after execution. For example, add two script commands in script
the field which are prestart
and respectively poststart
. The specific code is as follows:
"scripts": {"prestart": "echo 我首先执行","start": "node index.js","poststart": "echo 我最后执行"},
Execute in the terminal npm start
, and then view the console, there will be the following output:
npm
You can also use npm_package_字段名称
to get package.json
the information in , you need to execute script
the script , for example package.json
, there are the following definitions in :
"scripts": {"start": "node index.js"},
In index.js
the file there is the following code:
console.log(process.env.npm_package_version);
npm start
After executing in the terminal , it is found package.json
that the version information in is output. Here process.env
is package.json
the field value obtained through the environment variable.
You can also add more fields before executing commands in the terminal, for example:
npm start moment supper
You can use process.argv
to get the parameters passed in, and the following output will be output by printing:
Starting from the second value of the array is the additional parameter we pass in.
Some projects may need to execute multiple tasks at the same time when starting, and the execution sequence of multiple tasks determines the performance of the project.
For serial execution , the next task can only be executed after the previous task is executed successfully, and &&
the symbol to connect, for example:
"scripts": {"start": "node index.js && node test.js"},
In the execution of serial commands, as long as one execution fails, the entire script ends. In the picture below, the first command fails, and there will be no output afterwards:
The difference from the entire execution method above is parallel execution , that is, multiple commands can be selected and executed at the same time, using &
symbols to connect:
"scripts": {"start": "node index.js & node test.js"},
Here, if one of them reports an error, the rest can still be executed normally. The specific effect is as follows:
package-lock.json
When we run npm install
the command , we will find that a new file is automatically generated package-lock.json
. When the file npm
is modified node_modules
tree or package.json
file , the file will be automatically modified. If it is manually modified, it will npm install
be modified in the next execution.
package-lock.json
Files are intended to be committed to codebases, such GitHub
as , for various purposes:
- Describe a single concrete representation of the dependency tree to ensure that exactly the same dependencies are installed when the team develops and deploys;
- Provide users with a tool
time-travel
to get to the previous state, which meansnode_modules
what version you used in before. When you deletenode_modules
the file and share the code with others,npm install
the version of the package they downloaded through is also the same; - Optimize the installation speed, if there is a package with the same version, it will be skipped to improve the speed;
package-lock.json
The generation logic of can be explained by referring to the following example. For example, moment
in , the dependency package is installed X
, X
and the dependency package depends on the package Y
, and Y
depends on Z
. Please refer to the following code for specific dependencies:
// project moment
{ "name": "moment", "dependencies": { "X": "^1.0.0" }}
// package X
{ "name": "X", "version": "1.0.0", "dependencies": { "Y": "^1.0.0" }}
// package B
{ "name": "Y", "version": "1.0.0", "dependencies": { "Z": "^1.0.0" }}
// package Z
{ "name": "Z", "version": "1.0.0" }
In this case package-lock.json
the file would generate something like:
// package-lock.json
{ "name": "moment","version": "1.0.0","dependencies": {"X": { "version": "1.0.0" },"Y": { "version": "1.0.0" },"Z": { "version": "1.0.0" }}
}
In our later development, if we do not modify package.json
the file , then package-lock.json
the file will never be regenerated, even if X
the package releases a new version, although we package.json
write in the file ^1.0.0
, but because of the existence of package-lock.json
the file , the execution npm install
will not automatically upgrade.
In general, package.json
the file is to specify the range that the dependent package can use, and package-lock.json
the file is to use a specific value in this range.
at last
A front-end information package is prepared for everyone. Contains 54, 2.57G front-end related e-books, "Front-end Interview Collection (with answers and analysis)", difficult and key knowledge video tutorials (full set).
Friends in need, you can click the card below to receive and share for free