6

Before starting the server, I need to export ('set' actualy, I'm using Win7) NODE_PATH variable. I tried to do it with this command (not working even in command line):

set NODE_PATH=./ && node server.js

and for package.json:

  "scripts": {
    "start": "set NODE_PATH=. && node server.js"
  },

But it's not working. I got Error: Cannot find module which appears only if NODE_PATH not specified.

So, how this problem could be solved? I need a proper way to export NODE_PATH and run the server with inline command, or a way to specify two separate commands for 'start' script.

Maksim Nesterenko
  • 5,661
  • 11
  • 57
  • 91

3 Answers3

3

set NODE_PATH=./ ; && node server.js

On Windows, the command above works fine. And in general, it should work without a semicolon when you set environment variables. But here, probably, path string causes the problem?

Also notice that you should set environment variables in a different way for Linux, no set keyword must be used. If your scripts will be run on a different platform, probably it's better to use some plugin like cross-env to write your scripts.

This question helps Setting an environment variable before a command in Bash is not working for the second command in a pipe

Maksim Nesterenko
  • 5,661
  • 11
  • 57
  • 91
1

Below is what worked for me in Linux (CentOS). I needed this because the node modules in use were all installed globally.

"scripts": {
    "start": "export NODE_PATH=/usr/lib/node_modules && node myscript.js"
},

Use export instead of set. No semicolon needed.

Aaron Cicali
  • 1,496
  • 13
  • 24
0

you can

NODE_PATH=./ node server.js

without set and ; tested on bash