Software Development

The best way to add a non-npm dependency to bundle.json?

The best way to add a non-npm dependency to bundle.json?
Written by admin


One of many nice options of the npm ecosystem is the power to put in and handle packages from the npm registry. These dependencies are listed within the “dependencies” part of the challenge’s bundle.json file.  Nevertheless, generally you might want to make use of a dependency that isn’t obtainable 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 may add a non-npm dependency to your bundle.json file. On this article, we’ll talk about the way to add a non-npm dependency to your bundle.json file.

Options:

  • Non-npm dependencies might be any library or module that isn’t obtainable by way of npm, resembling an area library or a personal repository.
  • The method for including a non-npm dependency to bundle.json is much like including an everyday npm dependency.
  • As soon as added, non-npm dependencies might be imported and utilized in the identical method as common npm dependencies.

Syntax: So as to add a non-npm dependency to bundle.json, you should utilize the next syntax:

"dependency-name": "file:path/to/dependency"

The “dependency-name” might 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 bundle.json file.

Be aware: to run the challenge, bear in mind to sort the next in your terminal with the intention 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 below are the steps for including every sort of non-npm dependency to your bundle.json file:

Instance 1: 

Git Repositories:  If you wish to embody a dependency that’s hosted in a git repository, you should utilize the git+ protocol in your bundle.json file, as proven under: 

"dependencies": {
    "my-git-dependency": "git+https://github.com/consumer/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 information sort as a key for a hashmap object.

bundle.json

{
    "identify": "gfg-nj",
      "model": "1.0.0",
      "description": "",
      "foremost": "index.js",
      "scripts": {
        "check": "echo "Error: no check specified" && exit 1"
      },
      "dependencies": {
           "hashmap" : "https://github.com/flesler/hashmap.git"
      },
      "creator": "",
      "license": "ISC"
}

index.js: The index.js could also be created as follows.

Javascript

var HashMap = require('hashmap');

  

var map = new HashMap();

  

map[1] = "one";

map[[2, 3]] = "two";

map[[2, 3]] += "three";

map[3.2] = "three level two";

  

console.log(map)

Output:

 

It will mean you can require the git repository in your challenge to make use of the identical syntax as every other npm bundle.

Instance 2: 

Native Information: If you wish to use an area file as a dependency in your challenge, you may add it to your bundle.json file utilizing the file: protocol. The syntax is supplied under:

"dependencies": {
     "my-local-dependency": "file:../my-local-dependency"
}

It will mean you can require the native file in your challenge utilizing the next syntax:

const myLocalDependency = require('my-local-dependency');

module.js 

Javascript

  

operate sum(a, b) {

    return a + b;

}

To make use of this file as a dependency, the bundle.json could also be up to date to:

{
  "identify": "gfg-nj",
  "model": "1.0.0",
  "description": "",
  "foremost": "index.js",
  "scripts": {
    "check": "echo "Error: no check specified" && exit 1"
  },
  "dependencies": {
    "sum-module" : "file:C:CustomersphalaDesktopGFG-NJmodule.js"
  },

  "creator": "",
  "license": "ISC"
}

To check the module, an index.js file could also be created as follows:

Javascript

const sums = require('./module');

  

console.log(sums.sum(1, 2));

Output:

 

Updating Dependencies: Upon getting added a non-npm dependency to your bundle.json file, you may set up it utilizing the npm set up command. It will obtain and set up the dependency in your challenge’s node_modules listing. If it is advisable to replace the dependency to a more recent model, you should utilize the npm replace command. It will replace the dependency to the newest model laid out in your bundle.json file.

Benefits:

  • Non-npm dependencies can be utilized in circumstances the place the dependency shouldn’t be but or won’t ever be printed to npm. This may be helpful for tasks that use inner or non-public libraries.
  • It permits one to make use of a particular model or commit of the dependency, that’s not obtainable in npm.

Disadvantages:

  • Non-npm dependencies could be a supply of issue 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 might be more durable.
  • It might be more durable to search out the documentation or assist for these dependencies.

Functions:

  • To make use of inner or non-public libraries.
  • To make use of particular model of a library that’s not obtainable on npm.
  • To make use of libraries that can by no means be obtainable on npm.
  • Utilizing pre-release variations of packages that aren’t prepared for common consumption.

Including a non-npm dependency to your bundle.json file could be a helpful solution to embody libraries or modules that aren’t obtainable by way of npm. Whereas this course of is much like including common npm dependencies, it’s necessary to take into account that non-npm dependencies can convey 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 downsides. Generally, it’s finest to solely use non-npm dependencies when they’re obligatory, resembling within the case of inner or non-public libraries or to make use of particular model of a library not obtainable in npm.

About the author

admin

Leave a Comment