一二三三二一,跟我一起念:“啊~”

Windwos安装Node.js和npm的详细步骤

How to Install Node.js and NPM on Windows

Node.js和npm

安装 Node.js 的时候会自动安装 npm ,并且 npm 就是 Node.js 的包管理工具(node package manager 的缩写)。

参考:https://www.npmjs.com/about

所以,安装Node.js默认就会安装npm,安装npm最简单的方式就是安装node。

参考:https://docs.npmjs.com/downloading-and-installing-node-js-and-npm#using-a-node-installer-to-install-nodejs-and-npm

安装Node一般使用2种安装方式,

  • 使用nvm(Node版本控制器 Node version manager)安装

  • 直接去官网下载Node.exe安装包,双击运行即可安装

 

使用安装包安装node

直接从官网下载安装包:https://nodejs.org/en/

一般学习的时候都是下载最新版本(current)

其实安装很简单,除了 Step 4 要勾选 “Automatically install ...” ,其他全部默认 Next... 就好了。

 

详细安装步骤:

Step 1 End-User License Agreement

用户许可,勾选"I accept the terms in the License Agreement",然后Next...

 

Step 2 Destination Folder

目标文件夹,nodejs的安装目录,到时候会把node、npm等执行程序都放到整个目录下,我觉得默认就挺好,直接Next...

我的默认路径是:C:\Program Files\nodejs\

 

Step 3 Custom Setup

自定义设置:


  • Node.js runtime [Install the core Node.js runtime. Node]

  • npm package manager [Install npm, the recommentded package manager for Node.js.]

  • Online documentation shortcuts [Add start menu entries that link the online documentation for Node.js and the Node.js website.]

  • Add to PATH [Add Node.js, npm, and modules that were globally installed by npm to the PATH environment variable.]

    • Node.js and npm [Add Node.js and npm(if installed) to the PATH environment variable.]

    • npm modules [Add modules that are installed globally by npm to the PATH environment variable. This option works for the current user only; other users need o update their]


这里共有4个大项:

  1. 安装Node核心模块

  2. 安装npm

  3. 安装文档

  4. 把Node和npm及其模块添加到环境变量

这里可以看到1和2就是Node核心程序和npm,安装Node的时候,默认会把Node和npm都安装上了。 另外4是把Node和npm添加到环境变量,如果不选,之后在cmd窗口想运行node、npm等执行程序会报错,系统找不到指定文件,当然也可以再手动配置。

这里建议全选,其实也就是默认选项,直接 Next...

 

Step 4 Tools for Native Modules

本机模块工具

Tools for Native Modules Optionally install the tools necessary to compile native modules.

Some npm modules need to compiled from C/C++ when installing. If you want to be able to install such modules, some tools(Python 2 and Visual Studio Build Tools) need to be installed.

Automatically install the necessary tools. Note that this will also install Chocolatey. The script will pop-up in a new window after the installation copletes.

Alternatively, follow the instructions at https://github.com/nodejs/node-gyp#on-windows to install the dependencies yourself.

这是在告诉会给你自动安装2个工具:

  • 构建工具(Python 2和Visual Studio构建工具)

  • Chocolatey

构建工具是因为一些npm模块需要使用C/C++编译,如果想要编译这些模块,则需要安装这个工具。如果不安装这个构建工具,在之后使用 npm 安装模块的时候,会报错:

gyp ERR! find Python

如果见到这个错误,知道是因为没有安装 构建工具。

构建工具和 Chocolatey 都是必装的,如果现在没有安装,可以之后再手动安装,提示里已经给出了相关解释:https://github.com/nodejs/node-gyp#on-windows

该提示还告诉你 Chocolatey 会在后面弹出一个 cmd 来安装。

为了避免以后麻烦,这里强烈建议选择“Automatically install ...” ,然后 Next...

 

Step 5 Ready to install Node.js

Click Install to begin the installation. Click Back to review or change any of your installation settings. Click cancel to exit the wizard.

这一步就是告诉你,如果设置没问题,那就要开始安装了,点击 Next...

Okay, 开始安装,如果系统弹出是否允许控制权限,则按“是”。

 

Step 6 Completed the Node.js Setup Wizard

这是告诉你安装完成了,这几句英文连我都能看懂的话,那应该谁都能看懂了。然后点击 Finish...

 

[可选]Step 7 自动安装 Chocolatey

刚才 Step 4 提到的自动安装npm构建工具和Chocolatey,而Chocolatey就是在这时候通过cmd安装的。

所以如果在 Step 4 选择了“Automatically install...”,按Finish之后,会弹出一个cmd,我们在cmd窗口“按任意键继续”,然后只要等待安装完成即可。

如果这时关闭cmd窗口或者在 Step 4 没有选择“Automatically install...”,则按Finish之后安装步骤全部完成,不会弹出cmd窗口(不会安装这2个工具)。

等待cmd窗口按任意键继续,然后等待自动安装完成,一切都搞定。

 

查看是否安装完成

方法1:安装完成后,查看安装目录,会发现目录下已经有我们安装的node和npm,具体:

  • node.exe 我们需要的node

  • npm 我们需要的npm

  • npx npm的二进制文件执行工具

  • node_modules 模块文件夹

方法2:安装完成后打开 cmd(或powershell),输入命令 node -v 或者 npm -v 来查看node当前版本号或者npm当前版本。

> node -v
v13.1.0
> npm -v
6.12.1

 

常见错误

常见错误1:

‘npm’不是内部或外部命令,也不是可运行的程序

这是因为没有指定运行文件的位置,所以cmd找不到这个运行文件,可以手动添加 Windows 环境变脸。

手动配置 Windows 环境变量:

如果 step 3 没有选择“把Node和npm及其模块添加到环境变量”,那在使用npm或者node命令的时候,因为cmd找不到运行程序,这时候需要可以手动配置 Windows 环境变量。

(当然,还可以卸载了重新装,记得在 step 3 把所有安装想都装上)

前面说过,我的默认安装路径是 C:\Program Files\nodejs\ ,所以我们要添加的环境变量就是这个路径,添加完成之后,Windows 系统就能通过环境变量找到这个执行文件。

操作方式:

  1. 右键“我的电脑”

  2. 选择“属性”

  3. 在左侧栏选择“高级系统设置”

  4. 选中选项卡“高级”

  5. 点击右下角“环境变量(N)”

  6. 系统环境变量(s) ,双击变量名为“path”哪一行,添加 C:\Program Files\nodejs\

  7. 然后再打开 cmd 运行命令则一切正常。

> node -v
v13.1.0
> npm -v
6.12.1

这样目前看来一切都正常了,不过我发现如果是 step 3 选择自动添加环境变量的话,在“{user}的用户变量(U)”,也会配置一个环境变量。

双击“{user}的用户变量(U)”下的变量名为“Path”哪一行,添加值:

C:\Users\{user}\AppData\Roaming\npm

{user} 是你系统的当前用户名

 

常见错误2:

gyp ERR! find Python

这个错误的详细信息其实提示的很明确,就是没有安装 Python。

这个错误详细信息以及如何解决,如何手动安装这些工具,看这里:


当然,node安装卸载非常方便,如果发现漏了任何文件或者设置,有个简单的办法就是卸载了重新安装。

posted @ 2019-12-06 10:32  LucioLu  阅读(15339)  评论(1编辑  收藏  举报

正在研究中