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 的預設值

以下的 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

如果設置了 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
}