将命令行参数发送到npm脚本

本文翻译自:Sending command line arguments to npm script

The scripts portion of my package.json currently looks like this: 我的package.jsonscripts部分当前如下所示:

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

...which means I can run npm start to start the server. ...这意味着我可以运行npm start来启动服务器。 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 (eg npm start 8080 => node ./script.js server 8080 ). 但是,我希望能够运行npm start 8080类的东西,并将参数传递给script.js (例如npm start 8080 => node ./script.js server 8080 )。 Is this possible? 这可能吗?


#1楼

参考:https://stackoom.com/question/majh/将命令行参数发送到npm脚本


#2楼

Edit 2014.10.30: It's possible to pass args to npm run as of npm 2.0.0 编辑2014.10.30: 从npm 2.0.0开始可以将args传递给npm run

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. 需要将传递给npm命令本身的参数和传递给脚本的参数分开。

So if you have in package.json 所以如果你在package.json

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

Then the following commands would be equivalent: 那么以下命令将是等效的:

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

node server.js --port=1337 => npm run server -- --port=1337 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 ; 为了读取命名参数,最好使用yargsminimist之类的解析库; 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). nodejs全局公开process.argv ,其中包含命令行参数值,但这是一个低级API(由空格分隔的字符串数组,由操作系统提供给节点可执行文件)。


Edit 2013.10.03: It's not currently possible directly. 编辑2013.10.03:目前无法直接进行。 But there's a related GitHub issue opened on npm to implement the behavior you're asking for. 但是npm有一个相关的GitHub问题,可以实现您要求的行为。 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 假设您来自package.json的包名称是myPackage并且您还拥有

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

Then add in package.json : 然后添加package.json

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

And in your script.js : 并在您的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 start将使用8080。但是,您可以对其进行配置(该值将由npm存储在其内部存储中):

npm config set myPackage:myPort 9090

Then, when invoking npm start , 9090 will be used (the default from package.json gets overridden). 然后,当调用npm start ,将使用9090( package.json的默认值将被覆盖)。


#3楼

From what I see, people use package.json scripts when they would like to run script in simpler way. 从我的角度来看,人们希望以更简单的方式运行脚本时会使用package.json脚本。 For example, to use nodemon that installed in local node_modules, we can't call nodemon directly from the cli, but we can call it by using ./node_modules/nodemon/nodemon.js . 例如,使用nodemon是安装在本地node_modules,我们不能称之为nodemon直接从CLI,但我们可以通过调用它./node_modules/nodemon/nodemon.js So, to simplify this long typing, we can put this... 因此,为了简化这种长类型输入,我们可以将其...

...

    scripts: {
      'start': 'nodemon app.js'
    }

    ...

... then call npm start to use 'nodemon' which has app.js as the first argument. ...然后调用npm start以使用以app.js作为第一个参数的'nodemon'。

What I'm trying to say, if you just want to start your server with the node command, I don't think you need to use scripts . 我想说的是,如果您只想使用node命令启动服务器,我认为您不需要使用scripts Typing npm start or node app.js has the same effort. 输入npm startnode app.js的工作量相同。

But if you do want to use nodemon , and want to pass a dynamic argument, don't use script either. 但是,如果您确实要使用nodemon并希望传递动态参数,则也不要使用script Try to use symlink instead. 尝试改用symlink。

For example using migration with sequelize . 例如,通过sequelize使用迁移。 I create a symlink... 我创建一个符号链接...

ln -s node_modules/sequelize/bin/sequelize sequelize

... And I can pass any arguement when I call it ... ...当我称呼它时,我可以通过任何争论...

./sequlize -h /* show help */

./sequelize -m /* upgrade migration */

./sequelize -m -u /* downgrade migration */

etc... 等等...

At this point, using symlink is the best way I could figure out, but I don't really think it's the best practice. 在这一点上,使用symlink是我能弄清楚的最佳方法,但是我并不认为这是最佳实践。

I also hope for your opinion to my answer. 我也希望您的意见对我的回答。


#4楼

You could also do that: 您也可以这样做:

In package.json : package.json

"scripts": {
    "cool": "./cool.js"
}

In cool.js : cool.js

 console.log({ myVar: process.env.npm_config_myVar });

In CLI: 在CLI中:

npm --myVar=something run-script cool

Should output: 应该输出:

{ myVar: 'something' }

Update: Using npm 3.10.3, it appears that it lowercases the process.env.npm_config_ variables? 更新:使用npm 3.10.3,它似乎小写了process.env.npm_config_变量? I'm also using better-npm-run , so I'm not sure if this is vanilla default behavior or not, but this answer is working. 我还使用better-npm-run ,所以我不知道这是香草默认行为或没有,但这个答案正在工作。 Instead of process.env.npm_config_myVar , try process.env.npm_config_myvar 代替process.env.npm_config_myVar ,请尝试process.env.npm_config_myvar


#5楼

This doesn't really answer your question but you could always use environment variables instead: 这并不能真正回答您的问题,但是您始终可以使用环境变量:

"scripts": {
    "start": "PORT=3000 node server.js"
}

Then in your server.js file: 然后在您的server.js文件中:

var port = process.env.PORT || 3000;

#6楼

You asked to be able to run something like npm start 8080 . 您要求能够运行npm start 8080 类的东西。 This is possible without needing to modify script.js or configuration files as follows. 无需按以下方式修改script.js或配置文件,这是可能的。

For example, in your "scripts" JSON value, include-- 例如,在您的"scripts" JSON值中,包括-

"start": "node ./script.js server $PORT"

And then from the command-line: 然后从命令行:

$ PORT=8080 npm start

I have confirmed that this works using bash and npm 1.4.23. 我已经确认使用bash和npm 1.4.23可以正常工作。 Note that this work-around does not require GitHub npm issue #3494 to be resolved. 请注意,此替代方法不需要解决GitHub npm 问题#3494

发布了0 篇原创文章 · 获赞 52 · 访问量 35万+

猜你喜欢

转载自blog.csdn.net/CHCH998/article/details/105533040