管理相依套件

When you want to add, upgrade, or remove dependencies there are a couple of different commands you need to know.

Each command will automatically update your package.json and yarn.lock files.

Adding a dependency

If you want to use another package, you first need to add it as a dependency. In order to do that you should run:

yarn add [package]

[package]會被加入到package.json文件中的相依列表,同时yarn.lock也會被更新。

  {
    "name": "my-package",
    "dependencies": {
+     "package-1": "^1.0.0"
    }
  }

你可以用以下參數添加其它類型的相依:

  • yarn add --dev 添加到 devDependencies
  • yarn add --peer 添加到 peerDependencies
  • yarn add --optional 添加到 optionalDependencies

通過指定相依版本標籤(tag),你可以安装一個特定版本的套件:

yarn add [package]@[version]
yarn add [package]@[tag]

[version][tag] 會被添加到 package.json,並在安装相依時被解析。

例如:

yarn add package-1@1.2.3
yarn add package-2@^1.0.0
yarn add package-3@beta
{
  "dependencies": {
    "package-1": "1.2.3",
    "package-2": "^1.0.0",
    "package-3": "beta"
  }
}

更新相依套件

yarn upgrade [package]
yarn upgrade [package]@[version]
yarn upgrade [package]@[tag]

這會更新package.jsonyarn.lock 文件。

  {
    "name": "my-package",
    "dependencies": {
-     "package-1": "^1.0.0"
+     "package-1": "^2.0.0"
    }
  }

删除相依套件

yarn remove [package]

這會更新package.jsonyarn.lock 文件。