Author: Feng Weiyao
Making: tank0317
NPM run script-name is a familiar script that executes the configuration of the scripts object in package.json. But here’s what you might not know.
Npm-scirpt in the following is the script command configured in package.json scripts. Name-scirpt specifies a script command named name.
Lifecycle scripts/custom scripts
When we use the command NPM start, NPM will try to execute the start script command configured in package.json scripts. The default configuration of start-script is “start”: “node server.js”. So if there is a server.js file in the project root directory, NPM start will run the code in server.js directly.
In addition to start-script, when using the NPM start command, NPM will also try to find whether the prestart, poststart script commands are configured in package.json scripts. If all are configured, NPM executes the scripts in the following order.
- npm run prestart
- npm run start
- npm run poststart
Similarly, NPM test, NPM restart, and NPM stop execute the scripts configured in scripts in the preceding manner. NPM also identifies the current phase (the so-called lifecycle) through the NPM_lifecycle_event environment variable. For example, npM_lifecycle_event has the value “prestart” in the prestart-script script execution phase, and the value “start” in the start-script phase, This is the name of the script configured for the package.json scripts object.
The above is the script execution logic corresponding to NPM built-in commands. The above logic also applies to custom scripts that we are most familiar with. For example, we configure “build”: “Webpack –mode=production”, with prebuild and postbuild scripts configured, when using NPM run build, Prebuild-script, build-script, and postbuild-script are executed in the same order.
Any script
The script commands we configure, such as “start”: “node server.js”, are interpreted and executed as a line of code passed to the system SHELL. The actual SHELL used varies depending on the system platform. On unix-like systems, such as macOS or Linux, the SHELL is /bin/sh, while on Windows, the SHELL is cmd.exe.
Since it is up to the SHELL to interpret the execution, the script that explains the configuration can be any command that can be run in the SHELL, not just node scripts or JS programs. That is, if you have Python installed on your system (or if the system variable PATH contains Python commands), you can also set scripts to “myscript”: “python xxx.py”.
The environment variable
As mentioned above, NPM sets an environment variable npM_lifecycle_event when using the NPM run script-name command. In fact, NPM also sets many environment variables. You can see all the environment variables that NPM sets for the script to run by using the built-in command NPM run env. All fields set in package.json are set to environment variables starting with NpM_package_. If your packge.json Settings are as follows
{
"name": "npm-demo"."version": "1.0.0"."script": {
"build": "webpack --mode=production"
},
"files": ["src"]}Copy the code
Npm_package_name, NPM_Package_version, NPM_Package_script_build, nPM_package_files_0 and other variables can be obtained. Note that each field in the package.json object and array above has a corresponding environment variable.
Not only package.json, but also all configurations associated with NPM will have an environment variable starting with NPM_config_.
Also, note that even if the NPM run command is used in a subdirectory, the script will be run in the root directory of the project. If you want to distinguish where the NPM run command is used, use the INIT_CWD environment variable, which holds the absolute path to the directory where the NPM run command is run.
How are these environment variables used? If your script is a shell script, you can get the value directly from the corresponding environment variable name. If you are a Node script, you can get the value from the global variable process.env in nodejs. For example, to get the project version number, use process.env.npm_package_version.
PATH
As mentioned above, several environment variables are set before nPM-script is executed. One of the most important environment variables is PATH. NPM adds the absolute PATH of the project node_modules/.bin to the environment variable PATH. So we can use some of the command-line tools installed locally in the project in NPM-script. As in the build script set above: “build”: “webpack –mode=production”
As long as we have Webpack installed locally, we can see the WebPack executable in the node_modules/.bin path of our project. And because node_modules/.bin has been added to PATH, the script can find the webpack command in PATH and execute it smoothly.
Finally, why can the executable be found in node_modules/.bin after webpack is installed? You can view docs.npmjs.com/files/packa…
Reference
Docs.npmjs.com/cli/run-scr…
Docs.npmjs.com/misc/script…
Docs.npmjs.com/files/packa…