Overview
These utilities help locate module dependencies in different formats:- ES Module Imports:
import fs from 'fs'orimport { readFile } from 'node:fs' - CommonJS Requires:
const fs = require('fs')orconst { 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’)
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
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
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
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.