Default Node.js runner does not find (globaly installed) modules

workspace
nodejs

#1

I have recently created a blank workspace but the Node.js (default) runner seems to have problems finding the modules I install globally (with a “npm install -g” command, without “sudo”). After looking for a solution for some time, it seems like the runner does not know the path to search for the modules that NPM installs globally (in my case, at /home/ubuntu/.nvm/versions/node/v4.4.5/lib/node_modules).

I was able to sidestep the problem temporally by including including a “module.paths.push()” command at the beginning of the files, but it is hardly a perfect solution. Has anyone had the same problem? Is there an option or a command to fix this issue? Should it be reported as a bug? (because I understand this should work directly in a blank workspace)


#2

This is the default behavior of nodejs.
if you want to include global modules in search path for every module, you can modify bashrc to export NODE_PATH=/home/ubuntu/.nvm/versions/node/v4.4.5/lib/node_modules in node
or add

process.env.NODE_PATH = ...;
require("module")._initPaths();

to the root module of your app.


#3

Thanks for the help, but I created a new, clean workspace, installed a module globally (“npm install -g argparse”), updated the .bash file (“echo ‘export NODE_PATH=/home/ubuntu/.nvm/versions/node/v4.4.5/lib/node_modules’ >> ~/.bashrc”) and made sure it was processed (". ~/.bashrc"). Node.js is unable to locate the global module argparse. I rechecked the steps and restart the wokspace but to no avail. I do not know what I am doing wrong.

P.S. To clarify, I am using small Node.js files as tools to generate and compile JS code, not as a proper app. Therefore, I much prefer to find a project level solution.


#4

Strange, i can’t reproduce the issue. Could you share the test project?


#5

I used the Window->Share… option to send you (“harutyun” or, has the list says “xdx ftf”) the test workspace. It only contains a test.js file that request a Node module (argparse) that I had previously installed into the system with the “npm install -g” command. When I try to run it an “Error: Cannot find module ‘argparse’” error appears.

This code worked until two days ago in a previous workspace (that I deleted to properly restart a project). Since then I have been looking at many sites (stackooverflow, Node.js documentation, etc.) but it still doesn’t find the module.


#6

looks like something have changed in the order of scripts being loaded, putting export at the end of .profile instead of .bashrc works.


#7

You are right. I have tested it in another workspace and it certainly works. Thank you very much!

For future reference (and other people suffering the same problem), the command to solve this issue is:

echo ‘export NODE_PATH=/home/ubuntu/.nvm/versions/node/v4.4.5/lib/node_modules’ >> ~/.profile