NPM 使用介紹菜鳥教程_第1頁
NPM 使用介紹菜鳥教程_第2頁
NPM 使用介紹菜鳥教程_第3頁
NPM 使用介紹菜鳥教程_第4頁
NPM 使用介紹菜鳥教程_第5頁
已閱讀5頁,還剩17頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、NPM 使用介紹 菜鳥教程 NPM 使用介紹NPM是隨同NodeJS一起安裝的包管理工具,能解決NodeJS代碼部署上的很多問題,常見的使用場景有以下幾種:允許用戶從NPM服務(wù)器下載別人編寫的第三方包到本地使用。允許用戶從NPM服務(wù)器下載并安裝別人編寫的命令行程序到本地使用。允許用戶將自己編寫的包或命令行程序上傳到NPM服務(wù)器供別人使用。由于新版的nodejs已經(jīng)集成了npm,所以之前npm也一并安裝好了。同樣可以通過輸入 npm -v 來測試是否成功安裝。命令如下,出現(xiàn)版本提示表示安裝成功:$ npm -v2.3.0如果你安裝的是舊版本的 npm,可以很容易得通過 npm 命令來升級,命令如

2、下:$ sudo npm install npm -g/usr/local/bin/npm - /usr/local/lib/node_modules/npm/bin/npm-cli.jsnpm2.14.2 /usr/local/lib/node_modules/npm如果是 Window 系統(tǒng)使用以下命令即可:npm install npm -g使用淘寶鏡像的命令:cnpm install npm -g使用 npm 命令安裝模塊npm 安裝 Node.js 模塊語法格式如下:$ npm install 以下實(shí)例,我們使用 npm 命令安裝常用的 Node.js web框架模塊 express

3、:$ npm install express安裝好之后,express 包就放在了工程目錄下的 node_modules 目錄中,因此在代碼中只需要通過 require(express) 的方式就好,無需指定第三方包路徑。var express = require(express);全局安裝與本地安裝npm 的包安裝分為本地安裝(local)、全局安裝(global)兩種,從敲的命令行來看,差別只是有沒有-g而已,比如npm install express # 本地安裝npm install express -g # 全局安裝如果出現(xiàn)以下錯誤:npm err! Error: connect E

4、CONNREFUSED :8087 解決辦法為:$ npm config set proxy null本地安裝1. 將安裝包放在 ./node_modules 下(運(yùn)行 npm 命令時所在的目錄),如果沒有 node_modules 目錄,會在當(dāng)前執(zhí)行 npm 命令的目錄下生成 node_modules 目錄。2. 可以通過 require() 來引入本地安裝的包。全局安裝1. 將安裝包放在 /usr/local 下或者你 node 的安裝目錄。2. 可以直接在命令行里使用。如果你希望具備兩者功能,則需要在兩個地方安裝它或使用 npm link。接下來我們使用全局方式安裝 e

5、xpress$ npm install express -g安裝過程輸出如下內(nèi)容,第一行輸出了模塊的版本號及安裝位置。express4.13.3 node_modules/express escape-html1.0.2 range-parser1.0.2 merge-descriptors1.0.0 array-flatten1.1.1 cookie0.1.3 utils-merge1.0.0 parseurl1.3.0 cookie-signature1.0.6 methods1.1.1 fresh0.3.0 vary1.0.1 path-to-regexp0.1.7 content-ty

6、pe1.0.1 etag1.7.0 serve-static1.10.0 content-disposition0.5.0 depd1.0.1 qs4.0.0 finalhandler0.4.0 (unpipe1.0.0) on-finished2.3.0 (ee-first1.1.1) proxy-addr1.0.8 (forwarded0.1.0, ipaddr.js1.0.1) debug2.2.0 (ms0.7.1) type-is1.6.8 (media-typer0.3.0, mime-types2.1.6) accepts1.2.12 (negotiator0.5.3, mime

7、-types2.1.6) send0.13.0 (destroy1.0.3, statuses1.2.1, ms0.7.1, mime1.3.4, http-errors1.3.1)查看安裝信息你可以使用以下命令來查看所有全局安裝的模塊:$ npm list -g cnpm4.3.2 auto-correct1.0.0 bagpipe0.3.5 colors1.1.2 commander2.9.0 graceful-readlink1.0.1 cross-spawn0.2.9 lru-cache2.7.3如果要查看某個模塊的版本號,可以使用命令如下:$ npm list gruntprojec

8、tNameprojectVersion /path/to/project/folder grunt0.4.1使用 package.jsonpackage.json 位于模塊的目錄下,用于定義包的屬性。接下來讓我們來看下 express 包的 package.json 文件,位于 node_modules/express/package.json 內(nèi)容: name: express, description: Fast, unopinionated, minimalist web framework, version: 4.13.3, author: name: TJ Holowaychuk,

9、email: tjvision-media.ca , contributors: name: Aaron Heckmann, email: aaron.heckmann+ , name: Ciaran Jessup, email: , name: Douglas Christopher Wilson, email: , name: Guillermo Rauch, email: , name: Jonathan Ong, email: mejonglebe

10、 , name: Roman Shtylman, email: shtylman+ , name: Young Jae Sim, email: hanulhanul.me , license: MIT, repository: type: git, url: git+/strongloop/express.git , homepage: /, keywords: express, framework, sinatra, web, rest, restful, router,

