node.js 有没有办法指定哪个 ssh 密钥应该用于 npm install

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25937736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:54:01  来源:igfitidea点击:

Is there a way to specify which ssh key should be used for npm install

node.jsgitsshnpm

提问by Vadim

I have a private repo that I want to install in my package.json file.

我有一个要安装在我的 package.json 文件中的私有存储库。

"private-module": "git+ssh://[email protected]:private/private-module.git"

By default npm uses your default private key. I want to be able to specify which ssh key npm should use when running npm install. Is there any way to do this?

默认情况下,npm 使用您的默认私钥。我希望能够指定 npm 在运行时应该使用哪个 ssh 密钥npm install。有没有办法做到这一点?

回答by mscdex

Here are a few solutions:

这里有几个解决方案:

  • Add an entry to your ~/.ssh/config. For example:

    Host bitbucket.org
         IdentityFile ~/.ssh/bitbucket_key
         IdentitiesOnly yes
    
  • Use ssh-agentand add your key to the agent instance beforehand.

  • Use something like ssh-identfor choosing ssh agents and identities dynamically based on the current working directory or arguments passed to ssh-ident. As their readme states, you would typically alias ssh-ident to sshso that it's automatically used everywhere.

  • 向您的~/.ssh/config. 例如:

    Host bitbucket.org
         IdentityFile ~/.ssh/bitbucket_key
         IdentitiesOnly yes
    
  • ssh-agent事先使用您的密钥并将其添加到代理实例。

  • 使用类似ssh-ident 的东西根据当前工作目录或传递给 ssh-ident 的参数动态选择 ssh 代理和身份。正如他们的自述文件所述,您通常会将 ssh-ident 作为别名,ssh以便在任何地方自动使用它。

回答by mwotton

After you have made the changes in the first part of mscdex's answeryou might need to add the host to the list of known hosts - before the npm installcommand will work.

在 mscdex答案的第一部分进行更改后,您可能需要将主机添加到已知主机列表中 - 在npm install命令生效之前。

You can do this by cloning the private repo to another directory:

您可以通过将私有 repo 克隆到另一个目录来做到这一点:

git clone ssh://[email protected]:private/private-module.git

git clone ssh://[email protected]:private/private-module.git

You might be asked if you want to proceed, type yesand enter, then bitbucket.org is trusted. Go back to your project directory and retry npm install. This is what was needed for mscdex's answer to work for me.

您可能会被问到是否要继续、输入yes并输入,然后 bitbucket.org 是可信的。返回到您的项目目录并重试npm install。这是 mscdex 对我来说有效的答案所需要的。

There are other ways of adding trusted hosts, but this does that in addition to verify that you can actually get to the desired private repo.

还有其他添加受信任主机的方法,但这除了验证您是否确实可以访问所需的私有存储库之外。

回答by Wilk

Otherwise, you can use your NPM Tokenin your .npmrcfile:

否则,您可以在文件中使用NPM 令牌.npmrc

// .npmrc
//registry.npmjs.org/:_authToken=${YOUR_NPM_TOKEN}

Source: https://blog.npmjs.org/post/118393368555/deploying-with-npm-private-modules

来源:https: //blog.npmjs.org/post/118393368555/deploying-with-npm-private-modules