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

How to Install Node.js and NPM on Windows

Node和npm

由于Node.js里包含npm,安装node的时候会自动将npm也安装在本地,而且npm就是Node.js的包管理工具(package manager),npm = node package manager
参考:https://www.npmjs.com/about
因此,安装node默认就会安装npm,安装npm最简单的方式就是安装node。
参考:https://docs.npmjs.com/downloading-and-installing-node-js-and-npm#using-a-node-installer-to-install-nodejs-and-npmnode

安装Node通常使用2种安装方式,git

  • 使用nvm(Node版本控制器 Node version manager)安装
  • 直接去官网下载Node.exe安装包,双击运行便可安装

使用安装包安装node

直接从官网下载安装包:https://nodejs.org/en/
通常学习的时候都是下载最新版本(current)github

下面有详细的安装步骤,其实很简单,除了 Step 4 要勾选 “Automatically install ...” ,其余所有默认 Next... 就行了。web

安装有以下步骤:
Step 1 End-User License Agreement
用户许可,勾选"I accept the terms in the License Agreement",而后Next...shell

Step 2 Destination Folder
目标文件夹,nodejs的安装目录,到时候会把node、npm等执行程序都放到整个目录下,我以为默认就挺好,直接Next...
个人默认路径是:C:\Program Files\nodejs\npm

Step 3 Custom Setup
自定义设置:windows

  • 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
本机模块工具ui

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安装卸载很是方便,若是发现漏了任何文件或者设置,有个简单的办法就是卸载了从新安装。

相关文章
相关标签/搜索