我试图通过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"
}

正确的做法是什么?


当前回答

正如人们指出的,有多种方法可以做到这一点,但最短的版本是:

// from master
"depName": "user/repo",

// specific branch
"depName": "user/repo#branch",

// specific commit
"depName": "user/repo#commit",

// private repo
"depName": "git+https://[TOKEN]:x-oauth-basic@github.com/user/repo.git"

如。

"dependencies" : {
  "hexo-renderer-marked": "amejiarosario/dsa.jsd#book",
  "hexo-renderer-marked": "amejiarosario/dsa.js#8ea61ce",
  "hexo-renderer-marked": "amejiarosario/dsa.js",
}

其他回答

正如人们指出的,有多种方法可以做到这一点,但最短的版本是:

// from master
"depName": "user/repo",

// specific branch
"depName": "user/repo#branch",

// specific commit
"depName": "user/repo#commit",

// private repo
"depName": "git+https://[TOKEN]:x-oauth-basic@github.com/user/repo.git"

如。

"dependencies" : {
  "hexo-renderer-marked": "amejiarosario/dsa.jsd#book",
  "hexo-renderer-marked": "amejiarosario/dsa.js#8ea61ce",
  "hexo-renderer-marked": "amejiarosario/dsa.js",
}

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

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

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

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

在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”包依赖时,它不能做太多。以上似乎是一个直截了当的解决方案。

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

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

接受的答案是可行的,但我不太喜欢将安全令牌粘贴到package.json中的想法

我在其他地方找到了它,只需运行git-config manpage中记录的这个一次性命令。

git config --global url."https://${GITHUB_TOKEN}@github.com/".insteadOf git@github.com:

GITHUB_TOKEN可以设置为环境变量或直接粘贴

然后我安装私人github回购像:npm install user/repo—save


也适用于Heroku,只需设置上面的git配置…命令作为heroku预构建脚本包。将GITHUB_TOKEN设置为Heroku配置变量。