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

yarn init

这个命令通过交互式会话带你创建一个 package.json 文件。 一些默认值比如 license 和初始版本可以在 yarn 的 init-* 配置里找到。

这是一个在名为 testdir 的目录里运行命令的例子:


1. $ yarn init


1. question name (testdir): my-awesome-package
2. question version (1.0.0):
3. question description: The best package you will ever find.
4. question entry point (index.js):
5. question git repository: https://github.com/yarnpkg/example-yarn-package
6. question author: Yarn Contributor
7. question license (MIT):
8. question private:
9. success Saved package.json
10. ✨  Done in 87.70s.

这导致下面的 package.json


1. {
2. "name": "my-awesome-package",
3. "version": "1.0.0",
4. "description": "The best package you will ever find.",
5. "main": "index.js",
6. "repository": {
7. "url": "https://github.com/yarnpkg/example-yarn-package",
8. "type": "git"
9. },
10. "author": "Yarn Contributor",
11. "license": "MIT"
12. }

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


1. {
2. "name": "my-existing-package",
3. "version": "0.1",
4. "description": "I exist therefore I am.",
5. "repository": {
6. "url": "https://github.com/yarnpkg/example-yarn-package",
7. "type": "git"
8. },
9. "license": "BSD-2-Clause"
10. }

下面交互式会话期间默认值的结果:


1. $ yarn init


1. question name (my-existing-package):
2. question version (0.1):
3. question description (I exist therefore I am.):
4. question entry point (index.js):
5. question git repository (https://github.com/yarnpkg/example-yarn-package):
6. question author: Yarn Contributor
7. question license (BSD-2-Clause):
8. question private:
9. success Saved package.json
10. ✨  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 目录里:


1. $ yarn init --yes


1. warning The yes flag has been set. This will automatically answer yes to all questions which may have security implications.
2. success Saved package.json
3. ✨  Done in 0.09s.

这会生成下面的 package.json


1. {
2. "name": "yarn-example",
3. "version": "1.0.0",
4. "main": "index.js",
5. "license": "MIT"
6. }

yarn init —private/-p

自动添加 private: truepackage.json


1. $ 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.


1. question name (testdir): my-awesome-package
2. question version (1.0.0):
3. question description: The best package you will ever find.
4. question entry point (index.js):
5. question git repository: https://github.com/yarnpkg/example-yarn-package
6. question author: Yarn Contributor
7. question license (MIT):
8. success Saved package.json
9. ✨  Done in 87.70s.


1. {
2. "name": "my-awesome-package",
3. "version": "1.0.0",
4. "description": "The best package you will ever find.",
5. "main": "index.js",
6. "repository": {
7. "url": "https://github.com/yarnpkg/example-yarn-package",
8. "type": "git"
9. },
10. "author": "Yarn Contributor",
11. "license": "MIT",
12. "private": true
13. }

可以同时使用 yesprivate 标志。

像是:


1. $ yarn init -yp


1. warning The yes flag has been set. This will automatically answer yes to all questions which may have security implications.
2. success Saved package.json
3. ✨  Done in 0.05s.

这会生成下面的 package.json


1. {
2. "name": "yarn-example",
3. "version": "1.0.0",
4. "main": "index.js",
5. "license": "MIT",
6. "private": true
7. }

原文: https://yarnpkg.com/zh-Hans/docs/cli/init