1

I'm creating a Node.JS project based on Express and I'm trying to setup environment variables to separate the develop from production flux. I created a shell script file with some env variables like NODE_ENV or DEBUG to be executed by the predevelop script when I run the npm run develop on command line, but the variables are being lost after predevelop finish his execution and getting undefined when the Express app runs.

This is the ./scripts/development.sh

#!/bin/sh

echo 'Setting up development env';

export NODE_ENV='development';
export NODE_DEBUG='http';
export DEBUG='project:server';

echo 'NODE_ENV => '$NODE_ENV;
echo 'NODE_DEBUG => '$NODE_DEBUG;
echo 'DEBUG => '$DEBUG;

exit 0;

This is my package.json scripts

{
  // ...
  "scripts": {
    "predevelop": "/bin/bash ./scripts/development.sh",
    "develop": "echo 'NODE_ENV => '$NODE_ENV && echo 'NODE_DEBUG => '$NODE_DEBUG && echo 'DEBUG => '$DEBUG; node ./bin/www"
  }
  // ...
}

The console output from npm run develop:

┌─ [ jcorradi ] ~/Sites/pocs/project-server  
└─• npm run develop

> [email protected] predevelop /home/jcorradi/Sites/pocs/project-server
> /bin/bash ./scripts/development.sh

Setting up development env
NODE_ENV => development
NODE_DEBUG => http
DEBUG => project:server

> [email protected] develop /home/jcorradi/Sites/pocs/project-server
> echo 'NODE_ENV => '$NODE_ENV && echo 'NODE_DEBUG => '$NODE_DEBUG && echo 'DEBUG => '$DEBUG; node ./bin/www

NODE_ENV =>
NODE_DEBUG =>
DEBUG =>

// From ./bin/www
NODE_ENV => undefined
NODE_DEBUG => undefined
DEBUG => undefined

Someone knows if this is the best pratice or if there are other ways to make this work?

Obs: I have already tried set the env var directly on npm script and this still keeps failing.

Ex:

{
  // ...
  "scripts": {
    "predevelop": "NODE_ENV='development'; NODE_DEBUG='http'; DEBUG='project:server';",
    "develop": "echo 'NODE_ENV => '$NODE_ENV && echo 'NODE_DEBUG => '$NODE_DEBUG && echo 'DEBUG => '$DEBUG; node ./bin/www"
  }
  // ...
}
2
  • 1
    Is there any reason you aren't passing the vars to node via the command line. For example, "predevelop": "node myScript.js pre" and "develop":"node myScript.js dev". Then just have your script handle the command line arguments passed into process.argv array? Commented Aug 3, 2016 at 3:18
  • Because it's enviroment variables, not execution parameters. The NODE_ENV is used by Express to control caches and logs, for example. The same for DEBUG, just for the morgam log npm package. Commented Aug 3, 2016 at 17:11

2 Answers 2

2

Someone knows if this is the best pratice or if there are other ways to make this work?

The best practice is to avoid setting environment variables in package.json, as that should environment agnostic, in order to support environments that don't have bash, or have bash installed in /usr/bin/bash

Assuming you care not about that constraint, the way to make this work is to execute your environment in the actual run command:

{
  "scripts": {
    "develop": "NODE_ENV=development NODE_DEBUG=http DEBUG=project:server node ./bin/www"
  }
} 
Sign up to request clarification or add additional context in comments.

Comments

1

If you don't want to just pass the params as mentioned above you can always do something like this.

package.json

"scripts": {
    "start": ". development.sh | node path/to/app/www"
}

Within project root run "npm start" (also assuming development.sh is in your project root). This will execute the development.sh bash script and then your node script.

Two Notes:

  1. I remember reading the syntax above might be slightly dependent on your OS/version of node. So if you run into any issues take that into consideration.

  2. Also, within your node script, environmental vars will be key/values on the process object. This will be helpful to know for debugging purposes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.