Home·Terminal commands

yarn add

Packages and runtimes

Usage

yarn add [-D] <package>[@version]

Adds a package and updates yarn.lock; plain yarn means install, and Yarn 2 dropped yarn global add, so one-off tools go through yarn dlx.

Common flags

FlagMeaning
-DdevDependency
-PpeerDependency
-OoptionalDependency
-Eexact version, no range
--mode=update-lockfileonly touch yarn.lock, do not install

Examples

yarn add react react-dom

adds both and updates yarn.lock

yarn add -D typescript

adds a dev-only dependency

yarn add lodash@4.17.21

pins one version

The question is always what goes where: inside this project, or on the whole machine.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does yarn add do?

Adds a package and updates yarn.lock; plain yarn means install, and Yarn 2 dropped yarn global add, so one-off tools go through yarn dlx.

Q. How do I type it?

yarn add [-D] <package>[@version] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

5 are listed here; the full set is in man yarn. This command sits under Packages and runtimes.

Related commands

man page: man yarn-add