0

I can switch Node version with the command nvm use {version}. With different node versions used by projects, I wish to use a specific version for a local project without switching versions. But when I use nvm use {version} the version is set globally.

I read here you can use a local .nvmrc in the project then by the CLI nvm use to trigger it pick up the version number from .nvmrc .

Somehow .nvmrc is gitignored in the project . I want all developers on a project to use the same version of Node.

Any others alternatives ?

PS: yes, I could just change the .gitignore file . But this has been set for unknown historical reasons. Before I find the answer on the local .nvmrc , let's find alternatives

Raymond Chenon
  • 11,482
  • 15
  • 77
  • 110

1 Answers1

0

The following file settings in the root of your project should prevent proceeding with npm install commands that violate the engine requirements of your project.

You could probably create pre-commit hooks to check for and prevent node and npm version requirements that are more restrictive than your engines settings, but perhaps the engines settings should just align with your developer requirements.

package.json

  "engines": {
    "node": ">=16.13.2",
    "npm": ">=8.1.2"
  },

.npmrc

engine-strict=true

If your answer needs to strictly fit the post's title involving nvm ("use NVM to set NPM version") and this needs to be automated, you'd probably want to integrate a script that handles checking for nvm itself and the node version management into the earliest and/or most-often used developer workflow in your project. (npm install or npm run dev etc.)

Maintaining this sort of automation can be cumbersome especially if you need to manage cross-platform differences (nvm, nvm-windows, etc.) and the issue seems to bleed over into a larger developer environment concern possibly beyond an individual project's scope.

jmmygoggle
  • 921
  • 7
  • 18