JavaScript Dependency Regex Extractor

0.2.0 · maintenance · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

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.

view raw JSON →