创建 package.json 文件

您可以将 package.json 文件添加到您的包中,以方便其他人管理和安装。发布到注册表的包必须包含 package.json 文件。

一个 package.json 文件:

  • 列出你的项目所依赖的包
  • 指定您的项目可以使用 语义版本控制规则 使用的包的版本
  • 使您的构建可重现,因此更容易与其他开发人员共享

注意:为了让你的包更容易在 npm 网站上找到,我们建议在你的 package.json 文件中包含一个自定义的 description

package.json 字段

必填 name 和 version 字段

package.json 文件必须包含 "name""version" 字段。

JYbTCvArxT/BnuQY+0F/q0V61ZjSJZJv4cfjJuAxrVqgRfFqp6bZmOCqpJSSCQYTDVPWqYmApqOpQWGOGEia0gYOG9dzbR9fZBF20O/asiGIFaMNKOyYlFaLlOBviMCKT67kHiLJTd8zZlEjgD1+3gfXIomeO0ZI6lYEao55B5WOyEc/lYhoRrN3FOXrFmvp

+bzI15XtQOv078pjQAu/EdSd3KI0zYNp67NF+Bekd8tyX8vPDcQVRMozwOyP+9yNASw3Hy9ZY5aD3Fclig90eFhG0aF84xg1XcxNnBYYy/KoXQjV9IIYRyzfpX1ZTLUavkaoIkVvhDyA3Mcg+laC9Gb0uSnRSbgav+gtAjUgTjVv4N9g3HjaXh0BVAJ54//sUI686Uo0ni7Ko1lKtjHC1A==

作者字段

R1Z7RxulwVwxinDnXECIGgHuWhqr6XHKkhdHNv+ikMGl9XjYM9CPUx9XEXfxddFx3qPIMNcQin8A03N7friQILDjpX9DtMHsF51zrKp2jb+mdnd+MZ598qaiHdJ8gradS8BwrOYc3n7uw0KLvsALR+wQ+V1+0+D36PGyBy28vDerMXRCsmuFUfan2uOcyvTp

Your Name <email@example.com> (http://example.com)

示例

{
  "name": "my-awesome-package",
  "version": "1.0.0",
  "author": "Your Name <email@example.com>"
}

创建新的 package.json 文件

l3j0xpxKbIi7fau1+CgPXle9A7TRvivzAPGpDKwgAKAwBPUUjKOwj9mv7nG+voDkGXbdO5zS5GoRwHudBzFt3hOvb24BeAqAHiN9F6o/iA3tlWczQdEsVS7lIATSXjr0gHEq5drn0N2EOgnzZazxHPZGXpjLXgvIdafJKrnwoS4=

运行 CLI 问卷

yA6A4O8ety0D69x71gNw/BWXGvl3Alv15sjDER2d+nqmrf3yeQEN9oaUrU4sTuFi44K93ad/i2j1enZwr0mH4pkDb8PPuWpeQm1RncPOoOrkYJXMDQiJ6iO5DaIBvDPin+hxG90itLP4nAj/xNA8Kg==

  1. 1BvpBpH1Si1CdeqEHtz4Eyccjv/gSD6BZiAdJ3Iyv6vUWY5iyl6OgucjE0f95Dg6

    cd /path/to/package
    
  2. zIb6h1Y0QdBIWicOoKaXW6pAdFxkAWNmvHHDeW58W8k=

    npm init
    
  3. mjrKQRQpFPvb7tvJEQv7Pd/xqzGL4E+CowN+WREqTz1oDVNwLmkXs7JSAt7SwtU5

定制 package.json 问卷

xpnHjzWl6qrRuyC5nsIBG+g4Al9SSS3QzSmCs9B+P6rp2cZsRn5yv45fj3NLzVgg6xgHIG/iwu5ZlLt8EzxYS3qMTIOJTqqLxT4uNrW98s50q4tbuPgZ8Ds+3JprzhD1G68iBdvYON1OTlWWhtQq3Pycu/ofRWX+bmWmIOBkNb9CfdaDoAcFsi0MyVqoaq2WagbnBNivGF/xuzkh3+ksj7GLvcfjuExCKnuDoSOMsZfT9rbAQ0zzsZbplBc4ixQyGujJ3vDED2bPGN8aoX8W0l1im8LcFHSRNxUsZjTpj8A=

  1. h1sAh48dW6UyK/CFAy9sSGMI99e7iptnG9Gr2ctvdrnC71dZX3SaIwT6uIkeT7YyXwQPTs9KQfaRN5JDn7ov4BcFkdME/5eDDxmgR4YjibgkP5uYZf5T5E5xGrSpRHB5

  2. sYQpkpVXFJJU9gpiouNPPnrQePCAz3738EcIp2iR0Uu8VHqu440lSWKmI3m+MwubgZ/yd7ZP9tzXiYpdNgPYzfIUj3e48IbUkCLsCDcQd1kSxr58tEEKlRykMcB/7xdXExtUz/Pt1+SAB3idNVAyqg==

    module.exports = prompt("what's your favorite flavor of ice cream, buddy?", "I LIKE THEM ALL");
    
  3. sYQpkpVXFJJU9gpiouNPPmaJXlJkZs8BopT+2tygvcpMqlW+RzoYU5f2sJMCf5DOQETtys+0URaBw3u0j01yFib7Mwpgh4swq5tF7oHTS5h3AOKWV++AAIXXY7TW3xe8173x8xyj3S0sMaT7s5THXQ==

    module.exports = {
      customField: 'Example custom field',
      otherCustomField: 'This example field is really cool'
    }
    

mXD16KwWEWhuKMUT3OQZmYenQsSIR8O96/sTNebv1odfnEyCjAFiTvcfV92oMLsxkR0WmT+cB4duhgnWoD7HXGU3ZwXi/gNOcvDg7ztgSvXdgIlizv/1G9J9KZqCBwcdwCS91hSIQMqoNLlTSIw0FRjFHdEBm6aZXHC7Mag//7ufyPqMh1ppvlosWjmkXiwlPJTpYZLGuRX0Y9AxQ/hKReppJwn3NO3QYkc+hWcY7mg=

创建默认 package.json 文件

Jzc6uxvVwNAOeDPUtUIegV4/MciCmN+RMfLzPUObLwp44ZxSs5fKmGXQSrc0rRw2OnFcuoQ0AZV16gI9KY7KZd4jW4tXRaICBl709g5J6vi1KPPaJODXd+7qLPr0xo42wMpMJkaJbEC19O2gBrnt6vYJBPMH0FxK2on7ShBZ0uVkbhoS0VxwUuccQtTJvZa7hxtuCighvXy+kcVsPNC2B/NvmsnMAoOZaTamayxkjZDMHXi9TAqwFSvtEKr9IbrIFo1Skl0OJPrgvgqDtDtjbjPagW0mowiVahr/a/7ndloZTxugMnQS6UCh6o9M+tbhy7H2SK2yz34EJIwPR/JHWEIbtQqzc8n/gVSszrZrZQoAbypNP+zLaui9Qjamicwis6HNimOz3GAekq8IKcys+kt6gxilo5H+gxQ4Hmv9wqI=

  1. 1BvpBpH1Si1CdeqEHtz4Eyccjv/gSD6BZiAdJ3Iyv6vUWY5iyl6OgucjE0f95Dg6

    cd /path/to/package
    
  2. zIb6h1Y0QdBIWicOoKaXW6pAdFxkAWNmvHHDeW58W8k=

    npm init --yes
    

示例

> npm init --yes
Wrote to /home/monatheoctocat/my_package/package.json:

{
  "name": "my_package",
  "description": "",
  "version": "1.0.0",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/monatheoctocat/my_package.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/monatheoctocat/my_package/issues"
  },
  "homepage": "https://github.com/monatheoctocat/my_package"
}

