During development I used to WebStorm node_path =.
environment variable. I have set up a variable in the launch of the project settings. Now I want to try to run the project on the server, but do not know how to set this variable there. Help solve the problem!

- 2,031
- 3
- 22
- 22
-
3You need to set the environment variable `NODE_PATH`. Possibly in the script that starts up `node`. – Nitzan Shaked Nov 25 '13 at 06:32
3 Answers
Assuming it's a UNIX or Mac OS X server, use export NODE_PATH=
and append the path you want.
-
3could you provide a bit more detail as to where you would add the export? inside the .env file? do you need to import it somewhere ? – xunux Jan 29 '20 at 23:06
-
I know that's a late response, but just for comers, you have to execute it in the command prompt (i.e. terminal) as follows: export NODE_PATH=/YOUR_WHATEVER_PATH – محمد جعفر نعمة Nov 01 '21 at 10:29
-
1For me, when i've set this environment variable at my shell configuration file i.e. bash's ~/.bashrc file, using `export NODE_PATH="~/.local/lib/node_modules"`, the tilde `~` -which designates the home directory of the currently logged in user- presumably didn't get its respective pathname expanded correctly, and thus no global modules were able to be located by node. You can alternatively set the `node_modules` path value, using command line expansion as laid out by this [answer](https://stackoverflow.com/a/43504699/14135669) – polendina Jul 31 '22 at 16:38
I would recommend setting the variable right before you run the command like so:
NODE_PATH=src/ node myapp.js
This way the variable is set when needed. This is preferable unless you really need to change the path with different versions of your deployment.
If on windows, you can use this lil package to get the effect to work so it is consistent across dev and prod: win-node-env
For bonus points add it to your start script in package.json
like so:
"scripts": {
"start": "NODE_PATH=src/ node myapp.js"
}
Then in production all you need to do is run: npm start

- 1,494
- 1
- 17
- 28
-
this worked well for me, thanks! It's important to note that one might have to do some adjustments when deploying though, depending on your deployment strategy. – xunux Jan 29 '20 at 23:41
-
im having issues with this methodology, and ideally i would like to just have it in my .env but it seems to be getting ignored. This is causing issues on deployment – xunux Feb 07 '20 at 17:54
-
`.env` is read after the command has started to run. The NODE_PATH must be set before running any node commands. Don't think of NODE_PATH as your apps environment, instead it is nodes environment. – mr haven Feb 14 '20 at 19:30
-
I think NODE_PATH have to be set in the OS environment. Or, if you want to define it in package.json, then you have to use the full path not a relative path. At least, that what happens in my experiment. – محمد جعفر نعمة Nov 01 '21 at 10:26
-
I don't understand what NODE_PATH='' is supposed to do in the first place, the code runs fine without it! – Shayan Jan 23 '23 at 14:13
Add
export NODE_PATH=...
to your system environment setting (/etc/profile,~/.bash_profile...), make it works.
or
You can declare dependencies in package.json(project), like this:
{
...
"dependencies": {
"connect": "~2.0.3",
...
},
...
}
and run
npm install
in the same folder instead. Hope it helps.

- 23
- 6