Switch Node.js Version in One Command

Omar Aziz - 1 min read
August 10, 2023

  • nodejs

  • nvm

  • npm

  • npx

TL;DR

Run a different version of Node.js in one command

# npx --yes --package node@<version> -- <command>
node --version
> v18.17.1
 
npx --yes --package node@16 -- node --version
> v16.20.1

Among the many use cases is when engines in package.json specifies a different version and engineStrict is set to true.

// package.json
{
  "engines": {
    "node": "16.x"
  },
  "engineStrict": true
}

Running pnpm install with v18.17.1 will fail.

[ERR_PNPM_UNSUPPORTED_ENGINE]
Unsupported environment (bad pnpm and/or Node.js version)
 
Your Node version is incompatible ...
 
Expected version: 16.x
Got: v18.17.1

Fix with

npx --yes --package node@16 -- pnpm install