Skip to content
Search

Yarn

lastmod 2024-11-04created 2018-09-0221 edits

Yarn caches every package it downloads so it never needs to download it again. It also parallelizes operations to maximize resource utilization so install times are faster than ever.

Categories

Command

To initialize when starting a project (creates package.json):

$ yarn init

To install dependency modules from package.json:

$ yarn
# or
$ yarn install

To install a dependency module (same as npm install --save):

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

For reference, a global install (same as npm install -g):

$ yarn global add [package_name]

To add dependencies of other categories such as devDependencies, peerDependencies, and optionalDependencies:

$ yarn add [package] --dev
$ yarn add [package] --peer
$ yarn add [package] --optional

To upgrade a dependency module:

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

To remove a dependency module:

$ yarn remove [package]

List installed packages:

$ yarn list

Run an executable from an installed module.

$ yarn exec {executable file}

Comparing v1 and v2 commands

Yarn Classic (1.x)

Yarn (2.x)

Notes

yarn audit

yarn npm audit

yarn create

yarn dlx create-<name>

</code>yarn create</code> still works, but prefer using yarn dlx

yarn global

yarn dlx

Dedicated section

yarn info

yarn npm info

yarn login

yarn npm login

yarn logout

yarn npm logout

yarn outdated

yarn upgrade-interactive

Read more on GitHub

yarn publish

yarn npm publish

yarn tag

yarn npm tag

yarn upgrade

yarn up

Will now upgrade packages across all workspaces

yarn install --production

yarn workspaces focus --all --production

Requires the workspace-tools plugin

yarn install --verbose

<code>YARN_ENABLE_INLINE_BUILDS=true yarn install

yarn.lock

Because the package.json file downloads packages according to Semantic versioning rules, the versions downloaded can change over time. So a yarn.lock file is created that stores the versions of installed modules, so that dependencies have the same versions and structure everywhere.

In other words, this file must not be added to the.gitignore file.

yarn automatically creates yarn.lock on every yarn install. You can think of it as doing much the same as package-lock.json.

Publish

The command is the same as Npm#Publish. However, log in with npm first, referring to Npm#Login, and then publish.

yarn publish --access=public

How to use npx

Consider installing ynpx, or add the command to script in the package.json file.

{
  "scripts": {
    "11ty": "npx @11ty/eleventy"
  }
}

Plugin

Troubleshooting

Extracting tar content of undefined failed

Do not build in the $HOME directory.

The engine "node" is incompatible with this module

[email protected]: The engine "node" is incompatible with this module. Expected version ">=18.17.0". Got "18.16.1"

Upgrade your Node.js version.

Modern JS already guarantees Array#sort() is a stable sort, so this library is deprecated

warning accept-language > [email protected]: Modern JS already guarantees Array#sort() is a stable sort, so this library is deprecated. See the compatibility table on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#browser_compatibility

See also

Favorite site

author Zer0 sitemap