How to add a non-npm dependency to package.json?
Last Updated :
24 Apr, 2025
One of the great features of the npm ecosystem is the ability to install and manage packages from the npm registry. These dependencies are listed in the "dependencies" section of the project's package.json file. However, sometimes you may need to use a dependency that isn't available through npm, such as a library that you've created yourself, or one that hasn't been published to npm yet. In this case, you can add a non-npm dependency to your package.json file. In this article, we'll discuss how to add a non-npm dependency to your package.json file.
Features:
- Non-npm dependencies can be any library or module that isn't available through npm, such as a local library or a private repository.
- The process for adding a non-npm dependency to package.json is similar to adding a regular npm dependency.
- Once added, non-npm dependencies can be imported and used in the same way as regular npm dependencies.
Syntax: To add a non-npm dependency to package.json, you can use the following syntax:
"dependency-name": "file:path/to/dependency"
The "dependency-name" can be any name you choose, and "path/to/dependency" should be the file path to the dependency's source code. This can be either an absolute path or a path relative to the package.json file.
Note: to run the project, remember to type the following in your terminal in order to use the dependencies:
npm install .
There are a few different ways to do this, depending on the type of dependency you are trying to include. Here are the steps for adding each type of non-npm dependency to your package.json file:
Example 1: Git Repositories: If you want to include a dependency that is hosted in a git repository, you can use the git+ protocol in your package.json file, as shown below:
"dependencies": {
"my-git-dependency": "git+https://github.com/user/my-git-dependency.git"
}
For example, let us create a project that uses the hashmap library on GitHub, which allows one to use any data type as a key for a hashmap object.
package.json
{
"name": "gfg-nj",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"hashmap" : "https://github.com/flesler/hashmap.git"
},
"author": "",
"license": "ISC"
}
index.js: The index.js may be created as follows.
JavaScript
// use the hashmap library added as dependency
const HashMap = require('hashmap');
// Creating a new HashMap
let map = new HashMap();
// Adding key value pairs
map[1] = "one";
map[[2, 3]] = "two";
map[[2, 3]] += "three";
map[3.2] = "three point two";
// Displaying the map
console.log(map)
Output:
This will allow you to require the git repository in your project to use the same syntax as any other npm package.
Example 2: Local Files: If you want to use a local file as a dependency in your project, you can add it to your package.json file using the file: protocol. The syntax is provided below:
"dependencies": {
"my-local-dependency": "file:../my-local-dependency"
}
This will allow you to require the local file in your project using the following syntax:
const myLocalDependency = require('my-local-dependency');
module.js
JavaScript
// This file provides a definition for the sum method
function sum(a, b) {
return a + b;
}
To use this file as a dependency, the package.json may be updated to:
{
"name": "gfg-nj",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"sum-module" : "file:C:\\Users\\phala\\Desktop\\GFG-NJ\\module.js"
},
"author": "",
"license": "ISC"
}
To test the module, an index.js file may be created as follows:
JavaScript
// Load the module.js file
const sums = require('./module');
// Displaying the result of the sum method
console.log(sums.sum(1, 2));
Output:
Updating Dependencies: Once you have added a non-npm dependency to your package.json file, you can install it using the npm install command. This will download and install the dependency in your project's node_modules directory. If you need to update the dependency to a newer version, you can use the npm update command. This will update the dependency to the latest version specified in your package.json file.
Advantages:
- Non-npm dependencies can be used in cases where the dependency is not yet or will never be published to npm. This can be useful for projects that use internal or private libraries.
- It allows one to use a specific version or commit of the dependency, that is not available in npm.
Disadvantages:
- Non-npm dependencies can be a source of difficulty in the development and maintenance of the project, as they don't follow the same process as npm packages.
- Managing versions and updates of those dependencies can be harder.
- It could be harder to find the documentation or support for those dependencies.
Applications:
- To use internal or private libraries.
- To use a specific version of a library that is not available on npm.
- To use libraries that will never be available on npm.
- Using pre-release versions of packages that are not ready for general consumption.
Adding a non-npm dependency to your package.json file can be a useful way to include libraries or modules that aren't available through npm. While this process is similar to adding regular npm dependencies, it's important to keep in mind that non-npm dependencies can bring additional maintenance and management challenges. Careful consideration should be taken when deciding to use non-npm dependencies in your project, to weigh the advantages and disadvantages. In general, it's best to only use non-npm dependencies when they are necessary, such as in the case of internal or private libraries or to use a specific version of a library not available in npm.
Similar Reads
How to update dependency in package.json file ?
In this article, we will discuss how to update the dependencies of a project with npm. You must have heard about npm which is called a node package manager. So, we can run this command to install an npm package. npm install Note: The --save flag is no longer needed after the Node 5.0.0 version. The
3 min read
How To Use Node Modules with npm and package.json
NodeJS is a powerful runtime for server-side JavaScript & these modules are reusable pieces of code that can be easily imported and used in NodeJS applications. npm (Node Package Manager) is the default package manager for Node JS and is used to install, manage, and publish NodeJS packages. This
3 min read
How to use NPM Trends to Pick a Javascript Dependency?
Choosing the right JavaScript dependency for your project can be daunting, given the vast number of available packages. npm trends is a valuable tool that helps developers compare the popularity and usage of npm packages over time. By analyzing download statistics, developers can make more informed
3 min read
How to Force an NPM Package to Install?
Forcing an NPM package to install can be necessary in cases where the dependencies of a package are in conflict or when you need to override existing constraints or force the installation of a specific version. Forcing an NPM package to install refers to using specific commands to bypass version con
3 min read
Is there a way to force npm to generate package-lock.json?
The package-lock.json file in npm is used for ensuring consistent installations of dependencies across different environments or for collaborative projects. package-lock.json is created for locking the dependency with the installed version. It will install the exact latest version of that package in
1 min read
How to resolve error "npm WARN package.json: No repository field" ?
The warning "npm WARN package.json: No repository field" indicates that the package.json file in your project lacks a "repository" field. The "repository" field is crucial for providing information about the version-controlled source code repository of your project. While not mandatory, including th
3 min read
How to Update Local Package in NPM?
Updating the local packages in NPM is a common task for the developers. Whether it is for bug fixes, new features, or security patches. Keeping your dependencies up to date is essential. These are the following approaches to updating the local package in NPM: Table of Content Update to the Latest St
4 min read
How to document NPM packages ?
In this article, we will see how to write the documentation of an NPM package. Documentation is an essential part of any NPM package because it gives an idea about the package method and how to use them. Good documentation makes your npm package popular npm packages. The Documentation of the npm pac
2 min read
How local installation of a dependency done ?
The local installation of dependencies in Node.js is done by putting the local dependencies in a single place and running some simple commands from the terminal to install dependencies. A local dependency can't be accessed outside of the directory. All the local dependencies of a project are usually
2 min read
How to override nested NPM dependency versions?
In projects the packages download and used using npm are called dependency and each dependencies can have their own nested dependencies that also gets downloaded. These nested dependency creates conflicts due to the presence of multiple version of the same dependency. This will lead to issues like c
5 min read