How to install global npm packages without sudo on Ubuntu

Running npm on a server can be painful. Privileges are there for a reason, and so is sudo. Running npm with sudo is not the solution.

Avoiding sudo

It should be self-evident that providing your scripts sudo permissions is a bad idea. From concerns about third-party packages, missing security layers in your code to access issues and inconveniences - it would be too much to talk about the possible dangers. Let’s just agree that running via sudo is not an option.

One of my most-used utilities is neoan3-cli. Even if I am not using neoan3, the migration and credential handling is always welcome.

AWS Ubuntu instance

One of the most versatile hosting solutions is still Amazon’s EC2 instances. While Amazon’s Linux version seems to be the logical step, I am simply feeling more at ease with Ubuntu and am frankly a little skeptical about a distro that seems so coupled with a hosting solution (As far as I understand, the distro started out as a customization of CentOS and then branched into its own beast). Ubuntu is the perfect solution as everything we are going to talk about will be independent from your hosting solution. We are using version 18.04 but there shouldn’t be any differences to prior versions in our scenario.

NOTE: On AWS your username will likely be “ubuntu” in our scenario.

The directory of global npm packages

The first thing we want to do is creating a directory within our user’s home. On Ubuntu, this should be /home/USERNAME/ ( or ~/). We will want to create a directory for our global packages:

mkdir ~/.npm-packages

Next, let’s set up npm to use this directory:

npm config set prefix "${HOME}/.npm-packages"

PATH variable

In order for node & npm to find installed packages, we should add this directory to the PATH variable. On a server you might want to do this via nano ~/.bashrc.

Add the following lines:

NPM_STORE="${HOME}/.npm-packages"

export PATH="$PATH:$NPM_STORE/bin"

# Make sure this line is the last in the config (in case you defined MANPATH already)
# Otherwise, fall back to `manpath` so we can inherit from `/etc/manpath`.
export MANPATH="${MANPATH-$(manpath)}:$NPM_STORE/share/man"

Let’s try it out

Assuming everything went as planned, we should now be able to use global packages without the root user or sudo. Start a new terminal window (or restart your ssh connection) and try: npm i -g neoan3-cli. After successful installation, the command neoan3 -v should output the installed version.

Final thoughts

There is a popular script out there automating this process here. Please be aware that this solution does not play well with node version manager. Otherwise it is a solid solution with many happy users.

Image

neoan 01/28/2020
As some have pointed out on various platforms: You might need to apply the changes. To do so: . ~/.bashrc (or . ~/.profile if the file exists)

neoan @neoan

611 readers
Keywords
npm ubuntu aws
On topic

Install PHP 7.4 on Ubuntu

Finally PHP 7.4 is out! You have read about the new features, you have followed externals, you have gathered ideas on how new capabilities will save time. Let's get it running.

Static content pages - still the fastest web-experience

Tutorial: How to utilize blua.blue to generate static content for your website.

Scaffolding REST APIs with JWT authentication

Ever had the need for your own backend while developing your web-app?

Git: globally change GitHub-remotes from git@ to https

Does your IDE or composer set remote repositories to ssh rather than https? Or are repositories you are using set up that way? You are not alone. Let's fix it once and for all!

VueJS & neoan3: a love story.

Setting up neoan3 to play nice with Vue isn't hard. Here is how the two frameworks are combined to support fast, dynamic and rapid development.

SEO strategies for blua.blue

How to get your content listed where you want it to.

MySQL: ERROR1364 fix

The painful realization of why people use containers.

blua.blue PHP SDK

Create your own blog.

How to Build an Express App in Node That Reads From a Headless CMS

A headless CMS lets you design your own front-end, sometimes your own back-end. Let's set up a small application to get us started. Beginner friendly. Approx. 20 min. to reproduce / follow along

MySQL in PHP - how to deal with databases

How I handle MYSQL database transactions in PHP

Cross publishing to dev.to

How to publish your content to dev.to from blua.blue - hopefully

Transformer - falling in love with PHP's magic methods all over again

PHP's magic functions have been around for a long time. But there were always good reasons to avoid them. Transformer is a practical and reliable way to make use of the most common operations we program: CRUD operations.

What is composer?

Composer has become PHP's package & dependency manager. Why you should use it.