Yarn/npm cheatsheet

Yarn/npm cheatsheet

·

6 min read

Yarn Commandnpm EquivalentDescription
yarn initnpm initInitializes a new project
yarn installnpm installInstalls dependencies based on the package.json file
yarn add <package>npm install <package>Adds a package as a project dependency
yarn remove <package>npm uninstall <package>Removes a package from project dependencies
yarn upgradenpm updateUpgrades packages to their latest versions
yarn global add <package>npm install -g <package>Installs a package globally
yarn run <script>npm run <script>Runs a script defined in the package.json file
yarn buildnpm run buildBuilds the project according to the build script
yarn testnpm run testRuns tests defined in the project
yarn startnpm startStarts the project
yarn publishnpm publishPublishes the package to a registry
yarn cleannpx rimraf node_modulesCleans the project by removing the node_modules directory

yarn list

npm list

Lists installed packages and their dependencies

yarn outdated

npm outdated

Checks for outdated packages

yarn upgrade-interactive

npm install --interactive

Interactively upgrades packages to their latest versions

yarn add --dev <package>

npm install --save-dev <package>

Adds a package as a development dependency

yarn remove --dev <package>

npm uninstall --save-dev <package>

Removes a development dependency

yarn global remove <package>

npm uninstall -g <package>

Removes a globally installed package

yarn link

npm link

Creates a symbolic link between a package and a project

yarn link --all

-

Creates symlinks for all packages in a monorepo

yarn unlink

npm unlink

Removes a symbolic link between a package and a project

yarn unlink --all

-

Removes all symlinks created by yarn link --all

yarn cache clean

npm cache clean

Clears the package cache

yarn login

npm login

Logs in to a package registry

yarn logout

npm logout

Logs out from a package registry

yarn config set <key> <value>

npm config set <key> <value>

Sets a Yarn or npm configuration value

yarn config get <key>

npm config get <key>

Gets the value of a Yarn or npm configuration key

yarn config delete <key>

npm config delete <key>

Deletes a Yarn or npm configuration key and its value

yarn workspace <workspace> add <package>

npm workspace <workspace> add <package>

Adds a package to a specific workspace

yarn workspace <workspace> remove <package>

npm workspace <workspace> remove <package>

Removes a package from a specific workspace

yarn workspace <workspace> run <script>

npm workspace <workspace> run <script>

Runs a script in a specific workspace

yarn workspace <workspace> <command>

npm workspace <workspace> <command>

Runs a command in the context of a specific workspace

yarn workspaces info

npm workspaces info

Displays information about the workspaces in a project

yarn workspaces run <script>

npm run <script> --workspaces

Runs a script in all workspaces in a project

yarn workspaces foreach <command>

npm run <command> --workspaces

Runs a command in each workspace in a project

yarn workspaces list

npm workspaces list

Lists all the workspaces in a project

yarn workspaces run <script>

npm run <script> --workspaces

Runs a script in the context of all workspaces

yarn workspaces focus <workspace>

-

Sets the current workspace context for running commands

yarn autoclean --init

-

Initializes the Yarn autoclean feature

yarn autoclean --force

-

Removes unnecessary files from the Yarn cache

yarn autoclean --dry-run

-

Shows which files would be removed by autoclean

yarn generate-lock-entry

-

Generates a lockfile entry for a given package

yarn pack

npm pack

Creates a compressed tarball of the package

yarn install --check-files

-

Checks if installed package files have been tampered with

yarn install --flat

-

Forces the installation of a package to a single version

yarn install --pure-lockfile

-

Forces installation using only the lockfile dependencies

yarn install --prod

npm install --production

Installs only production dependencies, excluding devDeps

yarn install --no-lockfile

npm install --no-package-lock

Disables generation of a lockfile during installation

yarn install --ignore-scripts

npm install --ignore-scripts

Skips execution of lifecycle scripts during installation

yarn install --prefer-offline

npm install --prefer-offline

Uses the offline mirror for installation if available

yarn install --network-timeout <milliseconds>

npm install --network-timeout <milliseconds>

Sets the network timeout for package installation

yarn pack --filename <filename>

npm pack --filename <filename>

Creates a compressed tarball with a specific filename

yarn run <script>

npm run <script>

Runs a script defined in the package.json file

yarn run <command>

npm run <command>

Runs a custom command specified in the scripts section

yarn exec <command>

npm exec <command>

Executes a command within the context of the project

yarn clean

-

Removes the generated build artifacts and temporary files

yarn cache dir

npm config get cache

Displays the directory where Yarn or npm stores the cache

yarn cache clean

npm cache clean

Clears the package cache

yarn cache list

npm cache ls

Lists all the packages in the cache

yarn cache add <package>

npm cache add <package>

Adds a package to the cache without installing it

yarn cache clean <package>

npm cache clean <package>

Removes a specific package from the cache

yarn why <package>

npm why <package>

Displays information about why a package is installed

yarn licenses ls

npm ls --production --parseable --depth=0

Lists the licenses of installed packages

yarn licenses generate-disclaimer

-

Generates a license disclaimer for the project

yarn upgrade-interactive

-

Interactively upgrades packages to their latest versions

yarn dedupe

npm dedupe

Optimizes the dependency tree by deduplicating packages

yarn clean-cache

npm cache clean

Clears the Yarn or npm cache

yarn info <package>

npm show <package>

Displays detailed information about a specific package

yarn audit

npm audit

Performs a security audit on installed packages

yarn audit fix

npm audit fix

Fixes vulnerabilities found during the security audit

yarn import <package>

-

Imports a package into the project from an npm registry

yarn plugin import <package>

-

Imports a Yarn plugin into the project

yarn plugin list

-

Lists the installed Yarn plugins

yarn plugin remove <plugin>

-

Removes a Yarn plugin from the project

yarn constraints

-

Displays the active constraints for installed packages

yarn dlx <command>

npx <command>

Runs a command from a package without installing it

yarn version --new-version <version>

npm version <version>

Updates the version of the project

Did you find this article valuable?

Support Sravanth by becoming a sponsor. Any amount is appreciated!