Registry / devops / deps-regex

deps-regex

JSON →
library0.2.0jsnpmunverified

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.

npm install deps-regex
INSTALL
IMPORT
SIG · DEPS-REGEX
D
deps-regex
devopsjavascriptv0.2.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DepsRegex
import DepsRegex from 'deps-regex';
import { DepsRegex } from 'deps-regex';
ESM default import. Recommended for modern JavaScript projects using bundlers or Node.js ES Modules.
DepsRegex
const DepsRegex = require('deps-regex');
CommonJS import for Node.js environments. This is the pattern shown in the official documentation and widely used in older Node.js projects.
DepsRegexOptions
import type { DepsRegexOptions } from 'deps-regex';
import { DepsRegexOptions } from 'deps-regex';
TypeScript type import for configuring the regex matcher options. Should be imported as a type.

Demonstrates how to initialize `deps-regex` with various options and extract both CommonJS and ES Module dependencies from code snippets, highlighting its regex-based approach and potential false positives.

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.
Debug
Known issues
gotchaThis 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.
fix
Manually inspect the extracted dependency list to filter out incorrect matches or consider using a full AST parser (e.g., `@babel/parser`, `acorn`) for robust and accurate dependency analysis in complex scenarios.
affects: *
breakingGiven 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.
fix
Pin exact versions (e.g., `"deps-regex": "0.2.0"`) in your `package.json` and thoroughly test when upgrading to any new patch or minor version to prevent unexpected breakage.
affects: >=0.2.0
gotchaThe 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'`).
fix
Review the project's GitHub issues and source code for limitations or consider adopting a more actively maintained parsing solution if advanced or cutting-edge JavaScript syntax support is critical for your project.
affects: <0.3.0
Errors
Common errors & fixes
TypeError: deps_regex__WEBPACK_IMPORTED_MODULE_0___default.a is not a constructor
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).
fix
Use 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'
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.
fix
Implement 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)
Trying to execute code containing CommonJS `require` statements directly in a browser environment without a bundler that transpiles or polyfills `require`.
fix
Ensure 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.
Upgrade
Version history
0.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
deps-regex — npm install deps-regex · libregistry