Blog

I write on blua.blue, dev.to and groups.hyvor.com

How to install global npm packages without sudo on Ubuntu

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

What is composer?

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

image

MySQL: ERROR1364 fix

The painful realization of why people use containers.

image

Cyber wars: Defending your server

Maintaining your own server can be a thrill. High security standards can protect you from data leakage, injection attacks and DDoS attempts. But what about adaptive brute force?

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.

image

MySQL in PHP - how to deal with databases

How I handle MYSQL database transactions in PHP

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.

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.

image

Static content pages - still the fastest web-experience

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

image

blua.blue PHP SDK

Create your own blog.

image

dev.to plugin for headless CMS blua.blue (part 2)

A solution to supplying plugins to blua.blue

image

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.

The Uselessness of Pre-Assessment

After almost two decades in the industry, new jobs will still ask you for "assessment tests". A little rant...

image

SEO strategies for blua.blue

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

image

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

image

Help us document neoan3

Over 4000 brave developers are exploring the framework on their own.

image

When politics kill innovation

How misunderstood diversity killed the PHP Central Europe Conference for good.