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安裝卸載非常方便,如果發現漏了任何文件或者設置,有個簡單的辦法就是卸載了重新安裝。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM