Process.env.npm_config_argv value 3 parameters remain, cooked, original what meaning

When using Webpack for packaging, the judgment process.env.npm_config_argvvalue is usually to determine the packaging behavior according to the command line parameters. process.env.npm_config_argvIt is an environment variable that saves the currently running npm command and its parameters.

Specifically, process.env.npm_config_argvthe value of is a JSON string containing the details of the command-line arguments. By parsing the string, we can get the specific values ​​of the current npm command and its parameters.


In process.env.npm_config_argv: There are 3 attributes, remain, cooked, and original. What is the difference (meaning) between these three attributes?

remain : This is an array containing the remaining parameters after the npm command. For example, when you execute the npm run build --mode production command, the remain array will be an empty array [] because no additional arguments are passed to build. If the command you execute is npm run build – --watch, then the remain array will be ['--watch'] because --watch is a parameter passed to build.

cooked : This is also an array containing escaped command line arguments. It preserves the order and structure of arguments passed to npm commands. Using the example above, the cooked array would be ['run', 'build', '–mode', 'production'], where each parameter is represented as a string.

original : Also an array containing the unprocessed original command line parameters. This means it preserves the order of the arguments passed to the npm command, but does not do any escaping. For the example above, the original array would be ['run', 'build', '--mode', 'production'], the same as the cooked array.

By parsing and using these parameters, you can configure and adjust your packaging process according to your needs, such as setting different build modes, environment variables or other configuration options according to the specified parameters.


For example, if you executed the following command on the command line:

例1:npm run build --mode production

例2:npm run serve --test one

Then the value of process.env.npm_config_argv will be the following string:

例1结果:'{"remain":[],"cooked":["run","build","--mode","production"],"original":["run","build","--mode","production"]}'

例2结果:'{"remain":["one"],"cooked":["run","dev","--test","one"],"original":["run","dev","--test","one"]}'

In summary, using this information, we can configure Webpack's packaging behavior according to different command line parameters. For example, the packaging mode (development mode or production mode) can be determined according to the --mode parameter, and then different Webpack configuration files or optimization options can be selected.


It should be noted that it process.env.npm_config_argvis an environment variable specific to npm. If you use other tools or methods to perform Webpack packaging, you may not be able to obtain this variable. Therefore, in actual development, other methods can also be used to obtain command line parameters, such as using process.argvto obtain the command line parameters of the Node.js process.

Guess you like

Origin blog.csdn.net/weixin_52443895/article/details/131973126