Skip to main content
Utilities for finding ES module imports, CommonJS requires, and dynamic imports for specific Node.js modules.

Overview

These utilities help locate module dependencies in different formats:
  • ES Module Imports: import fs from 'fs' or import { readFile } from 'node:fs'
  • CommonJS Requires: const fs = require('fs') or const { readFile } = require('node:fs')
  • Dynamic Imports: const fs = await import('fs')
All detection functions automatically support both bare module names ('fs') and node: prefixed imports ('node:fs').

Core Functions

getModuleDependencies

Finds all module import/require statements for a specific Node.js module. This is the most comprehensive function that combines results from all other detection methods.
SgRoot<Js>
required
The root AST node to search
string
required
The Node.js module name to search for (e.g., ‘fs’, ‘path’, ‘util’)
Returns: SgNode<Js>[] - Array of nodes representing all import/require statements for the module
Under the hood, this function calls getNodeRequireCalls, getNodeImportStatements, and getNodeImportCalls, then combines the results.

ES Module Import Detection

getNodeImportStatements

Finds all ES module import statements for a specific Node.js module.
SgRoot<Js>
required
The root AST node to search
string
required
The Node.js module name to search for
Returns: SgNode<Js>[] - Array of import_statement nodes

getNodeImportCalls

Finds dynamic import calls assigned to variables. Excludes unassigned imports.
SgRoot<Js>
required
The root AST node to search
string
required
The Node.js module name to search for
Returns: SgNode<Js>[] - Array of variable_declarator or expression_statement nodes with import calls
This function captures both const fs = await import('fs') patterns and .then() chained patterns like import('fs').then(...), but ignores simple unassigned import('fs') calls.

CommonJS Require Detection

getNodeRequireCalls

Finds CommonJS require calls assigned to variables.
SgRoot<Js>
required
The root AST node to search
string
required
The Node.js module name to search for
Returns: SgNode<Js>[] - Array of variable_declarator nodes with require calls
Simple require('fs') calls without variable assignment are not captured, as they have no effect in codemod context.

Usage Examples

Finding All FS Module Usage

Detecting Import Style

Processing All Module Imports

Migration Example: Consolidating Imports