一些简单的字段
{
"name": "my-project", // 项目名称
"version": "1.0.0", // 项目版本
"description": "My awesome project", // 项目描述
"scripts": { // 自定义脚本命令
"start": "node index.js" // 启动命令
},
"homepage": "https://my-project.com", // 项目的主页
"keywords": [ // 项目关键词,有助于搜索
"node",
"javascript",
"example"
],
"repository": { // 项目代码仓库信息
"type": "git",
"url": "https://github.com/user/my-project.git"
},
"bugs": { // 提交缺陷的链接
"url": "https://github.com/user/my-project/issues"
},
"license": "MIT", // 项目的许可证
"author": "John Doe", // 项目作者
"contributors": [ // 贡献者列表
{
"name": "Jane Smith",
"email": "jane@example.com"
}
],
"engines": { // 指定 Node.js 和 npm 的版本范围
"node": ">=10.0.0",
"npm": ">=6.0.0"
}
}
以下是不简单的字段
private
默认值: false
。
当 private
字段被设置为 true
时,它有以下作用:
- 防止意外发布:如果你的项目是私有的,而且你不希望它被无意中发布到公共的包管理器(如
npm
),当你尝试运行npm publish
时,npm
将拒绝发布该项目。 - 不被列入默认搜索:如果你的项目是私有的,当其他开发者搜索包时,npm 默认情况下不会将私有包包含在搜索结果中。
main
当其他程序引用你的包时,会根据 main
字段指定的路径来找到入口文件。
默认情况下,如果 main
字段没有设置,Node.js 会尝试查找 index.js 或 index.json 文件作为入口文件。
如果你的入口文件不在根目录下,那么你需要提供相对于根目录的路径。
dependencies & devDependencies
dependencies
:项目的生产依赖项。
devDependencies
:项目的开发依赖项。
版本号规范:
version
必须完全匹配指定的版本号。>version
必须大于指定的版本号。>=version
等于或大于指定的版本号。<version
必须小于指定的版本号。<=version
等于或小于指定的版本号。~version
"约等于"指定的版本号。^version
"兼容于"指定的版本号。1.2.x
匹配1.2.0
、1.2.1
等,但不匹配1.3.0
。http://...
请参阅 “URLs as Dependencies”。*
匹配任意版本。""
(仅空字符串) 等同于*
。version1 - version2
等同于>=version1 <=version2
。range1 || range2
如果满足range1
或range2
中的任意一个,则通过。git...
请参阅 “Git URLs as Dependencies”。user/repo
请参阅 “GitHub URLs”。tag
指定版本的特定标签,查看 npm dist-tag。path/path/path
本地路径。
例如:
{
"dependencies": {
"foo": "1.0.0 - 2.9999.9999",
"bar": ">=1.0.2 <2.1.2",
"baz": ">1.0.2 <=2.3.4",
"boo": "2.0.1",
"qux": "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0",
"asd": "http://asdf.com/asdf.tar.gz",
"til": "~1.2",
"elf": "~1.2.3",
"two": "2.x",
"thr": "3.3.x",
"lat": "latest",
"dyl": "file:../dyl"
}
}
resolutions
如果在项目中存在依赖冲突,其中两个依赖项都使用了不同版本的 lodash
,而你希望强制使用 lodash
的版本为 4.17.21
,你可以这样设置:
{
"resolutions": {
"lodash": "4.17.21"
}
}