NPM installs modules in two ways: Local installation, global installation.
The difference between the two is that you add the-G option when using NPM install XXX.
The locally installed path is the path to the current CMD in win. For example, when I open cmd, the path is C:\Documents and settings\administrator. Then if I use NPM install KOA, the result will be in this directory to generate a Node_modules folder, which is the KOA folder. Obviously, most of the time, we're not going to place the work path on the C drive. At this point, if require (' KOA '), then you will be prompted not to find the module error.
What to do, is to first in the cmd inside switch directory to your work path, and then install. This is also the recommended way.
It is also important to note that it is installed with require (' KOA '), and, like normal JS, it is explicitly specified in its path. My chat_server.js, for example, refers to Socket.io. Require ('./node_modules/socket.io/index.js ') (server), preceded by a dot that represents the path of the current JS file.
Global installation: This is a good understanding, for example, many of the things you do are based on express, and there are a lot of places in the use of Express, it would be annoying to specify the path each time as above. If the global installation, you can directly use require (' Express ');
The default path for global installation is
Well, what if I don't like the path.
For example, my node. js is installed in the F:\nodejs, create a new Lib folder, inside the newly installed module.
First find the F:\NODEJS\NODE_MODULES\NPM inside the file, open can see only
prefix=${appdata}\npm
Change it to PREFIX=F:\NODEJS\LIB\NPM\NODE_MODULES\NPM. Can see
Remember to add the-G option when installing.
If this does not work, set the environment variable. Node_path=f:\nodejs\lib\npm\node_modules\npm\node_modules.
This is very silent, because last night I had to set the environment variable to change the global installation path, but now it is not. So I don't know if this is necessary.
Also, to reopen CMD after setting the environment variable to take effect for the newly set environment variable.
node. JS installation module A little pit