Switch Package Manager
npm install swpm











!swpm
---
- Getting Started
- Prerequisites
- Installing
- swpm
- swpx
- Alias
- Flags
- Default
- FAQ
- About
---
When switching between JavaScript projects, it's often easy to forget which package manager should be used. JavaScript package managers aren't quite compatible either and each one resolves dependencies differently, so accidentally installing with npm could cause a yarn (classic or berry), pnpm or bun project to break.
swpm is a CLI that intends to solve this problem by unifying the most used commands for the most common Node Package Managers into one. It will recognize the Package Manager used on the project and automatically will translate those commands.
This is an example of how #swpm works. The same command, no matter the package manager used on the project.
> Note:
> We will start with most used command, then other commands will be added gradually.
> Track the command progress implementation on CHEATSHEET.
>
> Progress: 90% of commands included.
What things you need to install?
- node.js
Install as global with any of this package managers
| Package Manager | Install Command |
| --------------- | ------------------------------------ |
| npm | npm install swpm --global |
| yarn | yarn global add swpm |
| pnpm | pnpm install swpm --global |
| bun | bun install -g swpm |
| volta | volta install swpm |
---
The swpm will run the command switching automatically searching for the lock file or the pinned Package Manager.
``bash`
swpm [
With swpm --help it will show a command help resume.
`text
swpm [
Commands:
swpm install [args] install packages from package.json [aliases: i]
swpm add
swpm remove
swpm update
swpm upgrade
swpm interactive [args] update packages interactive [aliases: ui]
swpm open [resource] [args] open in the file explore or browser[aliases: o]
swpm clean [args] clean packages [aliases: c]
Options:
-u, --use use a package manager
[choices: "npm", "yarn", "yarn@berry", "pnpm", "bun"]
-p, --pin pin a package manager
[choices: "npm", "yarn", "yarn@berry", "pnpm", "bun"]
--unpin unpin current package manager
-t, --test test command (without running)
[choices: "npm", "yarn", "yarn@berry", "pnpm", "bun"]
-m, --mute mute command translation
--alias show command alias [boolean]
--info show information and versions [boolean]
--help Show help [boolean]
`
#### Install
By default, will install all modules listed as dependencies in package.json.
`bash`
swpm install [args]
> Alias: i
This command installs a package and any packages that it depends on. If the package has a lock file, the installation of dependencies will be driven by that.
| Args | Alias | Description |
| ---------------- | ----- | ----------------------------------------------------------------------------- |
| --frozen | -F | install dependencies from lock file (without updating it). Also known as ci |--package-lock
| | -P | install dependencies but don't read or generate a lockfile |
> Warning:
> The --package-lock argument is not available on bun Package Manager.
#### Add
This command, no arguments, will add a package to local package.json file. Package will appear as dependencies by default.
`bash`
swpm add
> Alias: a
swpm add saves any specified packages into dependencies by default. Additionally, you can control where and how they get saved with some additional flags:
| Args | Alias | Description |
| ----------------- | ----- | ----------------------------------------------------------------------------------------------------- |
| --save-dev | -D | Package will appear in devDependencies |--save-optional
| | -O | Package will appear in optionalDependencies |--save-peer
| | | Package will appear in peerDependencies |--save-exact
| | -E | Dependencies will be configured with an exact version rather than using default semver range operator |--global
| | -g | Remove the current package context as a global package |
#### Remove
This command, no arguments, will remove a package to local package.json file. Package will be removed as dependencies by default.
`bash`
swpm remove
> Aliases: r, rm, uninstall, un
| Args | Alias | Description |
| ----------------- | ----- | ------------------------------------------------------ |
| --save-dev | -D | Package will be removed from devDependencies |--save-optional
| | -O | Package will be removed from optionalDependencies |--save-peer
| | | Package will be removed from peerDependencies |--global
| | -g | Remove the current package context as a global package |
#### Update
This command will update all the packages listed to the latest version (specified by the tag config), respecting the semver constraints of both your package and its dependencies (if they also require the same package). It will also install missing packages.
`bash`
swpm update [
> Aliases: up, ud
| Args | Alias | Description |
| ----------------- | ----- | ----------------------------------------------------------------------------------------------------- |
| --global | -g | Update the current package context as a global package |
> It will not made modifications on package.json file
#### Upgrade
This command will upgrade the packages to the latest version, ignoring ranges specified in package.json.
> It will save new version on package.json file
`bash`
swpm upgrade
> Alias: ug
| Args | Alias | Description |
| ----------------- | ----- | ----------------------------------------------------------------------------------------------------- |
| --save-exact | -E | Dependencies will be upgrade with an exact version rather than using default semver range operator |--global
| | -g | Upgrade the current package context as a global package |
> Warning:
> This command is not available on bun Package Manager.
#### Interactive
Show outdated dependencies and select which ones to upgrade.
`bash`
swpm interactive [args]
> Alias: ui
| Args | Alias | Description |
| ----------------- | ----- | --------------------------------------------------------------------- |
| --latest | -L | Updates all dependencies, ignoring ranges specified in package.json |--global
| | -g | Update the current package context as a global package |
> Warning:
> This command is not available on npm and bun Package Manager.
#### Open
This command does not exist in the package managers, but is a common task open a path or urls related to the project in the file explorer or browser.
`bash`
swpm open [resource] [args]
> Alias: o
| Args | Alias | Description |
| ------------------------ | ----- | ---------------------------------------------------------------------- |
| --explorer [path] | -E | Open path on file explorer (current path by default) |--git-repo
| | -R | Browse current repo on browser |--git-branch
| | -B | Browse current branch on browser |--git-pipelines
| | -P | Browse pipelines/actions tab of current repo on browser |--git-merge
| | -M | Browse merge/pull request tab of current repo on browser |--git-diff [branch]
| | -D | Browse diff current branch with another (dev by default) |--coverage [filePath]
| | -C | Browse coverage report on browser (./coverage/index.html by default) |--npm [package]
| | -N | Browse package on nmpjs.com (current name by default) |
> Info:
> Those --git flags are compatible with GitHub, GitLab and BitBucket.
Examples:
Assuming that the current path is /path/to/project, with --explorer it will open the path in the file explorer:
| Command | Alias | Path |
| --------------------------------- | ---------------- | ---------------------------- |
| swpm open --explorer | soe | /path/to/project |swpm open --explorer .
| | soe . | /path/to/project |swpm open --explorer subfolder
| | soe subfolder | /path/to/project/subfolder |swpm open --explorer ..
| | soe .. | /path/to |swpm open --explorer ...
| | soe ... | /path |swpm open --explorer ../another
| | soe ../another | /path/to/another |swpm open --explorer /
| | soe / | / (only on unixish OS) |swpm open --explorer ~
| | soe ~ | ~ (only on unixish OS) |
Assuming the current branch is new-feat, with --diff will open the diff comparison in the browser:
| Command | Alias | Diff |
| --------------------------- |-------------- | ------------------------------------------- |
| swpm open --diff |sgd | dev branch vs new-feat branch |swpm open --diff main
| |sgd main | main branch vs new-feat branch |swpm open --diff f92fbcf8
| |sgd f92fbcf8 | f92fbcf8 commit hash vs new-feat branch |swpm open --diff v1.0.0
| |sgd v1.0.0 | v1.0.0 tag vs new-feat branch |
Assuming you have generated a coverage report, with --coverage it will open the report in the browser:
| Command | Alias | File Path |
| ------------------------------------------------------- | -------------------------------------- | -------------------------------------------------- |
| swpm open --coverage | soc | /path/to/project/coverage/index.html |swpm open --coverage .
| | soc . | Fails (also need the file name) |swpm open --coverage ./index.html
| | soc ./index.html | /path/to/project/index.html |swpm open --coverage coverage/lcov-report/index.html
| | soc coverage/lcov-report/index.html | /path/to/project/coverage/lcov-report/index.html |swpm open --coverage ../index.html
| | soc ../index.html | /path/to/index.html |
Assuming that the property name in package.json file is swpm, with --npm will open the package on npmjs.com:
| Command | Alias | npm Url |
| ---------------------------- |---------------- | ------------------------------------------ |
| swpm open --npm |son | https://www.npmjs.com/package/swpm |swpm open --npm typescript
| |son typescript | https://www.npmjs.com/package/typescript |
#### Clean
This command does not exist in the package managers, but is one of the most repetitive tasks, deleting files and folders. Very useful, for example, in scenarios where you want to change completely the Package Manager.
`bash`
swpm clean [args]
> Alias: c
| Args | Alias | Description |
| ----------------- | ----- | ------------------------------------------------------------------------------------ |
| --modules | | node_modules, .yarn/cache, .yarn/unplugged folders and .pnp\* files |--lock
| | | lock files |--log
| | | log files |--build
| | | build folder |--coverage
| | | coverage folder |--fresh
| | | Run all args except --lock |--all
| | | Run all args and also delete the .yarn folder |
> --fresh is a good choice when you want to clean the project and reinstall all packages again with swpm install --frozen and preserving the lock files intact. --all
> is a good choice when you want to clean the project and migrate to a different Package Manager or reinstalling all packages and create/update the lock files.
---
There are commands that don't need the swpm translation tool, because share the same structure as all package managers.
#### Init
init or create can be used to set up a new or existing package.
`bash`
swpm init [--yes]
swpm create
> To run these commands in a path where a package.json didn't exist add the flag --use at the end or setup an SWPM environment variable.
#### Login/Logout
Login and logout to https://www.npmjs.com/.
Commonly used when you need to publish or admin packages.
`bash`
swpm login
swpm logout
> To run these commands in a path where a package.json didn't exist add the flag --use at the end or setup an SWPM environment variable.
#### Scripts
This runs an arbitrary command from a package's "scripts" object.
If no "command" is provided, it will list the available scripts.
`bash`
swpm run
swpm test
swpm build
> You can run test and build "scripts" without the run prefix.
#### Publish
Commands to pack, publish, unpublish or deprecate a package on https://www.npmjs.com/.
`bash`
swpm pack
swpm pack --pack-destination
swpm publish
swpm unpublish
swpm deprecate
#### Config
See the local configuration, or setup default values.
`bash`
swpm config list
swpm config set
Example:
`bash`
swpm config set save-exact true
swpm config set save-prefix '~'
> To run these commands in a path where a package.json didn't exist add the flag --use at the end.
#### Versions
outdated will check the registry to see if any (or, specific) installed packages are currently outdated.
`bash`
swpm outdated [
> To run this commands in a path where a package.json didn't exist with flag --global add the flag --use at the end or setup an SWPM environment variable.
---
The swpx will execute the command switching automatically to the pinned Package Manager without previous installing the package.
`bash`
swpx [
With swpx --help it will show a command help resume.
`text
swpx [
Options:
-u, --use use a package manager
[choices: "npm", "yarn", "yarn@berry", "pnpm", "bun"]
-t, --test test command (without running)
[choices: "npm", "yarn", "yarn@berry", "pnpm", "bun"]
-m, --mute mute command translation
--alias show command alias [boolean]
--info show information and versions [boolean]
--help Show help [boolean]
`
| Package / Command | sx | sx vitest |npx
| ----------------- | -------------------- | ----------------- |
| npm | | npx vitest |yarn dlx
| yarn | | yarn dlx vitest |pnpm dlx
| pnpm | | pnpm dlx vitest |bunx
| bun | | bunx vitest |
---
Quick and short aliases for swpm and swpx commands.
| Alias | Command |
| ------ | ------------------------------------------- |
| sp | swpm --pin |spn
| | swpm --pin npm |spy
| | swpm --pin yarn |spyb
| | swpm --pin yarn@berry |spp
| | swpm --pin pnp |spb
| | swpm --pin bun |
| Package/Alias | si |npm install
| ------------- | -------------- |
| npm | |yarn install
| yarn | |pnpm install
| pnpm | |bun install
| bun | |
| Package/Alias | sif |npm ci
| -------------- | -------------------------------- |
| npm | |yarn install --frozen-lockfile
| yarn | |yarn install --immutable
| yarn@berry | |pnpm install --frozen-lockfile
| pnpm | |bun install --frozen-lockfile
| bun | |
| Package/Alias | sa | sa vite |npm add
| -------------- | -------------------- | --------------- |
| npm | | npm add vite |yarn add
| yarn | | yarn add vite |pnpm add
| pnpm | | pnpm add vite |bun add
| bun | | bun add vite |
| Package/Alias | sae | sae vite |npm add
| -------------- | --------------------------------- | ---------------------------- |
| npm | | npm add vite --save-exact |yarn add
| yarn | | yarn add vite --exact |pnpm add
| pnpm | | pnpm add vite --save-exact |bun add
| bun | | bun add vite --save-exact |
| Package/Alias | sad | sad vite |npm add
| -------------- | ------------------------------- | -------------------------- |
| npm | | npm add vite --save-dev |yarn add
| yarn | | yarn add vite --dev |pnpm add
| pnpm | | pnpm add vite --save-dev |bun add
| bun | | bun add vite --save-dev |
| Package/Alias | sade | sade vite |npm add
| -------------- | -------------------------------------------- | --------------------------------------- |
| npm | | npm add vite --save-dev --save-exact |yarn add
| yarn | | yarn add vite --dev --exact |pnpm add
| pnpm | | pnpm add vite --save-dev --save-exact |bun add
| bun | | bun add vite --save-dev --save-exact |
| Package/Alias | sag | sag eslint |npm add
| -------------- | -------------------------------------- | ----------------------------------- |
| npm | | npm add eslint --global |yarn add global
| yarn | | yarn add global eslint |pnpm add
| pnpm | | pnpm add eslint --global |bun add
| bun | | bun add eslint --global |volta install
| volta | | volta install eslint |
| Package/Alias | srm | srm vite |npm uninstall
| -------------- | -------------------------- | --------------------- |
| npm | | npm uninstall vite |yarn remove
| yarn | | yarn remove vite |pnpm uninstall
| pnpm | | pnpm uninstall vite |bun remove
| bun | | bun remove vite |
| Package/Alias | srg | srg eslint |npm uninstall
| -------------- | -------------------------------------------- | ----------------------------------------- |
| npm | | npm uninstall eslint --global |yarn remove global
| yarn | | yarn remove global eslint |pnpm uninstall
| pnpm | | pnpm uninstall eslint --global |bun remove
| bun | | bun remove eslint --global |volta uninstall
| volta | | volta uninstall eslint |
| Package/Alias | sup [ | sup vite |npm update [
| -------------- | ---------------------------- | --------------------- |
| npm | | npm update vite |yarn upgrade [
| yarn | | yarn upgrade vite |yarn semver up [
| yarn@berry | | yarn semver up vite |pnpm update [
| pnpm | | pnpm update vite |bun update [
| bun | | bun update vite |
| Package/Alias | sug | sug vite |npm add
| -------------- | --------------------------------- | ---------------------------- |
| npm | | npm add vite@latest |yarn upgrade
| yarn | | yarn upgrade vite --latest |yarn up
| yarn@berry | | yarn up vite |pnpm update
| pnpm | | pnpm update vite --latest |
| bun | N/A | N/A |
| Package/Alias | sui |yarn upgrade-interactive
| -------------- | --------------------------- |
| npm | N/A |
| yarn | |pnpm update --interactive
| pnpm | |
| bun | N/A |
| Package/Alias | scr | scr vite |npm create
| -------------- | ----------------------- | ------------------ |
| npm | | npm create vite |yarn create
| yarn | | yarn create vite |pnpm create
| pnpm | | pnpm create vite |bun create
| bun | | bun create vite |
| Package/Alias | sr