一步一步在 Windows 安裝配置 Node.js 和 npm
零、Node.js 與 npm 的關係
Node.js 是一個基於 Chrome V8 引擎的 JavaScript 運行環境。 我們如果想愉快的使用 JavaScript 做一些東西,node.js 是必須安裝的工具。
在實際工作中,我們會用到很多大佬寫的 JavaScript 包或模塊。如果每次都根據包命搜索官網或GitHub,下載代碼,解壓,再使用,而且要解決包之間的依賴關係,隨着項目需要的包越來越多,那麼這將會是一項非常繁瑣工作。爲了方便地搜索並安裝這些包,我們急需一個包管理器,NPM隨後被開發出來。它幫我們安裝,刪除,更新,最主要是解決這些包的依賴關係。總之,npm 相當於開發者的管家,讓我們避免解決這些瑣事,專注於 JavaScript 的應用開發。
一、下載及安裝
1.下載
- https://nodejs.org/en/download/current/
2.安裝
-
next
-
next
-
這裏選擇安裝目錄
選安裝目錄時候,需要注意沒必要安裝在C盤,後續安裝包會佔用資源。建議在其他盤創建目錄,然後以node版本號命名的方式安裝。比如我的爲
E:\node\node-12.16.2
-
next
-
next
-
install
-
安裝完成
-
安裝後,點擊回車關閉窗口
3.檢測是否安裝成功
-
使用
win+R
打開cmd: -
輸入
node --version
-
如果顯示如下內容,node 安裝成功
-
輸入
npp --version
-
如果顯示如下內容,npm 安裝成功
二、配置
配置 npm 的模塊全局存放路徑和緩存路徑
-
打開cmd,輸入命令
npm config set prefix "E:\nodejs\node-12.16.2" npm config set cache"E:\nodejs\node-12.16.2"
-
現在嘗試安裝一個包
npm install express -g
這裏的
-g
指的是全局安裝。會安裝到我們設置的目錄下比如E:\nodejs\node-12.16.2\node_modules
如果沒有這個參數的命令
npm install express
叫做本地安裝。將安裝包放在 ./node_modules 下(運行 npm 命令時所在的目錄),如果沒有 node_modules 目錄,會在當前執行 npm 命令的目錄下生成 node_modules 目錄。 -
我們檢查包的安裝位置
E:\nodejs\node-12.16.2\node_modules\express
cnpm 的安裝
-
在實際使用時,安裝部分包會發現npm 奇慢無比,這是因爲npm的包倉庫在國外。爲了解決這個問題,需要使用淘寶提供的cnpm作爲管理工具,下面我們來安裝
-
打開cmd,輸入
npm install -g cnpm --registry=https://registry.npm.taobao.org
-
安裝完成,我們測試是否安裝成功
cnpm -v
-
如果出現如下,則安裝成功
三、NPM 使用
1.安裝模塊
npm install express # 本地安裝
npm install express -g # 全局安裝
2.卸載
npm uninstall express
3.更新
npm update express
4.搜索
npm search express
5.查看安裝信息
npm list -g
6.查看模塊版本號
npm list grunt
7.Package.json
package.json 位於項目的node_modules
目錄下,用於管理項目用到的包。
同時,package.json 位於包的目錄下,用於定義包的屬性。看下剛剛安裝的 express 包的 package.json 文件,位於 E:\nodejs\node-12.16.2\node_modules\express/package.json
內容及每個字段的介紹:
- name: 包名
- version: 包的版本號
- description: 包的描述
- homepage: 包的官網 url
- author: 包的作者姓名
- contributors: 包的其他貢獻者姓名
- dependencies: 依賴包列表。如果依賴包沒有安裝,npm 會自動將依賴包安裝在 node_module 目錄下
- repository: 包代碼存放的地方的類型,可以是 git 或 svn,git 可在 Github 上
- main: main 字段指定了程序的主入口文件,require(‘moduleName’) 就會加載這個文件。這個字段的默認值是模塊根目錄下面的 index.js
- keywords: 關鍵字
{
"_from": "express",
"_id": "[email protected]",
"_inBundle": false,
"_integrity": "sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==",
"_location": "/express",
"_phantomChildren": {},
"_requested": {
"type": "tag",
"registry": true,
"raw": "express",
"name": "express",
"escapedName": "express",
"rawSpec": "",
"saveSpec": null,
"fetchSpec": "latest"
},
"_requiredBy": [
"#USER"
],
"_resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz",
"_shasum": "4491fc38605cf51f8629d39c2b5d026f98a4c134",
"_spec": "express",
"_where": "C:\\Users\\Administrator",
"author": {
"name": "TJ Holowaychuk",
"email": "[email protected]"
},
"bugs": {
"url": "https://github.com/expressjs/express/issues"
},
"bundleDependencies": false,
"contributors": [
{
"name": "Aaron Heckmann",
"email": "[email protected]"
},
{
"name": "Ciaran Jessup",
"email": "[email protected]"
},
{
"name": "Douglas Christopher Wilson",
"email": "[email protected]"
},
{
"name": "Guillermo Rauch",
"email": "[email protected]"
},
{
"name": "Jonathan Ong",
"email": "[email protected]"
},
{
"name": "Roman Shtylman",
"email": "[email protected]"
},
{
"name": "Young Jae Sim",
"email": "[email protected]"
}
],
"dependencies": {
"accepts": "~1.3.7",
"array-flatten": "1.1.1",
"body-parser": "1.19.0",
"content-disposition": "0.5.3",
"content-type": "~1.0.4",
"cookie": "0.4.0",
"cookie-signature": "1.0.6",
"debug": "2.6.9",
"depd": "~1.1.2",
"encodeurl": "~1.0.2",
"escape-html": "~1.0.3",
"etag": "~1.8.1",
"finalhandler": "~1.1.2",
"fresh": "0.5.2",
"merge-descriptors": "1.0.1",
"methods": "~1.1.2",
"on-finished": "~2.3.0",
"parseurl": "~1.3.3",
"path-to-regexp": "0.1.7",
"proxy-addr": "~2.0.5",
"qs": "6.7.0",
"range-parser": "~1.2.1",
"safe-buffer": "5.1.2",
"send": "0.17.1",
"serve-static": "1.14.1",
"setprototypeof": "1.1.1",
"statuses": "~1.5.0",
"type-is": "~1.6.18",
"utils-merge": "1.0.1",
"vary": "~1.1.2"
},
"deprecated": false,
"description": "Fast, unopinionated, minimalist web framework",
"devDependencies": {
"after": "0.8.2",
"connect-redis": "3.4.1",
"cookie-parser": "~1.4.4",
"cookie-session": "1.3.3",
"ejs": "2.6.1",
"eslint": "2.13.1",
"express-session": "1.16.1",
"hbs": "4.0.4",
"istanbul": "0.4.5",
"marked": "0.6.2",
"method-override": "3.0.0",
"mocha": "5.2.0",
"morgan": "1.9.1",
"multiparty": "4.2.1",
"pbkdf2-password": "1.2.1",
"should": "13.2.3",
"supertest": "3.3.0",
"vhost": "~3.0.2"
},
"engines": {
"node": ">= 0.10.0"
},
"files": [
"LICENSE",
"History.md",
"Readme.md",
"index.js",
"lib/"
],
"homepage": "http://expressjs.com/",
"keywords": [
"express",
"framework",
"sinatra",
"web",
"rest",
"restful",
"router",
"app",
"api"
],
"license": "MIT",
"name": "express",
"repository": {
"type": "git",
"url": "git+https://github.com/expressjs/express.git"
},
"scripts": {
"lint": "eslint .",
"test": "mocha --require test/support/env --reporter spec --bail --check-leaks test/ test/acceptance/",
"test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/ test/acceptance/",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/ test/acceptance/",
"test-tap": "mocha --require test/support/env --reporter tap --check-leaks test/ test/acceptance/"
},
"version": "4.17.1"
}