首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

yarn init

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

yarn init

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

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

代码语言:javascript
复制
$ yarn init
代码语言:javascript
复制
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结果:

代码语言:javascript
复制
{
  "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

代码语言:javascript
复制
{
  "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"
}

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

代码语言:javascript
复制
$ yarn init
代码语言:javascript
复制
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目录中:

代码语言:javascript
复制
$ yarn init --yes
代码语言:javascript
复制
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

代码语言:javascript
复制
{
  "name": "yarn-example",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT"
}

yarn init --private/-p

自动添加private: truepackage.json

代码语言:javascript
复制
$ yarn init --private

如果该private标志已设置,则该private键将自动设置为true,并且您仍然完成init进程的其余部分。

代码语言:javascript
复制
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.
代码语言:javascript
复制
{
  "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标志

像这个:

代码语言:javascript
复制
$ yarn init -yp
代码语言:javascript
复制
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

代码语言:javascript
复制
{
  "name": "yarn-example",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "private": true
}

扫码关注腾讯云开发者

领取腾讯云代金券