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 捆绑发布。人们普遍以为 npm 是一个首字母缩写词,但其实不是。据 npm 的 FAQ 所说,它提供了一种简易的方式来为你的应用程序安装模块,并且包括所有必要的依赖模块。Node 依靠 package.json 规约来进行包的配置。在服务器端 JavaScript 项目中,使用 npm 来拖入所需的开源依赖库是很普遍的做法;但最近也有人开始把它用于客户端项目。
(译注:好吧,八卦一下。npm 实际上就是“Node Package Manager”的缩写,没有第二种含义,你不用怀疑自己的智商。只不过 npm 官方的这些人否认了这一点,并且强调这个词应该全小写,具体可以参见 npm 的 FAQ,看起来很二很扯淡。不过实际上背景是这样的,软件业内有一股另类风气,就是开发者很热衷于把自己的软件名解释为一个很扯的递归缩写,比如 LAME 就声称自己是“Lame Aint an MP3 Encoder”的缩写,可是地球人都知道你丫就是啊!又比如 GNU 声称“GNU's Not Unix”,一定要与 UNIX 划清界限,等等等等。于是,npm 官方的这群奇异动物借自己的项目对此吐槽了一把。你可能注意到了,本文作者也提到了“npm 的 FAQ”这个梗。)
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 有很多指令,相关文档也很详尽,不过对本书来说,只需要向你介绍其中最常修改的那些指令,就足以让一个常规应用跑起来了:
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:
如果你想构建一个 Node 应用,首先要做的事情就是创建一个服务器。有一个最简单的方法,就是使用 Express,它是一个精简的 Node 应用程序框架。在开始之前,你应该优先考虑最新的版本。当你读到这里的时候,本书使用的版本应该已经不是最新版了。
$ npm info express
3.0.0rc5
Now you can add it to your `package.json` file: 现在你可以把它加入到你的 `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.
请注意 Express 的版本被指定为 3.0.x 。这里的 x 的作用相当于通配符。它将会安装最新的 3.0 版本,无需指定补丁版本号。换句话说就是,“给我最新的 bug 修复版,但 API 不要变”。Node 模块使用 语义化版本管理方式。它的格式是 主版本号.次版本号.补丁版本号。我们从后向前看,bug 修复性的更新将会增加补丁版本号,非破坏性的 API 更新将会增加次版本号,而无法向后兼容的大更新将会增加主版本号。主版本号为零表示这是首个开发版本,模块的公开 API 还不够稳定,并且版本字符串也不会告诉你是否存在向后兼容的问题。
Now that your package and dependencies are declared, return to the console and run:
既然你的包和依赖关系都已经声明好了,让我们回到控制台并且运行一下:
$ 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.
当你运行 npm install 时,系统就会寻找 package.json 文件,然后为你安装所有的依赖库,其中也包括 Express 自己声明的依赖库。
点赞+转发,让更多的人也能看到这篇内容(收藏不点赞,都是耍流氓-_-)
关注 {我},享受文章首发体验!
每周重点攻克一个前端技术难点。更多精彩前端内容私信 我 回复“教程”
原文链接:https://github.com/cssmagic/blog/issues/37