11、 app, api , dependencies: accepts: 1.2.12, array-flatten: 1.1.1, content-disposition: 0.5.0, content-type: 1.0.1, cookie: 0.1.3, cookie-signature: 1.0.6, debug: 2.2.0, depd: 1.0.1, escape-html: 1.0.2, etag: 1.7.0, finalhandler: 0.4.0, fresh: 0.3.0, merge-descriptors: 1.0.0, methods: 1.1.1, on-finish

12、ed: 2.3.0, parseurl: 1.3.0, path-to-regexp: 0.1.7, proxy-addr: 1.0.8, qs: 4.0.0, range-parser: 1.0.2, send: 0.13.0, serve-static: 1.10.0, type-is: 1.6.6, utils-merge: 1.0.0, vary: 1.0.1 , devDependencies: after: 0.8.1, ejs: 2.3.3, istanbul: 0.3.17, marked: 0.3.5, mocha: 2.2.5, should: 7.0.2, superte

13、st: 1.0.1, body-parser: 1.13.3, connect-redis: 2.4.1, cookie-parser: 1.3.5, cookie-session: 1.2.0, express-session: 1.11.3, jade: 1.11.0, method-override: 2.3.5, morgan: 1.6.1, multiparty: 4.1.2, vhost: 3.0.1 , engines: node: = 0.10.0 , files: LICENSE, History.md, Readme.md, index.js, lib/ , scripts

14、: 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/_mo

15、cha - -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/ , gitHead: ef7ad681b245fbace94f6bcb8e275bbb8e, bugs: url: /strongloop/express/issues , _id: express4.13.3, _

16、shasum: ddb2f1fb4502bf33598d2b032bca6c80a3, _from: express*, _npmVersion: 1.4.28, _npmUser: name: dougwilson, email: , maintainers: name: tjholowaychuk, email: tjvision-media.ca , name: jongleberry, email: , name: dougwilson, email: dougsomethingdoug

17、.com , name: rfeng, email: , name: aredridel, email: , name: strongloop, email: , name: defunctzombie, email: , dist: shasum: ddb2f1fb4502bf33598d2b032bca6c80a3, tarball: /express/-/express-4.13.3.

18、tgz , directories: , _resolved: /express/-/express-4.13.3.tgz, readme: ERROR: No README data found!Package.json 屬性說明name - 包名。version - 包的版本號。description - 包的描述。homepage - 包的官網(wǎng) url 。author - 包的作者姓名。contributors - 包的其他貢獻(xiàn)者姓名。dependencies - 依賴包列表。如果依賴包沒有安裝,npm 會自動將依賴包安裝在 node_m

19、odule 目錄下。repository - 包代碼存放的地方的類型,可以是 git 或 svn,git 可在 Github 上。main - main 字段指定了程序的主入口文件,require(moduleName) 就會加載這個文件。這個字段的默認(rèn)值是模塊根目錄下面的 index.js。keywords - 關(guān)鍵字卸載模塊我們可以使用以下命令來卸載 Node.js 模塊。$ npm uninstall express卸載后,你可以到 /node_modules/ 目錄下查看包是否還存在,或者使用以下命令查看:$ npm ls更新模塊我們可以使用以下命令更新模塊:$ npm update

20、express搜索模塊使用以下來搜索模塊:$ npm search express創(chuàng)建模塊創(chuàng)建模塊,package.json 文件是必不可少的。我們可以使用 NPM 生成 package.json 文件,生成的文件包含了基本的結(jié)果。$ npm initThis utility will walk you through creating a package.json file.It only covers the most common items, and tries to guess sensible defaults.See npm help json for definitive do

21、cumentation on these fieldsand exactly what they do.Use npm install -save afterwards to install a package andsave it as a dependency in the package.json file.Press C at any time to : (node_modules) runoob # 模塊名version: (1.0.0) description: Node.js 測試模塊() # 描述entry point: (inde

22、x.js) test command: make testgit repository: /runoob/runoob.git # Github 地址keywords: author: license: (ISC) About to write to /node_modules/package.json: # 生成地址 name: runoob, version: 1.0.0, description: Node.js 測試模塊(), Is this ok? (yes) yes以上的信息,你需要根據(jù)你自己的情況輸入。在最后輸入 ye

23、s 后會生成 package.json 文件。接下來我們可以使用以下命令在 npm 資源庫中注冊用戶(使用郵箱注冊):$ npm adduserUsername: mcmohdPassword:Email: (this IS public) 接下來我們就用以下命令來發(fā)布模塊:$ npm publish如果你以上的步驟都操作正確,你就可以跟其他模塊一樣使用 npm 來安裝。版本號使用NPM下載和發(fā)布代碼時都會接觸到版本號。NPM使用語義版本號來管理代碼,這里簡單介紹一下。語義版本號分為X.Y.Z三位,分別代表主版本號、次版本號和補(bǔ)丁版本號。當(dāng)代碼變更時,版本號按以下原則更新。如果只是修復(fù)bug,需要更新Z位。如果是新增了功能,但是向下兼容,需要更新Y位。如果有大變動,向下不兼容,需要更新X位。版本號有了這個保證后,在申明第三方包依賴時,除了可依賴于一個固定版本號外,還可依賴于某個范圍的版本號。例如argv: 0.0.x表示依賴于0.0.

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論