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).

4
  • Does this answer your question? How to set environment variables from within package.json
    – RobC
    Commented Dec 2, 2020 at 8:45
  • 1
    Refer to this answer specifically. Essentially, redefine your npm script as: "start": "env ENV=development node server.js" (note the initial env prefix). Alternatively for cross-platform utilize the cross-env package.
    – RobC
    Commented Dec 2, 2020 at 9: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). Commented Dec 2, 2020 at 19:03
  • Have you tried: 1) Installing 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
    Commented Dec 3, 2020 at 8:33

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.