Thursday, March 13, 2014

Npm install from Github or Bitbucket

Nodejs comes bundled with a very useful package management tool npm. We can easily install modules suchs as connect, express, koa and etc into our project by using the command npm install <module>. Where <module> are in the npmjs.org repository.

Suppose you have a repository on Github or Bitbucket, how can you npm install the module?


If you are getting your own repo, please make sure there are name and version properties in the package.json file.


1. Configure SSH

Back to TOC
You will need ssh keys in order to access repo via git+ssh protocol.

If you have not create ssh key pairs, you can refer the guide: SSH - Generate Public and Private Keys.

Add the public key:
- For Github: Account Settings > SSH Keys > Add SSH Key
- For BitBucket: Manage Account > SSH Keys > Add Key


2. Test SSH Access

Back to TOC

## for Github
ssh -T git@github.com

## for BitBucket
ssh -T git@bitbucket.org

You may need to key in your ssh passphrase, not the github/bitbucket password.

If you can see your Github/BitBucket login username, you have succeed.


3. Variables usage definition

Back to TOC
To avoid confusion, here’s a list of the variable names to be used and their usage definition.

<username>
Github/Bitbucket login username
<password>
Github/BitBucket login password
<repo-owner>
Repo Owner’s username. If it is your repo then this will be the same as your username.
<repo>
The name of the repo project to be used.
[#<branch>]
Optional branch/tag/version. Eg, master, v0.0.1, fedcba

4. Github

Back to TOC
You can ommit “.git” in <repo> path.

Note that i do not have a Github’s private repository to try out so I only provide ssh method for private repo.

## Public repos
npm install git+https://github.com/<repo-owner>/<repo>.git[#branch]
npm install git+ssh://git@github.com/<repo-owner>/<repo>.git[#branch]
npm install git://github.com/<repo-owner>/<repo>.git[#branch]

## Private repos
npm install git+ssh://git@github.com/<repo-owner>/<repo>.git[#branch]

5. BitBucket

Back to TOC
You can ommit “.git” in <repo> path.

Use of git+https on public repo prompt for my ssh passphrase, weird.
git+https does not prompt for ssh passphrase in private repo, weird.

## Public repos
npm install git+ssh://git@bitbucket.org/<repo-owner>/<repo>.git[#branch]

## Private repos
npm install git+https://<username>:<password>@bitbucket.org/<repo-owner>/<repo>.git[#<branch>]
npm install git+ssh://git@bitbucket.org/<repo-owner>/<repo>.git[#branch]

Hope this helps.






Copyright © Zev23.com 2014 All Rights Reserved. No part of this website may be reproduced without Zev23.com’s express consent.

No comments: