使用npm命令启动带参数的NodeJS项目

我正在尝试创build两个单独的npm命令来在开发机器上和生产模式下本地启动我的NodeJS项目。 我希望能够将参数分别传递给机器以提供正确的依赖关系 – 可以是生产环境中的CDN或本地机器。

这是我在package.json中看到的

"run": "node ./server/app.js", /* for running locally*/ "start": "node ./server/app.js", /* for running in production*/ 

如果我尝试调用npm run – 会产生这个错误:

 npm ERR! npm run-script [<pkg>] <command> npm ERR! not ok code 0 

我也希望能够发送包含URL的命令行参数。

你做得不对

要调用自定义脚本,您必须运行

 npm run-script run 

你的package.json应该有:

 "scripts": { "start": "node ./server/app.js", "run": "node ./server/app.js" } 

请参阅: https : //npmjs.org/doc/cli/npm-run-script.html

你可以使用这个bash脚本直接运行任何(甚至是嵌套的)模块。 不要忘记将~/Develop/node_modules为本地path。

 #!/bin/bash if [ $# -eq 0 ]; then echo Usage: npmrun module [args] exit fi data=~/bin/npmrun.data if [ -f $data ]; then source $data else declare -A where fi cd ~/Develop/node_modules if [ -z ${where[$1]} ]; then path=$(find -path '*/bin/'$1) if [ -z $path ]; then echo "Module \"$1\" not founded!" exit else where[$1]=$path; declare -p where > $data fi fi ${where[$1]} ${@:2}