Pass private environment variables as arguments to npm scripts
See original GitHub issueBasically, this example pretty much sums up what I want to do.
Although, I’ll provide you with my use-case.
I have a secret key called API_KEY that I want to access inside of package.json’s scripts.
package.json
{
"scripts": {
"start": "web-ext run --api-key=API_KEY"
}
}
My .env file contains API_KEY which I want to keep private:
API_KEY=abc123
Now if I use cmd-env, I can’t pass it as an argument to --api-key
I want the following to work 👇
{
"scripts": {
"start": "cmd-env web-ext run --api-key=$API_KEY"
}
}
Any way to add that?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Passing environment variables in npm-scripts - Stack Overflow
This will run my start-app.js script and set the process.env.NODE_ENV environment variable to "production". See here for syntax explanation. The ...
Read more >Three Things You Didn't Know You Could Do with npm Scripts
Argument passing and parsing. So far we covered how to create scripts, which environment variables are set and how to call the scripts....
Read more >Handling Command-line Arguments in NPM Scripts
You can pass parameters to any command. Named Parameters. From NPM docs: "Any environment variables that start with npm_config_ will be ...
Read more >config | npm Docs
Run npm config ls -l to see a set of configuration parameters that are internal to npm, and are defaults if nothing else...
Read more >scripts - npm Docs
json file, then your package scripts would have the npm_package_name environment variable set to "foo", and the npm_package_version set to "1.2.5". You can ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hey sorry for taking so long to reply to this.
The original goal was for the
--use-shelloption to be able to enable the functionality of replacing$VARsimilar to howcross-envdoes it here: https://github.com/kentcdodds/cross-env#cross-env-vs-cross-env-shell. Unfortunately, that does not seem to work as expected.I tried a couple of different ways to make it work, but no dice. I think what I am going to have to do is implement some sort of parsing of the full shell command before execution and manually replace
$VARand${VAR}and%VAR%(windows) with the final env value. Not sure how well this will work, but it is a start.@omeid interesting idea, but I do not use
cross-envin the package and I would rather not add an additional dependency, unless absolutely necessary. I liked your PR so I merged that one.