Module Graphs and Import Order

Learn via video courses

Overview

A remote package that is included in our project and upon which it depends is referred to as a dependency. Our dependencies must be monitored or at the very least, listed. All of our requirements are listed in a package.json file, a JSON file containing details about our project and the dependencies it requires. Module graph is a module in NodeJs in which we can get information about the dependencies and dependents of a certain module. Importing dependencies can also be arranged using the order-imports module.

Introduction to modules-graph in Node.js

Any additional modules that a specific module (represented by the package.json) needs to function properly are specified using the dependencies value. Module graph is a module in Node.js in which we can get information about the dependencies and dependents of a certain module. Small library to get module dependencies info during runtime.

Installation of modules-graph

To make use of the modules-graph module in Node js, you must first install the module-graphs package using the Node Package Manager (NPM). Run the following command to install the module-graphs module :

After installing the module, you will be able to see modules-graph under the dependencies section in the package.json.

Usage of modules-graph

The ModulesGraph object will hook into the node require function when the function is created. Then the ModulesGraph object will log every call required while keeping track of which module is called which.

Then you can obtain dependencies and dependent modules for any module by calling the methods on the modulesGraph object. All methods accept the filepath parameter, which must be the module's absolute path.

Call the restore method to stop the logging of dependencies.

Different Methods in modules-graph

  • result = modulesGraph.getImmediateDependencies(filepath);
  • result = modulesGraph.getImmediateDependants(filepath);
  • result = modulesGraph.getDependencies(filepath);
  • result = modulesGraph.getDependants(filepath);
  1. getImmediateDependencies: The getImmediateDependencies function returns an array containing the absolute path of the modules that are directly imported or required in the module whose absolute path has been passed as a parameter.

    result = modulesGraph.getImmediateDependencies(filepath);

    You can use the above function as follows :

    In the above code, moduleAFilenamecontains the absolute path of the module whose immediate dependencies are required to be found.

  2. getImmediateDependants: The getImmediateDependants function returns an array containing the absolute path of the modules that are directly importing or requiring the module whose absolute path has been passed as a parameter.

    result = modulesGraph.getImmediateDependants(filepath);

    You can use the above function as follows :

    In the above code moduleAFilenamecontains the absolute path of the module whose immediate dependents are required to be found.

  3. getDependencies - The getDependencies function returns an array containing the absolute path of the modules that are imported or required directly or indirectly that is through some other module in the module whose absolute path has been passed as a parameter.

    result = modulesGraph.getDependencies(filepath);

    You can use the above function as follows :

    In the above code moduleAFilenamecontains the absolute path of the module whose all the dependencies are required to be found.

  4. getDependants - The getDependants function returns an array containing the absolute path of the modules that are directly or indirectly importing or requiring the module whose absolute path has been passed as a parameter.

    result = modulesGraph.getDependants(filepath);

    You can use the above function as follows :

    In the above code moduleAFilenamecontains the absolute path of the module whose all the dependents are required to be found.

Implementation

Initialize a new project in Node.js using the below command in a new directory.

After the command is successfully executed, install the modules-graph module using the npm.

Let us now create some sample modules on which we will test the various functions of the modules-graph module.

Create a new folder, test-modules and in this folder, create the following three modules :

In file test-module-a write the following code :

In file test-module-b write the following code :

In file test-module-c write the following code :

Examples

Now we will be writing some tests using the mocha library. So first we need to install two modules :

Now create a folder named test in the main project directory and create a test.js file.

In the test.js file import the assert function from the chai module. Also, you need to import the modules-graph module as shown below.

Now you need to create a global describe function inside which you will be defining the before and after functions.

Example:

In the above code in the before() function, an instance of the ModulesGraph has been created. We have imported the three sample modules and their absolute paths have been stored in three variables moduleAFilename, moduleBFilename, and moduleCFilename. In the after() method the restore function has been called to stop logging the dependencies.

Tests

Now let us start writing various tests for the different functions in the module-graph module.

Let us first write some tests for the getImmediateDependants function. The getImmediateDependants function returns an array containing the absolute path of the modules that are directly importing or requiring the module whose absolute path has been passed as a parameter.

We will write two tests, one for module A and one for module B. As we know, module A is not imported into any other module; hence its immediate dependents array must not contain any module’s path. Hence the result for module A has been checked to not include any module’s path.

