Can npm be used AWS CodeArtifact for private packages, while all npm.org is used with public packages?

Viewed 2412

The docs read a bit like the developer is supposed to give over all "package registry duties" to AWS CodeArtifact. But I want to continue using npm.org for some packages.

Given a javascript app that uses private and public packages, can I setup npm/AWS so:

  • privately scoped packages (my code) are pulled from AWS CodeArtifact, and
  • publically scoped packages (e.g. lodash) are pulled npm.org?
2 Answers

I was also confused by this, but after much reading, aws codeartifact wants you to do a one to one mapping of public packages. What this means is if you want lodash from the public npm repository, you'll need to add it to your codeartifact repository. Luckily aws made this easy, there're 2 solutions to accomplish this

Solution 1:

Setup an upstream on your codeartifact repository to the npm registry.

in console

This will automatically create a codeartifact repository named "npm-store", when you npm install public packages (after logging in of course) from your package.json it'll add packages not already installed on "npm-store", then your codeartifact repository (the one you upstreamed npm-store to) will download it from there, which will then be used in your build. You can also setup another upstream to say pypi on the same repository. Will work the same way

Solution 2:

Use associate-external-connection - there can only be one external connection per codeartifact repository. What you do is create a codeartifact repository, lets named it "my-external-repo". Now I could be wrong, but I dont see any place in the UI to set this up, but if you're using the aws cli you can run a command similar to this

aws codeartifact associate-external-connection \ --domain "my-org" --domain-owner "account-id" \ --repository "my-external-repo" --external-connection public:npmjs

Then set "my-external-repo" as an upstream to your codeartifact repositories that'll use public npm packages. Now when you npm install (after logging in of course), public packages not on "my-external-repo" will be added, then the various repositories you upstreamed "my-external-repo" to will download the packages

There is way to explicitly set two npm registries, therefore achieving what you want. Instead of using aws codeartifact login --tool npm --repository my-repo --domain my-domain to login into aws you should use a more granular approach use the following commands:

# get endpoint 
endpoint = aws codeartifact get-repository-endpoint --domain my_domain --domain-owner 111122223333 --repository my_repo --format npm

# set a scoped registry
npm config set registry endpoint --scope=@my-package

# get token
token = aws codeartifact get-authorization-token --domain my_domain --domain-owner 111122223333 --repository my_repo

# set token
npm config set //my_domain-111122223333.d.codeartifact.region.amazonaws.com/npm/my_repo/:_authToken=token

# always truth
npm config set //my_domain-111122223333.d.codeartifact.region.amazonaws.com/npm/my_repo/:always-auth=true

These commands are a deconstruction of aws codeartifact login --tool npm --repository my-repo --domain my-domain (more info), with the difference that instead of setting a general registry at your .npmrc file (used to set configurations for your npm) will set a scoped registry (more info). In this way you will be able to have you fetch your packages from the sources you want.

Related