1.package.json是什么
package.json是用來(lái)管理本地依賴包的一個(gè)文件。使用package.json可以帶來(lái)很多好處花盐。
它可以作為你項(xiàng)目依賴包的一個(gè)說(shuō)明文檔。
它用來(lái)指定你項(xiàng)目中所使用依賴包的版本號(hào)。
它可以讓你更方便地和其他開(kāi)發(fā)者來(lái)分享你的項(xiàng)目。
2.package.json的組成
package.json文件中必須包含的內(nèi)容有:
1."name"
1)all lowercase
2)one word, no spaces
3)dashes and underscores allowed
2."version"
1)in the form of x.x.x
2)follows (semver spec)[[https://docs.npmjs.com/getting-started/semantic-versioning](https://docs.npmjs.com/getting-started/semantic-versioning)]
3.創(chuàng)建一個(gè)package.json
使用npm提供的初始化工具可以方便地創(chuàng)建一個(gè)package.json文件
npm init
然后根據(jù)提示填寫對(duì)應(yīng)項(xiàng)目暇屋。
- name: defaults to author name unless in a git directory, in which case it will be the name of the repository
- version: always 1.0.0
- main: always index.js
- scripts: by default creates a empty test script
- keywords: empty
- author: whatever you provided the CLI
- 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
也可以用初始化命令行來(lái)對(duì)一些參數(shù)進(jìn)行初始化
> npm set init.author.email "wombat@npmjs.com"
> npm set init.author.name "ag_dubs"
> npm set init.license "MIT"
備注:如果項(xiàng)目中的package.json文件中沒(méi)有任何內(nèi)容囱嫩,那么npm將會(huì)使用README.md中的第一行來(lái)代替栽烂。package.json文件的描述可以方便別人查閱你的項(xiàng)目所依賴的packages,并且極大地方便你對(duì)自己項(xiàng)目的管理度帮。
4.指定依賴包
為了指定你在項(xiàng)目中應(yīng)用到的依賴包扬霜,你需要在packages.json文件中將它們列出來(lái)定鸟,有以下兩類依賴包需要被列出。
- "dependencies":這里列出你產(chǎn)品的application中需要用到的依賴包著瓶;
- "devDependencies":這里列出在開(kāi)發(fā)和測(cè)試中所用到的依賴包联予。
這兩個(gè)屬性可以在package.json文件手動(dòng)進(jìn)行編輯,也可以在安裝這些包的時(shí)候用命令行參數(shù)自動(dòng)將她們添加在文件中材原。
在"dependencies"中添加一項(xiàng)的命令:
npm install <package_name> --save
在"devDependencies"中添加一項(xiàng)的命令:
npm install <package_name> --save-dev