Create a Free Private NPM Repository on AWS With CodeArtifact

Damian Pieszczyński
The Startup
Published in
4 min readOct 11, 2020

--

*free tier is available

Just recently AWS has started offering their new developers and devops oriented service CodeArtifact — which basically allows anybody to create artifact repository for free or later pay as you go if you go over free allowance (cheapest in US regions)
[free tier: a generous 2GB-month file storage and 100k requests/month — actual for Oct. 2020]

I will quickly guide you trough configuring it for usage with NPM, but as a matter of fact you can use also with many others like: Gradle, Maven, npm, yarn, pnpm, twine and pip.

Prerequisites:

  1. configured AWS CLI (a must)
  2. NodeJS (for npm)

AWS Console

In AWS Console go to Developer Tools -> CodeArtifact or search for it, click Create Repository on the right:

Create Repository

Next fill in the only required Repository Name. There is no need to select Public upstream repository (and possibly pay for transfer ;) if you can get it free anyway). It only gives you a bit of comfort, that you don’t need to name your packages with “@scope” prefix. But for company packages you probably will want that @scope anyway ;) :

Create Domain

Next step would be to create a Domain
(a domain is just basically there to help you group repositories according to your org rules or other criteria).
I chose “This AWS Account”, put in my “domain name” (it will become a part of the Domain Url with aws account id) and as Customer Master Key I will go here with AWS managed key.

I have edited out my Account ID

On next screen there will be a summary of our choices and an option to edit them. Click Create Repository if you’re fine with the configuration.

Configure connection to artifact repository

Great now the repository has been created — I know that “View connection instructions” button that caught your eyes for sure ;) and unless you want other people to be able to access this repository — you can also ignore “Apply repository policy” button (as we will do for the sake of this quick guide)

In the Connection instructions choose npm:

Ignore the “Recommended setup” as in case of npm it will set global npm registry to your new private registry (unless you know what you’re doing and enabled the pass-trough in “Public upstream repositories”) you would not want that. Instead pick the “Manual setup”

Run the export command ( you should first run the command with aws cli to check if you get the token correctly [the one starting with: aws codeartifact get-….].
Super important: this token will be valid only 12 hours so after that time you need to run the export CODEARTIFACT_AUTH_TOKEN=`aws… again
(I can write a follow up article on how to go around this if needed just let me know in the comments)

Configure .npmrc

Next step would be to add the lines to our .npmrc file.
What we want to do here is to link the registry with the scope. So copy the contents from AWS and open the .npmrc file in text editor.

Paste the copied repository config just at the start add your scope in a format:

@scope:copied config from aws

So in our case here — as we got medium-perfect-repository as our fictional company name / domain

@medium-perfect-repository:registry=https://medium-perfect-repository-AWS_ACCOUNT_ID.d.codeartifact.us-east-2.amazonaws.com/npm/my-wonderful-repository/
My full .npmrc

How to check if everything is setup correctly?

O.K. It’s time to test the setup

run npm config get registryyou should get https://registry.npmjs.org/
and then run npm config get @medium-perfect-repository:registry and get the registry only for this scope which should be the one we just configured:

Wrap up and configure your package

and that’s basically it… now as a bonus just a quick hint how to configure you package.json to use that repository for publishing your packages — you need to add publishConfig property in your package.json file ie:

"publishConfig": { 
"registry": "https://medium-perfect-repository-YOUR_AWS_ID.d.codeartifact.us-east-2.amazonaws.com/npm/my-wonderful-repository/"
},

If you have any questions leave a comment, Thanks!
You can also ping me up on LinkedIn https://www.linkedin.com/in/damian-pieszczynski/

--

--