Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

node.js

npm

ssh

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

"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?

like image 526
Vadim Avatar asked Sep 19 '14 15:09

Vadim


People also ask

Does npm use SSH?

[BUG] NPM v7 uses SSH instead of an explicit HTTPS for GitHub repos #2610.

How do I specify npm I?

For npm install specific version, use npm install [package-name]@[version-number]. Use npm view [package-name] version to know the specific latest version of a package available on the npm registry. Use npm list [package-name] to know the specific latest version of an installed package.

Does npm install locally by default?

Install the dependencies to the local node_modules folder. In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package. By default, npm install will install all modules listed as dependencies in package.

Do I need to use the -- save with npm install?

As of npm 5.0. 0, installed modules are added as a dependency by default, so the --save option is no longer needed.


2 Answers

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-agent and add your key to the agent instance beforehand.

  • Use something like ssh-ident for 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 ssh so that it's automatically used everywhere.

like image 108
mscdex Avatar answered Sep 28 '22 17:09

mscdex


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

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

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

You might be asked if you want to proceed, type yes and 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.

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.

like image 35
mwotton Avatar answered Sep 28 '22 16:09

mwotton