There are many ways to install Node.js, from operating system package managers to single version installers; you can even download and build from source! Unfortunately, the vast majority of installations can put Node.js in a location that requires the need for elevated permissions when using either the
npm commands on the command line or via tools and scripts.
Requiring the need for sudo on Unix-like operating systems when using Node.js at the command line is not only laborious but also likely to lead to all sorts of strange issues for certain globally installed Node.js modules.
Today we will look at how to clean up and reinstall Node.js so that there is no need to use elevated permissions; as a bonus, we will also gain the ability to jump between Node.js versions with ease.
We will be using a Node.js version manager, NVM, to enable sudo free usage of any
npm based commands. Building from source sure is a fun way to spend an afternoon and we highly encourage it! but for everyday use, a good Node.js version manager is the way to go.
sudo free is not technically true. Some global modules may require sudo if they are installing or working with directories that are only available with root permission. Still, nearly always is better than never!
You can tell if you need elevated permissions to run Node.js based on whether or not you need to append commands with
sudo, which stands for Super-User DO. Any command run by prefixing it with sudo will be run with elevated permissions. It is important to note, if you are a Windows user, there is no need for anything more than installing node from official sources; it should work as is, out of the box.
If you do not require the use of sudo due to changing file permissions, you should reset your permissions first and then follow this guide. Messing with permissions is a sure-fire way to lead to all sorts of awful problems later on. On OSX, permissions can be reset by following this guide. On Linux there is no easy way to reset permissions. In general, a reinstall is the best route to go here.
If you have any global Node.js modules installed via NPM, now would be a good time to delete them. We will most likely be changing the destination of the global modules folder and will have to reinstall them anyway.
We first need to check if Node.js is already installed on your system. This can be done by running the command below at the command line:
which node which npm
If there is no output, great! If there is, don’t panic! We are going to look at how to completely remove each one in turn. Be sure to make a note of the output of each command, we will be using these shortly.
Remove existing Node.js installs
The steps below for removing Node.js involve the use of the
rm -rf command which can be read as ReMove -Recursively -Force. There is no undo for this command and you will not be warned before deletion. Be extremely careful of the directory name you enter. Check each directory name you enter, twice, before hitting return.
There are many ways to remove Node.js from your system, the exact details will depend upon how it was installed. If you have used an operating system package manager, consult the respective documentation for details on how to remove Node.js; most have a simple installer to handle it.
If Node.js has been installed manually from the site, or from source, you will need to remove it manually. Luckily we have the instruction on how to do that just below.
First up, we need to remove Node.js by using
rm -rf on each of the directories listed in the which command.
sudo rm -rf /directory/of/node/npm
As well as the above, there are some other odds and ends that hang around. Each command will need to be ran in turn.
sudo rm -f /usr/local/share/man/man1/node.1 sudo rm -f /usr/local/lib/dtrace/node.d sudo rm -rf ~/.npm sudo rm -rf ~/.node-gyp
Before going any further, run each
which command again. If any directories still appear, go ahead and
rm -rf each of those directories too.
Install Node.js via NVM
Introduced above, NVM is a Node.js/NVM version manager that works with most Unix-like OSes, including OSX. To install NVM used to be a bit of a manual effort, thankfully there is now a script that does the hard work for you. The get the latest script, click the link below,
Note that this script works for both Bash and ZSH, and will pick the correct profile automagically! NVM allows you to install many versions of Node.js. To install the latest stable version of Node.js simply run:
nvm install stable
Next set stable as the default version of Node.js for any new console window created:
nvm alias default stable
You can test this by creating a new console window and typing
node --version. The version should match the stable version of Node.js.
NVM quick look
We now have sudo free use of the
npm commands for the stable version of Node.js and NPM; but what if we want to work with other versions or even IO.js?
To list all versions available for install, including IO.js:
To install a particular version:
nvm install 10.9.0
To list installed versions:
To use an installed version for the current console session:
nvm use 10.9.0
To set an installed version as the default for all sessions:
nvm alias default 10.9.0
When switching versions of Node.js it is important to run the
npm build command directly after. This rebuilds any native addons for the target version of Node.js amongst other things.
By removing old Node.js installations and installing NVM we are now able to use both
npm commands without the need for elevated permissions in the vast majority of cases. We also have the added benefit of using NVM to manage different versions of Node.js using our quick look section above. NVM installs all versions of Node.js the same way; so regardless of the particular version you are using it will always work, sudo free!
At NearForm, we help businesses implement and evolve modern application development technologies, processes and tools that support greater agility and speed to market. As one of the leading contributors to the Node.js community, we have many years of experience in adopting Node.js in enterprise environments and improving the business outcomes for our global clients.
For more information about our consulting approach contact us or email us at firstname.lastname@example.org