Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 612
j_d Avatar asked Jun 05 '26 10:06

j_d


1 Answers

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.

like image 56
peteb Avatar answered Jun 08 '26 01:06

peteb