我试图通过npm安装github私有存储库,包括其他私有github存储库作为依赖。

我尝试了很多方法和帖子,但没有一个是有效的。这是我正在做的:

npm install git+https://github.com/myusername/mygitrepository.git

在包中。Json是这样的:

"dependencies": {
    "repository1name": "git+https://github.com/myusername/repository1.git",
    "repository2name": "git+https://github.com/myusername/repository2.git"
}

正确的做法是什么?


当前回答

还有SSH密钥-仍然要求密码和密码短语

使用ssh-add ~/。Ssh /id_rsa没有本地keychain。

这样就避免了与令牌纠缠不清。

其他回答

对于那些来这里获取公共目录的人,请访问npm文档:https://docs.npmjs.com/files/package.json#git-urls-as-dependencies

Git url作为依赖项

Git url可以是这样的:

git://github.com/user/project.git#commit-ish
git+ssh://user@hostname:project.git#commit-ish
git+ssh://user@hostname/project.git#commit-ish
git+http://user@hostname/project/blah.git#commit-ish
git+https://user@hostname/project/blah.git#commit-ish

commit-ish可以是任何标记、sha或分支,可以作为git签出的参数提供。默认为master。

"dependencies": {
  "some-package": "github:github_username/some-package"
}

或者只是

"dependencies": {
  "some-package": "github_username/some-package"
}

https://docs.npmjs.com/files/package.json#github-urls

如果您想添加既不锚定到master也不锚定到特定提交的依赖项,可以使用semver来完成。像这样:

"dependencies": {
  "some-package": "github:github_username/some-package#semver:^1.0.0"
}

对于我的私有存储库引用,我不想包括一个安全令牌,其他简单的(即仅在package.json中指定)都不起作用。以下是行之有效的方法:

去GitHub.com 导航到私有存储库 点击“克隆或下载”,复制URL(与上面的例子不匹配) 添加# commit-sha 运行npm install

进一步,为了使密钥的访问安全

在package. env所在的目录级别创建.env文件。json驻留。 提到PERSONAL_ACCESS_TOKEN =******************************* 到.env文件 别忘了加上‘。Env '到.gitingore列表,这将防止暴露密钥给外部世界,当你让git提交到你的回购。 现在您可以在包中添加依赖项。Json格式如下:

Package.json

“依赖”:{ ... :“my-private-github-repo git + https:// $ {ENV.PERSONAL_ACCESS_TOKEN} @github.com/USER/abcd-repo-3.4.0.git ", ... }

还有其他方法使用“DOTENV”npm包,但当我们试图解决“Github”包依赖时,它不能做太多。以上似乎是一个直截了当的解决方案。