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. 并不是说NodeJS和NPM安装好了以后就能直接使用了,这也是新手经常犯的一个错误之一。因为默认情况下,NPM安装的模块并不会安装到NodeJS的程序目录,比如上面安装的时候我们设置的D:\Program Files\nodejs\目录,我们用个示例来看一下,我们先用npm安装一个cluster模块,如下面第一张图所示,执行命令:

    npm install cluster

    从图中可以看到,默认情况下把一下信息保存到C:\Users\v1\目录下,我们打开这个目录看看。可以看到这个目录底下有个node_modules目录,点开一看,如下面第二张图所示:

    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
  2. 可以看到把cluster目录装到了这个目录下面,这就说明,如果不修改npm的模块安装目录,那么它默认情况下都会安装到这里,随着你测试开发各种不同的项目,安装的模块越来越多,那么这个文件夹的体积会越来越大,直到占满你的C盘。这就是为什么要修改npm的配置的原因。

    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
  3. 这里我们要分两步,第一步修改NPM的缓存目录和全局目录路径,将对应的模块目录改到D盘nodejs的安装目录,第二步是配置npm和nodejs的环境变量,这样nodejs才能正确地调用对应的模块。

    我们先来做第一步,在D盘nodejs目录下创建两个目录,分别是node_cache和node_global,这是用来放安装过程的缓存文件以及最终的模块配置位置。配置完成后,执行下面这两个命令:

    npm config set prefix "D:\Program Files\nodejs\node_global"npm config set cache "D:\Program Files\nodejs\node_cache"

    将npm的全局模块目录和缓存目录配置到我们刚才创建的那两个目录:

    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
  4. 然后我们打开cmd命令行界面,在使用命令安装刚才的cluster模块,命令如下:

    npm install cluster -g

    然后打开刚才创建的node_global目录,可以看到此时cluster目录就安装到这个目录底下了。

    NodeJS, NPM installation and configuration steps (windows version)
    NodeJS, NPM installation and configuration steps (windows version)
  5. 然后我们来配置npm的环境变量和nodejs的环境变量。在计算机图标上点右键,选属性,然后点击高级系统配置,弹出来的新窗口右下角有个环境路径,点击去,就能看到环境路径的配置界面,我们点击新建。然后在弹出来的窗口里,变量名填:NODE_PATH

    变量值填:D:\Program Files\nodejs\node_modules\

    填写好后点确定,然后就能看到我们配置好的NPM环境路径。

    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. 此时还需要修改一些nodejs默认的模块调用路径,因为模块的安装位置变了,如果nodejs的命令还到原来的位置去找,肯定是找不到安装的模块了。我们在环境变量窗口,选择Path,然后点击右下角的编辑,然后选择npm那个。点击右边的编辑,将其修改为:D:\Program Files\nodejs\node_global\

    然后点确定,保存这个配置。

    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. 这个时候所有的配置工作才算完成了,然后打开一个新的cmd窗口。先输入命令:

    node

    进入nodejs的交互式命令控制台,然后输入:

    require('cluster')

    如果能正常输出cluster模块的信息,说明上面的所有配置就算生效了。

    NodeJS, NPM installation and configuration steps (windows version)
  8. 我们还需要做的最后一个工作是,将npm的模块下载仓库从默认的国外站点改为国内的站点,这样下载模块的速度才能比较快,只需要一个命令即可,命令是:

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

    上面的命令是临时使用国内一家npm源的地址来安装cluster模块。从下面图中可以看到,速度非常快。只用了不到0.8秒。而我们上面第一次安装的时候用了差不多3秒多,速度快了3倍多。

    如果想一直使用这个源的地址,那么可以使用下面这个命令来配置。

    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=324502719&siteId=291194637