0

I have a script section in my package.json file as shown below

"scripts": {
    "start": "node_modules/.bin/grunt transpile && node dist/src/app.js",
}

when I run "npm start" from command shell it works fine. Now I have the requirement to introduce two environment variables which my js code is accessing as below

if (process.env.APP_DATA_MODE === "complete") {
    // perform some actions here
} else {
    // perform some actions here
}

if (process.env.HOST_NAME === "dev") {
    // do some dev env related code here
} else {
    // do prod related code here
}

What I want to know is how should I introduce these two environment variables in the "start" command above OR at the very least how I can run the npm start command and also specify arguments for the two environment variables listed above

Already tried stack overflow article which does look like a close match but having no luck there as the environment variable keep coming as blank during code execution

Asif
  • 393
  • 9
  • 17

1 Answers1

0

This should work

"start": "node_modules/.bin/grunt transpile && APP_DATA_MODE=complete HOST_NAME=dev node dist/src/app.js"

Assuming you need pass them to your app.js

Okay so I guess I wasn't convincing enough so let me provide a simple proof Let's write a simple script

#!/bin/bash

echo "VAR1 = $VAR1"
echo "VAR2 = $VAR2"

Here's the output

no env vars set

with env vars

avpav
  • 452
  • 4
  • 7
  • That returns with error with module not found saying my-app\APP_DATA_MODE=complete cannot be resolved If I run it with node dist/src/app.js APP_DATA_MODE=complete HOST_NAME=dev it runs fine but it doesn't set the environment variables – Asif Nov 08 '17 at 16:08
  • Sorry 'node' call was in the wrong place. Have updated the answer – avpav Nov 08 '17 at 16:12
  • Yeah I figured. Even after putting the node call first and then adding arguments after like below node dist/src/app.js APP_DATA_MODE=complete HOST_NAME=dev Command runs fine but it doesn't set the environment variables – Asif Nov 08 '17 at 16:19
  • You've missed the point env vars should be at the beginning before node – avpav Nov 08 '17 at 16:45