package.json文件(包說(shuō)明文件)
- package.json 文件就好像產(chǎn)品的說(shuō)明書(shū)屎暇,里面存放這項(xiàng)目相關(guān)的信息。
npm init
npm init
- 作用:用于生成
package.json文件
根悼,問(wèn)答的形式,需要填寫(xiě)一些基本的信息剩彬。
例:
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (packjson) demo //項(xiàng)目名字
version: (1.0.0) //版本號(hào)
description: hello world //項(xiàng)目描述
entry point: (index.js) //項(xiàng)目入口
test command: //
git repository: //github倉(cāng)庫(kù)
keywords: //npm安裝時(shí)的關(guān)鍵字
author: 少年聽(tīng)雨 //作者
license: (ISC) //協(xié)議
About to write to E:\good good study\NodeJs\packjson\package.json:
{
"name": "demo",
"version": "1.0.0",
"description": "hello world",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "少年聽(tīng)雨",
"license": "ISC"
}
Is this OK? (yes) yes
- 進(jìn)行完上面的配置矿卑,我們就會(huì)的到一個(gè)
package.json
的文件里面包含有我們剛剛填寫(xiě)的信息
- package.json文件
{
"name": "demo",
"version": "1.0.0",
"description": "hello world",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "少年聽(tīng)雨",
"license": "ISC"
}
npm install xxx --save
npm install xxx --save
- 作用:安裝第三方模塊,并將信息保存在
package.json
文件的dependencies
對(duì)象當(dāng)中瀑晒。
例:
- 首先,在命令行輸入
PS E:\good good study\NodeJs\packjson> npm install art-template --save
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN demo@1.0.0 No repository field.
+ art-template@4.13.2
added 33 packages from 133 contributors and audited 40 packages in 11.686s
found 0 vulnerabilities
- 然后,打開(kāi)
package.json文件
轩褐,就會(huì)發(fā)現(xiàn)多了一個(gè)dependencies
對(duì)象
{
"name": "demo",
"version": "1.0.0",
"description": "hello world",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "少年聽(tīng)雨",
"license": "ISC",
"dependencies": {
"art-template": "^4.13.2"
}
}
- 在實(shí)際開(kāi)發(fā)中把介,我們傳一個(gè)項(xiàng)目啸臀,是不傳
node_modules
文件夾的,太大了,我們只需要傳package.json
文件就行了枚碗,我們可以通過(guò)npm install
這個(gè)命令迁酸,來(lái)安裝項(xiàng)目的依賴包券膀。