Not Found - PUT https://npm.pkg.github.com/package-name

空扰寡人 提交于 2020-07-18 16:14:18

问题


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?


回答1:


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.




回答2:


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.




回答3:


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/" 
 },

This did work for me




回答4:


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


来源:https://stackoverflow.com/questions/57938784/not-found-put-https-npm-pkg-github-com-package-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!