26

I'm trying to upload a package on GPR (Github Package registry). I log in successfully:

npm login --registry=https://npm.pkg.github.com

and then run these command:

npm set registry https://npm.pkg.github.com/

npm publish

which returns this error:

npm ERR! 404 Not Found - PUT https://npm.pkg.github.com/package-name
npm ERR! 404 
npm ERR! 404  'package-name@version' is not in the npm registry.

Seems it tries to upload a package on npm registry instead of github package registry. How should I fix this problem?

kaxi1993
  • 4,535
  • 4
  • 29
  • 47
  • 1
    What does your `package.json` look like? Are you specifying the `publishConfig.registry` there? Remarkably, specifying the registry on the command line does not override that. – Edward Thomson Sep 16 '19 at 09:36
  • No, I don't specify `publishConfig` field in package.json at all – kaxi1993 Sep 16 '19 at 11:41

5 Answers5

32

There are two ways to solve this problem:

  1. Specify publishConfig option in package.json:
"publishConfig": {
    "registry":"https://npm.pkg.github.com/@OWNER"
},
  1. Add .npmrc file to your project with this content:
registry=https://npm.pkg.github.com/@OWNER

replacing OWNER with the name of the user or organization account on GitHub that owns the repository where you will publish the package.

kaxi1993
  • 4,535
  • 4
  • 29
  • 47
19

For example:

{
  "name": "@elvisjs/calling-elvis",
  "repository": {
    "type": "git",
    "url": "https://github.com/elvisjs/calling-elvis"
  },
  "publishConfig": {
    "registry": "https://npm.pkg.github.com/elvisjs"
  }
}

The name, repository/url and the publishConfig/registry must be matched.

clearloop
  • 671
  • 5
  • 15
  • 1
    Thanks a lot I tried to understand why I can't publish for hours! Soooo weird, why I need to have a repository block for publishing??? And even don't get a nice error message and no clear description in docs for that. It is just crazy... – Max Jul 11 '20 at 21:30
  • Your solution works for me too. But i need to add something, dont mix hyphen and under score like me :) – Atakan Savaş Mar 12 '21 at 21:58
13

Github Package registry expected the name properties on package.json to be "@{github username}/{package name} for example:-

"name": "@pravanjan/local-time",
"publishConfig": { 
     "registry": "https://npm.pkg.github.com/" 
 },

If "publishConfig" is not set in package.json we can directly set the registry parameter in terminal

npm publish --registry=https://npm.pkg.github.com/ 

This did work for me

Pravanjan
  • 698
  • 7
  • 16
2

As hinted by other answers here, the root cause of the above error is that GPR (unlike https://www.npmjs.com/) requires that packages have a scope.

However, it seems that all other suggested solutions (updating package.json and etc.) would not allow to keep publishing the package to https://www.npmjs.com/ without a scope. Here's my solution that allows both:

Assuming that:

  1. package.json contains simple package name without scope as often is the case when publishing public packages to https://www.npmjs.com/
  2. GitHub Workflow is configured using GitHub's Node.js Package template

Add additional step to publish-gpr job before the run: npm ci default step in order to dynamically insert current repository's owner into package name in package.json:

- name: Insert repository owner as scope into package name
  run: |
    node <<EOF
    const fs = require('fs').promises;
    fs.readFile('package.json', 'utf8').then((data) => JSON.parse(data)).then((json) => {
        json.name = '@$(echo "$GITHUB_REPOSITORY" | sed 's/\/.\+//')/' + json.name;
        console.info('Package name changed to %s', json.name);
        return fs.writeFile('package.json', JSON.stringify(json), 'utf8');
    }).catch(error => {
        console.error(error);
        process.exit(1);
    });
    EOF
Andrei LED
  • 2,560
  • 17
  • 21
0

Option 1) To publish into github package registry (https://npm.pkg.github.com/) through github workflow, 3 files have to configured correctly.

Step 1: From package root repository, create the file in ./github/workflows/release-package.yml Ensure the registry-url is https://npm.pkg.github.com/. Refer to https://github.com/vernGlobe/json-light-query/blob/main/.github/workflows/release-package.yml

Step 2: Configure in "package.json":

"name": "@<username>/json-light-query",
"publishConfig": {
   "@<username>:registry":"https://npm.pkg.github.com/"
 }

Step 3: Add into the file ".npmrc":

@<username>:registry=https://npm.pkg.github.com

Note: replace <username> with your GitHub account username.

Option 2: Publish into npm package registry (https://registry.npmjs.org/). Only package.json file need to configure correctly.

"name": "json-light-query",
"publishConfig": {
    "@<username>:registry": "https://registry.npmjs.org/"
}

Note: replace <username> with your npmjs account username.

Download/Search the package from https://www.npmjs.com/

crystal
  • 185
  • 1
  • 4