将命令行参数发送到 npm 脚本 [英] Sending command line arguments to npm script

查看:40
本文介绍了将命令行参数发送到 npm 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 package.jsonscripts 部分目前看起来像这样:

"scripts": {开始":节点 ./script.js 服务器"}

...这意味着我可以运行 npm start 来启动服务器.到目前为止一切顺利.

但是,我希望能够运行类似 npm start 8080 的东西,并将参数传递给 script.js(例如 npm开始 8080 => node ./script.js 服务器 8080).这可能吗?

解决方案

npm 2 及更新版本

可以将参数传递给npm run,因为npm 2 (2014).语法如下:

npm run <命令>[-- ]

注意 -- 分隔符,用于分隔传递给 npm 命令本身的参数和传递给脚本的参数.

package.json为例:

 脚本":{咕噜":咕噜",服务器":节点 server.js"}

以下是将参数传递给这些脚本的方法:

npm run grunt -- task:target//调用`grunt task:target`npm run server -- --port=1337//调用`node server.js --port=1337`

注意:如果您的参数不是以 --- 开头,则具有明确的 -- 不需要分隔符;但为了清楚起见,最好还是这样做.

npm run grunt task:target//调用`grunt task:target`

注意下面的行为差异(test.jsconsole.log(process.argv)):以 --- 被传递给 npm 而不是脚本,并在那里被默默吞噬.

$ npm run test foobar['C:\Program Files\nodejs\node.exe', 'C:\git\myrepo\test.js', 'foobar']$ npm run test -foobar['C:\Program Files\nodejs\node.exe', 'C:\git\myrepo\test.js']$ npm run test --foobar['C:\Program Files\nodejs\node.exe', 'C:\git\myrepo\test.js']$ npm run test -- foobar['C:\Program Files\nodejs\node.exe', 'C:\git\myrepo\test.js', 'foobar']$ npm run test -- -foobar['C:\Program Files\nodejs\node.exe', 'C:\git\myrepo\test.js', '-foobar']$ npm run test -- --foobar['C:\Program Files\nodejs\node.exe', 'C:\git\myrepo\test.js', '--foobar']

当你使用一个参数npm实际使用时,区别会更明显:

$ npm test --help//这是伪装的`npm --help test`npm 测试 [-- <args>]别名:tst, t

要获取参数值,请查看此问题.要读取命名参数,最好使用解析库,例如 yargsminimist;nodejs 全局公开 process.argv,包含命令行参数值,但这是一个低级 API(以空格分隔的字符串数组,由操作系统提供给节点可执行文件).>


Edit 2013.10.03:目前无法直接使用.但是在 npm 上打开了一个相关的 GitHub 问题来实现你要求的行为.似乎共识是实现这一点,但这取决于之前解决的另一个问题.


原始答案 (2013.01): 作为某种解决方法(虽然不是很方便),您可以执行以下操作:

package.json 说你的包名是 myPackage 你也有

脚本":{开始":节点 ./script.js 服务器";}

然后在package.json中加入:

配置":{我的端口":8080"}

在你的 script.js 中:

//如果脚本不是通过npm run-script"调用的,则默认为 8080;但直接var port = process.env.npm_package_config_myPort ||8080

这样,默认情况下 npm start 将使用 8080.但是您可以配置它(该值将由 npm 存储在其内部存储中):

npm config set myPackage:myPort 9090

然后,当调用 npm start 时,将使用 9090(package.json 中的默认值被覆盖).

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?

解决方案

npm 2 and newer

It's possible to pass args to npm run since npm 2 (2014). The syntax is as follows:

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

Note the -- separator, used to separate the params passed to npm command itself, and the params passed to your script.

With the example package.json:

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

here's how to pass the params to those scripts:

npm run grunt -- task:target  // invokes `grunt task:target`
npm run server -- --port=1337 // invokes `node server.js --port=1337`

Note: If your param does not start with - or --, then having an explicit -- separator is not needed; but it's better to do it anyway for clarity.

npm run grunt task:target     // invokes `grunt task:target`

Note below the difference in behavior (test.js has console.log(process.argv)): the params which start with - or -- are passed to npm and not to the script, and are silently swallowed there.

$ npm run test foobar
['C:\Program Files\nodejs\node.exe', 'C:\git\myrepo\test.js',  'foobar']

$ npm run test -foobar
['C:\Program Files\nodejs\node.exe', 'C:\git\myrepo\test.js']

$ npm run test --foobar
['C:\Program Files\nodejs\node.exe', 'C:\git\myrepo\test.js']

$ npm run test -- foobar
['C:\Program Files\nodejs\node.exe', 'C:\git\myrepo\test.js', 'foobar']

$ npm run test -- -foobar
['C:\Program Files\nodejs\node.exe', 'C:\git\myrepo\test.js', '-foobar']

$ npm run test -- --foobar
['C:\Program Files\nodejs\node.exe', 'C:\git\myrepo\test.js', '--foobar']

The difference is clearer when you use a param actually used by npm:

$ npm test --help      // this is disguised `npm --help test`
npm test [-- <args>]

aliases: tst, t

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 (2013.01): 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).

这篇关于将命令行参数发送到 npm 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