5

I have a basic App created using npm init -y. In package.json I have a main entry which points to server.js.

{
  "name": "rest-api",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "private": true,
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "prestart": "SET NODE_ENV=dev"
}

I am trying to set the NODE_ENV variable in prestart and let npm to call main to invoke npm start. But environment variable set in the prestart is not carry forwarded and is undefined. When I run 'npm start', console outputs that both commands are executed in order.

    PS D:\test\RestAPI> npm start

    > rest-api@1.0.0 prestart D:\test\RestAPI
    > set NODE_ENV=dev

    > rest-api@1.0.0 start D:\test\RestAPI
    > node server.js

    undefined
    [undefined] Listening on http://localhost:3000

but when I print the variable from the app, it is undefined. Is there anything that I am doing wrong here, or is this how it is supposed to behave? Is there a way to invoke and set env variable using 'SET NODE_ENV=dev' without chaining it to 'node server.js'

When I combine both in the 'start' as below, then the environment variable is set.

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
     "start": "set NODE_ENV=dev && node server.js"
  }

I am testing this on Windows 10, npm version 3.10.10. Appreciate your help.

I know how this can be done in package.json using 'start'. This question is specific to how this can be achieved through 'prestart'.

Thanks.

Dreamy_Coder
  • 51
  • 1
  • 5
  • Possible duplicate of [How to set Environment variables from within package.json \[Node.js\]](http://stackoverflow.com/questions/25112510/how-to-set-environment-variables-from-within-package-json-node-js) – arjabbar Mar 20 '17 at 17:33
  • 1
    What about putting both commands on a separate line in a .bat file? then set the start script as the path to the .bat file. – Taylor Ackley Mar 20 '17 at 17:35
  • 1
    How is this a duplicate of that? I have clearly mentioned that I need to hook this in the prestart. If you read my question, you can see that in my last code block I have provided how to do that. – Dreamy_Coder Mar 20 '17 at 17:36
  • Why do you want to do this? What is it that you are trying to achieve? – Yasser Hussain Mar 20 '17 at 17:38
  • @TaylorAckley, that can be done with the help of combining both in 'start' itself. I am confused as I have no clue what prestart command didn't work. – Dreamy_Coder Mar 20 '17 at 17:44
  • I believe `npm start` runs prestart and poststart in sequence. https://docs.npmjs.com/misc/scripts – Taylor Ackley Mar 20 '17 at 18:03

2 Answers2

1

The short answer is NOT possible.

1. Why : this is not be possible because each script executed by different processes that npm spawns for this purpose which has its own environment variables.

To realise that, create test project and configure both scripts to be like

"start":   "pause&&set VAR1",
"prestart" : "pause&&set VAR1=value&&set VAR1&&pause",

On windows open the task manager and pay close attention how many cmd process es are listed before running the script.

run the command "npm start" and at each request "press any key to continue..." just notice how processes created are created. I attached screenshots for this in order enter image description here

2. Unless : you change how npm executes different scripts to use one cmd for all the scripts which I think is complicated and probably will create bugs.

Ahmed Bahtity
  • 536
  • 4
  • 9
0

If you want to chain scripts and add env variables along the way then checkout the example given in the cross-env package:

{
  "scripts": {
    "parentScript": "cross-env GREET=\"Joe\" npm run childScript",
    "childScript": "cross-env-shell \"echo Hello $GREET\""
  }
}
droid001
  • 163
  • 10