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
| Flag | Meaning |
|---|---|
| -D | devDependency |
| -P | peerDependency |
| -O | optionalDependency |
| -E | exact version, no range |
| --mode=update-lockfile | only touch yarn.lock, do not install |
Examples
yarn add react react-domadds both and updates yarn.lock
yarn add -D typescriptadds a dev-only dependency
yarn add lodash@4.17.21pins 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