npmjs is the most used host for public and private node packages, but - you know - it’s a little bit expensive. A small team would easily pay 670$/year. And it is unreliable, at least 1 outage every 2 weeks, that will basically stop your entire dev process!

If you want to have an unlimited, reliable and cost-effective npm repo, then you’ll probably benefit from this post, as we’ll be using Hakuna for cutting the cloud hosting costs up to 95%, enabling the repo for just 1-2 dollars a month. Pretty cool!


Installation

We are going to create a cloud server on DigitalOcean, install and configure Nexus3 as npm repo, and we will stop the server when it won’t be used to save a lot of money (eg during nights and weekends) but keeping the registry always available (oncall emergencies).

Digitalocean Droplet

Our droplet runs RancherOS, a simplified and lightweight Linux distribution built from containers, for containers. It’s a light operating system with a lower startup time.

Droplet configuration with Ubuntu 16.04.4
Droplet operating system

We will choose the 4gb/2vCPU as droplet size - even more!. Don’t worry about the cost and free your cloud!

With Hakuna Cloud ModProxy we can keep it lower than 5$/month.

Droplet configuration with Ubuntu 16.04.4
Droplet size

Choose the region nearest to you and launch the droplet.

Configuring Nexus as a npm repo

SSH in the droplet: ssh rancher@<droplet_ip> (the user rancher is built-in) and deploy Sonatype Nexus 3 in 2 lines

docker volume create --driver local --name nexus-data
docker run -d -p 80:8081 --name nexus --restart always -v nexus-data:/nexus-data sonatype/nexus3

The first command create a Docker volume, to persists packages and configurations. The second command runs the Sonatype Nexus 3 image:

  • -p 80:8081 to expose the repository on the port 80
  • --restart always to start automatically the container (also at hot reboot)
  • -v nexus-data:/nexus-data and persist the data in the volume

Last, we will create a new DNS record that points to the droplet, eg npm.hakuna.dev.

Configuring Nexus3 as npm registry

We will use our Nexus to host only our private packages. It’s possible to use our repo also as a proxy for the public packages hosted in npmjs.

We will have 2 scopes, @my-ideas and @hk, each one in a different repo so we can fine tune the authorization/authentication.

Firt of all, open a browser and surf to the repository: http://<droplet_ip>. The default username is admin and the password is admin123/ You must change it.

Create a private npm repository

Add a new npm (hosted) repository. We have crated my-ideas and Hakuna Cloud. For both of them, we choosed to not allow the redeploy policy.

Enable bearer token authentication & add users

Enable the npm Bearer token realm.

npm bearer token

We have disabled the Anonymous access (by default, anonymous users have read access). Create your users and grant them access to your repos.

Create a role to grant permissions for the repo. nx-repositoy-view-npm-<your_repo>-* will grant read/write access only to <your_repo>.

Last, add all the users you need :)

Save up to 85% of cost with Hakuna Cloud

Our droplet costs $240/year, or $0.030/hr by running 24 hour/day and 7 days/week. Do we really need it to run on weekends? At 2am? Maybe it may happen that we need to run npm install in the middle of the night… But how many times per day do our devs install packages?

Hakuna Cloud has a tool that can help us, ModProxy. ModProxy is an http/https proxy that can stop a cloud server, as our droplet, if it doesn’t receive requests for it in 30 minutes and it can start it as soon as a new request is received. By stopping the instance when it is not required, the costs will just drop.

As a reference, last month (February 2019) we paid our droplet for 31 hours over 672 hours in a month. Yes, we used Nexus only for the 4% of a month, so we saved 96% on the monthly price.

The configuraiton is very easy.

  • Install the cli and signup/login;
  • Create an auth token in DigitalOcean and register a new provider with hakuna provider create;
  • Register our droplet in Hakuna: hakuna vhost create.

The last step is to point our DNS record to ModProxy, which is enabled and configured by default for any VHost. Just update our dns like this:

npm.hakuna.dev CNAME 

(US users can use the load balancer in the US, )

That’s all! The droplet will be shut down (aka will cost us $0) for more than 95% of a month. When we need a package hosted on it, we do what we usually do: npm install @hk/package and the droplet will be started.

Client setup

Now, on our dev machine, we need to configure npm to use our private repo for the scoped packages - only for these package. For everyhitng else, the public repo is great! For each @scope/repo, run the following command to route npm to the private repo:

npm config set @hk:registry http://npm.hakuna.dev/repository/porketta/
npm config set @my-ideas:registry http://npm.hakuna.dev/repository/my-ideas/

And then, log in

npm login --registry=http://npm.hakuna.dev/repository/hk/
npm login --registry=http://npm.hakuna.dev/repository/my-ideas/

So, do you want to save your money and pay only for your real cloud hosting needs?