Multiple Node.js versions with n
I had some big problems lately with Node.js and NPM.
It all boiled down to older projects needing older versions of Node.js and NPM to run. I seemingly had different versions of Node.js installed and was able to go upwards in Node versions but not downwards.
In order to have different Node.js versions I tried NVM but it never seemed to function properly. But n on the other hand was more stable.
I took the following steps:
- Uninstall Node.js to start over. See this post: https://stackoverflow.com/a/11178106. And also read the comments for other recommendations. Do this thoroughly to get out of problems.
- Install Homebrew as package manager. Check ‘brew’ to check if it’s already installed. See https://brew.sh/.
- Install Node.js with Homebrew: ‘brew install node’. This installs the latest version of Node.js.
- Install n to have different Node.js versions, see https://github.com/tj/n with the command ‘npm install -g n’.
- Install the correct version of your project. For example, install Node.js version 0.12.18 with ’n 0.12.18’. To switch version use ’n’. This shows all the available versions. Choose the version you want with arrows and Enter.
Important takeaway: permissions are key! Avoid using sudo. If you have to use sudo to install something, try changing the folder (and all the folders and files in that folder) afterwards to your own admin user. For example, in my case NPM (under /usr/local/lib/node_modules) was owned by sudo. I changed this to my admin user to get up and running.