2.7.1 • Published 28 days ago

@joshmeads/swpm v2.7.1

Weekly downloads
-
License
MIT
Repository
github
Last release
28 days ago

Switch Package Manager

build publish Sonar-reliability sonar-security sonar-maintainability sonar-coverage
npm-version npm-downloads node-engine volta js-standard-style license

swpm


Menu


Getting Started

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.

swpm-example

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.

Prerequisites

What things you need to install?

Installing

Install as global with any of this package managers

Package ManagerInstall Command
npmnpm install swpm --global
yarnyarn global add swpm
pnpmpnpm install swpm --global
bunbun install -g swpm
voltavolta install swpm

Back to menu


swpm

The swpm will run the command switching automatically searching for the lock file or the pinned Package Manager.

swpm [<command>] [args] [FLAGS]

Help

With swpm --help it will show a command help resume.

swpm [<command>] [args] [FLAGS]

Commands:
  swpm install [args]            install packages from package.json [aliases: i]
  swpm add <package> [args]      add package                        [aliases: a]
  swpm remove <package> [args]   remove package  [aliases: r, rm, uninstall, un]
  swpm update <package> [args]   update package                [aliases: up, ud]
  swpm upgrade <package> [args]  upgrade package to latest         [aliases: ug]
  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]

Commands

Install

By default, will install all modules listed as dependencies in package.json.

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.

ArgsAliasDescription
--frozen-Finstall dependencies from lock file (without updating it). Also known as ci
--package-lock-Pinstall 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.

swpm add <package> [args]

Alias: a

swpm add <package> saves any specified packages into dependencies by default. Additionally, you can control where and how they get saved with some additional flags:

ArgsAliasDescription
--save-dev-DPackage will appear in devDependencies
--save-optional-OPackage will appear in optionalDependencies
--save-peerPackage will appear in peerDependencies
--save-exact-EDependencies will be configured with an exact version rather than using default semver range operator
--global-gRemove 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.

swpm remove <package> [args]

Aliases: r, rm, uninstall, un

ArgsAliasDescription
--save-dev-DPackage will be removed from devDependencies
--save-optional-OPackage will be removed from optionalDependencies
--save-peerPackage will be removed from peerDependencies
--global-gRemove 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.

swpm update [<package>] [args]

Aliases: up, ud

ArgsAliasDescription
--global-gUpdate 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

swpm upgrade <package> [args]

Alias: ug

ArgsAliasDescription
--save-exact-EDependencies will be upgrade with an exact version rather than using default semver range operator
--global-gUpgrade 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.

swpm interactive [args]

Alias: ui

ArgsAliasDescription
--latest-LUpdates all dependencies, ignoring ranges specified in package.json
--global-gUpdate 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.

swpm open [resource] [args]

Alias: o

ArgsAliasDescription
--explorer [path]-EOpen path on file explorer (current path by default)
--git-repo-RBrowse current repo on browser
--git-branch-BBrowse current branch on browser
--git-pipelines-PBrowse pipelines/actions tab of current repo on browser
--git-merge-MBrowse merge/pull request tab of current repo on browser
--git-diff [branch]-DBrowse diff current branch with another (dev by default)
--coverage [filePath]-CBrowse coverage report on browser (./coverage/index.html by default)
--npm [package]-NBrowse 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:

CommandAliasPath
swpm open --explorersoe/path/to/project
swpm open --explorer .soe ./path/to/project
swpm open --explorer subfoldersoe subfolder/path/to/project/subfolder
swpm open --explorer ..soe ../path/to
swpm open --explorer ...soe .../path
swpm open --explorer ../anothersoe ../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:

CommandAliasDiff
swpm open --diffsgddev branch vs new-feat branch
swpm open --diff mainsgd mainmain branch vs new-feat branch
swpm open --diff f92fbcf8sgd f92fbcf8f92fbcf8 commit hash vs new-feat branch
swpm open --diff v1.0.0sgd v1.0.0v1.0.0 tag vs new-feat branch

Assuming you have generated a coverage report, with --coverage it will open the report in the browser:

CommandAliasFile Path
swpm open --coveragesoc/path/to/project/coverage/index.html
swpm open --coverage .soc .Fails (also need the file name)
swpm open --coverage ./index.htmlsoc ./index.html/path/to/project/index.html
swpm open --coverage coverage/lcov-report/index.htmlsoc coverage/lcov-report/index.html/path/to/project/coverage/lcov-report/index.html
swpm open --coverage ../index.htmlsoc ../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:

CommandAliasnpm Url
swpm open --npmsonhttps://www.npmjs.com/package/swpm
swpm open --npm typescriptson typescripthttps://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.

swpm clean [args]

Alias: c

ArgsAliasDescription
--modulesnode_modules, .yarn/cache, .yarn/unplugged folders and .pnp* files
--locklock files
--loglog files
--buildbuild folder
--coveragecoverage folder
--freshRun all args except --lock
--allRun 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.

Back to menu


Shared Commands

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.

swpm init [--yes]
swpm create <name> [<args>]

To run these commands in a path where a package.json didn't exist add the flag --use <npm|yarn[@berry]|pnpm|bun> 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.

swpm login
swpm logout

To run these commands in a path where a package.json didn't exist add the flag --use <npm|yarn[@berry]|pnpm|bun> 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.

swpm run <command> [<args>]
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/.

swpm pack
swpm pack --pack-destination <path>
swpm publish
swpm unpublish <package>
swpm deprecate <package> <message>

Config

See the local configuration, or setup default values.

swpm config list
swpm config set <key> <value>

Example:

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 <npm|yarn[@berry]|pnpm|bun> at the end.

Versions

outdated will check the registry to see if any (or, specific) installed packages are currently outdated.

swpm outdated [<package>] [--global]

To run this commands in a path where a package.json didn't exist with flag --global add the flag --use <npm|yarn[@berry]|pnpm|bun> at the end or setup an SWPM environment variable.

Back to menu


swpx

The swpx will execute the command switching automatically to the pinned Package Manager without previous installing the package.

swpx [<command>] [FLAGS]

Help

With swpx --help it will show a command help resume.

swpx [<command>] [FLAGS]

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 / Commandsx <package>sx vitest
npmnpx <package>npx vitest
yarnyarn dlx <package>yarn dlx vitest
pnpmpnpm dlx <package>pnpm dlx vitest
bunbunx <package>bunx vitest

Back to menu


Alias

Quick and short aliases for swpm and swpx commands.

sp?

AliasCommand
spswpm --pin <npm\|yarn[@berry]\|pnpm\|bun>
spnswpm --pin npm
spyswpm --pin yarn
spybswpm --pin yarn@berry
sppswpm --pin pnp
spbswpm --pin bun

si (install)

Package/Aliassi
npmnpm install
yarnyarn install
pnpmpnpm install
bunbun install

sif (install frozen)

Package/Aliassif
npmnpm ci
yarnyarn install --frozen-lockfile
yarn@berryyarn install --immutable
pnpmpnpm install --frozen-lockfile
bunbun install --frozen-lockfile

sa (add)

Package/Aliassa <package>sa vite
npmnpm add <package>npm add vite
yarnyarn add <package>yarn add vite
pnpmpnpm add <package>pnpm add vite
bunbun add <package>bun add vite

sae (add save exact)

Package/Aliassae <package>sae vite
npmnpm add <package> --save-exactnpm add vite --save-exact
yarnyarn add <package> --exactyarn add vite --exact
pnpmpnpm add <package> --save-exactpnpm add vite --save-exact
bunbun add <package> --save-exactbun add vite --save-exact

sad (add save dev)

Package/Aliassad <package>sad vite
npmnpm add <package> --save-devnpm add vite --save-dev
yarnyarn add <package> --devyarn add vite --dev
pnpmpnpm add <package> --save-devpnpm add vite --save-dev
bunbun add <package> --save-devbun add vite --save-dev

sade (add save dev exact)

Package/Aliassade <package>sade vite
npmnpm add <package> --save-dev --save-exactnpm add vite --save-dev --save-exact
yarnyarn add <package> --dev --exactyarn add vite --dev --exact
pnpmpnpm add <package> --save-dev --save-exactpnpm add vite --save-dev --save-exact
bunbun add <package> --save-dev --save-exactbun add vite --save-dev --save-exact

sag (add global)

Package/Aliassag <package>sag eslint
npmnpm add <package> --globalnpm add eslint --global
yarnyarn add global <package>yarn add global eslint
pnpmpnpm add <package> --globalpnpm add eslint --global
bunbun add <package> --globalbun add eslint --global
voltavolta install <package>volta install eslint

srm (remove)