Module B has been imported into both module A and module C. Hence they are its immediate dependents. The result for immediate dependents of module B has been checked to contain the path of module A and module C. It should not contain its path.

Example:

Explanation:

In the above code, two it functions, one for module A and the other for module B. The result of modulesGraph.getImmediateDependants() is stored in the result variable, and then the assert.include() and assert.notInclude() functions have been used.

Now we will write some tests for the getImmediateDependencies function. The getImmediateDependencies function returns an array containing the absolute path of the modules that are directly imported or required in the module whose absolute path has been passed as a parameter.

We will write two tests, one for module A and one for module B. As we know, module B imports module C, hence its immediate dependencies array must contain only the module C path. Hence the result for module B has been checked to include only module C’s path. Module B has been imported into both module A and module C hence it is their immediate dependency. The result for the immediate dependency of module A has been checked to contain the path of module B. It should not contain its path.

Example:

Explanation:

In the above code, two it functions, one for module A and the other for module B. The result of modulesGraph.getImmediateDependencies() is stored in the result variable, and then the assert.include() and assert.notInclude() functions have been used.

Let us now write some tests for the getDependants function. The getDependants function returns an array containing the absolute path of the modules that are directly or indirectly importing or requiring the module whose absolute path has been passed as a parameter.

We will write three tests, one each for module A, module B, and module C. As we know, module A is not imported into any other module; hence its dependents array must not contain any module’s path. Hence the result for module A has been checked to not include any module’s path.

Module B has been imported into both module A and module C; hence they are its dependents. Also, module C is imported into module B, and hence module B will be importing itself. The result for dependents of module B has been checked to contain the path of all three modules.

Module C has been imported into module B. Module B has been imported into both modules A and C. Hence module C will have all three modules as its dependants. Hence the result for dependents of module C has been checked to contain the path of all three modules.

Example:

In the above code, three it functions, one for module A, one for module B and the last one for module C. The result of modulesGraph.getDependants() is stored in the result variable and then the assert.include() and assert.notInclude() functions have been used.

Now we will write some tests for the getDependencies function. The getDependencies function returns an array containing the absolute path of the modules that are imported or required directly or indirectly, that is through some other module in the module whose absolute path has been passed as a parameter.

The result for dependents of module B has been checked to contain the path of all three modules. As we know, module B imports module C, and module C imports module B. Hence its dependencies array must contain both module-b and module-c paths. Hence the result for module B has been checked to include both module B's and module C’s path. The same will be the case for module C.

Module B has been imported into both module A and module C and hence it is their dependency. The result for the dependency of module A has been checked to contain the path of module B. Module B requires module C. Hence module C also becomes the dependency of module A. The result for module A must contain module B and module C's path.

Example:

Explanation:

In the above code, three it functions, one for module A, one for module B and the last one for module C. The result of modulesGraph.getDependencies() is stored in the result variable, and then the assert.include() and assert.notInclude() functions have been used.

How to Arrange Imports in a Certain Order?

Invocation

The following command can be used to arrange your imports of the files of a certain directory using the order-imports module.

Configuration

The rules in which you want the order-imports module to arrange your imports can be specified in a JSON file as shown below :

Example :

Let us create a new react application and then you can apply the rules of the exampleConfig.json.

In the React application create a javascript file with the following code in the file :

Now install the order-imports module using the below command :

Place the exampleConfig.json in the main folder of the application and run the following command :

Now the imports of your file would have become :

Hence you can easily order your imports using the order-imports module.

Conclusion

  • The ModulesGraph object will hook into the node require function when it is created. Then the ModulesGraph object will log every call required while keeping track of which module is called.

  • The getImmediateDependencies function returns an array containing the absolute path of the modules that are directly imported into the module.

    result = modulesGraph.getImmediateDependencies(filepath);

  • The getImmediateDependants function returns an array containing the absolute path of the modules that are directly importing or requiring the module.

    result = modulesGraph.getImmediateDependants(filepath);

  • The getDependencies function returns an array containing the absolute path of the modules that are imported or required directly or indirectly that is through some other module.

    result = modulesGraph.getDependencies(filepath);

  • The getDependants function returns an array containing the absolute path of the modules that are directly or indirectly importing or requiring the module.

    result = modulesGraph.getDependants(filepath);

  • Call the restore method to stop the logging of dependencies.

  • The following command can be used to arrange your imports of the files of a certain directory using the order-imports module.

    node order-imports src -c exampleConfig.json