I am trying to use npm publish
to publish a javascript library to a private NPM repository hosted on AWS Codeartifact.
When I try running npm publish
on my local machine, it works without any issues. However, when I try to run it on Jenkins, it looks like the package is trying to be published to NPM's package repository instead of AWS codeartifact and I get the following error:
> @company/[email protected] prepare .
> npm run co:login
> @company/[email protected] co:login /usr/src/components
> aws codeartifact login --tool npm --domain <DOMAIN> --repository <REPOSITORY>
Successfully configured npm to use AWS CodeArtifact repository <AWS_CODEARTIFACT_URL>
Login expires in 12 hours at 2020-12-08 11:15:37+00:00
npm notice
npm notice package: @company/[email protected]
npm notice === Tarball Contents ===
npm notice 2.9kB package.json
npm notice 1.6kB README.md
npm notice 268B dist/index.d.ts
npm notice === Tarball Details ===
npm notice name: @company/package
npm notice version: 0.2.2
npm notice package size: 2.1 kB
npm notice unpacked size: 4.8 kB
npm notice shasum: <SHASUM>
npm notice integrity: <INTEGRITY>
npm notice total files: 3
npm notice
npm ERR! code E404
npm ERR! 404 Not Found - PUT https://registry.npmjs.org/@company%2fpackage - Not found
npm ERR! 404
npm ERR! 404 '@company/[email protected]' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-12-08T00_20_19_949Z-debug.log
If I understand the AWS codeartifact documentation, it should set the right settings in .npmrc
when we run aws codeartifact login
command. I still don't understand why it is trying to push to the npm registry instead of AWS.
Here is my package.json:
{
"name": "@company/package",
"version": <VERSION>,
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"dependencies": {...},
"scripts": {
"co:login": "aws codeartifact login --tool npm --domain <DOMAIN> --repository <REPOSITORY>",
"prepare": "npm run co:login"
},
"eslintConfig": {...},
"browserslist": {...},
"peerDependencies": {...},
"devDependencies": {...}
}
Update
I added the link to my CodeArtificat using
npm set registry <LINK_TO_CODE_ARTIFACT>
When I execute the publish script locally, I get this error on the script's first run:
Unable to authenticate, need: Bearer realm="company/package", Basic realm="company/package"
That error disappears on subsequent runs. However, when I try the same on my Jenkins pipeline, I keep getting this error. Not sure what this means. I saw a thread related to this error on Azure, but am unable to find anything on AWS CodeArtifact.
The problem was that I was using two different package managers,
yarn
andnpm
to do several tasks in this project. The code was built usingyarn
but the publish was happening overnpm
. In order to be uniform, i.e. useyarn
across all our tasks, I triedyarn publish
and it magically worked.Moral of the story: Be consistent with your package managers.