1

I am using a specific task runner to execute my node scripts called Yoshi. I want to run a script from package.json, for example, yoshi test but with a custom environment variable (for example special_variable):

"scripts": {
    "start": "yoshi start", 
    "test":  "special_variable=value yoshi test // this case"
}
4
  • 3
    Possible duplicate of How to set Environment variables from within package.json [Node.js] Commented Jun 26, 2018 at 9:28
  • They are explicitly speaking about the NODE_ENV variable and not a custom one... Commented Jun 26, 2018 at 11:39
  • The solution in the linked answer equally applies to custom variables too. E.g. FOO=bar - I fail to see what makes your variable any more special than NODE_ENV? If you're using a Bash shell you can check this by using printenv in an npm-script. For instance: "list-env-vars" : "FOO=bar printenv", then run npm run list-env-vars and you'll see FOO=bar listed as an env variable. Commented Jun 26, 2018 at 12:50
  • Weird, I tried it before and it didn't work, but now, looks like you are right. I'm closing this one. Commented Jun 27, 2018 at 10:31

2 Answers 2

3

Apparently, I can pass an environment variable using the ENV prefix in my npm-script like this:

"scripts": {
    "start": "yoshi start", 
    "test":  "ENV special_variable=value yoshi test"
}
Sign up to request clarification or add additional context in comments.

Comments

2

I think you can create .env file and add your variables there.

Comments

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.