npm 包管理器
npm is a package manager that comes bundled with Node. Contrary to popular belief npm is not an acronym. According to the npm FAQ It provides an easy way to install modules for your Application, including all required dependencies. Node relies on the package.json specification for package configuration. It's common to use npm to pull in all of your project's open source JAVAScript requirements on the server-side, but there is also a movement forming to use npm to pull in open-source dependencies for the client-side, as well.
npm 是一款包管理器,隨 Node 捆綁發(fā)布。人們普遍以為 npm 是一個(gè)首字母縮寫詞,但其實(shí)不是。據(jù) npm 的 FAQ 所說,它提供了一種簡(jiǎn)易的方式來為你的應(yīng)用程序安裝模塊,并且包括所有必要的依賴模塊。Node 依靠 package.json 規(guī)約來進(jìn)行包的配置。在服務(wù)器端 JavaScript 項(xiàng)目中,使用 npm 來拖入所需的開源依賴庫是很普遍的做法;但最近也有人開始把它用于客戶端項(xiàng)目。
(譯注:好吧,八卦一下。npm 實(shí)際上就是“Node Package Manager”的縮寫,沒有第二種含義,你不用懷疑自己的智商。只不過 npm 官方的這些人否認(rèn)了這一點(diǎn),并且強(qiáng)調(diào)這個(gè)詞應(yīng)該全小寫,具體可以參見 npm 的 FAQ,看起來很二很扯淡。不過實(shí)際上背景是這樣的,軟件業(yè)內(nèi)有一股另類風(fēng)氣,就是開發(fā)者很熱衷于把自己的軟件名解釋為一個(gè)很扯的遞歸縮寫,比如 LAME 就聲稱自己是“Lame Aint an MP3 Encoder”的縮寫,可是地球人都知道你丫就是啊!又比如 GNU 聲稱“GNU's Not Unix”,一定要與 UNIX 劃清界限,等等等等。于是,npm 官方的這群奇異動(dòng)物借自己的項(xiàng)目對(duì)此吐槽了一把。你可能注意到了,本文作者也提到了“npm 的 FAQ”這個(gè)梗。)
Directives
指令
npm has a number of well documented directives, but for the purposes of this book, you'll only need to know the ones you'll commonly need to modify in order to get your typical app up and running:
npm 有很多指令,相關(guān)文檔也很詳盡,不過對(duì)本書來說,只需要向你介紹其中最常修改的那些指令,就足以讓一個(gè)常規(guī)應(yīng)用跑起來了:
- name - The name of the package.
- version - Package version number. npm modules must use semantic versioning.
- author - Some information about the author.
- description - A short description of the package.
- keywords - Search terms to help users find the package.
- main - The path of the main package file.
- scripts - A list of scripts to expose to npm. Most projects should define a "test" script that runs with the command npm test. Use it to execute your unit tests.
- repository - The location of the package repository.
- dependencies, bundledDependencies - Dependencies your package will require().
- devDependencies - A list of dependencies that developers will need in order to contribute.
- engines - Specifies which version of Node to use.
- name - 包的名稱。
- version - 包的版本號(hào)。npm 模塊必須使用語義化版本管理方式。
- author - 有關(guān)作者的信息。
- description - 包的簡(jiǎn)要描述。
- keywords - 可以幫助用戶找到這個(gè)包的搜索關(guān)鍵詞。
- main - 包的主文件的所在路徑。
- scripts - 需要暴露給 npm 的腳本的清單。大多數(shù)項(xiàng)目應(yīng)該定義一個(gè) "test" 腳本,可以通過 npm test 命令來運(yùn)行。用這個(gè)命令來執(zhí)行單元測(cè)試。
- repository - 包的代碼倉庫所在的位置。
- dependencies, bundledDependencies - 你的包需要 require() 的依賴庫清單。
- devDependencies - 開發(fā)者所需要的依賴庫清單,以便他們貢獻(xiàn)代碼。
- engines - 指定適用的 Node 版本。
If you want to build a node app, one of the first things you'll need to do is create a server. One of the easiest ways to do that is to use Express, a minimal application framework for Node. Before you begin, you should look at the latest version available. By the time you read this, the version you see here should no longer be the latest:
如果你想構(gòu)建一個(gè) Node 應(yīng)用,首先要做的事情就是創(chuàng)建一個(gè)服務(wù)器。有一個(gè)最簡(jiǎn)單的方法,就是使用 Express,它是一個(gè)精簡(jiǎn)的 Node 應(yīng)用程序框架。在開始之前,你應(yīng)該優(yōu)先考慮最新的版本。當(dāng)你讀到這里的時(shí)候,本書使用的版本應(yīng)該已經(jīng)不是最新版了。
$ npm info express
3.0.0rc5
Now you can add it to your `package.json` file: 現(xiàn)在你可以把它加入到你的 `package.json` 文件中: Example 5-1. `package.json` 示例 5-1. `package.json` ```js { "name": "simple-express-static-server", "version": "0.1.0", "author": "Sandro Padin", "description": "A very simple static file server. For development use only.", "keywords": ["http", "web server", "static server"], "main": "./server.js", "scripts": { "start": "node ./server.js" }, "repository": { "type": "git", "url": "https://github.com/spadin/simple-express-static-server.git" }, "dependencies": { "express": "3.0.x" }, "engines": { "node": ">=0.6" } }
Notice that the express version is specified as "3.0.x". The x acts like a wildcard. It will install the latest 3.0 version, regardless of the patch number. It's another way of saying, "give me bug fixes, but no API changes". Node modules use Semantic Versioning. Read it as Major.Minor.Patch. Working backwards, bug fixes increment the patch version, non-breaking API changes increment the minor version, and backward breaking changes increment the major version. A zero for the major version indicates initial development. The public API should not be considered stable, and there is no indication in the version string for backward-breaking changes.
請(qǐng)注意 Express 的版本被指定為 3.0.x 。這里的 x 的作用相當(dāng)于通配符。它將會(huì)安裝最新的 3.0 版本,無需指定補(bǔ)丁版本號(hào)。換句話說就是,“給我最新的 bug 修復(fù)版,但 API 不要變”。Node 模塊使用 語義化版本管理方式。它的格式是 主版本號(hào).次版本號(hào).補(bǔ)丁版本號(hào)。我們從后向前看,bug 修復(fù)性的更新將會(huì)增加補(bǔ)丁版本號(hào),非破壞性的 API 更新將會(huì)增加次版本號(hào),而無法向后兼容的大更新將會(huì)增加主版本號(hào)。主版本號(hào)為零表示這是首個(gè)開發(fā)版本,模塊的公開 API 還不夠穩(wěn)定,并且版本字符串也不會(huì)告訴你是否存在向后兼容的問題。
Now that your package and dependencies are declared, return to the console and run:
既然你的包和依賴關(guān)系都已經(jīng)聲明好了,讓我們回到控制臺(tái)并且運(yùn)行一下:
$ npm install express@3.0.0rc5 node_modules/express ├── methods@0.0.1 ├── fresh@0.1.0 ├── range-parser@0.0.4 ├── cookie@0.0.4 ├── crc@0.2.0 ├── commander@0.6.1 ├── debug@0.7.0 ├── mkdirp@0.3.3 ├── send@0.1.0 (mime@1.2.6) └── connect@2.5.0 (pause@0.0.1, bytes@0.1.0, send@0.0.4, formidable@1.0.11, qs@0.5.1)
When you run npm install, it will look at package.json and install all of the dependencies for you, including those that were declared by the express package.
當(dāng)你運(yùn)行 npm install 時(shí),系統(tǒng)就會(huì)尋找 package.json 文件,然后為你安裝所有的依賴庫,其中也包括 Express 自己聲明的依賴庫。
希望本文能幫助到您!
點(diǎn)贊+轉(zhuǎn)發(fā),讓更多的人也能看到這篇內(nèi)容(收藏不點(diǎn)贊,都是耍流氓-_-)
關(guān)注 {我},享受文章首發(fā)體驗(yàn)!
每周重點(diǎn)攻克一個(gè)前端技術(shù)難點(diǎn)。更多精彩前端內(nèi)容私信 我 回復(fù)“教程”
原文鏈接:https://github.com/cssmagic/blog/issues/37