node. JS installation module A little pit

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.