Setting NODE_PATH on Windows

If your getting errors like ‘cannot find module x’ when trying to start a nodejs server on windows then you probably have some code which is looking for node modules but they are not being found. This could be a number of reasons but node will look in the default folders for the module and then recursively up the directories appending ‘node_modules/’ and looking in there for the module. If it has no luck then it will look inside the directories specified in your NODE_PATH environment variable.

This is the error you might see if node cannot find your module.

Image

Setting the NODE_PATH environment variable

Setting the NODE_PATH environment variable is different based upon your OS and the terminal your using to run node.

Then when you use require.(‘module_name’) in your code, node will then do a recursive search upwards appending node_modules dir (relative path if you have not used /, ./ or ../):

1. /home/folder1/folder2/node_modules/module_name.js
2. /home/folder1/node_modules/module_name.js
3. /home/node_modules/module_name.js
4. /node_modules/module_name.js

Then it will search the directories in your NODE_PATH (absolute path):

1. NODE_PATH/module_name.js
2. NODE_PATH/modules_name/index.js
3. NODE_PATH/modules_name/index.node

Set NODE_PATH using Windows & GitBash

You need to use the export command so it’s available to all processes not just inside the shell.

Image

Set NODE_PATH using Windows & CMD

You need to use the SET command and absolute path to your node module folder(s).

Image

There is more reading in the references below.

refs:
http://stackoverflow.com/questions/5390886/nodejs-require-paths-resolve-problem
http://nodejs.org/api/modules.html#loading_from_node_modules_Folders