Installing configuration for node. js, NPM, and environment variables

Source: Internet
Author: User

Because the node. JS platform is running JavaScript code on the backend, you must first install the node environment in the native computer.

I. Installing node. JS

First of all, download the corresponding platform from the node. JS website installation program, the slow speed of children's shoes please step into the domestic mirror. Be sure to select all components when installing on Windows, including tick Add to Path .

After the installation is complete, in the Windows environment, open a command prompt, and then enter node -v , if installed properly, you should see v8.10.0 such output. Continue at the command prompt node and you will enter node. JS's interactive environment at the moment. In an interactive environment, you can enter any JavaScript statement, for example 100+200 , a carriage return will result in the output. To exit the node. JS environment, double-click CTRL + C two times.

Second, NPM

NPM is actually a package manager for node. js. Why do we need a package management tool? Because we developed it on node. js, we used a lot of JavaScript code written by someone else. If we want to use someone else to write a package, each time according to the name of the official website search, download code, decompression, re-use, very cumbersome. So a centralized management of the tool came into being: we all put their own modules into the development of the NPM online, if you want to use, directly through the NPM installation can be directly used, no tube code exists where, should be downloaded from.

More importantly, if we want to use module A, and module A is dependent on module B, module B also relies on module X and module y,npm to download and manage all dependent packages based on dependencies. Otherwise, by our own manual management, must be both troublesome and error prone.

With so much to say, where is NPM? In fact, NPM has been installed in the node. js in passing. We should see similar output at the command prompt or terminal input npm -v . If you enter directly npm , you will see output similar to the following. As shown below

A bunch of words above tell you that you npm need to keep the order. Now we don't have to worry about these orders, we'll talk about them later. For now, all you need to do is make sure that NPM is properly installed and can run.

iii. Configuring Nodejs and NPM

1, not to say that Nodejs and NPM installed in the future can be directly used, which is also a novice often made one of the mistakes. Because the module installed by NPM is not installed to the NODEJS program directory by default, such as the D:\Program files\nodejs\ directory we set up when installing it, we use a sample to see if we first install a cluster module with NPM, Execute the command as shown in the first figure below:

NPM Install Cluster

As you can see, by default the information is saved to the C:\Users\Administrator\ directory, we open this directory to see. You can see that there is a node_modules directory under this directory, click to open it, as shown in the second picture below:

2, you can see the cluster directory installed in the directory below, which means that if you do not modify the NPM module installation directory, it will be installed by default, as you test the development of a variety of projects, the installation of more and more modules, then the volume of this folder will become larger, Until you fill up your C-plate. This is why you need to modify the configuration of NPM.

3, here we have two steps:

The first step: Modify the NPM cache directory and the global directory path, the corresponding module directory to the D-disk Nodejs installation directory;

The second step is to configure the environment variables for NPM and Nodejs so that Nodejs can correctly invoke the corresponding module.

Let's start with the first step, create two directories in the D-disk Nodejs directory, respectively, Node_cache and Node_global, which are the cache files for the installation process and the final module configuration location.

After the configuration is complete, perform the following two commands:

NPM config set prefix "D:\Program files\nodejs\node_global"

NPM config set cache "D:\Program Files\nodejs\node_cache"

Configure the NPM Global module directory and the cache directory to the two directories we just created:

4, then we open the CMD command line interface, in the use of the command to install just the cluster module, the command is as follows:

NPM Install Cluster-g

Then open the Node_global directory you just created, and you can see that the cluster directory is now installed under this directory.

5. Then we configure NPM environment variables and NODEJS environment variables.

Right click on the computer icon, select Properties, and then click Advanced System Configuration, pop out of the new window in the lower right corner has an environment path, click in, you can see the Environment path configuration interface, we click New. Then in the pop-up window, the variable name is filled: node_path, variable value: D:\Program files\nodejs\node_modules\, fill in the OK after the point, then you can see our configured NPM environment path.

6, at this time also need to modify some Nodejs default module call path, because the module installed position changed, if the Nodejs command also to the original location to find, is certainly not find the module installed. In the Environment Variables window, select Path, then click Edit in the lower right corner and select NPM, click Edit to change it to: D:\Program files\nodejs\node_global\, then click OK, Save this configuration.

Modified to:

7, this time all the configuration work is completed, and then open a new CMD window. Enter the command first:

Node

Enter the interactive command console for Nodejs and enter:

Require (' cluster ')

If the information of the cluster module can be output normally, all the above configuration will be taken into effect.

8, we also need to do the last job is to add NPM module download warehouse from the default foreign site to the domestic site , so that the speed of the download module can be relatively fast, only need a command, command is:

  NPM--registry https://registry.npm.taobao.org Install cluster

The above command is to temporarily install the cluster module using the address of a local NPM source. As you can see from the figure below, it's very fast. It took only 1 seconds. The first time we installed the above used for almost 6 seconds more, 6 times times faster.

If you want to always use this source's address, then you can use the following command to configure.

  NPM install-g cnpm--registry=registry_url

Registry_url refers to the domestic supply of some NPM warehouse address, commonly used are:

https://registry.npm.taobao.org

http://r.cnpmjs.org/

Both of these can be used. As shown in the second picture below, after the configuration of domestic sources, the installation of a larger module express, speed improvement is very considerable. At this point, the configuration of Nodejs and NPM on Windows is complete.

Installing configuration for node. js, NPM, and environment variables

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.