包无法发布到npm(不在npm注册表中)

41

我想将我的git仓库发布到npm,以便在其他项目中使用。但是当我运行npm publish命令时,我会收到以下错误:

npm ERR! code E404
npm ERR! 404 Not Found - PUT https://npm.pkg.github.com/vue-toggle-component
npm ERR! 404
npm ERR! 404  'vue-toggle-component@0.1.0' 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!     C:\Users\niels\AppData\Roaming\npm-cache\_logs\2020-10-29T10_47_26_952Z-debug.log

尝试修复错误时,我尝试使用npm adduser命令和npm login命令以确保已登录。这两者都未解决我的问题,因为看起来我已经登录了。 我的package.json文件:
{
  "name": "vue-toggle-component",
  "version": "0.1.0",
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "core-js": "^3.6.5",
    "vue": "^2.6.11"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "babel-eslint": "^10.1.0",
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^6.2.2",
    "vue-template-compiler": "^2.6.11"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "parserOptions": {
      "parser": "babel-eslint"
    },
    "rules": {}
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead"
  ],
  "publishConfig": {
    "registry": "https://npm.pkg.github.com/"
  },
  "description": "## Project setup ``` yarn install ```",
  "main": "babel.config.js",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/nehlis/vue-toggle-component.git"
  },
  "keywords": [
    "Vue.js",
    "Toggle",
    "component",
    "Lightweight",
    "Checkbox"
  ],
  "author": "Niels Bosman",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/nehlis/vue-toggle-component/issues"
  },
  "homepage": "https://github.com/nehlis/vue-toggle-component#readme"
}

有人知道如何修复这个问题吗?


你确切使用了哪个命令?是 npm publish <folder> 吗? - Hollyol
@Hollyol 我刚刚使用cd命令进入了目录并运行了 npm publish。除此之外没有其他操作。 - Niels Bosman
我分享了一个NPM发布教程,还有这里的一些答案,但是我无法再帮助你了 :/ - Hollyol
我认为在我的情况下,这是因为我没有权限在给定的@namespace中发布新包。 - user3064538
8个回答

38

尝试使用npm login。在使用npm publish时,有时会显示误导性的消息。


1
尽管这不是被接受的答案,但这是我需要的答案。别忘了通过CLI登录!谢谢! - Calvin Ellis

13
根据错误输出中出现的https://npm.pkg.github.com/,您试图发布到GitHub Packages而不是npm registry(虽然GitHub现在拥有npm,但它们仍然是分开的)。根据GitHub packages文档

GitHub Packages仅支持作用域npm包。作用域包的名称格式为@owner/name。作用域包始终以@符号开头。您可能需要更新package.json中的名称以使用作用域名称。例如,“name”:“@codertocat/hello-world-npm”。

因此,您需要将配置更改为指向npm注册表而不是GitHub packages注册表,或者将package.json中的name字段更改为作用域包名称。

1
谢谢!我已经完成了这个任务,并添加了一个包含 registry=https://npm.pkg.github.com/@nehlis 的 .npmrc 文件。 - Niels Bosman

8

我有相同的问题,这个问题来自于已发布的包无法访问,因此您需要添加.npmrc以连接您的私有存储库,使其如下所示。

registry=https://npm.pkg.github.com/yourcompany

或者
@yourcompany:registry=https://npm.pkg.github.com

5

正如另一位用户提到的那样,您需要将自己添加到公司或组织的私有仓库中。但如果您没有设置 .npmrc 文件,这个命令不仅会自动创建该文件,还会更新您的注册表:

npm config set registry https://registry.your-company-registry.npme.io/

然而,最稳健的方法是简单地执行

npm login

1

这都与身份验证有关。

似乎使用用户名/密码验证已经不能在npm publish中使用了。

您需要生成访问令牌enter image description here

然后使用它来发布。

NPM_TOKEN=blahblahblahblaha npm publish

1

我曾经遇到过同样的问题,使用了几乎所有我认为有效的解决方案。如果你将其发布在GitHub上,那么这可能是有帮助的。

  • 将npm更新到最新版本
  • 使用以下命令设置注册表 npm config set registry https://registry.your-company-registry.npme.io/
  • 重置token

但它仍然没有起作用。

如果您的GitHub用户名中有大写字母,并且您正在使用GitHub在包部分下提供的命令

请确保在本地运行该命令时更新您的用户名。 默认情况下,GitHub不会在包部分的命令中显示大写字母。

因此,如果您的用户名是UserName123,包名是my-first-package, GitHub将显示以下命令:

npm install @username123/my-first-package@1.0.0

请注意,在安装软件包时用户名区分大小写, 因此,请将您的命令更新为

npm install @Username123/my-first-package@1.0.0

希望这样就可以正常工作了。


0
在 .npmrc 文件中添加以下代码。
always-auth = true
@your_company:registry=https://npm.artifactory.your_company.com/artifactory/api/npm/npm/
registry=https://npm.artifactory.your_company.com/artifactory/api/npm/npm/

在GIT bash中执行命令。
npm config set registry https://registry.npmjs.org/
npm install

0
正确发布的步骤:
1. 设置您想要发布的存储库,您可以更改为您自己的注册服务器。
npm config set registry https://registry.npmjs.org/

2.- 登录(需要先在npm或您的自定义注册表中注册)
npm login

3. - 去你的包裹并设定你的范围
cd /your/package/path
npm init --scope=@my-org
#or
npm init --scope=@my-username

4. 最后发布您的软件包。
npm publish --access public

参考: [https://docs.npmjs.com/creating-and-publishing-scoped-public-packages](https://docs.npmjs.com/creating-and-publishing-scoped-public-packages)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接