liveBook · Manning

Dependency

As explained in Secrets of the JavaScript Ninja, Second Edition

A module or component that another module requires in order to function correctly.

A dependency is a module or component another module requires to work correctly. In AMD, a module lists required module IDs, and its factory receives the corresponding modules as arguments. section 11.1.2

AMD requests dependencies asynchronously and calls the factory after they are downloaded and evaluated. CommonJS uses require to obtain a dependency, as in a module that requires jQuery. section 11.1.2

Listing 11.3. Using AMD to specify a module dependent on jQuery
define('MouseCounterModule',['jQuery'], $ => {             #A
  let numClicks = 0;
  const handleClick = () => {
    alert(++numClicks);
  };
  
  return {                                                 #B
    countClicks: () => {
      $(document).on("click", handleClick);
    }
  };
});

An AMD module depends on jQuery to register a click handler for a click-counting module. section 11.1.2

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Compares AMD and CommonJS dependency declarations and loading, but defers setup and project integration.
Examples
Uses a click-counting module that depends on jQuery, shown with AMD and CommonJS declarations.
What you can do after
Apply dependencies via AMD define or CommonJS require to obtain modules for use in a module.
How it connects
Connects dependencies with JavaScript modules, AMD, CommonJS, and jQuery.

Also explained in

Compare how 27 books explain dependency →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Second Edition. The section links point to the parts of the book it draws on.