简体   繁体   English

使用 npm 从私有 gitlab 仓库安装依赖项

[英]Install dependencies with npm from private gitlab repo

I'm trying to install a package that exists on our private repo.我正在尝试安装我们私人仓库中存在的 package。 The goal is to share the repo with a partner, but I need to make sure they can install it.目标是与合作伙伴共享 repo,但我需要确保他们可以安装它。 In theory, it should work, but none of the documented solutions are working for me从理论上讲,它应该有效,但没有一个记录的解决方案对我有用

I've tried adding the package to the package.json file我试过将 package 添加到 package.json 文件

"dependencies": {
    "package_name": "git+https://<deploy-token-name>:<deploy-token>@gitlab.domain.com/group/repo.git"
}

And then installing with npm but it seems to fail on the deploy token, but it's hard to say because the log isn't very helpful and then our whole gitlab deployment goes down for a moment #fun然后使用 npm 安装,但它似乎在部署令牌上失败,但很难说,因为日志不是很有帮助,然后我们整个 gitlab 部署暂时停止#fun

29 error
29 error undefined
29 error exited with error code: 128

I've also tried with a private access token我也试过使用私人访问令牌

"package-name": "https://oauth2:<access-token>@gitlab.domain.com/group/repo.git"

This results in an actual error that I can understand, except the error is saying there isn't a package.json in the repository, but there most certainly is这导致了一个我能理解的实际错误,除了错误是说存储库中没有 package.json,但肯定有

npm ERR! package.json Non-registry package missing package.json: package-name@https://oauth2:<access-token>@gitlab.domain.com/group/repo.git.
npm ERR! package.json npm can't find a package.json file in your current directory.

I've tried with ssh (with a ssh key setup that works for commits etc)我试过 ssh(使用 ssh 密钥设置,适用于提交等)

git+ssh://git@my-domain.com:my-project/my-repo#my-branch

And that results in an error这会导致错误

npm ERR! premature close

I've read through a dozen related issues and articles, but nothing is working for me我已经阅读了十几个相关的问题和文章,但对我没有任何帮助

You should use Scopes if you want to install private npm packages. 如果要安装私有npm软件包,则应使用Scopes。

You can define a scope in .npmrc file of your repository. 您可以在存储库的.npmrc文件中定义范围。

example .npmrc in your application repo: 您的应用程序仓库中的示例.npmrc:

@scopeName:registry=http://private-npm-registry/
always-auth=true

So npm can handle a dependency like: @scopeName/yourprivateModule@version 因此npm可以处理类似的依赖项:@ scopeName / yourprivateModule @ version

The token to access this repo should not be included in your Repository in should be configured in your home folders .npmrc . 访问此存储库的令牌不应包含在您的存储库中,而应在主文件夹.npmrc中进行配置。 Npm will first look in ther current folder for a .npmrc and later in your home folder. Npm将首先在当前文件夹中查找.npmrc,然后在您的主文件夹中查找。 The configured options will be merged. 配置的选项将被合并。

But this approach is only good for you if you publish your package to a private npm registry. 但是,仅当您将程序包发布到私有npm注册表时,此方法才对您有用。 https://docs.gitlab.com/ee/user/project/packages/npm_registry.html#authenticating-with-an-oauth-token https://docs.gitlab.com/ee/user/project/packages/npm_registry.html#authenticating-with-an-oauth-token

So to summarize this: 所以总结一下:

  • configure your scope in a .npmrc file of your repository 在存储库的.npmrc文件中配置范围
  • configure access token in .npmrc file of your environment user 在环境用户的.npmrc文件中配置访问令牌
  • add scoped dependency to your package.json 将范围依赖添加到package.json

    eg 例如

npm i @scopeName/yourprivateModule@version

I've got the same issue and I managed to fix it. 我遇到了同样的问题,并且设法解决了。 The gitlab repository that I wanted to install didn't have a package.json file. 我要安装的gitlab存储库没有package.json文件。

Check dependency package.json检查依赖 package.json

On npm install, the error npm ERR! premature close关于npm安装,报错npm ERR! premature close npm ERR! premature close seems common if there's any problems with depency package.json's...如果依赖 package.json 有任何问题, npm ERR! premature close似乎很常见......

  • Does a package.json exist? package.json 是否存在?
  • Is everything inside absolutely correct?里面的一切都绝对正确吗?
  • Everything ok with dependencies dependencies?依赖关系一切正常吗?

In my case, I had an illegal version number ("1" instead of "1.0.0").在我的例子中,我有一个非法的版本号(“1”而不是“1.0.0”)。 This was not an issue on my local environment, but only when deploying to remote production.这在我的本地环境中不是问题,但只有在部署到远程生产环境时才会出现。 Fixing this cleared the error right away.解决此问题可立即清除错误。

For reference this is my dependency line:作为参考,这是我的依赖行:

"myCustomRepo" : "git+https://MyGithubUsername:MyGithubPublicAccessToken@github.com/MyGithubUsername/myCustomRepo"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM