How to use a package from the git repository as a node module

Pravin Lolage
Pravin Lolage
Published in
2 min readJul 8, 2018

Sometimes we need our own package to be used by all of your team, or just to be used within your organization without publishing it on npm to avoid making it public.

We have seen using npm package in your node.js application using the following simple steps.

npm install <packageName>e.g npm install --save lodash

This will save the lodash package in your node_modules directory and also the version of the lodash will be added to your package.json file.

How to use your own package as a git repo without publishing on NPM

Step 1: Upload your package to GitHub, Gitlab or Bitbucket wherever you are comfortable. Make sure you have created package.json in your package.

Step 2: Now in your application, where you want to use that package, just run the following simple steps.

npm install --save git+https://<gitHost>/<userName>/<repoName>.git#mastere.g. npm install --save git+https://github.com/pravindot17/mysqllib.git#master

The above example will create a mysqllib folder in your node_modules, and you can use this package just like the general npm package. Here #master indicates the name of the branch and it’s optional.

e.g. let mysqlLib = require('mysqllib');

Making it private

Now, suppose you want to apply some security layer over it, you need to make it private so that it can’t be directly accessible by anyone unless some kind of authentication. For this authentication, GitHub provides personal tokens, which will allow access to install that package using npm.

This can be achieved in the following ways:

npm install --save git+https://<token-from-github>:x-oauth-basic@github.com/<user>/<GitRepo>.git#master

The token can be easily generated on GitHub using this link.

For Gitlab users, the following will work

npm install --save git+http://<groupName>:<token-from-gitlab>@git.<yourDomain>.com/<groupName>/<repoName>.git#master

Conclusion

So in this way you are able to import your own package within your application without publishing over npm.

Thanks for reading!

If you like the above article please clap the same and if you don’t like please put your thoughts in comments so that I can improve it.

You can reach me out on Linkedin, Quora.

--

--

Pravin Lolage
Pravin Lolage

A software enthusiast with almost 8+ years of experience in programming trying to share my knowledge.