Package/Aliassrm <package>srm vite
npmnpm uninstall <package>npm uninstall vite
yarnyarn remove <package>yarn remove vite
pnpmpnpm uninstall <package>pnpm uninstall vite
bunbun remove <package>bun remove vite

srg (remove global)

Package/Aliassrg <package>srg eslint
npmnpm uninstall <package> --globalnpm uninstall eslint --global
yarnyarn remove global <package>yarn remove global eslint
pnpmpnpm uninstall <package> --globalpnpm uninstall eslint --global
bunbun remove <package> --globalbun remove eslint --global
voltavolta uninstall <package>volta uninstall eslint

sup (update)

Package/Aliassup [<package>]sup vite
npmnpm update [<package>]npm update vite
yarnyarn upgrade [<package>]yarn upgrade vite
yarn@berryyarn semver up [<package>]yarn semver up vite
pnpmpnpm update [<package>]pnpm update vite
bunbun update [<package>]bun update vite

sug (upgrade)

Package/Aliassug <package>sug vite
npmnpm add <package>@latestnpm add vite@latest
yarnyarn upgrade <package> --latestyarn upgrade vite --latest
yarn@berryyarn up <package>yarn up vite
pnpmpnpm update <package> --latestpnpm update vite --latest
bunN/AN/A

sui (interactive)

Package/Aliassui
npmN/A
yarnyarn upgrade-interactive
pnpmpnpm update --interactive
bunN/A

scr (create)

Package/Aliasscr <package>scr vite
npmnpm create <package>npm create vite
yarnyarn create <package>yarn create vite
pnpmpnpm create <package>pnpm create vite
bunbun create <package>bun create vite

sr (run)

Package/Aliassr <script>sr dev --port 3030
npmnpm run <script>npm run dev -- --port 3030
yarnyarn run <script>yarn run dev --port 3030
pnpmpnpm run <script>pnpm run dev --port 3030
bunbun run <script>bun run dev --port 3030

sx (execute)

Package / Commandsx <package>sx vitest
npmnpx <package>npx vitest
yarnyarn dlx <package>yarn dlx vitest
pnpmpnpm dlx <package>pnpm dlx vitest
bunbunx <package>bunx vitest

soe (open explorer)

Aliassoe [path]
soeswpm open --explorer
soe [path]swpm open --explorer [path]

sg<?> (open git)

AliasCommandGitHubGitLabBitbucket
sgrswpm open --git-repo
sgbswpm open --git-branch
sgpswpm open --git-pipelineactionspipelinespipelines
sgmswpm open --git-mergepullsmerge_requestspull-requests

sgd (open git-diff)

Aliassgd [branch]
sgdswpm open --git-diff
sgd [branch]swpm open --git-diff [branch]

soc (open coverage)

Aliassoc [fileName]
socswpm open --coverage
soc [path]swpm open --coverage [fileName]

son (open npm package)

Aliasson [package]
sonswpm open --npm
son [package]swpm open --npm [package]

sc<?> (clean)

AliasCommand
scnswpm clean --modules
sclswpm clean --lock
scbswpm clean --build
scdswpm clean --dist
sccswpm clean --coverage
scaswpm clean --all
scfswpm clean --fresh && swpm install --frozen
sciswpm clean --all && swpm install

Back to menu


Flags

Flags are important to swpm and swpx because can modify or set his behavior.

Use

The <swpm|swpx> --use flag allows you to choose your Package Manager for a project.

swpm <command> [args] --use <npm|yarn[@berry]|pnpm|bun>
swpx <command> [args] -u <npm|yarn[@berry]|pnpm|bun>

It will run the command using the selected Package Manager, no matter the swpm property in your package.json.

Info:
Previously it will ask to run the set command when use yarn:
yarn set version classic for yarn yarn set version berry for yarn@berry

Pin

The swpm --pin flag allows you to choose your Package Manager for a project.

swpm --pin <npm|yarn[@berry]|pnpm|bun>
swpm -p <npm|yarn[@berry]|pnpm|bun>

It will store the pinned Package Manager in the package.json file, so you can commit your choice of tools to version control:

{
+ "swpm": "<package-manager-name>"
}

Info:
Additionally it will ask to run the set command when pin yarn:
yarn set version classic for yarn yarn set version berry for yarn@berry

You also can set it manually. Just take care writing a valid Package Manager: npm, yarn[@berry], pnpm or bun. And also remember to run the set command for yarn projects.

Unpin

