简体   繁体   中英

Using environment variables from ~/.bashrc in “npm start”?

Sorry if this is a duplicate. I tried to find a similar question but couldn't. Here's the breakdown:

I need to use a secret key with a package I'm building and I don't want to publish it so I'm trying to set it as a local bash environment variable. In my .bashrc file I have this:

# Obviously this is not the REAL key, just an example
MY_KEY="1111111111111111"

And then in my Gulpfile, I have a task called "dev". For simplicity sake, let's say it looks like this:

gulp.task('dev', function () {
  console.log(process.env.SECRET_KEY);
});

Then, in order to get the secret key into the environment, I have the following in my package.json:

"scripts": {
  "start": "SECRET_KEY=$MY_KEY gulp dev"
}

So the problem is, when I run the command npm start , my gulp task logs undefined for the secret key. But when I manually run the command SECRET_KEY=$MY_KEY gulp dev , the gulp task logs 1111111111111111 . So, for some reason, npm start is not correctly accessing my bash variable and passing it into the Node environment. Is there a way to make this work?

This just creates a shell variable , not an environment variable:

MY_KEY="1111111111111111"

To export that shell variable into the environment:

export MY_KEY

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM