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!
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).
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.
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.
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:8081to expose the repository on the port 80
--restart alwaysto start automatically the container (also at hot reboot)
-v nexus-data:/nexus-dataand persist the data in the volume
Last, we will create a new DNS record that points to the droplet, eg
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,
@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
You must change it.
Create a private npm repository
Add a new
npm (hosted) repository. We have crated
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.
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
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:
(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.
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?
CEO @ Hakuna Cloud
10 years as CTO, former Software Engineer at Amazon AWS, Cloud Solution Architect with projects in US, Europe and United Arab Emirates.
"I am a DevOps and automation advocate; you can test, deploy, analyze and improve even you’re grandma recipes. "