How to publish to npm using scripts in package.json?

Viewed 1163

In my project, I have a requirement where I need to publish a package to 2 registries.

Currently, whenever I need to publish, I do the following steps.

  1. npm config set registry https://registry.npmjs.org/ and then npm publish.
  2. npm config set registry http://npm-registry.myorg.com and then npm publish

This becomes a tiring process for me as there are a lot of new changes that I publish. So I thought of reducing some work by creating a script in package.json which can do this automatically.

This is what I added.

{
 scripts: {
  "deploy:private": "npm config set registry http://npm-registry.myorg.com && npm publish",
  "deploy:public": "npm config set registry https://registry.npmjs.org/ && npm publish"
  }
}

So when I run yarn deploy:private, it gives below to me

npm ERR! code ENEEDAUTH
npm ERR! need auth auth required for publishing
npm ERR! need auth You need to authorize this machine using `npm adduser`

How can I resolve this, please help?

----------UPDATE-----------

After googling I have found this https://docs.npmjs.com/using-private-packages-in-a-ci-cd-workflow

Based on this I have set my token as env variable and in the project I have created a local .npmrc, with below contents

.npmrc file

//registry.npmjs.org/:_authToken=${NPM_TOKEN}
//npm-registry.tokopedia.com/:_authToken=${NPM_TOKEN_PRIVATE}

But still, I get the error when I try to run the above commands.

0 Answers
Related