nodejs require module can not find how to solve?

After installing the module globally with npm, sometimes when the program require the use of the module can not be found will be reported, how to solve? nodejs is looking in the directory module included in the list of module.paths, therefore, to solve this problem is to be added to module.paths global module directory list.

 

After installing the module in the overall program error which require this module, the module can not be found. , First in the normal way npm install -g express global express module mounted below. Then a test require ( 'express') given

Then the problem lies in nodejs how to find the module. First of all, we need to know the global npm install the module installed in the end to which directory. Running in a terminal npm prefix -g command to print out the installation path. The nodejs searching module is to look at module.paths directory listing below.

 

 

Therefore, one solution is the installation path in the global program npm added to the module.paths.

module.paths.push ( 'global installation path'). Then test possible.

This program is only valid for the current js.

 

Another is to add environmental variables NODE_PATH, the value is set to a global installation path. As shown, after addition of the test feasible.

 

 

 

In fact, after adding environment variables NODE_PATH, when we go to see module.paths will find the path environment variable has also been in the module.paths.

Therefore, the most convenient solution is: npm prefix -g find the path to global install, and then added to the environment variable NODE_PATH in.

 

 

 

 

From: https://m.html.cn/qa/node-js/10597.html

 

Guess you like

Origin www.cnblogs.com/xiaofenguo/p/12084391.html