npm vs yarn vs pnpm vs bun commands cheatsheet
Tags: npm, yarn, pnpm, bunjs
Published: 2022-05-10 13:34:10

> If you want to get rid off all those different commands on each package manager I higly recommened to install [SWPM](https://www.npmjs.com/package/swpm) and say good bye to package manager confussion. It will do all the commands translation for you.

This is a cheat sheet that you can use as a handy reference for [npm](https://www.npmjs.com/), [yarn](https://yarnpkg.com/), [pnpm](https://pnpm.io/) and [bun](https://bun.sh/) commands.

> Note: `<package>` follow this structure `<package[@latest|@#.#.#]>`

## Package Commands

| command                                     | npm                                                 | yarn                                            | yarn (berry)                                    | pnpm                                            | bun                                  |
| ------------------------------------------- | --------------------------------------------------- | ----------------------------------------------- | ----------------------------------------------- | ----------------------------------------------- | ------------------------------------ |
| clean cache                                 | `npm cache clean`                                   | `yarn cache clean`                              | `yarn cache clean`                              |  N/A                                            | `bun pm cache rm`                    |
| install from `package.json`                 | `npm install`                                       | `yarn [install]`                                | `yarn [install]`                                | `pnpm install`                                  | `bun install`                        |
| don't read or generate a lockfile           | `npm install --no-package-lock`                     | `yarn install --no-lockfile`                    | `yarn install --no-lockfile`                    |  N/A                                            | `bun install --no-save`              |
| don't generate a lockfile                   |                                                     | `yarn install --pure-lockfile`                  | `yarn install --pure-lockfile`                  |                                                 |  N/A                                 |
| lockfile is not updated                     | `npm ci`                                            | `yarn install --frozen-lockfile`                | `yarn install --immutable`                      | `pnpm install --frozen-lockfile`                | `bun install --frozen-lockfile`      |
| add package                                 | `npm install <package> [--location=global]`         | `yarn [global] add <package>`                   | `yarn [global] add <package>`                   | `pnpm add <package> [--global]`                 | `bun add <package> [--global]`       |
| add package as `dependencies`               | `npm install <package>`                             | `yarn add <package>`                            | `yarn add <package>`                            | `pnpm add <package>`                            | `bun add <package>`                  |
| add package as `devDependencies`            | `npm install <package> --save-dev`                  | `yarn add <package> --dev`                      | `yarn add <package> --dev`                      | `pnpm add <package> --save-dev`                 | `bun add <package> --dev`            |
| add package as `optionalDependencies`       | `npm install <package> --save-optional`             | `yarn add <package> --optional`                 | `yarn add <package> --optional`                 | `pnpm add <package> --save-optional`            | `bun add <package> --optional`       |
| add package as `peerDependencies`           | `npm install <package> --save-peer`                 | `yarn add <package> --peer`                     | `yarn add <package> --peer`                     | `pnpm add <package> --save-peer`                |  N/A                                 |
| add exact version                           | `npm install <package> --save-exact`                | `yarn add <package> --exact`                    | `yarn add <package> --exact`                    | `pnpm add <package> --save-exact`               | `bun add <package> --exact`          |
| remove package                              | `npm uninstall <package> [--location=global]`       | `yarn [global] remove <package>`                | `yarn [global] remove <package>`                | `pnpm uninstall <package> [--global]`           | `bun remove [<package>] [--global]`  |
| remove package as `dependencies`            | `npm uninstall <package>`                           | `yarn remove <package>`                         | `yarn remove <package>`                         | `pnpm uninstall <package>`                      | `bun remove <package>`               |
| remove package as `devDependencies`         | `npm uninstall <package> --save-dev`                | `yarn remove <package> --dev`                   | `yarn remove <package> --dev`                   | `pnpm uninstall <package> --save-dev`           | `bun remove <package> --dev`         |
| remove package as `optionalDependencies`    | `npm uninstall <package> --save-optional`           | `yarn remove <package> --optional`              | `yarn remove <package> --optional`              | `pnpm uninstall <package> --save-optional`      | `bun remove <package> --optional`    |
| remove package as `peerDependencies`        | `npm uninstall <package> --save-peer`               | `yarn remove <package> --peer`                  | `yarn remove <package> --peer`                  | `pnpm uninstall <package> --save-peer`          |  N/A                                 |
| update package (no `package.json`)          | `npm update [<package>] [--location=global]`        | `yarn [global] upgrade [<package>]`             | `yarn [global] semver up [<package>]`           | `pnpm update [<package>] [--global]`            |  N/A                                 |
| upgrade package on `package.json`           | `npm install <package>@latest [--location=global]`  | `yarn [global] upgrade <package> --latest`      | `yarn [global] up <package>`                    | `pnpm update <package> --latest [--global]`     |  N/A                                 |
| upgrade interactive                         |  N/A                                                | `yarn upgrade-interactive`                      | `yarn upgrade-interactive`                      | `pnpm update --interactive`                     |  N/A                                 |
| list all package at the top level           | `npm list --depth 0 [--location=global]`            | `yarn [global] list --depth 0`                  | `yarn [global] list --depth 0`                  | `pnpm list --depth 0 [--global]`                | `bun pm ls`                          |
| audit vulnerable dependencies               | `npm audit [fix]`                                   | `yarn audit`                                    | `yarn audit`                                    | `pnpm audit [--fix]`                            |                                      |

## Shared Commands

Use the same command structure between package managers.

| command                                | npm                                            | yarn                                            | yarn (berry)                                    | pnpm                                            | bun                              |
| -------------------------------------- | ---------------------------------------------- | ----------------------------------------------- | ----------------------------------------------- | ----------------------------------------------- | -------------------------------- |
| init or create                         | `npm init`                                     | `yarn init`                                     | `yarn init`                                     | `pnpm init`                                     | `bun init`                       |
| login/logout                           | `npm <login or logout>`                          | `yarn <login or logout>`                          | `yarn <login or logout>`                          | `pnpm <login or logout>`                          |  TBA                             |
| run scripts                            | `npm run <script>`                             | `yarn run <script>`                             | `yarn run <script>`                             | `pnpm [run] <script>`                           | `bun run <script>`               |
| run test                               | `npm test`                                     | `yarn test`                                     | `yarn test`                                     | `pnpm test`                                     | `bun test`                       |
| crate bundle package                   | `npm build`                                    | `yarn build`                                    | `yarn build`                                    | `pnpm build`                                    | `bun build`                      |
| publish                                | `npm publish`                                  | `yarn publish`                                  | `yarn npm publish`                              | `pnpm publish`                                  |  TBA                             |
| unpublish                              | `npm unpublish <package>[@#.#.#]`              | `yarn unpublish <package>[@#.#.#]`              | `yarn unpublish <package>[@#.#.#]`              | `pnpm unpublish <package>[@#.#.#]`              |  TBA                             |
| deprecate                              | `npm deprecate <package>[@#.#.#] <message>`    | `yarn deprecate <package>[@#.#.#] <message>`    | `yarn deprecate <package>[@#.#.#] <message>`    | `pnpm deprecate <package>[@#.#.#] <message>`    |  TBA                             |
| config list                            | `npm config list`                              | `yarn config list`                              | `yarn config list`                              | `pnpm config list`                              |  TBA                             |
| config `--save-default` as default     | `npm config set save-exact true`               | `yarn config set save-exact true`               | `yarn config set save-exact true`               | `pnpm config set save-exact true`               |  TBA                             |
| config `~` as default instead `^`      | `npm config set save-prefix '~'`               | `yarn config set save-prefix '~'`               | `yarn config set save-prefix '~'`               | `pnpm config set save-prefix '~'`               |  TBA                             |
| list outdated packages                 | `npm outdated [<package>] [--location=global]` | `yarn [global] [<package>] outdated`            | `yarn [global] [<package>] upgrade-interactive` | `pnpm outdated [<package>] [--global]`          |  TBA                             |
| link local package                     | `npm link [<folder>]`                          | `yarn link [<folder>]`                          | `yarn link [<folder>]`                          | `pnpm link [<folder>]`                          | `bun link [<folder>]`            |
| unlink local package                   | `npm unlink [<folder\|package> --no-save]`     | `yarn unlink [<folder\|package>]`               | `yarn unlink [<folder\|package>]`               | `pnpm unlink [<folder\|package>]`               | `bun unlink [<folder\|package>]` |

## Run Remotely

Run a command without installing it.

| command      | npm              | yarn                 | yarn (berry)         | pnpm                  | bun              |
| ------------ | ---------------- | -------------------- | -------------------- | --------------------- | ---------------- |
| run package  | `npx <package>`  | `yarn dlx <package>` | `yarn dlx <package>` | `pnpm dlx <package>`  | `bunx <package>` |

---

## CLI documentation

- [npm](https://docs.npmjs.com/cli/v8/commands)
- [yarn](https://classic.yarnpkg.com/en/docs/cli/)
- [pnpm](https://pnpm.io/cli/install)
- [bun](https://github.com/oven-sh/bun#using-bun-as-a-package-manager)

---

## Source

Contributions are welcome.

{% github deinsoftware/swpm %}

---

**That’s All Folks!**
**Happy Coding** 🖖

[![beer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mkgsx4aw6qn9i9ax6as9.png)](https://github.com/sponsors/deinsoftware)