你可以向你的包中添加一个 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:
注意: 为了让你的包在 npm 网站上更容易被找到,我们建议在你的 package.json 文件中包含自定义的 description。
package.json 字段🌐 package.json fields
name 和 version🌐 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 without any spaces. May contain hyphens, dots, 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 inclusive package author information, in the "author" field use the following format (email and website are both optional):
Your Name <email@example.com> (https://example.com)
🌐 Example
{"name": "my-awesome-package","version": "1.0.0","author": "Your Name <email@example.com> (https://example.com)"}
package.json 文件🌐 Creating a new package.json file
你可以通过运行命令行问答来创建一个 package.json 文件,或者创建一个默认的 package.json 文件。
🌐 You can create a package.json file by running a CLI questionnaire or creating a default package.json file.
🌐 Running a CLI questionnaire
要使用你提供的值创建 package.json 文件,请使用 npm init 命令。
🌐 To create a package.json file with values that you supply, use the npm init command.
在命令行上,导航到包的根目录。
cd /path/to/package
运行以下命令:
npm init
回答命令行问卷中的问题。
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.
在你的主目录中,创建一个名为 .npm-init.js 的文件。
要添加自定义问题,请使用文本编辑器,使用 prompt 函数添加问题:
module.exports = prompt("what's your favorite flavor of ice cream, buddy?", "I LIKE THEM ALL");
要添加自定义字段,使用文本编辑器,将所需字段添加到 .npm-init.js 文件中:
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".
在命令行上,导航到包的根目录。
cd /path/to/package
运行以下命令:
npm init --yes
🌐 Example
> npm init --yesWrote 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:当前目录名version:总是 1.0.0description:关于该包的信息,或一个空字符串 ""scripts:默认会创建一个空的 test 脚本keywords:emptyauthor:emptylicense:ISCbugs:来自当前目录的信息(如果存在)homepage:来自当前目录的信息(如果存在)🌐 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"