简体   繁体   English

将命令行变量传递给npm脚本?

[英]Pass command line variable into npm script?

I am trying to pass a command line (specifically not in version control) option to my main index.js script, which does some stuff based on a specific S3 bucket. 我试图将命令行选项(特别是不在版本控制中)传递给我的主要index.js脚本,该脚本根据特定的S3存储桶执行一些操作。

In my package.json: 在我的package.json中:

"scripts": {
  "start": "bucket_name=${bucket_name:=null} node babel.runtime.js"
},

And via command line: 并通过命令行:

npm start -- --bucket_name="test"

But bucket_name keeps coming back as null, so the var isn't being passed correctly. 但是bucket_name始终返回为null,因此未正确传递var。 I assume this is something simple, but I can't figure out what I'm doing wrong. 我认为这很简单,但是我无法弄清楚自己在做什么。

You presented two separate ways of assigning variables to the process. 您介绍了两种为过程分配变量的方法。

The first one, using an npm script 第一个,使用npm script

"scripts": {
  "start": "bucket_name=${bucket_name:=null} node babel.runtime.js"
}

This approach sets the bucket_name environment variable which is accessible from process.env.bucket_name . 此方法设置可以从process.env.bucket_name访问的bucket_name 环境变量

The second approach, via command line 第二种方法,通过命令行

npm start -- --bucket_name="test"

This approach sets the bucket_name as an argument of the running process. 此方法将bucket_name设置为正在运行的进程的参数 This is accessible from process.argv array. 这可以从process.argv数组访问。

Its not that the variable isn't being set for one way or the other, its just about where you're accessing. 并不是说变量没有被设置为一种或另一种方式,而是关于您正在访问的位置。 Each of these sets the bucket_name in different places. 每个bucket_name在不同的位置设置bucket_name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM