Skip to content

Instantly share code, notes, and snippets.

@HuakunShen
Last active November 10, 2022 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HuakunShen/de921120e19040696649e3e577e77212 to your computer and use it in GitHub Desktop.
Save HuakunShen/de921120e19040696649e3e577e77212 to your computer and use it in GitHub Desktop.
How to publish npm package to different registry

Install Package

If you want to install package from some registry, set the registry first

# choose one of the following if you don't have a .npmrc file
npm config set @crosscopy:registry https://registry.npmjs.org
npm config set @crosscopy:registry https://npm.pkg.github.com

Publish Package

# publish to npmjs registry
npm config set @crosscopy:registry https://registry.npmjs.org
npm publish --access public

# publish to github registry
npm config set @crosscopy:registry https://npm.pkg.github.com
npm publish

.npmrc

Create a .npmrc file also works, but then it seems to have the highest priority, doesn't work if you want to publish to multiple registry.

.npmrc should have something like the following the set the registry for a specific scope.

@crosscopy:registry=https://npm.pkg.github.com

package.json scripts

You can add the following scripts in package.json's scripts

"install:github": "npm config set @crosscopy:registry https://npm.pkg.github.com && npm i",
"publish:github": "npm config set @crosscopy:registry https://npm.pkg.github.com && npm publish",
"publish:npmjs": "npm config set @crosscopy:registry https://registry.npmjs.org && npm publish"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment