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"
}
RobC
  • 22,977
  • 20
  • 73
  • 80
Technotronic
  • 8,424
  • 4
  • 40
  • 53
  • 3
    Possible duplicate of [How to set Environment variables from within package.json \[Node.js\]](https://stackoverflow.com/questions/25112510/how-to-set-environment-variables-from-within-package-json-node-js) – RobC Jun 26 '18 at 09:28
  • They are explicitly speaking about the NODE_ENV variable and not a custom one... – Technotronic Jun 26 '18 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`](https://ss64.com/bash/printenv.html) 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. – RobC Jun 26 '18 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. – Technotronic Jun 27 '18 at 10:31

2 Answers2

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"
}
RobC
  • 22,977
  • 20
  • 73
  • 80
Technotronic
  • 8,424
  • 4
  • 40
  • 53
2

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

Aliaksandr Sushkevich
  • 11,550
  • 7
  • 37
  • 44