rescuecreative rescuecreative - 2 months ago 8
Node.js Question

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

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 () {

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
for the secret key. But when I manually run the command
, the gulp task logs
. 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:


To export that shell variable into the environment:

export MY_KEY