管理本地安裝的包的最好方法是創建一個package.json文件。
package.json文件會給你提供很多好東西:
1. 它用作你的項目的包依賴管理文檔。
2. 它允許你使用語義化版本管理規則,指定項目中能使用的包的版本。
3. 使你的構建版本可以重新生成,這意味著你可以更易于與其他開發者分享代碼。
## 需求
最少配置項, package.json 必須包括以下幾項:
"name"
* 全部為小寫字母
* 一個單詞,無空格
* 允許半角破折號和下劃線
"version"
* 格式為 x.x.x
* 遵循語義化版本號規則 (semver spec)[https://docs.npmjs.com/getting-started/semantic-versioning]
示例:
~~~
{
"name": "my-awesome-package",
"version": "1.0.0"
}
~~~
## 創建package.json文件
要創建package.json文件,運行以下命令:
~~~
> npm init
~~~
此為初始化項目命令,會在你運行此命令的文件夾根目錄下創建項目配置文件:package.json。同時每行會出現一個問題,你輸入答案后會出來另一個問題。這些問題最終會記錄到package.json文件中。
### “--yes”標簽
擴展的命令行問答式流程不是必須的。通常你可以輕松地使用package.json文件快速配置項目。
你可以運行帶`--yes`或`-y`標簽的`npm init`命令,來生成默認的package.json文件:
~~~
> npm init --yes
~~~
這樣只會問你作者是誰。其他的問題都是用默認值填充的:
~~~
> npm init --yes
~~~
寫入`/home/ag_dubs/my_package/package.json`文件的內容如下:
~~~
{
"name": "my_package",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "ag_dubs",
"license": "ISC",
"repository": {
"type": "git",
"url": "https://github.com/ashleygwilliams/my_package.git"
},
"bugs": {
"url": "https://github.com/ashleygwilliams/my_package/issues"
},
"homepage": "https://github.com/ashleygwilliams/my_package"
}
~~~
* name:默認為作者名字,除非在git目錄中,它會是git倉庫的名字;
* version:版本號,剛初始化的項目總是1.0.0;
* main:總是index.js;
* scripts:默認創建一行空的測試腳本;
* keywords:為空
* author:作者
* license:ISC開源證書
* repository: will pull in info from the current directory, if present
* bugs: will pull in info from the current directory, if present
* homepage: will pull in info from the current directory, if present
你也可以通過`set`命令來設置一些配置項。比如下邊的這些:
~~~
> npm set init.author.email "wombat@npmjs.com"
> npm set init.author.name "ag_dubs"
> npm set init.license "MIT"
~~~
注釋:
If there is no description field in the package.json, npm uses the first line of the README.md or README instead. The description helps people find your package on npm search, so it's definitely useful to make a custom description in the package.json to make your package more discoverable.
## 指定依賴包
To specify the packages your project depends on, you need to list the packages you'd like to use in your package.json file. There are 2 types of packages you can list:
* "dependencies": these packages are required by your application in production
* "devDependencies": these packages are only needed for development and testing
## 手動編輯package.json
You can manually edit your package.json. You'll need to create an attribute in the package object called dependencies that points to an object. This object will hold attributes named after the packages you'd like to use, that point to a semver expression that specifies what versions of that project are compatible with your project.
If you have dependencies you only need to use during local development, you will follow the same instructions as above but in an attribute called devDependencies.
For example: The project below uses any version of the package my_dep that matches major version 1 in production, and requires any version of the package my_test_framework that matches major version 3, but only for development:
~~~
{
"name": "my_package",
"version": "1.0.0",
"dependencies": {
"my_dep": "^1.0.0"
},
"devDependencies" : {
"my_test_framework": "^3.1.0"
}
}
~~~
## `--save` 和 `--save-dev`安裝標記
The easier (and more awesome) way to add dependencies to your package.json is to do so from the command line, flagging the npm install command with either --save or --save-dev, depending on how you'd like to use that dependency.
在命令行中使用這兩個標記,是添加依賴到你的package.json文件的更簡單(也更酷)的方式。
添加package.json依賴的入口:
`npm install <package_name> --save`
添加package.json開發環境依賴的入口:
`npm install <package_name> --save-dev`
## 管理依賴包的版本
npm使用語義化版本管理依賴包,也就是我們常說的“SemVer”。
如果在項目文件夾下有`package.json`文件,你在此文件夾下運行命令`npm install`,npm就會檢查文件中列出的依賴包,并下載所有滿足語義化規則的最新版本的依賴包。
要學習更多關于語義化版本管理的內容,請查看本章第13節——語義化版本號。