?yb? 2012-07-20 14:02 采纳率: 100%
浏览 333
已采纳

向 npm 脚本发送命令行参数

The scripts portion of my package.json currently looks like this:

"scripts": {
    "start": "node ./script.js server"
}

...which means I can run npm start to start the server. So far so good.

However, I would like to be able to run something like npm start 8080 and have the argument(s) passed to script.js (e.g. npm start 8080 => node ./script.js server 8080). Is this possible?

转载于:https://stackoverflow.com/questions/11580961/sending-command-line-arguments-to-npm-script

  • 写回答

10条回答 默认 最新

  • 妄徒之命 2013-01-18 17:20
    关注

    Edit 2014.10.30: It's possible to pass args to npm run as of npm 2.0.0

    The syntax is as follows:

    npm run <command> [-- <args>]

    Note the necessary --. It is needed to separate the params passed to npm command itself and params passed to your script.

    So if you have in package.json

    "scripts": {
        "grunt": "grunt",
        "server": "node server.js"
    }
    

    Then the following commands would be equivalent:

    grunt task:target => npm run grunt -- task:target

    node server.js --port=1337 => npm run server -- --port=1337

    To get the parameter value, see this question. For reading named parameters, it's probably best to use a parsing library like yargs or minimist; nodejs exposes process.argv globally, containing command line parameter values, but this is a low-level API (whitespace-separated array of strings, as provided by the operating system to the node executable).


    Edit 2013.10.03: It's not currently possible directly. But there's a related GitHub issue opened on npm to implement the behavior you're asking for. Seems the consensus is to have this implemented, but it depends on another issue being solved before.


    Original answer: As a some kind of workaround (though not very handy), you can do as follows:

    Say your package name from package.json is myPackage and you have also

    "scripts": {
        "start": "node ./script.js server"
    }
    

    Then add in package.json:

    "config": {
        "myPort": "8080"
    }
    

    And in your script.js:

    // defaulting to 8080 in case if script invoked not via "npm run-script" but directly
    var port = process.env.npm_package_config_myPort || 8080
    

    That way, by default npm start will use 8080. You can however configure it (the value will be stored by npm in its internal storage):

    npm config set myPackage:myPort 9090
    

    Then, when invoking npm start, 9090 will be used (the default from package.json gets overridden).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(9条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)