Search code examples
parameter-passingpackage.jsonnpm-runyargsnodenv

How do I pass parameters to an NPM script that uses nodenv?


I have a NodeJS app that uses the NPM package node-env-run (i.e. to load environment variables from .env file). The package.json, I have a script that looks like this:

"scripts": {
     "run-jobs": "nodenv tests/run-jobs.js"
}

Sometimes when I run npm run run-jobs, I don't want to run all jobs, but only one or a few. I am looking for a way to pass the names of those jobs to the script so that I can filter for them in the script by looking at process.argv

I have tried following answers on Stackoverflow (example) and elsewhere, but I cannot get this to work. For example, I have tried:

  • npm run run-jobs myjob1 myjob2
  • npm run run-jobs -- myjob1 myjob2
  • npm run run-jobs --myjob1 --myjob2 ...

All of these are either not being picked up in argv or resulting in errors from nodenv.

Is there a way to do this, and if so what is the syntax?


Solution

  • After a bunch of trial and error and digging into the node-env-run.js code, I've got it.

    The syntax to get this working is

    npm run run-jobs -- -- myjob1 myjob2