question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pass private environment variables as arguments to npm scripts

See original GitHub issue

Basically, 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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
toddbluhmcommented, Nov 1, 2019

Hey sorry for taking so long to reply to this.

The original goal was for the --use-shell option to be able to enable the functionality of replacing $VAR similar to how cross-env does 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 $VAR and ${VAR} and %VAR% (windows) with the final env value. Not sure how well this will work, but it is a start.

0reactions
toddbluhmcommented, Dec 29, 2019

@omeid interesting idea, but I do not use cross-env in the package and I would rather not add an additional dependency, unless absolutely necessary. I liked your PR so I merged that one.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found