Debugging issues with NPM and updating Node Versions with NVM
A common problem with NPM is setting up your project with a Node version that is lower than the version used by recent packages in NPM.
Trying to do an install in a project will at most times bring about errors complaining about the versions.
This can bring big issues especially when you try to build on or make an update to a previous project that you happened to use a different version of Node to install the packages.
A quick fix to this problem usually is to install the packages forcefully to your project and this will at most times be the solution.
npm install --force
This will forcefully install packages that are compatible or cloase to being compatible to the current version of Node in your system.
Using Node Version Manager(NVM)
This is a version manager that is made specifically made for managing and installing specific versions of Node.
It is a project that is actively maintained and you can get it from Github and set it up depending on your operating system.
I will show the set up guide for Linux and MacOS. To get a guide on how to set it up on Windows, you can follow the guide here. NVM
To set it up on Linux or Mac, do the following;
This works if you have curl installed.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/<version>/install.sh | bash
If you have wget installed, you can also use it.
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/<version>/install.sh | bash
After installing it, you should restart your terminal for it to read in and reflect.
Open up your terminal and you should be able to access the nvm command.
You can now choose the version that you want or rather install the current Long Term Support Version which is usually recommended.
To install a specific version,
nvm install v<version>
To install the Long Term Support Version,
nvm install --lts
This should now work and you have node together with NPM installed.
However, if you have installed two or more versions of Node with NVM, you will need to specify the version that you want to use.
To do this,
nvm user v<version>
Now you are good to go with solving NPM issues using NVM.
Another common and much much better way to solve NPM issues is to always build and package your project with Docker.
By doing so, Docker will create and image and store your packages with the version of Node that you specified.
As long as your project remains packaged with Docker, there will be no issues with your packages since Docker will install the packages correctly using the Node version that you specified.
The beauty of Docker also comes in where you can run the same project on different environments or Operating Systems and Docker will reproduce everything with no issues at all.
You will usually specify this inside a Docker file in every project that you make like this one below.
COPY package*.json .
RUN npm install
COPY . .
CMD ['npm', 'start']
This will set up everything correctly and the environments can now be reproduced.
With the following set ups, you should no longer have issues with NPM again.
GitHub - nvm-sh/nvm: Node Version Manager - POSIX-compliant bash script to manage multiple active…
nvm allows you to quickly install and use different versions of node via the command line. Example: $ nvm use 16 Now…