简体   繁体   中英

How to set up environment variables for node.js in NPM task

I have npm task that loads concurrently, node-inspector, and node-supervisor.

{
"start": "concurrently --kill-others \"node-inspector\" \"set NODE_PATH=.&&supervisor -n error -- ./bin/www --debug\"",
"prestart": "start chrome http://localhost:3000 http://localhost:8080/debug?port=5858"
}

Result:

[1] set NODE_PATH=.&&supervisor -n error -- ./bin/www --debug exited with code 0
--> Sending SIGTERM to other processes..
[0] Node Inspector v0.12.7
[0] Visit http://127.0.0.1:8080/?port=5858 to start debugging.

But when I'm trying localhost:3000 I get ERR_CONNECTION_REFUSED .

QUESTION 1: How I can pass NODE_PATH=. (or any other ENV variable) to supervisor - so my node runs correctly?

QUESTION 2: Or maybe you know, how to organize two watch tasks within npm scripts? for example: node-supervisor and node-inspector

Note1: If I run set NODE_PATH=.&&supervisor -n error -- ./bin/www --debug directly from command line - works well.

Note2: If I run npm start without NODE_PATH=.&& it works well but node error - cause it can't find modules for example require('helpers/log') or require('models/user') cause i don't want to place everything in node_modules .

Stats:

  • Windows 8.1
  • Node 4.4.0
  • npm 2.4.0

For your first question, check out the handy helper module cross-env

It's designed for exactly this purpose, and is easy to use. Just use cross-env where you'd typically use UNIX-style env, and away you go. Mine, for example, looks like:

"scripts": {
    "start": "cross-env DEBUG=express:router webpack-dev-server ...."
} 

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