一些package.json配置示列
ant-design-mobile
{
"name": "antd-mobile",
"version": "5.10.2",
"scripts": {
"start": "dumi dev",
"build": "gulp",
"build-doc": "dumi build",
"lint": "eslint \"src/**/*.{ts,tsx}\"",
"postinstall": "husky install",
"test": "jest",
"test-with-coverage": "jest --coverage",
"publish": "npm publish ./lib --registry=https://registry.npmjs.org",
"publish:alpha": "npm publish ./lib --registry=https://registry.npmjs.org --tag alpha",
"publish:dev": "npm publish ./lib --registry=https://registry.npmjs.org --tag dev"
},
"dependencies": {
"@floating-ui/dom": "^0.4.2",
"@react-spring/web": "^9.4.4",
"@types/big.js": "^6.1.3",
"@types/react-is": "^17.0.3",
"@types/resize-observer-browser": "^0.1.7",
"@use-gesture/react": "^10.2.10",
"ahooks": "^3.1.14",
"antd-mobile-icons": "^0.2.2",
"antd-mobile-v5-count": "^1.0.1",
"big.js": "^6.1.1",
"classnames": "^2.3.1",
"dayjs": "^1.11.0",
"lodash": "^4.17.21",
"rc-field-form": "^1.24.0",
"react-is": "^17.0.2",
"staged-components": "^1.1.2",
"tslib": "^2.3.1"
},
"devDependencies": {
"@babel/cli": "^7.17.6",
"@babel/core": "^7.17.8",
"@babel/plugin-transform-modules-commonjs": "^7.17.7",
"@babel/preset-env": "^7.16.11",
"@babel/preset-react": "^7.16.7",
"@babel/preset-typescript": "^7.16.7",
"@docsearch/react": "^3.0.0",
"@jest/types": "27.5.1",
"@testing-library/jest-dom": "^5.16.3",
"@testing-library/react": "^12.1.4",
"@testing-library/react-hooks": "7.0.2",
"@testing-library/user-event": "13.5.0",
"@types/jest": "^27.4.1",
"@types/jest-axe": "3.5.3",
"@types/lodash": "^4.14.180",
"@types/react": "^17.0.43",
"@types/react-helmet": "^6.1.5",
"@typescript-eslint/eslint-plugin": "^5.16.0",
"@typescript-eslint/parser": "^5.16.0",
"babel-loader": "^8.2.4",
"css-loader": "^6.7.1",
"del": "^6.0.0",
"dumi": "^1.1.40",
"eslint": "^8.12.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-react": "^7.29.4",
"eslint-plugin-react-hooks": "^4.3.0",
"gulp": "^4.0.2",
"gulp-babel": "^8.0.0",
"gulp-less": "^5.0.0",
"gulp-postcss": "^9.0.1",
"gulp-rename": "^2.0.0",
"gulp-replace": "^1.1.3",
"gulp-typescript": "^6.0.0-alpha.1",
"husky": "^7.0.4",
"jest": "27.5.1",
"jest-axe": "^5.0.1",
"jest-environment-jsdom": "27.5.1",
"jest-watch-typeahead": "1.0.0",
"less": "^4.1.2",
"lorem-ipsum": "^2.0.4",
"mockdate": "^3.0.5",
"postcss": "^8.4.12",
"postcss-px-multiple": "^0.1.5",
"postcss-pxtorem": "^6.0.0",
"prettier": "^2.6.1",
"pretty-quick": "^3.1.3",
"prism-react-renderer": "^1.3.1",
"prismjs": "^1.27.0",
"qrcode.react": "^1.0.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-helmet": "^6.1.0",
"react-test-renderer": "^17.0.2",
"react-virtualized": "^9.22.3",
"style-loader": "^3.3.1",
"through2": "^4.0.2",
"ts-jest": "27.1.4",
"ts-node": "10.7.0",
"typescript": "~4.5.5",
"vite": "^2.8.6",
"webpack": "^5.70.0",
"webpack-bundle-analyzer": "^4.5.0",
"webpack-stream": "^7.0.0"
},
"peerDependencies": {
"react": "^16.8.0 || ^17.0.0"
},
"main": "./cjs/index.js",
"module": "./es/index.js",
"types": "./es/index.d.ts",
"typings": "./es/index.d.ts",
"unpkg": "./umd/antd-mobile.js",
"GravityCDN": "./umd/antd-mobile.js",
"files": [
"./lib"
],
"sideEffects": [
"**/*.css",
"**/*.less",
"./es/index.js",
"./src/index.ts",
"./es/global/index.js",
"./src/global/index.ts"
],
"publishConfig": {
"registry": "https://registry.npmjs.org"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ant-design/ant-design-mobile.git",
"branch": "master",
"platform": "github"
},
"packageManager": "yarn@3.2.0"
}
npm初始化
{
"name": "css-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"babel-preset-stage-0": "^6.24.1",
"cross-env": "^7.0.3",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"webpack": "^5.72.0",
"webpack-cli": "^4.9.2",
"webpack-dev-server": "^4.8.1",
"webpack-merge": "^5.8.0"
},
"devDependencies": {
"babel-plugin-import": "^1.13.3",
"css-loader": "^6.7.1",
"file-loader": "^6.2.0",
"jsx-loader": "^0.13.2",
"less": "^4.1.2",
"less-loader": "^10.2.0",
"style-loader": "^3.3.1",
"url-loader": "^4.1.1"
}
}
配置字段解析
name
name: 这个很好理解,就是package的名称 过需要注意的是,name有长度限制(虽然一般都不会超),而且name不能以 【点】 或者 【下划线】开头,name中不能有大写字母 在业务代码中,通过require(${name})就可以引入对应的程序包了
{
name: "ant-design-mobile"
}
version
version: package的版本 ame和version共同决定了唯一一份代码 npm是用[npm-semver来解析版本号的 版本号的规则、含义其实蛮多的,可以参考这篇文章
{
"name": "antd-mobile",
"version": "5.10.2",
}
author 、contributors
author 和 contributors 字段的功能类似。它们都是 people 字段,可以是”Name” 格式的字符串,也可以是具有 name,email,url 字段的对象。email 和 url 都是可选的
license
license:开源协议。 对于开源组件库,这个十分重要 之前react还因为这事儿没少被社区嫌弃。开源协议略微复杂,用阮一峰前辈的一张图来说明一下吧。注:图里少了ISC, ISC和BSD差不多
desription:包的描述 开发组件库时必需,简明的向库的使用者介绍这个库是干嘛的。对于公司的业务项目,这个配置项一般无所谓
{
"name": "antd-mobile",
"version": "5.10.2",
"desription": "这是一个移动端react组件库",
}
keywords
keywords:关键词 一个字符串数组,对这个npm包的介绍。组件库必需,便于使用者在npm中搜索 对于公司业务项目,这个配置一般无所谓,对于组件库,工具库,在npm发布的重要
private
若package.json中设置了 “private”: true,npm 将拒绝发布它 web 项目大多不需要提交到 npm 仓库,建议将 private 设为 true
homepage
- homepage: 项目主页。对于开发组件库来说挺有用的。
repository
对于组件库很有用。 让组件库使用者找到你的代码库地址。这个配置项会直接在组件库的npm首页生效
"repository": {
"type": "git",
"url": "git+https://github.com/CoyPan/react-scroll-to-show-cb.git"
},
bugs
开发者的联系方式,代码库的issues地址等。如果代码使用者发现了bug,可以通过这个配置项找到提bug的地方。
scripts
scripts控制项目生命周期的脚本
"scripts": {
"start": "dumi dev",
"build": "gulp",
"build-doc": "dumi build",
"lint": "eslint \"src/**/*.{ts,tsx}\"",
"postinstall": "husky install",
"test": "jest",
"test-with-coverage": "jest --coverage",
"publish": "npm publish ./lib --registry=https://registry.npmjs.org",
"publish:alpha": "npm publish ./lib --registry=https://registry.npmjs.org --tag alpha",
"publish:dev": "npm publish ./lib --registry=https://registry.npmjs.org --tag dev"
},
engines
保证node版本开发一致 我们很难保证同事间的开发环境统一,例如不同开发者的 node 版本是不一致的,有人是8.x, 有人是10.x 有人是14.x。来个新人问一下你们 node 用什么版本,累不累。配置下 engines 吧
"engines": {
"node": ">=12.10.0 <15"
}
main
包的入口文件。默认是项目根目录下的index.js
"main": "index.js"
files
files用于决定哪些文件被发布到 npm 仓库去 cjs,ejs等模块加载规范
"files": [
"src",
"dist/*.js",
"types/*.d.ts"
],
bin
命令行工具入口,使用 npm 开发一个命令行工具 当你的包提供了一个命令行工具时,你需要为命令行工具指定一个入口。命令名称和本地可指定文件的对应关系
"bin": {
"paytm-security-cli": "index.js"
},
config
用于添加命令行的环境变量
directories
对整个代码结构的描述 告诉代码包使用者可以在哪里找到对应的文件
dependencies
项目的依赖。通过npm install —save安装的包会出现在这里 注意:不要把测试工具、代码转换器或者打包工具等放在这里。
"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"
}
devDependencies
项目的依赖。 通过npm run install —save-dev安装的包会出现在这里。主要是在开发过程中依赖的一些工具
"devDependencies": {
"@babel/cli": "^7.17.6",
"@babel/core": "^7.17.8",
"@babel/plugin-transform-modules-commonjs": "^7.17.7",
"@babel/preset-env": "^7.16.11",
"@babel/preset-react": "^7.16.7",
"@babel/preset-typescript": "^7.16.7",
"@docsearch/react": "^3.0.0",
"@jest/types": "27.5.1",
"@testing-library/jest-dom": "^5.16.3",
"@testing-library/react": "^12.1.4",
"@testing-library/react-hooks": "7.0.2",
"@testing-library/user-event": "13.5.0",
"@types/jest": "^27.4.1",
"@types/jest-axe": "3.5.3",
"@types/lodash": "^4.14.180",
"@types/react": "^17.0.43",
"@types/react-helmet": "^6.1.5",
"@typescript-eslint/eslint-plugin": "^5.16.0",
"@typescript-eslint/parser": "^5.16.0",
"babel-loader": "^8.2.4",
"css-loader": "^6.7.1",
"del": "^6.0.0",
"dumi": "^1.1.40",
"eslint": "^8.12.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-react": "^7.29.4",
"eslint-plugin-react-hooks": "^4.3.0",
"gulp": "^4.0.2",
"gulp-babel": "^8.0.0",
"gulp-less": "^5.0.0",
"gulp-postcss": "^9.0.1",
"gulp-rename": "^2.0.0",
"gulp-replace": "^1.1.3",
"gulp-typescript": "^6.0.0-alpha.1",
"husky": "^7.0.4",
"jest": "27.5.1",
"jest-axe": "^5.0.1",
"jest-environment-jsdom": "27.5.1",
"jest-watch-typeahead": "1.0.0",
"less": "^4.1.2",
"lorem-ipsum": "^2.0.4",
"mockdate": "^3.0.5",
"postcss": "^8.4.12",
"postcss-px-multiple": "^0.1.5",
"postcss-pxtorem": "^6.0.0",
"prettier": "^2.6.1",
"pretty-quick": "^3.1.3",
"prism-react-renderer": "^1.3.1",
"prismjs": "^1.27.0",
"qrcode.react": "^1.0.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-helmet": "^6.1.0",
"react-test-renderer": "^17.0.2",
"react-virtualized": "^9.22.3",
"style-loader": "^3.3.1",
"through2": "^4.0.2",
"ts-jest": "27.1.4",
"ts-node": "10.7.0",
"typescript": "~4.5.5",
"vite": "^2.8.6",
"webpack": "^5.70.0",
"webpack-bundle-analyzer": "^4.5.0",
"webpack-stream": "^7.0.0"
}
bundledDependencies
数组,打包时的依赖 如果配置了bundledDependencies,在项目中执行 npm pack将项目打包时,最后生成的.tgz包中,会包含bundledDependencies中配置的依赖 bundledDependencies中的依赖必须在devDependencies或者dependencies中声明过
peerDependencies
指定当前组件的依赖以其版本 如果组件使用者在项目中安装了其他版本的同一依赖,会提示报错
"peerDependencies": {
"react": "^16.8.0 || ^17.0.0"
},
publishconfig
一组配置值,发布时使用 当我们开发完自己的项目想要将它发布到 Npm 仓库就需要使用这个属性,默认情况下我们是往 Npm 公共仓库中发布包,默认地址是:https://www.npmjs.com/,此时在 package.json 文件中可以不加这个属性 但是如果公司内部搭建了 Npm 私有仓库,此时发布包就不是往 https://www.npmjs.com/ 这个地址上发布了,而是公司内部提供的地址,如:http://gongsineibu/nexus/repository/npm-hosted/
"publishConfig": {
"registry": "http://gongsineibu/nexus/repository/npm-hosted/"
}
type
type字段的产生用于定义package.json文件和该文件所在目录根目录中.js文件和无拓展名文件的处理方式 值为’moduel’则当作es模块处理;值为’commonjs’则被当作commonJs模块处理 目前node默认的是如果pacakage.json没有定义type字段,则按照commonJs规范处理 node官方建议包的开发者明确指定package.json中type字段的值 无论package.json中的type字段为何值,.mjs的文件都按照es模块来处理,.cjs的文件都按照commonJs模块来处理
"type": "module"