We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
console.log(process.argv);
在终端输入 node shell.js
[ '/Users/wangyaxing/.nvm/versions/node/v10.7.0/bin/node', '/Users/wangyaxing/temp/vue-project/shell.js' ]
在终端输入 node shell.js abc
[ '/Users/wangyaxing/.nvm/versions/node/v10.7.0/bin/node', '/Users/wangyaxing/temp/vue-project/shell.js', 'abc' ]
process.argv的用法是第一个是node文件, 第二个是脚本文件, 第三个是参数
npm 允许在package.json文件里面,使用scripts字段定义脚本命令。
{ // ... "scripts": { "build": "node build.js" } }
上面代码是package.json文件的一个片段,里面的scripts字段是一个对象。它的每一个属性,对应一段脚本。比如,build命令对应的脚本是node build.js。
执行npm run ,会自动创建一个shell, 在这个shell里面执行指定的脚本命令。 比较特别的是, 这个shell 会自动将当前目录下的node_modules/.bin子目录加入PATH,执行结束, 再将PATH变量恢复原样
PATH
由于 npm 脚本就是 Shell 脚本,因为可以使用 Shell 通配符。
"lint": "jshint *.js" "lint": "jshint **/*.js"
上面代码中,*表示任意文件名,**表示任意一层子目录。 如果要将通配符传入原始命令,防止被 Shell 转义,要将星号转义。
"test": "tap test/\*.js"
将命令行参数发送到npm脚本:
npm run [command] [-- <args>]
注意必要的--,需要将参数传递到npm命令本身,并将其传递给脚本。
--
plugins: [ new webpack.DefinePlugin({ 'domain': process.argv[2] }), }
npm scripts 使用指南 如何向npm脚本发送命令行参数? 编译环境中的几种传参方法
The text was updated successfully, but these errors were encountered:
No branches or pull requests
node中可以通过process.argv来获取参数
在终端输入 node shell.js
在终端输入 node shell.js abc
process.argv的用法是第一个是node文件, 第二个是脚本文件, 第三个是参数
npm scripts(npm脚本)发送命令行参数
什么是npm scripts
npm 允许在package.json文件里面,使用scripts字段定义脚本命令。
上面代码是package.json文件的一个片段,里面的scripts字段是一个对象。它的每一个属性,对应一段脚本。比如,build命令对应的脚本是node build.js。
原理
执行npm run ,会自动创建一个shell, 在这个shell里面执行指定的脚本命令。
比较特别的是, 这个shell 会自动将当前目录下的node_modules/.bin子目录加入
PATH
,执行结束, 再将PATH
变量恢复原样通配符
由于 npm 脚本就是 Shell 脚本,因为可以使用 Shell 通配符。
上面代码中,*表示任意文件名,**表示任意一层子目录。
如果要将通配符传入原始命令,防止被 Shell 转义,要将星号转义。
传参
将命令行参数发送到npm脚本:
注意必要的
--
,需要将参数传递到npm命令本身,并将其传递给脚本。使用webpack.DefinePlugin在打包时对文件中的变量进行替换
参考
npm scripts 使用指南
如何向npm脚本发送命令行参数?
编译环境中的几种传参方法
The text was updated successfully, but these errors were encountered: