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

¥You can add a package.json file to your package to make it easy for others to manage and install. Packages published to the registry must contain a package.json file.

一个 package.json 文件:

¥A package.json file:

  • 列出你的项目所依赖的包

    ¥lists the packages your project depends on

  • 指定你的项目可以使用 语义版本控制规则 使用的包的版本

    ¥specifies versions of a package that your project can use using semantic versioning rules

  • 使你的构建可重现,因此更容易与其他开发者共享

    ¥makes your build reproducible, and therefore easier to share with other developers

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

¥Note: To make your package easier to find on the npm website, we recommend including a custom description in your package.json file.

package.json 字段

¥package.json fields

必填 nameversion 字段

¥Required name and version fields

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

¥A package.json file must contain "name" and "version" fields.

"name" 字段包含你的包的名称,必须是小写和一个单词,并且可以包含连字符和下划线。

¥The "name" field contains your package's name, and must be lowercase and one word, and may contain hyphens and underscores.

"version" 字段必须采用 x.x.x 格式,并且遵循 语义版本控制指南

¥The "version" field must be in the form x.x.x and follow the semantic versioning guidelines.

作者字段

¥Author field

如果要在 "author" 字段中包含包作者信息,请使用以下格式(电子邮件和网站都是可选的):

¥If you want to include package author information in "author" field, use the following format (email and website are both optional):

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

示例

¥Example

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

创建新的 package.json 文件

¥Creating a new package.json file

你可以通过运行 CLI 问卷或创建默认 package.json 文件来创建 package.json 文件。

¥You can create a package.json file by running a CLI questionnaire or creating a default package.json file.

运行 CLI 问卷

¥Running a CLI questionnaire

要使用你提供的值创建 package.json 文件,请使用 npm init 命令。

¥To create a package.json file with values that you supply, use the npm init command.

  1. 在命令行上,导航到包的根目录。

    ¥On the command line, navigate to the root directory of your package.

    cd /path/to/package
  2. 运行以下命令:

    ¥Run the following command:

    npm init
  3. 回答命令行问卷中的问题。

    ¥Answer the questions in the command line questionnaire.

定制 package.json 问卷

¥Customizing the package.json questionnaire

如果你希望创建许多 package.json 文件,你可以自定义在 init 过程中提出的问题和创建的字段,以便所有 package.json 文件都包含一组标准信息。

¥If you expect to create many package.json files, you can customize the questions asked and fields created during the init process so all the package.json files contain a standard set of information.

  1. 在你的主目录中,创建一个名为 .npm-init.js 的文件。

    ¥In your home directory, create a file called .npm-init.js.

  2. 要添加自定义问题,请使用文本编辑器,使用 prompt 函数添加问题:

    ¥To add custom questions, using a text editor, add questions with the prompt function:

    module.exports = prompt("what's your favorite flavor of ice cream, buddy?", "I LIKE THEM ALL");
  3. 要添加自定义字段,请使用文本编辑器将所需字段添加到 .npm-init.js 文件:

    ¥To add custom fields, using a text editor, add desired fields to the .npm-init.js file:

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

要了解有关创建高级 npm init 自定义项的更多信息,请参阅 init-package-json GitHub 存储库

¥To learn more about creating advanced npm init customizations, see the init-package-json GitHub repository.

创建默认 package.json 文件

¥Creating a default package.json file

要使用从当前目录中提取的信息创建默认 package.json,请使用带有 --yes-y 标志的 npm init 命令。有关默认值的列表,请参阅“从当前目录中提取的默认值”。

¥To create a default package.json using information extracted from the current directory, use the npm init command with the --yes or -y flag. For a list of default values, see "Default values extracted from the current directory".

  1. 在命令行上,导航到包的根目录。

    ¥On the command line, navigate to the root directory of your package.

    cd /path/to/package
  2. 运行以下命令:

    ¥Run the following command:

    npm init --yes

示例

¥Example

> npm init --yes
Wrote to /home/monatheoctocat/my_package/package.json:
{
"name": "my_package",
"description": "make your package easier to find on the npm website",
"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"
}

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

¥Default values extracted from the current directory

  • name:当前目录名

    ¥name: the current directory name

  • version:总是 1.0.0

    ¥version: always 1.0.0

  • description:有关包的信息,或空字符串 ""

    ¥description: info about the package, or an empty string ""

  • scripts:默认创建一个空的 test 脚本

    ¥scripts: by default creates an empty test script

  • keywords:空的

    ¥keywords: empty

  • author:空的

    ¥author: empty

  • licenseISC

  • bugs:来自当前目录的信息(如果存在)

    ¥bugs: information from the current directory, if present

  • homepage:来自当前目录的信息(如果存在)

    ¥homepage: information from the current directory, if present

设置 init 命令的配置选项

¥Setting config options for the init command

你可以为 npm init 命令设置默认配置选项。例如,要设置默认作者电子邮件、作者名称和许可证,请在命令行上运行以下命令:

¥You can set default config options for the npm init command. For example, to set the default author email, author name, and license, on the command line, run the following commands:

> npm set init-author-email "example-user@example.com"
> npm set init-author-name "example_user"
> npm set init-license "MIT"
npm 中文网 - 粤ICP备13048890号