NodeJS, NPM installation and configuration steps (windows version)

Node.js is a JavaScript runtime environment based on the Chrome V8 engine. Node.js uses an event-driven, non-blocking I/O model, making it lightweight and efficient. Node.js uses the package manager npm to manage the installation, configuration, deletion and other operations of all modules. It is very convenient to use, but it is still a little complicated to configure the use environment of npm. Follow me to learn about the windows system. Configure NodeJS and NPM on it.

NodeJS, NPM installation and configuration steps (windows version)

Tools/Materials

  • Windows 10 Pro

Install NodeJS and NPM

  1. 1

    Open the official website of NodeJS. By default, the home page provides a download link for the Windows version. We download the 8.9.4 LTS version. LTS stands for long-term support version. Generally, novices recommend using this version, because this version is used by the most people, and problems can be solved. The probability of finding a solution is high. As shown below:

    NodeJS, NPM installation and configuration steps (windows version)
  2. 2

    After the download is complete, double-click the downloaded nodejs-v8.9.4-x64 file to start the installation, click next to enter the next step, check the agreement to agree, and continue to the next step. Then choose the installation location, the default is C drive, change it to D drive installation, this is a good habit, it is recommended to keep it. Then enter the module configuration step.

    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
  3. 3

    This step is to choose which modules to install. The default is to install all of them. For beginners, it is recommended to install all of them. Click on the + sign in front of the add path option, and we can see that the command paths of the two modules NodeJS and NPM will be added to the system path, which is very convenient for us. Click next to continue to the next step, then confirm the information, click Install to start the installation, and then the program starts to copy files and a series of steps. until the installation is complete.

    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
  4. 4

    After the installation is complete, click finish to end the installation process, then right-click on the desktop icon and click Run. After entering cmd, hit Enter, and in the command line interface that opens, enter the commands in sequence:

    node -v

    npm -v

    If the version number is output correctly, it means that our NodeJS and NPM are installed, as shown in the following figure:

    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
    END

Configure NodeJS and NPM

  1. It's not that NodeJS and NPM can be used directly after installation, which is one of the mistakes that newbies often make. Because by default, the modules installed by NPM will not be installed in the program directory of NodeJS, such as the D:\Program Files\nodejs\ directory that we set when installing above. Let's take a look at an example. Let's install it with npm first. A cluster module, as shown in the first picture below, executes the command:

    npm install cluster

    As you can see from the figure, the following information is saved to the C:\Users\v1\ directory by default, let's 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:

    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
  2. You can see that the cluster directory is installed under this directory, which means that if you do not modify the npm module installation directory, it will be installed here by default. As you test and develop various projects, the more modules you install, the more modules you install. The more you add, the bigger the folder will be until it fills up your C drive. That's why you need to modify npm's configuration.

    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
  3. Here we have two steps. The first step is to modify the NPM cache directory and global directory path, and the corresponding module directory is changed to the nodejs installation directory of the D disk. The second step is to configure the environment variables of npm and nodejs, so that nodejs can be correct. to call the corresponding module.

    Let's do the first step, create two directories in the nodejs directory of the D disk, node_cache and node_global, which are used to store the cache files of the installation process and the final module configuration location. After the configuration is complete, execute 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 npm's global module directory and cache directory to the two directories we just created:

    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
  4. Then we open the cmd command line interface, and use the command to install the cluster module just now, 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 installed under this directory.

    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
  5. Then we configure the environment variables of npm and nodejs. Right-click on the computer icon, select Properties, and then click Advanced System Configuration. There is an environment path in the lower right corner of the new window that pops up. Click Go, you can see the configuration interface of the environment path, and we click New. Then in the pop-up window, fill in the variable name: NODE_PATH

    Fill in the variable value: D:\Program Files\nodejs\node_modules\

    After filling in, click OK, and then you can see the NPM environment path we configured.

    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
  6. At this time, it is also necessary to modify some default module calling paths of nodejs, because the installation location of the module has changed. If the command of nodejs is still in the original location, the installed module cannot be found. We are in the environment variables window, select Path, then click Edit in the lower right corner, and then select the npm one. Click Edit on the right and change it to: D:\Program Files\nodejs\node_global\

    Then click OK to save the configuration.

    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
  7. At this time, all the configuration work is completed, and then a new cmd window is opened. First enter the command:

    node

    Go to the interactive command console of nodejs and enter:

    require('cluster')

    If the information of the cluster module can be output normally, it means that all the above configurations are valid.

    NodeJS, NPM installation and configuration steps (windows version)
  8. The last work we need to do is to change the npm module download repository from the default foreign site to a domestic site, so that the download speed of the module can be faster, only one command is needed, the command is:

    npm --registry https://registry.npm.taobao.org install cluster

    The above command is to temporarily use the address of a domestic npm source to install the cluster module. As you can see from the picture below, the speed is very fast. It only took less than 0.8 seconds. When we installed it for the first time, it took almost 3 seconds, and the speed was more than 3 times faster.

    If you want to use the source address all the time, you can use the following command to configure it.

    npm install -g cnpm --registry=registry_url

    registry_url refers to some npm warehouse addresses provided in China. The commonly used ones are:

    https://registry.npm.taobao.org

    http://r.cnpmjs.org/

    Both of these can be used. As shown in the second picture below, after configuring the domestic source and installing a larger module express, it only took a little more than 6 seconds, and the speed improvement is still very considerable. At this point, the configuration of nodejs and npm on windows is complete.

    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
    END

Precautions

  • The environment path must be configured accurately
  • It is recommended to replace the download warehouse with a domestic one, which will be much faster.
  • I am original, please declare when reprinting.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324502598&siteId=291194637