风行视频最新版
56.29MB · 2025-11-08
每一个 Node.js 项目都绕不开一个文件——package.json。
它像项目的“身份证”,又像“瑞士军刀”:
本文用“渐进式”结构,带你从最小可运行一路到企业级开源库所需的全部字段与技巧。读完可以:
package.jsondependencies 与 peerDependencies新建文件夹,执行 npm init -y,得到:
{
"name": "demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
即可:
node index.js 运行npm test 触发脚本但离“能用”还差 3 件事:
license 换成 SPDX 标准标识(MIT / Apache-2.0)"private": true,防止误发 npm{
"name": "demo",
"version": "1.0.0",
"main": "index.js",
"private": true,
"scripts": {
"start": "node index.js"
},
"license": "MIT"
}
| 场景 | 命令 | 写入字段 | 例子 |
|---|---|---|---|
| 生产依赖 | npm i lodash | dependencies | "lodash": "^4.17.21" |
| 开发依赖 | npm i -D vite | devDependencies | "vite": "^5.0.0" |
| 宿主依赖 | 手动写 | peerDependencies | "react": ">=18.0.0" |
口诀:
踩坑:
peerDependencies 版本范围过严会导致用户无法安装!">=16.8.0""16.8.0"(只允许 exact 版本)"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"test": "vitest",
"lint": "eslint . --ext .ts,.tsx",
"lint:fix": "eslint . --ext .ts,.tsx --fix",
"prepare": "husky install",
"prepublishOnly": "npm run build && npm test"
}
生命周期图解:
npm install → 触发 prepare → 自动装 husky 钩子
npm publish → 触发 prepublishOnly → 保证发布的是 dist 产物
跨平台兼容:
"build": "cross-env NODE_ENV=production tsc && vite build"
| 字段 | 历史地位 | 适用场景 | 备注 |
|---|---|---|---|
main | CommonJS 时代 | 老项目兼容 | 仅支持单一路径 |
module | 社区约定 | ESM 打包工具 | 无官方标准 |
exports | Node 12+ 官方 | 统一替代上面两项 | 支持条件导出、子路径 |
现代库模板:
{
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./utils": {
"types": "./dist/utils.d.ts",
"import": "./dist/utils.mjs",
"require": "./dist/utils.cjs"
}
}
}
收益:
import {} from 'your-lib/utils' 直接子路径{
"files": ["dist", "README.md", "LICENSE"],
"sideEffects": false,
"publishConfig": {
"registry": "https://registry.npmjs.org",
"access": "public"
}
}
files 白名单:防止把源码、测试、脚本发到 npmsideEffects: false 告诉 webpack 可以摇树优化registry 指向私有 Verdaccio / Nexus| 步骤 | 工具 / 字段 | 命令 |
|---|---|---|
| 1. 版本号合规 | SemVer | npm version patch |
| 2. 依赖漏洞扫描 | npm audit | npm audit fix |
| 3. 声明 Node 版本 | engines | { "node": ">=18.0.0" } |
| 4. 协议合规 | license | npx license-checker |
| 5. 自动化发布 | GitHub Actions | npm publish 在 CI 触发 |
示例 CI 片段(.github/workflows/publish.yml):
- name: Publish
if: github.ref == 'refs/heads/main'
run: |
npm config set //registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}
npm publish
(文字版)
package.json
├─ 元信息(name、version、license、private)
├─ 依赖(dependencies / dev / peer / optional)
├─ 脚本(scripts + 生命周期)
├─ 入口(main → module → exports 渐进)
├─ 发布(files、sideEffects、publishConfig)
└─ 约束(engines、os、cpu)
| 报错 | 原因 | 快速修复 |
|---|---|---|
Cannot find module 'xxx' | 忘了把 dist 加入 files | 补 files:["dist"] |
Multiple versions of React | 把 react 放 dependencies | 改 peerDependencies |
npm WARN deprecated xxx | 依赖版本过老 | npx npm-check-updates -u |
403 Forbidden on publish | 包名冲突或未登录 | 换名 / npm login |
package.json 很小,却贯穿了 Node.js 项目的全生命周期:
开发 → 测试 → 构建 → 发布 → 依赖治理。
掌握它,你就拥有了:
把本文 checklist 保存为备忘录,下次新建项目直接套用,即可一步到位。
Happy coding!
GitHub 搜 npm-lib-boilerplate-2025,已集成:
package.json 与本文明细 100% 对应克隆即可开始你的下一个开源库。