yarn init

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

yarn init

这个命令通过交互式会话带你创建一个 package.json 文件。 一些默认值比如 license 和初始版本可以在 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"
}

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,它会用这个文件的条目作为默认值。

现有下面的 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 设置默认值

下面的 config 变量可被用于自定义 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: truepackage.json

$ yarn init --private

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
}

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

像是:

$ 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
}