package.json文件(包说明文件)
- package.json 文件就好像产品的说明书,里面存放这项目相关的信息。
npm init
npm init
- 作用:用于生成
package.json文件
,问答的形式,需要填写一些基本的信息。
例:
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 //项目名字
version: (1.0.0) //版本号
description: hello world //项目描述
entry point: (index.js) //项目入口
test command: //
git repository: //github仓库
keywords: //npm安装时的关键字
author: 少年听雨 //作者
license: (ISC) //协议
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": "少年听雨",
"license": "ISC"
}
Is this OK? (yes) yes
- 进行完上面的配置,我们就会的到一个
package.json
的文件里面包含有我们刚刚填写的信息
- package.json文件
{
"name": "demo",
"version": "1.0.0",
"description": "hello world",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "少年听雨",
"license": "ISC"
}
npm install xxx --save
npm install xxx --save
- 作用:安装第三方模块,并将信息保存在
package.json
文件的dependencies
对象当中。
例:
- 首先,在命令行输入
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
- 然后,打开
package.json文件
,就会发现多了一个dependencies
对象
{
"name": "demo",
"version": "1.0.0",
"description": "hello world",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "少年听雨",
"license": "ISC",
"dependencies": {
"art-template": "^4.13.2"
}
}
- 在实际开发中,我们传一个项目,是不传
node_modules
文件夹的,太大了,我们只需要传package.json
文件就行了,我们可以通过npm install
这个命令,来安装项目的依赖包。