Package.json multi-env script setup

I want to have a package.json that looks something like:

"scripts": {
   "my-script": "doppler run -c ${ENV:-dev} -- node index.js",

index.js itself takes arguments, so I want to be calling this like npm run my-script prod -- --my-arg-1 123 --my-arg-2 hello

However, I’m getting errors because it’s passing “prod” in as an argument to the script itself, which is not what I want.

In case it’s helpful, I’m using command-line-args - npm to parse the arguments within the script.

Any thoughts?

Hi @bjuni!

Welcome to the Doppler Community!

Are you intending for the prod parameter to be used as the environment? If so, then you might try something like this:

"scripts": {
   "my-script": "doppler run -c ${npm_config_env:-dev} -- node index.js",

and then use a command like this:

npm run my-script --env=prod --my-arg-1=123 --my-arg-2=hello

Then, inside your script you can access those parameters with process.env.npm_config_$NAME. So, for --my-arg-1 you would use process.env.npm_config_my_arg_1.

Will that work for what you’re after?


That’s one solution but it would get away from the intended usage of command-line-args - npm, so it’s not ideal