NPM is a node package management tool used to download or publish node packages via the npm package registry. It comes bundled with
npmjs offers numerous open-source packages, such as Lodash, React, and Chalk to accelerate the development process.
Often, packages are not published on the
npmjs registry, but they still can be used in a node project using the
npm CLI tool. This guide explains the details of installing public and private NPM packages from GitHub.
npm command can install public packages from
npmjs registry using the
1 2 3
npm install package-name package-name2 # or npm i package-name package-name2
Sometimes packages are not published on the
npmjs registry, but it can still be installed using
npm tool can access and install any public node project as a dependency from GitHub:
npm i https://github.com/user_name/node_project_name
npm command will try to install the package using
git clone. The
npm command can also install the package from different GitHub repository states using a
commit hash value, which can be used to install the package with a commit id:
npm install use_name/node_project#commit
@symbol represents the
npmscope, a technique to group all the dependencies of a user or org in a folder. A package name without
name\namepattern will be treated as a GitHub package repository.
branch name can be used to install a branch as a package:
npm install use_name/node_project#branch_name
version names can be used to install a specific version of a GitHub package:
gist can also be added using the id of a
npm install gist/gist_id
npm package can be installed from a private GitHub repository using an SSH repository link. SSH links are only available to logged-in users and can be used to access the private repositories of your GitHub. The SSH protocol uses a public key cryptography algorithm to authenticate the command to access GitHub repositories, and it needs to be configured with the GitHub account using the SSH keys. A private GitHub repository can be installed using the
git+ssh as protocol:
npm install git+ssh://[email protected]:user_name/node_project.git
Note: A GitHub package must have a meaningful
package.jsonfile to be installed as a package.
npm install command also provides many other features. For example,
range can used to install a package with a given range for the version:
npm install use_name/node_project">=1.0.0 <=2.0.0"
Versions are often based on semver.
--force will install a package from the remote package repository:
npm install pacakge_name --force
global packages are installed in the global npm package folder, which is accessible to every project:
npm install pacakge_name -global
--ignore-scripts flag is used to skip the execution of
And finally, the
uninstall command is used to remove the dependencies from local projects, and the
-g flag is used to remove global dependencies.
npm i package-namewith
npmversion 5 or above without the
npm5+ will automatically add the package as a dependency.
npm installation from GitHub is quite useful for testing packages. It also gives the flexibility to install any specific branch, version, tag, and so on. Happy coding!