重要提示:该文档介绍 Yarn 1(经典版)。
有关 Yarn 2+ 文档和迁移指南,请参阅 yarnpkg.com。

yarn init

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

yarn init

该命令会引导您完成交互式会话以创建 package.json 文件。一些默认设置(如许可证和初始版本)位于 Yarn 的 init-* 配置设置中。

以下是在名为 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

{
  "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

如果您已有现有的 package.json 文件,则会使用该文件的条目作为默认设置。

以下现有的 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"
}

在交互式会话中生成以下默认设置

$ 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 设置默认值

以下 配置 变量可用于自定义 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 目录内

$ 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

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

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

$ yarn init --private

如果 private 标志被设置,private 密钥将自动设置成 true 并且您仍然会完成剩余的初始化流程。

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 标志

如下所示

$ 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

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