optre
一位前端开发者,keep going!
posts - 45,comments - 2,views - 61259

package.json配置说明详解

基础配置

{
  "name": "my-package", // 包名 必填字段
  "version": "1.0.0", // 版本号 必填字段
  "description": "This is my first npm package.", // 描述信息
  "keywords": [   // 关键词,用于搜索和分类。
    "npm",
    "package",
    "example"
  ],
  "homepage": "https://github.com/user/repo#readme", // 项目主页链接
  "bugs": { // 报告问题的链接
    "url": "https://github.com/user/repo/issues"
  },
  "repository": { // 代码仓库信息
    "type": "git",
    "url": "https://github.com/user/repo.git"
  },
  "license": "MIT", // 许可证信息
  "author": { // 作者信息
    "name": "John Doe",
    "email": "johndoe@example.com",
    "url": "https://johndoe.com"
  },
  "main": "src/index.js", //入口文件
  "bin": { // 用于指定命令行程序的入口文件路径,通常用于全局安装的包。
    "my-cli": "./bin/my-cli.js"
  },
  "type": "module", //指定包的类型。
  "files": [ // 指定需要打包到发布的包中的文件。
    "lib/",
    "index.js",
    "README.md"
  ],
  "engines": { // 指定 Node.js 和 npm 的版本要求
    "node": ">=10.0.0",
    "npm": ">=6.0.0"
  },
  "os": [ // 指定支持的操作系统
    "darwin",
    "linux"
  ],
  "cpu": [ // 指定支持的 CPU 架构
    "x64",
    "arm"
  ],
  "peerDependencies": { // 指定依赖的版本范围
    "react": "^16.8.0",
    "react-dom": "^16.8.0"
  },
  "optionalDependencies": { // 指定可选依赖。
    "canvas": "^2.0.0"
  },
  "resolutions": { // 指定依赖的版本解析规则。
    "some-package": "2.x"
  },
  "dependencies": { // 项目的依赖包
    "react": "^16.8.0",
    "react-dom": "^16.8.0"
  },
  "devDependencies": { // 项目的开发依赖包
    "babel": "^7.0.0",
    "webpack": "^4.0.0"
  },
  "scripts": { //自定义脚本命令,使用 npm run 执行
    "start": "node index.js",
    "test": "jest",
    "build": "webpack"
  },
  "workspaces": [ // 指定工作区的配置,用于管理多个包
    "packages/*"
  ],
  "publishConfig": { // 发布到 npm 的配置
    "access": "public",
    "registry": "https://registry.npmjs.org/"
  },
  "exports": { // 指定你的 JavaScript 模块的入口点(即导出的对象)
    ".": {
      "require": "./dist/main.js",
      "import": "./dist/main.mjs"
    }
  },
  "private": true, // 标记是否为私有包
  "enginesStrict": true //指定 Node.js 和 npm 的版本要求,严格模式
}

代码校验、检测配置

使用了 ESLint、Prettier、Jest 和 Husky

{
  "name": "my-package",
  // ...
  "devDependencies": {
    "@babel/core": "^7.0.0",
    "@babel/preset-env": "^7.0.0",
    "@babel/preset-react": "^7.0.0",
    "babel-jest": "^26.6.3",
    "eslint": "^7.24.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^3.4.0",
    "jest": "^26.6.3",
    "prettier": "^2.2.1",
    "husky": "^6.0.0"
  },
  "scripts": {
    "start": "node index.js",
    "test": "jest",
    "build": "webpack",
    "lint": "eslint src/**/*.{js,jsx}",
    "format": "prettier --write src/**/*.{js,jsx}"
  },
  "husky": {
    "hooks": {
      "pre-commit": "npm run lint && npm run format && git add ."
    }
  },
  // ...
}
posted on   optre  阅读(398)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示