2

I have installed git-bash to be able to set env vars like that <VAR>=<value> node ..., but now it's working only if I run node. If I do it through npm start ("start": "ENV=development node server.js"), ENV would not be set. That's strange.

But if I do ENV=development npm start or ENV=development node server.js, it sets.

Where could the problem be?

P.S. I run bash through typing bash in powershell (I tried to do bash selectable in visual code terminal, even though I added ../git/bin/ to the PATH I still don't see it).

RobC
  • 22,977
  • 20
  • 73
  • 80
  • Does this answer your question? [How to set environment variables from within package.json](https://stackoverflow.com/questions/25112510/how-to-set-environment-variables-from-within-package-json) – RobC Dec 02 '20 at 08:45
  • 1
    Refer to [this answer](https://stackoverflow.com/questions/25112510/how-to-set-environment-variables-from-within-package-json#answer-61525615) specifically. Essentially, redefine your npm script as: `"start": "env ENV=development node server.js"` (note the initial [env](https://ss64.com/bash/env.html) prefix). Alternatively for cross-platform utilize the [cross-env](https://github.com/kentcdodds/cross-env) package. – RobC Dec 02 '20 at 09:07
  • @RobC that's it. I haven't scrolled down enough in that question. It solves this issue, but not the main, I wanted to set env vars in Windows and Linux the same way. I don't use dotenv because I will have to create multiple .env for development and production (it is not recommended by dotenv doc). – Radmir Akzhigitov Dec 02 '20 at 19:03
  • Have you tried: **1)** Installing [cross-env](https://github.com/kentcdodds/cross-env) in your project: `npm i -D cross-env`. **2)** Redefining your npm script as `"start": "cross-env ENV=development node server.js"` - that should work for both Windows and Linux. – RobC Dec 03 '20 at 08:33

0 Answers0