Micromatch Glob Matching Utility
Micromatch is a comprehensive and highly performant JavaScript utility for glob matching, serving as a faster and more feature-rich alternative to older libraries like minimatch and multimatch. The current stable version is 4.0.8, actively maintained with regular updates to address bug fixes and security vulnerabilities. Key differentiators include its speed and extensive support for the Bash 4.3 specification, often surpassing Bash itself in terms of adherence to its glob test suite. It is widely adopted across the JavaScript ecosystem for file system operations, build tooling, and configuration management, providing a robust solution for pattern matching in both Node.js and browser environments.
Common errors
-
npm ERR! code ERESOLVE
cause Dependency conflict during installation, often due to peer dependency mismatches or incompatible package versions.fixTry installing with `--legacy-peer-deps` (`npm install micromatch --legacy-peer-deps`) or `--force` (`npm install micromatch --force`) as a temporary workaround, then investigate the underlying dependency tree to resolve conflicts properly. -
Error: Cannot find module 'micromatch'
cause The `micromatch` package was not installed or resolved correctly in your project.fixEnsure `micromatch` is listed in your `package.json` and run `npm install` or `yarn install`. Verify the package exists in your `node_modules` directory and that your import path is correct (e.g., `import micromatch from 'micromatch';`). -
TypeError: micromatch is not a function (for .isMatch or other methods)
cause Incorrect import statement when using ESM, or attempting to destructure methods from the default import incorrectly. `micromatch` is the default export, and its utility methods are properties of that default export.fixUse `import micromatch from 'micromatch';` and then call methods as `micromatch.isMatch(...)` or `micromatch.not(...)`. Do not attempt `import { isMatch } from 'micromatch';` as these are not named exports. -
Patterns with backslashes behave unexpectedly or don't match correctly.
cause From v3 onwards, micromatch treats backslashes as escape characters, not path separators. This is a change from v2 behavior.fixAlways use forward slashes (`/`) as path separators in your glob patterns, even on Windows. If you need to match a literal backslash, you must escape it (e.g., `\\`).
Warnings
- breaking Micromatch v4.x introduced several breaking changes from v3.x, including a minimum Node.js requirement of `>=8.6`. The `micromatch.braces()` method no longer accepts an array of patterns, `strictErrors` was replaced by `strictBrackets=true`, and caching options/methods were removed.
- breaking Upgrading from micromatch v2.x to v3.x changed how backslashes are handled. Previously, backslashes were converted to forward slashes; now they are respected as escape characters per Bash spec. This can alter matching behavior for existing patterns using backslashes.
- gotcha Micromatch 4.0.8 fixes CVE-2024-4067 and CVE-2024-4068, which are Regular Expression Denial of Service (ReDoS) vulnerabilities in the `micromatch.braces()` function. Maliciously crafted patterns with greedy regex or unclosed brace patterns could lead to catastrophic backtracking, causing severe performance degradation or application unresponsiveness.
- gotcha Due to parser accuracy improvements in v4, some previously 'invalid' glob patterns that might have worked by coincidence in older versions may now behave differently or fail. This is considered a correction, not a regression.
Install
-
npm install micromatch -
yarn add micromatch -
pnpm add micromatch
Imports
- micromatch
const micromatch = require('micromatch');import micromatch from 'micromatch';
- micromatch.isMatch
import { isMatch } from 'micromatch';import micromatch from 'micromatch'; micromatch.isMatch('file.js', '*.js'); - micromatch (TypeScript types)
import * as micromatch from 'micromatch'; // While sometimes works, less idiomatic for default exports
import micromatch from 'micromatch'; // Type definitions are available via @types/micromatch
Quickstart
import micromatch from 'micromatch';
// Basic usage: filter a list of strings against glob patterns
const files = ['foo.txt', 'bar.js', 'baz.md', 'qux.ts', 'image.png'];
const jsAndMdFiles = micromatch(files, ['*.js', '*.md']);
console.log('Matching .js and .md files:', jsAndMdFiles); // => ['bar.js', 'baz.md']
// Using negation to exclude patterns
const allButJsFiles = micromatch(files, ['*', '!*.js']);
console.log('All files except .js:', allButJsFiles); // => ['foo.txt', 'baz.md', 'qux.ts', 'image.png']
// Boolean matching for a single string
const isSourceFile = micromatch.isMatch('src/index.ts', ['src/**/*.ts', 'src/**/*.tsx']);
console.log('Is src/index.ts a source file?', isSourceFile); // => true
const isConfigFile = micromatch.isMatch('config.json', '**/config.json');
console.log('Is config.json a config file?', isConfigFile); // => true