The glob matching library used internally by npm, converting glob expressions to JavaScript RegExp. Current version 10.x, ESM/CJS dual mode.
Use when the user needs glob matching, file pattern matching, or .gitignore-style matching.
bash
npm install minimatch
js
import { minimatch } from 'minimatch';
// or: const { minimatch } = require('minimatch');
minimatch('bar.foo', '*.foo'); // true
minimatch('bar.foo', '*.bar'); // false
minimatch('bar.foo', '*.+(bar|foo)', { debug: true }); // true + stderr debug output
{a,b}, {1..3}+(a|b), *(a|b), ?(a|b), @(a|b), !(a|b)** matches any number of directory levels[[:alpha:]] (full Unicode support, e.g., é)minimatch(path, pattern, options?)#Test whether a path matches a pattern:
js
minimatch('src/app.js', '*.js', { matchBase: true }); // true
minimatch.filter(pattern, options?)#Returns a filter function that can be passed to Array.filter:
js
const jsFiles = fileList.filter(minimatch.filter('*.js', { matchBase: true }));
minimatch.match(list, pattern, options?)#Performs fnmatch/glob style matching on a list of files. Returns the pattern itself when no matches are found and nonull: true:
js
minimatch.match(fileList, '*.js', { matchBase: true });
minimatch.escape(pattern)#Escapes all special characters in a glob pattern so it will match literal text only:
js
minimatch.escape('*.js'); // '\*.js'
minimatch.unescape(pattern)#Unescapes a pattern:
js
minimatch.unescape('\\*.js'); // '*.js'
minimatch.makeRe(pattern, options?)#Generates a RegExp object from a pattern:
js
const re = minimatch.makeRe('*.js');
re.test('foo.js'); // true
js
import { Minimatch } from 'minimatch';
const mm = new Minimatch('**/*.js', { dot: true });
mm.match('src/foo.js'); // true
mm.match('.hidden.js'); // true (because dot: true)
mm.hasMagic(); // true
mm.makeRe(); // Returns RegExp
mm.negate; // false — whether it's a negated ! pattern
mm.comment; // false — whether it's a # comment pattern
mm.matchOne(fileArray, patternArray, partial?)#Matches path components after splitting by /, primarily used by glob-walkers to reduce filesystem calls.
false)#OptionDescriptiondebugOutput debug information to stderrnobraceDisable {a,b} and {1..3} brace expansionnoglobstarDisable ** multi-level directory matchingdotAllow matching filenames starting with . (disabled by default)noextDisable extglob patterns such as +(a|b)``nocaseCase-insensitive matchingnocaseMagicOnlyOnly effective when nocase: true, makes case-insensitive only for parts containing magic charactersnonullWhen minimatch.match finds no matches, return [pattern] instead of []``magicalBracesAffects hasMagic(): treats braces without other magic characters as magicmatchBasePatterns without / match against the path basenamenocommentDisable comment patterns starting with #``nonegateDisable negated ! patternsflipNegateReverse the result of negated patterns (return false on match)partialPartial path matching, used when traversing directory trees to determine if a match is possiblewindowsPathsNoEscapeOn Windows, \ acts only as a path separator, not an escape characterwindowsNoMagicRootOn Windows + nocase, do NOT make UNC roots/drive letters case-insensitivepreserveMultipleSlashesPreserve consecutive / (default a///b matches a/b)optimizationLevelOptimization level 0/1/2 (default 1), see reference documentationplatformDefaults to process.platform, setting to 'win32' triggers Windows behavior
minimatch uses JavaScript regular expressions. Never pass user input as a pattern to this library —
If you build a system that takes user input and uses it directly as a regex pattern, whether with minimatch or any JS glob matcher, you will be pwned.
Future versions may switch to a non-backtracking matching algorithm, but this will not be backported.
/ in patterns — \ is treated as an escape character\ in paths is automatically matched against ///?/C:/..., //Server/Share/...) receive special handlingreferences/optimization.md