FrontEnd web
Published in

FrontEnd web

How to manage multiple nodejs versions with pnpm?
How to manage multiple nodejs versions with pnpm?

How to manage multiple nodejs versions with pnpm?

PNPM is a new node package manager similar to npm and yarn. pnpm is faster as compared to npm and yarn.

PNPM help to reduce the size of the node_module folder in the project. PNPM is so fast to install the nodejs package from npm.

Everybody uses nodejs in daily life to build projects. So for everyday life, you need multiple nodejs active versions.

NVM is an excellent tool for installing multiple versions of nodejs. However, maybe you do not like nvm or use different package versions, for example, n, nodenv, and asdf.

To install pnpm on a laptop, you do not need to install nodejs firstly. Instead, you can install pnpm and then help of the pnpm, install any nodejs version and manage multiple nodejs versions.

In javascript, there are five prominent nodejs version managers.

Follow youtube tutorial

Follow youtube video

Questions

  1. How to install different nodejs version managers with pnpm?
  2. How to install a specific nodejs version according to the workspace with pnpm?

How to install different nodejs version managers with pnpm?

PNPM provide inbuilt pnpm env use --global < OPTIONS > command to install nodejs. you can check all OPTIONS with pnpm env --help command.

check all pnpm env usage
check all pnpm env usage

Syntax

pnpm env use --global < OPTIONS >
  • pnpm env use --global lts command install the LTS version of nodejs
  • with pnpm env use --global < version > command, you install any version of nodejs
  • with pnpm env use --global latest command, you install the current version of nodejs
  • With pnpm env use --global < name > command, you install nodejs by name.

Example

pnpm env use — global lts

install lts version of nodejs with pnpm
install lts version of nodejs with pnpm

pnpm env use — global < version >

install 15 version of nodejs with pnpm
install 15 version of nodejs with pnpm

pnpm env use — global latest

install the latest version of nodejs with pnpm
install the latest version of nodejs with pnpm

pnpm env use — global < name >

install nodejs by name with pnpm
install nodejs by name with pnpm

After installing nodejs with pnpm env You check the version with node -v

❯ node -v
v18.9.0

How to install a specific nodejs version according to the workspace with pnpm?

You install a particular version of nodejs according to a workspace with pnpm. For installation, you need a .npmrc file.

Steps

  1. Create a .npmrc file
  2. Initialization the pnpm init
  3. Add Script
  4. Run pnpm test

Create a .npmrc file

The first step is to create a .npmrc file in the root folder.

.npmrc 
use-node-version=11.15.0

Initialization the pnpm init

The Initialization of the new project with pnpm init command. The pnpm init command creates a new package.json file.

Initialization the pnpm init
Initialization the pnpm init

Add Script

You can replace scripts with the package.json file.

"scripts": {   "test": "node -v"},

Run pnpm test

In the end, you can run pnpm test command and your output look like this.

Run pnpm test command
Run pnpm test command

Note

In the terminal, you run node -v in the same working folder and without pnpm, your node -v version print defiantly in the terminal.

Check the nodejs version in the terminal
Check the nodejs version in the terminal

Conclusion

PNPM is a new node package manager and comes with lots of features. The feature pnpm part is memory (space) management, and the second is multiple node version manager.

Developers continuously adapt and learn new techniques for a better future. pnpm is one of them. It is not required. But pnpm helps enhance your working speed.

You can use any other nodejs version manager. However, if you are a beginner, I suggest starting with pnpm. pnpm is very easy as compared to other nodejs version managers.

You can share and follow on Twitter and Linkedin. If you like my work, please read more content on the officialrajdeepsingh. dev, frontend web, and sign up for a free newsletter on the frontend web.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rajdeep singh

Rajdeep singh

526 Followers

JavaScript || Reactjs || Nextjs || Python || Rust || Biotechnology || Bioinformatic || Front-end Developer || Writer || https://officialrajdeepsingh.dev/