本文根据 Devhints / package.json 翻译。代码示例及版本范围沿用原文。原文标注更新日期:2020-06-23。
基础
{
"name": "expo",
"description": "My package",
"version": "0.1.0",
"license": "MIT",
"keywords": ["http", "server"],
"author": "Rico Sta. Cruz <rstacruz@users.noreply.github.com>",
"engines": {
"node": ">=0.8.0"
},
"main": "index",
"bin": {
"command": "./bin/command"
},
"repository": {
"type": "git",
"url": "https://github.com/rstacruz/___.git"
},
}
高亮显示的行是必需的。
依赖项 (Dependencies)
"dependencies": {
"colors": "*",
"flatiron": "0.1.x",
"flatiron": "~0.1.0",
"plates": "https://github.com/user/project/tarball/branch",
"stuff": "git://github.com/user/project.git#commit-ish"
},
"devDependencies": { ··· },
"peerDependencies": { ··· },
"optionalDependencies": { ··· },
参见 Semver 速查表 关于版本范围的说明。
脚本
"scripts": {
"start": "node ./bin/xxx", /* npm start */
"test": "vows --spec --isolate", /* npm test */
"postinstall": "...",
"prepublish": "grunt build", /* after 'npm install' and before 'npm
publish' */
}
杂项
"private": true,
"preferGlobal": true
配置
{
"config": {
"foobar": "hello"
},
"scripts": {
"run": "echo $npm_package_config_foobar"
}
}
其中的键(keys) config 会作为环境变量(env vars)传递给脚本。
参考资料
- http://package.json.nodejitsu.com/
npm help package.json