从当前目录中提取的默认值

    wm9vGhX3r13pINcQj55DqYeM2ySwXgvEA5fJCHZciqjHVP2LGx3/IBn5M5fvCgI9LsRQU2omPkp/3e4fPaZfQQnm84mkiUZvRBc8TSCcLwxzMAS0Q315Tc3t11TZ4baR24ZFMJSc2iNhY8o5JSfJ4GBgz6BHLg1+sjD+dOLCC8Mj4HwHy4AguQDHduxfxodqOSUt4Sf4OEZik/nq7+bkjWb2LX0X/5PPiPdU3Q/OjJQh2DDmTgjTuWsNJrB8CK5kh4ip/YRk2w8Sf2PssTgYZrmIXBctBAxLNkukqGmnP8GbwX/FdOPvxoCmtUldvWfls3RW3D5MwqGGTV7BuVemSSjayHrd4QHZxAvKGIQBn/eTSyuHxoqYntvrIb8fwnLOHb5AerqtppEW9HEGkPSW1Yk5Vru1GdHKsfqnRH1f5Lx60c2G986KXbu0Jk1syx02Nzf+kZDNIQy50+0KRXM+9FXuwW7vOLZUuhOwMYlOTjwDAMqFGvQshHn/VUlDeopKGhTi9paNB8RPc9NLznosCNg5oz1Ts/EkhPOI8ZtUfkoUfNkxLJMBZPEW/n45rZ5C9T70M2vMAk0GrHTo6nSKguOshnMtHcY4ib5OmLhsOLCdz7NZcoh+tomIOPIFv/Dvg4MI3jd0AFc05+TzeO5ZHBubTzxxnRGLdxp6iAFpDlJMqZxNA5JTZ0yy6XDgcsoQuB7jGCWe8kmFypk3iq+iR6lyy1/gw+vVqVayWSB0G4sjpVEp5W9TkY7ZjDclVUOTaMpE/ykMEyrzpuh8SQhERabaFKYX6AmOl7PeBS/TKkscjSt5d+g9Mzwc1UxCsDrw

设置 init 命令的配置选项

VcfgfjKc1ebVYocz1ezr2kM7I0uDcVHybAkm8Sx3zTlY4miTfqS84+GgwoBB25OzluOppHbz7aqO5vLs/V/pxrHdEG4Ppu305gzklqtrsyn61rUsJOCxbmEL+21h+s3kkbBX46AQ3ZJKFzz4GHYHSOSI/vD6xHsfAGufUBcIKMK/YB6seKwAI86JNtqejyayoIfpevbPE1P4gWW/NbKdK+KsxS3zrCPbOhVnEvPzUU4=

> npm set init-author-email "example-user@example.com"
> npm set init-author-name "example_user"
> npm set init-license "MIT"