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:
API_KEY=abc123
How can I access the value of API_KEY inside package.json's scripts while still keeping it a secret because I need to push package.json publicly?
Currently, I do the following which works but not cross-platform:
package.json
{
"scripts": {
"start": "web-ext run --api-key=$API_KEY"
}
}
And when running start script I do it like:
API_KEY=abc123 npm start
This works thanks to Bash Programming Language but it doesn't work on Windows. I need to replace $API_KEY in start script with %API_KEY%. But I want it to be cross-platform. Is there any other way?
env-cmdwhich might've worked for my case but it can't as i want to use the variable as an argument to--api-keyso can't do that according to github.com/toddbluhm/env-cmd-examples/issues/…startscript I do it likeAPI_KEY=abc123 npm start" - why use environment variables at all when you have a cli parameter for that? Just drop the--api-key=API_KEYfrom the package.json - no issues with cross-platform compatibility - and call it likenpm start --api-key=abc123.--api-key&--api-secret. So rather than that, I find my own solution to be good suggested in the question. Only thing to make it work on Windows, is to change$API_KEYto%API_KEY%. When I posted the question, I thought a simpler solution exists but unfortunately it doesn't :(