JavaScript Dependency Regex Extractor
deps-regex is a JavaScript utility that provides a regular expression to match `require()` and `import` statements within source code. It is currently at version 0.2.0. This library is designed for performance-critical scenarios where using a full Abstract Syntax Tree (AST) parser would introduce too much overhead. Its primary differentiator is its lightweight, regex-based approach, which comes with inherent fragility and a higher likelihood of false positives compared to syntax tree parsing. Release cadence is infrequent, and its low version number suggests a focused, specific-purpose tool rather than a broadly maintained library. Developers should be aware of its limitations regarding parsing accuracy and potential issues with complex or non-standard code structures.
Common errors
-
TypeError: deps_regex__WEBPACK_IMPORTED_MODULE_0___default.a is not a constructor
cause Attempting to use `DepsRegex` as a named import or incorrectly importing it in a modern module environment (e.g., Webpack/Vite bundles CJS default exports in this manner).fixUse a default import for ESM: `import DepsRegex from 'deps-regex';` or stick to CommonJS `const DepsRegex = require('deps-regex');` if that's the intended environment. -
Unexpected dependency 'some-string-in-comment'
cause The regex-based parser matched a `require()` or `import` like string within a comment block or a multi-line string literal, leading to an inaccurate dependency report.fixImplement post-processing logic to filter out known false positives, or, for higher accuracy, transition to an AST-based parser that understands code context and syntax. -
ReferenceError: require is not defined (when running in a browser)
cause Trying to execute code containing CommonJS `require` statements directly in a browser environment without a bundler that transpiles or polyfills `require`.fixEnsure your application code is properly bundled for the browser using tools like Webpack, Rollup, or Vite, or use ES Module `import` syntax if targeting modern browsers natively.
Warnings
- gotcha This library uses regular expressions for parsing, which can lead to false positives when `require` or `import` strings appear in comments, string literals, or other contexts not intended as actual dependency declarations.
- breaking Given its low major version (0.2.0), minor updates may introduce breaking changes without a major version bump, as per typical pre-1.0.0 semantic versioning behavior. APIs might not be stable.
- gotcha The regex might not cover all edge cases or new syntax variations introduced in newer JavaScript versions (e.g., dynamic `import()`, `import assertions` or `import attributes`, `export * as ns from 'mod'`).
Install
-
npm install deps-regex -
yarn add deps-regex -
pnpm add deps-regex
Imports
- DepsRegex
import { DepsRegex } from 'deps-regex';import DepsRegex from 'deps-regex';
- DepsRegex
const DepsRegex = require('deps-regex'); - DepsRegexOptions
import { DepsRegexOptions } from 'deps-regex';import type { DepsRegexOptions } from 'deps-regex';
Quickstart
import DepsRegex from 'deps-regex';
import type { DepsRegexOptions } from 'deps-regex';
// Configure the regex matcher to include ES Modules and internal paths
const options: DepsRegexOptions = {
matchInternal: true, // e.g., require('./util')
matchES6: true, // e.g., import { A } from 'package-a'
matchCoffeescript: false, // Disable CoffeeScript matching by default
};
const re = new DepsRegex(options);
// Example 1: Code with CommonJS require statements
const cjsCode = "var foo = require('bar'); const baz = require('./util');";
console.log('CJS Dependencies:', re.getDependencies(cjsCode));
// Expected output: ['bar', './util']
// Example 2: Code with ES Module import statements
const esmCode = "import { A } from 'package-a'; import B from './local-b';";
console.log('ESM Dependencies:', re.getDependencies(esmCode));
// Expected output: ['package-a', './local-b']
// Example 3: Mixed code, demonstrating potential false positives
const mixedCode = `
const external = require('axios');
// This is a comment requiring('something-else');
module.exports = 'require("false-positive-string");';
import { Helper } from 'my-utils';
`;
console.log('Mixed Dependencies:', re.getDependencies(mixedCode));
// Expected: ['axios', 'my-utils'], but 'false-positive-string' might appear due to regex limitations.