Glob Pattern Matcher
Minimatch is a JavaScript utility library that provides robust glob matching functionality, converting glob expressions into JavaScript `RegExp` objects for efficient pattern matching. It is famously used internally by npm for its file system operations. The current stable version is 10.2.5, with releases typically occurring as needed to address bugs, enhance features, or align with npm's requirements. Key features include support for brace expansion, extended glob matching, globstar (`**`), and Posix character classes (e.g., `[[:alpha:]]`), which are Unicode-aware. A critical aspect of minimatch is its explicit warning regarding Regular Expression Denial of Service (ReDoS) vulnerabilities, advising users to never use untrusted input as glob patterns due to the inherent risks of RegExp-based matching. It also provides specific guidance for Windows users, emphasizing the exclusive use of forward slashes in glob expressions to avoid misinterpretation of backslashes as escape characters.
Common errors
-
RangeError: Maximum call stack size exceeded
cause A complex or maliciously crafted glob pattern caused excessive backtracking in the underlying regular expression engine, leading to a ReDoS condition.fixReview glob pattern sources. If patterns originate from user input, implement strict sanitization or whitelisting. Avoid using complex patterns with untrusted input. -
Glob pattern with backslashes on Windows does not match expected files.
cause On Windows, backslashes (`\`) in glob patterns are interpreted as escape characters, not path separators, causing patterns like `foo\bar` to match `foo\bar` exactly, not `foo/bar`.fixEnsure all glob patterns consistently use forward slashes (`/`) as path separators, even when running on Windows. Convert any `\` to `/` in the pattern string. -
TypeError: minimatch is not a function OR TypeError: minimatch is not iterable
cause Attempting to use `minimatch` as a default import (e.g., `import minimatch from 'minimatch'`) or without destructuring in CommonJS (e.g., `const minimatch = require('minimatch')`).fixUse a named import for ESM: `import { minimatch } from 'minimatch'`. For CommonJS, use destructuring: `const { minimatch } = require('minimatch')`.
Warnings
- gotcha Glob patterns derived from untrusted user input can lead to Regular Expression Denial of Service (ReDoS) attacks due to the library's reliance on JavaScript regular expressions. This is an inherent risk for any RegExp-based matcher.
- gotcha On Windows, glob expressions must exclusively use forward slashes ('/') as path separators. Backslashes ('\') will always be interpreted as escape characters within patterns, leading to incorrect matching.
- breaking This package requires Node.js version 18, 20, or greater than or equal to 22. Older Node.js versions are not supported.
- deprecated Future versions of minimatch may introduce a different matching algorithm to mitigate ReDoS. These improvements will NOT be backported to legacy versions. Any future ReDoS reports against older versions will be considered 'working as intended' due to inherent RegExp limitations.
Install
-
npm install minimatch -
yarn add minimatch -
pnpm add minimatch
Imports
- minimatch
import minimatch from 'minimatch'
import { minimatch } from 'minimatch' - minimatch (CJS)
const minimatch = require('minimatch')const { minimatch } = require('minimatch') - Minimatch class
import { minimatch } from 'minimatch'; new minimatch(...)import { Minimatch } from 'minimatch'
Quickstart
import { minimatch, Minimatch } from 'minimatch';
// Basic usage
console.log(minimatch('foo/bar/baz.js', 'foo/**/baz.js')); // true
console.log(minimatch('foo/bar/file.txt', '*.txt')); // false (needs path)
console.log(minimatch('file.txt', '*.txt')); // true
// With options: debug and nobrace
const pattern = 'a/{b,c}/d';
const options = { debug: false, nobrace: false };
console.log(minimatch('a/b/d', pattern, options)); // true
console.log(minimatch('a/c/d', pattern, options)); // true
// Using the Minimatch class for pre-compiled patterns
const mm = new Minimatch('src/**/*.ts', { matchBase: true });
console.log(mm.match('src/components/button.ts')); // true
console.log(mm.match('dist/index.js')); // false
// Example with Posix character classes (Unicode aware)
console.log(minimatch('é', '[[:alpha:]]')); // true
console.log(minimatch('123', '[[:digit:]]')); // false