{"id":10734,"library":"deps-regex","title":"JavaScript Dependency Regex Extractor","description":"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.","status":"maintenance","version":"0.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/mathieudutour/deps-regex","tags":["javascript","exec","expression","expressions","find","match","matcher","matches","matching","typescript"],"install":[{"cmd":"npm install deps-regex","lang":"bash","label":"npm"},{"cmd":"yarn add deps-regex","lang":"bash","label":"yarn"},{"cmd":"pnpm add deps-regex","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM default import. Recommended for modern JavaScript projects using bundlers or Node.js ES Modules.","wrong":"import { DepsRegex } from 'deps-regex';","symbol":"DepsRegex","correct":"import DepsRegex from 'deps-regex';"},{"note":"CommonJS import for Node.js environments. This is the pattern shown in the official documentation and widely used in older Node.js projects.","symbol":"DepsRegex","correct":"const DepsRegex = require('deps-regex');"},{"note":"TypeScript type import for configuring the regex matcher options. Should be imported as a type.","wrong":"import { DepsRegexOptions } from 'deps-regex';","symbol":"DepsRegexOptions","correct":"import type { DepsRegexOptions } from 'deps-regex';"}],"quickstart":{"code":"import DepsRegex from 'deps-regex';\nimport type { DepsRegexOptions } from 'deps-regex';\n\n// Configure the regex matcher to include ES Modules and internal paths\nconst options: DepsRegexOptions = {\n  matchInternal: true,      // e.g., require('./util')\n  matchES6: true,           // e.g., import { A } from 'package-a'\n  matchCoffeescript: false, // Disable CoffeeScript matching by default\n};\nconst re = new DepsRegex(options);\n\n// Example 1: Code with CommonJS require statements\nconst cjsCode = \"var foo = require('bar'); const baz = require('./util');\";\nconsole.log('CJS Dependencies:', re.getDependencies(cjsCode));\n// Expected output: ['bar', './util']\n\n// Example 2: Code with ES Module import statements\nconst esmCode = \"import { A } from 'package-a'; import B from './local-b';\";\nconsole.log('ESM Dependencies:', re.getDependencies(esmCode));\n// Expected output: ['package-a', './local-b']\n\n// Example 3: Mixed code, demonstrating potential false positives\nconst mixedCode = `\n  const external = require('axios');\n  // This is a comment requiring('something-else');\n  module.exports = 'require(\"false-positive-string\");';\n  import { Helper } from 'my-utils';\n`;\nconsole.log('Mixed Dependencies:', re.getDependencies(mixedCode));\n// Expected: ['axios', 'my-utils'], but 'false-positive-string' might appear due to regex limitations.","lang":"typescript","description":"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."},"warnings":[{"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.","message":"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.","severity":"gotcha","affected_versions":"*"},{"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.","message":"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.","severity":"breaking","affected_versions":">=0.2.0"},{"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.","message":"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'`).","severity":"gotcha","affected_versions":"<0.3.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","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).","error":"TypeError: deps_regex__WEBPACK_IMPORTED_MODULE_0___default.a is not a constructor"},{"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.","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.","error":"Unexpected dependency 'some-string-in-comment'"},{"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.","cause":"Trying to execute code containing CommonJS `require` statements directly in a browser environment without a bundler that transpiles or polyfills `require`.","error":"ReferenceError: require is not defined (when running in a browser)"}],"ecosystem":"npm"}