0
The problem

Ideally, I need to use a variable passed via terminal in some npm scripts. For example, with the command set TOKEN=blabla&& npm run staging, where the scripts look like the following:

"staging": "run-s build:steps:* && run-s langcheck",
"build:steps:env": "npx dotenv-vault pull .env --dotenvMe=$TOKEN",

I want the $TOKEN to be replaced by the value passed in the terminal.

However, this did not work. And trying to troubleshoot, I ended up with a very minimal reproduction that I documented below.

Minimal Reproduction

Need to pass an environment variable to npm script inside package.json.

...
"scripts": {
    "check": "echo $SECRET",
}
...

Using the following command on Windows

set SECRET=hello&& npm run check
Actual Output vs Expected Output

The output shows $SECRET instead of the "hello" that is intended

> echo $SECRET

$SECRET

How can I "receive" the passed variable inside the npm script?

What I tried

I have tried following this answer from another StackOverflow question, but does not seem to work in my case.

Chique
  • 735
  • 3
  • 15
  • I find the wording of the quesion unclear: If I'm not mistaken, the problem is not how to **pass** an environment variable, but how to actually **use** such an environment variable inside npm. If I'm right, the tag _npm-scripts_ should be sufficient and I suggest to remove the other tags. – user1934428 Dec 05 '22 at 07:50
  • Whats this got to do with Powershell? – Scepticalist Dec 05 '22 at 08:18
  • I have updated the tags. Removed the unrelated ones and rephrased the question. – Chique Dec 05 '22 at 09:32
  • SET MY_ENV_VAR=BLA works on my Windows 10, I can get "BLA" value in runtime (python/vscode environment) – ihsany Mar 15 '23 at 13:37

0 Answers0