5

I run this commande in a package.json file (scripts > preinstall) or (scripts > prepare):

aws codeartifact login --tool npm --repository my-repo --domain my-domain --domain-owner <123456789> --profile <me>

full file: (<123456789> and <me> is modified for stackoverflow)

{
  "name": "my-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "preinstall": "npm run co:login",
    "co:login": "aws codeartifact login --tool npm --repository my-repo --domain my-domain --domain-owner <123456789> --profile <me>",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "uuid": "^3.3.2",
    "@my-npm/my-common": "1.0.0"
  }
}

My .npmrc file before aws codeartifact login .. commande:

registry=https://registry.npmjs.org
@my-npm:registry=https://my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/-repo/

My .npmrc file is modified:

registry=https://my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/-repo/
@my-npm:registry=https://my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/-repo/
//my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/my-repo/:always-auth=true
//my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/my-repo/:_authToken=eyJ2ZXIiOjEsIml....

but I need keep this:

registry=https://registry.npmjs.org
@my-npm:registry=https://my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/-repo/
//my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/my-repo/:always-auth=true
//my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/my-repo/:_authToken=eyJ2ZXIiOjEsIml....
Stéphane GRILLON
  • 11,140
  • 10
  • 85
  • 154

2 Answers2

12

add --namespace @my-npm at the end of commande line

This parameter added change only the scope @my-npm:registry=... in .npmrc file.

Stéphane GRILLON
  • 11,140
  • 10
  • 85
  • 154
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/28467371) – Marek Urbanowicz Mar 05 '21 at 12:38
  • I would like to see that explanation in the answer. We need to provide complete answers with explanations so people can understand instead of dummy copy paste – Marek Urbanowicz Mar 06 '21 at 08:54
  • @MarekUrbanowicz, yes, that answers the question. if you add --namespace `@my-npm` at the end of command line, the default registry is not affected. – Stéphane GRILLON May 27 '21 at 09:00
2

A solution I've been using for this matter is instead of using aws codeartifact login --tool npm --repository my-repo --domain my-domain to login into aws I use a more granular approach using 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. Applying this to the package.json you can insert those commands the value of co:login or even isolate the commands in a script and call these script as the value.

sebassebas1313
  • 363
  • 2
  • 8