Appearance
package.json文件字段描述
- name
npm包的名字,如果你打算发布你的包,name是必填的字段,如果你不打算发布你的包,那么name字段是可选的。一个npm包是由名字加版本作为唯一标识的。npm包的名称必须要小于等于214个字符,除了范围包之外的npm包名称不允许以'.'和'_'开头,名称中不能有大写字母,由于包名称也是URL中的一部分,所以不能包含任何非URL安全字符。- 另外官方建议我们不要和node的某些核心模块取一样的名称,包名中不要有
js以及node这样的字符。由于我们是需要通过包名引入包,所以包名最好是简短的,描述性合理的。
- type
- 值为
commonjs(默认值),适用于 Node.js 环境(服务端)的CommonJS模块化规范 module,即 ES Module 语法,适用于浏览器环境(客户端)的ESM模块化规范
version
npm包的版本号,关于版本号的说明可见npm包版本管理packageManager
字段定义了在处理当前项目时预期使用的包管理器,新增于nodejsv16.9.0description
npm包的描述,它将展示在npm包的搜索列表里,帮助其它的开发者在npm上找到你的包。author, contributors
npm包的作者及参与贡献人员,author是单个,contributors是数组。repository
包的仓库地址bugs
你的项目提issue的地址homepage
npm包的主页地址license
你需要给你的npm包声明一个许可证,来让用户知道他们的使用权利和限制。许可证类型可见七种开源许可证keywords
npm包的关键字,是一个字符串数组,它也会展示在npm包的搜索列表里,用来帮助其它的开发者在npm上找到你的包。sideEffects
与webpack相关的字段,声明该模块是否包含sideEffects(副作用),从而可以为tree-shaking提供更大的优化空间。exports
exports字段提供了一种方法来为不同的环境和JavaScript风格公开您的包模块,同时限制对其内部部分的访问。- 优先级高于
main、module和types字段
main
npm包的入口文件,通过require(<某包>)导入的时候,实际上导入的就是main字段的入口文件。module
- 定义
npm包的ESM规范的入口文件,browser环境和node环境均可使用 - 优先级高于
main字段
types 只在
TypeScript中生效的字段,指明主声明文件,指向bundled的声明文件files
files是一个可选项,形式为数组,描述了npm publish的时候推送到npm服务器的文件列表,支持目录和通配,相反的,你可以使用.gitignore或者.npmignore来排除不需要上传的文件。scripts 用来定义脚本命令
dependencies 项目运行时所需要的依赖
devDependencies 项目开发时所需要的依赖
config 添加一些配置,可以供
scripts读取用,同时这里的值也会被添加到系统的环境变量中。
示例
json
{
"name": "@trinasolar/utils",
"type": "module",
"version": "0.1.2-beta.0",
"packageManager": "pnpm@7.11.0",
"description": "Collection of Js/Ts utils",
"author": "Sam Xu <twonian@qq.com>",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com:didengren/utils.git"
},
"bugs": {
"url": "https://github.com/didengren/utils/issues"
},
"homepage": "https://github.com/didengren/utils#readme",
"license": "Apache-2.0",
"keywords": [
"utils"
],
"sideEffects": false,
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
}
},
"main": "dist/index.cjs",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist",
"*.d.ts"
],
"scripts": {
"start": "esno src/index.ts",
"dev": "tsup src/**/*.ts --watch src",
"build": "tsup src/**/*.ts --format cjs,esm --dts --splitting --clean",
"lint": "eslint --cache --max-warnings 100 \"src/**/*.ts\" --fix",
"format": "prettier --write \"src/**/*.{ts,json,css,less,scss,md}\"",
"typecheck": "tsc --noEmit",
"test": "vitest",
"coverage": "vitest run --coverage",
"release": "bumpp --commit --push --tag && pnpm publish --access public"
},
"dependencies": {
"ali-oss": "^6.17.1",
"js-cookie": "^3.0.1",
"lodash-es": "^4.17.21",
"resize-observer-polyfill": "^1.5.1"
},
"devDependencies": {
"@types/ali-oss": "^6.16.6",
"@types/js-cookie": "^3.0.2",
"@types/lodash-es": "^4.17.6",
"@types/node": "16.11.12",
"@typescript-eslint/eslint-plugin": "5.20.0",
"@typescript-eslint/parser": "5.20.0",
"@vitest/coverage-c8": "^0.25.1",
"bumpp": "^8.2.1",
"commitizen": "^4.2.5",
"cz-customizable": "^7.0.0",
"eslint": "8.13.0",
"eslint-config-prettier": "8.5.0",
"eslint-define-config": "1.2.0",
"eslint-plugin-prettier": "4.0.0",
"esno": "^0.16.3",
"happy-dom": "^7.6.7",
"prettier": "2.6.2",
"tsup": "^6.4.0",
"typescript": "4.8.4",
"vitest": "^0.25.1"
},
"config": {
"commitizen": {
"path": "node_modules/cz-customizable"
},
"cz-customizable": {
"config": ".cz-config.cjs"
}
}
}