Blog

Simplifying the use of multiple NodeJS package managers

Updated at January 18, 2024

Shell
NodeJS
Package manager

Simplify your JavaScript package managers across various projects with a shell script. Automate the selection of npm, yarn, pnpm, or bun, enhancing your development workflow.

Introduction

In the world of web development, it’s common to find ourselves working on multiple projects, each with its own JavaScript package manager—npm, yarn, pnpm, or bun. Switching between these tools can be inconvenient and time-consuming. In this post, I’ll introduce a simple yet powerful shell script, pm, that allows you to forget about which package manager you need to run for each project, no more manual package manager typing.

The Script

You can copy and paste the script provided below and save it in your shell runtime configuration file (~/.zshrc | ~/.bashrc).

(Optional) If you are using the script with an external sh file, make sure that the script is executable. Also, make sure to load the external script into your shell’s configuration file.

Make it executable:

  chmod +x /path/to/pm.sh

Load into shell’s rc file:

  [...]
  source /path/to/pm.sh
  [...]

After everything is set up, you can use the pm command in your project directory to run the appropriate package manager based on the project’s lockfile.

   pm install
Conclusion

In summary, the pm shell script simplifies and accelerates your package management workflow. It checks the project’s lockfile (check the table below) and automatically runs the appropriate package manager. Integrate the script into your environment, save time, and enjoy a more efficient development process.

Lockfile Command
package-lock.json npm
yarn.lock yarn
pnpm-lock.yaml pnpm
bun.lockb bun

Feel free to share your thoughts or suggestions (in gist comments or by reaching me out).

Happy coding!