Principle of NPM Run
Each time NPM run is executed, a new shell will be created, and the script will be executed within the shell. Therefore, the script must not only be a Node script, but must be executed within the shell.
Scripts module can be used to run our own scripts, for example
scripts:{
"test":"node .. /test.js"
}
Use NPM run test to execute the script,
NPM run adds the node_modules/. Bin subdirectory of the current directory to the PATH variable. After execution, the PATH variable is restored, so we can also directly execute the local installation dependency command. NPM run provides the node_modules/.bin PATH for scripts, and any binaries that are locally installed can be used without the node_modules/.bin prefix.
Where do the executables under node_modules/.bin come from?
NPM is a package management tool installed with Node.js. It is used for node.js package publishing, dissemination, and dependency control. Behind NPM is a Couchdb-based database, which records the detailed information of each package, including author, version, dependency, and authorization information. One of its important functions is to free developers from the tedious work of package management (versions, dependencies, etc.) and focus more on the development of features.
The package.json file of NPM records the details of the downloaded packages, and the node_modules file is where the installation packages are stored.
In node_modules, you can see the packsge.json file that every uploaded package has, which contains a number of configuration parameters, including bin, such as Lint-staged
At installation time, NPM will symlink the file to prefix/bin for global installation or /node_modules/.bin/ local installation.
After installation, we can find a file named Lint-staged under node_modules/.bin, as shown below:
#! What is /usr/bin/env/node? Why is the first line of the upload package script this line?
Specifies the interpreter to execute the script.
#! Called Shebang, it often appears on the first line of a script on unix-like systems, as the first two characters that identify the interpreter executing the script file. If the #! The subsequent interpreter is an executable, so when the script executes, it passes the name of the file and its arguments to the interpreter for execution. If the #! The specified interpreter does not exist, an error is reported, and shell execution is used if there is no permission. /usr/bin/env is used to tell the user to look for node in the path directory, #! The /usr/bin/env node allows the system to dynamically look up nodes to solve the problem of different user Settings on different machines. The scripts we wrote in the project need to use Node XXX in scripts to implement the call, NPM run to implement the temporary shell creation, find node in the environment variable to execute.
NPM script exit code!! Because scripts run in the shell, you must follow the rules of shell scripting. If the exit code is not 0, NPM considers the script failed, as shown:
Running command cases in development:
Husky and Lint-staged implementation constraint code specifications can be seen as follows:
{
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": { "*.js": "eslint --fix" }
}
Copy the code
Git’s hook functions (misnomer) are used to invoke lint-staged commands to implement constraints.
NPM automatically creates a link based on bin in package.json to generate a script file that can be directly called by /node_modules/.bin/.
Order of execution of NPM
If you need to execute multiple tasks in an NPM script, you need to know in what order they should be executed. If the execution is parallel (that is, parallel execution at the same time), you can use the ampersand symbol. $NPM run script1.js & NPM run script2.js for secondary execution (i.e., the next task is executed only if the previous task succeeds), use the && symbol. $NPM run script1.js && NPM run script2.js are Bash functions.