3

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.

In my 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. I assume this is something simple, but I can't figure out what I'm doing wrong.

j_d
  • 2,818
  • 9
  • 50
  • 91
  • Possible duplicate of [Sending command line arguments to npm script](https://stackoverflow.com/questions/11580961/sending-command-line-arguments-to-npm-script) – Mikko Aug 03 '17 at 17:54
  • Set your start script to: `node babel.runtime.js` and do `BUCKET_NAME=test npm start` from your terminal. It should make it available in `process.env.BUCKET_NAME` – Saugat Aug 03 '17 at 18:07

1 Answers1

4

You presented two separate ways of assigning variables to the process.

The first one, using an 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.

The second approach, via command line

npm start -- --bucket_name="test"

This approach sets the bucket_name as an argument of the running process. This is accessible from process.argv array.

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.

peteb
  • 18,552
  • 9
  • 50
  • 62