The swpm --unpin flag allows you to remove the current pinned Package Manager for a project.

swpm --unpin

It will remove the pinned Package Manager in the package.json file.

Test

The <swpm|swpx> --test flag show the equivalent command using the selected Package Manager, but it will not run the command

swpm <command> [args] --test <npm|yarn[@berry]|pnpm|bun>
swpm <command> [args] -t <npm|yarn[@berry]|pnpm|bun>
swpx <command> -t <npm|yarn[@berry]|pnpm|bun>

It will show the command using the selected Package Manager, no matter the swpm property in your package.json.

Mute

The <swpm|swpx> --mute flag hide the package manager command translation.

swpm <command> [args] --mute
swpx <command> [args] --mute

Alias

The <swpm|swpx> --alias flag show the command aliases available.

swpm --alias
swpx --alias

Info

The <swpm|swpx> --info flag show the current Package Manager used and some versions information.

swpm --info
swpx --info

It will search firs the swpm property on the package.json file, and if doesn't not found it, will try to infer the Package Manager in use with help of the lock's file.

Back to menu


Default

You can set a default or global pin Package Manager in order to avoid the --use flag on paths where no exist a package.json or --pin flag on each project.

Create an SWPM environment variable with one of this values <npm|yarn[@berry]|pnpm|bun>.

OSCommand
winsetx SWPM "<npm\|yarn[@berry]\|pnpm\|bun>"
macOSecho 'export SWPM="<npm\|yarn[@berry]\|pnpm\|bun>"' >> <~/.bash_profile\|~/.zshrc>
linuxecho 'export SWPM="<npm\|yarn[@berry]\|pnpm\|bun>"' >> <~/.bash_profile\|~/.zshrc>

Back to menu


FAQ

How infer the Package Manager?

swpm and swpx search some characteristics following this order.

IconStage
📌Search the swpm property pinned on package.json file
📦Search the packageManager property on package.json file
🔒Search for a lock file
🌐Search a SWPM environment variable

What can I use as Package parameter?

The <package> parameter should follow one of these structures:

[<@scope>/]<name>
[<@scope>/]<name>@<tag>
[<@scope>/]<name>@<version>
[<@scope>/]<name>@<version range>
<alias>@npm:<name>
<git-host>:<git-user>/<repo-name>
<git repo url>
<tarball file>
<tarball url>
<folder>

Monorepos

swpm is compatible with monorepos. You can run it from any subdirectory and it will search upwards until it finds the closest package.json and lock file.

Non documented commands

swpm is not restrictive, if a command translations was not included yet, you can write the command as the package manager expect, then swpm will try to infer the package manager and preserve the rest of the command and arguments without changes and run them.

But, if you found one of this cases, please open a command compatibility issue.

Alternatives

A minimalistic solution focus only in the most common used commands:

Back to menu


About

Built With

  • VS Code - Code editing redefined.
  • TypeScript - JavaScript With Syntax For Types.
  • WSL - Windows Subsystem for Linux.
  • Windows Terminal - A modern terminal application for users of command-line tools and shells.
  • Node.js - A JavaScript runtime built on Chrome's V8 JavaScript engine.
  • ESLint - Find and fix problems in your JavaScript code.
  • vitest - A blazing fast unit-test framework powered by Vite ⚡️.
  • Codeium - Free AI code completion and chat.

NPM Packages

  • Chalk - Terminal string styling done right.
  • command-exists - node module to check if a command-line command exists.
  • common-tags - A set of well-tested, commonly used template literal tag functions for use in ES2015+.
  • find-up - Find a file or directory by walking up parent directories.
  • open - Open stuff like URLs, files, executables. Cross-platform.
  • prompts - Lightweight, beautiful and user-friendly interactive prompts
  • semver - The semantic versioner for npm.
  • spinnies create and manage multiple spinners in CLI progrags
  • update-notifier - Update notifications for your CLI app.
  • Yargs - Yargs be a node.js library fer hearties tryin' ter parse optstrings.

Contributing

Please read CONTRIBUTING for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the swpm on GitHub.

Brand

If you want to help, spread the word of swpm in a project or web, please use this BRAND's images.

Authors

Contributors

equiman TylerBarnes

See also the list of contributors who participated in this project.

Sponsors

If this project helps you, consider buying me a cup of coffee.

GitHub Sponsors paypal

License

This project is licensed under the MIT License - see the LICENSE file for details.

Back to menu