yarn init

以交互方式创建或更新 package.json 文件。

yarn init

此命令将引导你完成交互式会话以创建 package.json 文件。一些默认值(例如许可证和初始版本)可以在 yarn 的 init-* 配置设置中找到。

¥This command walks you through an interactive session to create a package.json file. Some defaults such as the license and initial version are found in yarn’s init-* config settings.

以下是在名为 testdir 的目录中运行命令的示例:

¥Here’s an example of running the command inside of a directory named testdir:

$ yarn init
question name (testdir): my-awesome-package
question version (1.0.0):
question description: The best package you will ever find.
question entry point (index.js):
question git repository: https://github.com/yarnpkg/example-yarn-package
question author: Yarn Contributor
question license (MIT):
question private:
success Saved package.json
✨  Done in 87.70s.

这会产生以下 package.json

¥This results in the following package.json:

{
  "name": "my-awesome-package",
  "version": "1.0.0",
  "description": "The best package you will ever find.",
  "main": "index.js",
  "repository": {
    "url": "https://github.com/yarnpkg/example-yarn-package",
    "type": "git"
  },
  "author": "Yarn Contributor",
  "license": "MIT"
}

默认情况下,如果 question private 的答案为空,则 private 键不会添加到 package.json

¥By default, if answer given to question private is passed in as empty, the private key will not be added to package.json

如果你已有 package.json 文件,那么它将使用该文件的条目作为默认值。

¥If you already have an existing package.json file, then it will use the file’s entries as defaults.

现有 package.json 如下:

¥The following existing package.json:

{
  "name": "my-existing-package",
  "version": "0.1",
  "description": "I exist therefore I am.",
  "repository": {
    "url": "https://github.com/yarnpkg/example-yarn-package",
    "type": "git"
  },
  "license": "BSD-2-Clause"
}

在交互式会话期间会产生以下默认值:

¥Results in the following defaults during the interactive session:

$ yarn init
question name (my-existing-package):
question version (0.1):
question description (I exist therefore I am.):
question entry point (index.js):
question git repository (https://github.com/yarnpkg/example-yarn-package):
question author: Yarn Contributor
question license (BSD-2-Clause):
question private:
success Saved package.json
✨  Done in 121.53s.

设置 yarn init 的默认值

¥Setting defaults for yarn init

以下 config 变量可用于自定义 yarn init 的默认值:

¥The following config variables can be used to customize the defaults for yarn init:

  • init-author-name

  • init-author-email

  • init-author-url

  • init-version

  • init-license

yarn init --yes/-y

此命令会跳过上面提到的交互式会话并根据你的默认值生成 package.json。某些默认值可能会被修改,更改 init-* 配置设置,如上所述。例如,假设全新安装了 Yarn 并且位于 yarn-example 目录中:

¥This command skips the interactive session mentioned above and generates a package.json based on your defaults. Some defaults may be modified changing init-* config settings like mentioned above. For example, given a fresh install of Yarn and inside a yarn-example directory:

$ yarn init --yes
warning The yes flag has been set. This will automatically answer yes to all questions which may have security implications.
success Saved package.json
✨  Done in 0.09s.

产生以下 package.json

¥Which produces the following package.json:

{
  "name": "yarn-example",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT"
}

yarn init --private/-p

自动将 private: true 添加到 package.json

¥automatically add private: true to the package.json

$ yarn init --private

如果设置了 private 标志,则 private 键将自动设置为 true,并且你仍然可以完成初始化过程的其余部分。

¥If the private flag is set, the private key will be automatically set to true and you still complete the rest of the init process.

question name (testdir): my-awesome-package
question version (1.0.0):
question description: The best package you will ever find.
question entry point (index.js):
question git repository: https://github.com/yarnpkg/example-yarn-package
question author: Yarn Contributor
question license (MIT):
success Saved package.json
✨  Done in 87.70s.
{
  "name": "my-awesome-package",
  "version": "1.0.0",
  "description": "The best package you will ever find.",
  "main": "index.js",
  "repository": {
    "url": "https://github.com/yarnpkg/example-yarn-package",
    "type": "git"
  },
  "author": "Yarn Contributor",
  "license": "MIT",
  "private": true
}

你可以同时使用 yesprivate 标志

¥You can use both the yes and the private flags at the same time

像这样:

¥Like this:

$ yarn init -yp
warning The yes flag has been set. This will automatically answer yes to all questions which may have security implications.
success Saved package.json
✨  Done in 0.05s.

产生以下 package.json

¥Which produces the following package.json:

{
  "name": "yarn-example",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "private": true
}