package.json 文件介绍

93 阅读1分钟
{
  "name": "my-project",    // 项目名称
  "version": "1.0.0",      // 项目版本号
  "description": "A sample project.",    // 项目描述
  "main": "index.js",      // 项目入口文件
  "scripts": {             // 自定义命令脚本
    "start": "node index.js",  // 启动应用程序
    "test": "mocha test/*.js"  // 运行测试脚本
  },
  "keywords": [            // 项目关键词
    "sample",
    "project"
  ],
  "author": {              // 作者信息
    "name": "John Smith",
    "email": "john@example.com"
  },
  "license": "MIT",        // 项目许可证
  "dependencies": {        // 生产环境依赖
    "express": "^4.17.1",
    "body-parser": "^1.19.0"
  },
  "devDependencies": {     // 开发环境依赖
    "mocha": "^8.4.0",
    "chai": "^4.2.0",
    "nodemon": "^2.0.4"
  },
  "repository": {          // 项目源代码存储库
    "type": "git",
    "url": "https://github.com/john/my-project.git"
  },
  "bugs": {                // 问题跟踪系统
    "url": "https://github.com/john/my-project/issues"
  },
  "homepage": "https://github.com/john/my-project",   // 项目主页
  "engines": {             // 所需的 Node.js 版本
    "node": ">=10.0.0"
  },
  "os": [                  // 支持的操作系统
    "darwin",
    "linux",
    "win32"
  ],
  "cpu": [                 // 支持的 CPU 架构
    "x64",
    "arm"
  ],
  "private": true,          // 禁止将包发布到 npm
  "husky": {                // husky 配置对象 
  "hooks": {                // Git 钩子
  "pre-commit": "lint-staged",// 执行 pre-commit 钩子时运行 lint-staged 命令
  "pre-push": "npm test" // 执行 pre-push 钩子时运行 npm test 命令
  }
}

在上面的示例中,dependencies包含了项目在生产环境中使用的依赖项,devDependencies包含了仅在开发过程中使用的依赖项。scripts包含了自定义的脚本命令,例如starttestrepositorybugs属性分别包含了项目的源代码存储库URL和问题跟踪系统URL。homepage包含了项目的主页URL。engines定义了项目所需的Node.js版本,oscpu属性分别定义了项目支持的操作系统和CPU架构。最后,private属性设置为true,表示不允许将包发布到npm注册表上。