在线文档教程

yarn init

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 privated的回答为空,那么该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: truepackage.json

$ yarn init --private

如果该private标志已设置,则该private键将自动设置为true,并且您仍然完成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): 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 }

您可以同时使用yes和private标志

像这个:

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