One of many nice options of the npm ecosystem is the flexibility to put in and handle packages from the npm registry. These dependencies are listed within the “dependencies” part of the challenge’s package deal.json file. Nonetheless, typically you might want to make use of a dependency that isn’t out there by way of npm, resembling a library that you simply’ve created your self, or one which hasn’t been printed to npm but. On this case, you’ll be able to add a non-npm dependency to your package deal.json file. On this article, we’ll talk about the right way to add a non-npm dependency to your package deal.json file.
Options:
- Non-npm dependencies could be any library or module that isn’t out there by way of npm, resembling a neighborhood library or a non-public repository.
- The method for including a non-npm dependency to package deal.json is just like including a daily npm dependency.
- As soon as added, non-npm dependencies could be imported and utilized in the identical method as common npm dependencies.
Syntax: So as to add a non-npm dependency to package deal.json, you need to use the next syntax:
"dependency-name": "file:path/to/dependency"
The “dependency-name” could be any identify you select, and “path/to/dependency” ought to be the file path to the dependency’s supply code. This may be both an absolute path or a path relative to the package deal.json file.
Observe: to run the challenge, bear in mind to sort the next in your terminal in an effort to use the dependencies:
npm set up .
There are a couple of alternative ways to do that, relying on the kind of dependency you are attempting to incorporate. Listed here are the steps for including every sort of non-npm dependency to your package deal.json file:
Instance 1:
Git Repositories: If you wish to embrace a dependency that’s hosted in a git repository, you need to use the git+ protocol in your package deal.json file, as proven beneath:
"dependencies": { "my-git-dependency": "git+https://github.com/person/my-git-dependency.git" }
For instance, allow us to create a challenge that makes use of the hashmap library on GitHub, which permits one to make use of any knowledge sort as a key for a hashmap object.
package deal.json
{ "identify": "gfg-nj", "model": "1.0.0", "description": "", "major": "index.js", "scripts": { "take a look at": "echo "Error: no take a look at specified" && exit 1" }, "dependencies": { "hashmap" : "https://github.com/flesler/hashmap.git" }, "writer": "", "license": "ISC" }
index.js: The index.js could also be created as follows.
Javascript
|
Output:

This may let you require the git repository in your challenge to make use of the identical syntax as another npm package deal.
Instance 2:
Native Information: If you wish to use a neighborhood file as a dependency in your challenge, you’ll be able to add it to your package deal.json file utilizing the file: protocol. The syntax is offered beneath:
"dependencies": { "my-local-dependency": "file:../my-local-dependency" }
This may let you require the native file in your challenge utilizing the next syntax:
const myLocalDependency = require('my-local-dependency');
module.js
Javascript
|
To make use of this file as a dependency, the package deal.json could also be up to date to:
{ "identify": "gfg-nj", "model": "1.0.0", "description": "", "major": "index.js", "scripts": { "take a look at": "echo "Error: no take a look at specified" && exit 1" }, "dependencies": { "sum-module" : "file:C:CustomersphalaDesktopGFG-NJmodule.js" }, "writer": "", "license": "ISC" }
To check the module, an index.js file could also be created as follows:
Javascript
|
Output:

Updating Dependencies: After getting added a non-npm dependency to your package deal.json file, you’ll be able to set up it utilizing the npm set up command. This may obtain and set up the dependency in your challenge’s node_modules listing. If you should replace the dependency to a more recent model, you need to use the npm replace command. This may replace the dependency to the newest model laid out in your package deal.json file.
Benefits:
- Non-npm dependencies can be utilized in instances the place the dependency will not be but or won’t ever be printed to npm. This may be helpful for tasks that use inside or non-public libraries.
- It permits one to make use of a selected model or commit of the dependency, that isn’t out there in npm.
Disadvantages:
- Non-npm dependencies is usually a supply of problem within the improvement and upkeep of the challenge, as they don’t observe the identical course of as npm packages.
- Managing variations and updates of these dependencies could be more durable.
- It may very well be more durable to search out the documentation or help for these dependencies.
Functions:
- To make use of inside or non-public libraries.
- To make use of particular model of a library that isn’t out there on npm.
- To make use of libraries that may by no means be out there on npm.
- Utilizing pre-release variations of packages that aren’t prepared for normal consumption.
Including a non-npm dependency to your package deal.json file is usually a helpful technique to embrace libraries or modules that aren’t out there by way of npm. Whereas this course of is just like including common npm dependencies, it’s vital to understand that non-npm dependencies can deliver further upkeep and administration challenges. Cautious consideration ought to be taken when deciding to make use of non-npm dependencies in your challenge, to weigh the benefits and drawbacks. Typically, it’s finest to solely use non-npm dependencies when they’re obligatory, resembling within the case of inside or non-public libraries or to make use of particular model of a library not out there in npm.