0

In my package.json I heve these scripts:

"scripts": {
    "do-build": "ENV=DEV node ./src/setEnvironment.js",
    "do-build:prod": "ENV=PROD node ./src/setEnvironment.js",
    "do-build:dev": "ENV=DEV node ./src/setEnvironment.js"
  }

So, I assume that when I run npm run do-build it will run "do-build": "ENV=DEV node ./src/setEnvironment.js", and when doing npm run do-build --prod it would execute "do-build:prod": "ENV=PROD node ./src/setEnvironment.js", but it always executes the default do-build script("do-build": "ENV=DEV node ./src/setEnvironment.js"). I am not sure why this is happening?

0

2 Answers 2

1

npm does not support CLI arguments that influence which script should be executed. Therefore npm run do-build:prod will execute the production build task.

Sign up to request clarification or add additional context in comments.

Comments

0

That's not how npm scripts work. If you want to run do-build:prod you would have to do it like so: npm run do-build:prod

Your npm scripts can accept parameters if you like, eg if you had this:

  "scripts": {
    "foo": "echo $1"
  }

You could run it like so: npm run foo bar and your script would receive bar as the 1st command line parameter.

To have richer support for command line options, I'd suggest you look into richer task runners such as gulp or grunt instead of using npm scripts.

2 Comments

If I use this npm run do-build prod I get prod in process.argv values array, but only after my node bin directory path and .js file path that I am pointing to. Maybe you can help me to understand why it is so and how prevent these paths from getting into this array?
You can't prevent those paths from appearing in argv, that's just how node works by design nodejs.org/docs/latest/api/process.html#process_process_argv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.