Promise version of glob - wrapper around glob library providing Promise-based API
npx @tessl/cli install tessl/npm-glob-promise@6.0.0glob-promise provides a Promise-based wrapper around the popular glob library for Node.js, enabling asynchronous file pattern matching using shell-style wildcards and patterns. It transforms the callback-based glob API into a modern Promise-based interface, making it easier to integrate with async/await workflows and Promise chains.
npm install glob-promise globconst glob = require('glob-promise');For named imports:
const { promise, sync, hasMagic, Glob } = require('glob-promise');TypeScript:
import glob = require('glob-promise');const glob = require('glob-promise');
// Basic promise-based globbing
glob('src/**/*.js')
.then(files => {
console.log('Found files:', files);
})
.catch(err => {
console.error('Glob error:', err);
});
// With async/await
async function findFiles() {
try {
const files = await glob('**/*.md');
return files;
} catch (error) {
throw new Error(`Failed to find files: ${error.message}`);
}
}
// With options
const files = await glob('lib/**/*.js', {
ignore: ['**/node_modules/**'],
cwd: '/project/root'
});
// Using the callback-based glob function
const { glob: globCallback } = require('glob-promise');
globCallback('src/**/*.js', (err, files) => {
if (err) throw err;
console.log('Found files:', files);
});The main export provides Promise-based file pattern matching using glob patterns.
/**
* Promise-based glob function for file pattern matching
* @param pattern - Glob pattern string (e.g., "src/**/*.js")
* @param options - Optional glob options object
* @returns Promise that resolves to array of matched file paths
*/
function glob(pattern: string, options?: import('glob').IOptions): Promise<string[]>;Explicit named export of the main promise function.
/**
* Named export of the promise-based glob function
* @param pattern - Glob pattern string
* @param options - Optional glob options object
* @returns Promise that resolves to array of matched file paths
*/
function promise(pattern: string, options?: import('glob').IOptions): Promise<string[]>;Synchronous version of glob that returns results immediately.
/**
* Synchronous glob function that returns results immediately
* @param pattern - Glob pattern string
* @param options - Optional glob options object
* @returns Array of matched file paths
*/
function sync(pattern: string, options?: import('glob').IOptions): string[];Usage Example:
const { sync } = require('glob-promise');
const files = sync('test/**/*.js');
console.log('Test files:', files);Direct access to the original glob callback-based function via the glob export.
/**
* Original callback-based glob function from glob library
* @param pattern - Glob pattern string
* @param options - Optional glob options object
* @param callback - Callback function (err, matches) => void
*/
const { glob: globCallback } = require('glob-promise');
function globCallback(pattern: string, options?: import('glob').IOptions, callback?: (err: Error | null, matches: string[]) => void): void;
function globCallback(pattern: string, callback: (err: Error | null, matches: string[]) => void): void;Usage Example:
const { glob: globCallback } = require('glob-promise');
// With options
globCallback('src/**/*.js', { ignore: '**/node_modules/**' }, (err, matches) => {
if (err) throw err;
console.log('Found files:', matches);
});
// Without options
globCallback('test/**/*.js', (err, matches) => {
if (err) throw err;
console.log('Test files:', matches);
});Tests whether a pattern contains glob magic characters (wildcards, brackets, etc.).
/**
* Tests if a pattern contains glob magic characters
* @param pattern - Pattern string to test
* @param options - Optional glob options object
* @returns True if pattern contains magic characters, false otherwise
*/
function hasMagic(pattern: string, options?: import('glob').IOptions): boolean;Usage Example:
const { hasMagic } = require('glob-promise');
console.log(hasMagic('src/**/*.js')); // true
console.log(hasMagic('src/index.js')); // false
console.log(hasMagic('src/*.{js,ts}')); // trueDirect access to the underlying Glob class for advanced usage and streaming.
/**
* Glob class constructor for advanced usage and streaming
* @param pattern - Glob pattern string
* @param options - Optional glob options object
* @param callback - Optional callback function
*/
class Glob {
constructor(pattern: string, options?: import('glob').IOptions, callback?: (err: Error | null, matches: string[]) => void);
constructor(pattern: string, callback: (err: Error | null, matches: string[]) => void);
}Usage Example:
const { Glob } = require('glob-promise');
const g = new Glob('**/*.js', { ignore: '**/node_modules/**' }, (err, matches) => {
if (err) throw err;
console.log('Found files:', matches);
});
// The Glob class is an EventEmitter
g.on('match', (match) => {
console.log('Found:', match);
});/**
* Type definition for the promise-based glob function
*/
type GlobPromise = (pattern: string, options?: import('glob').IOptions) => Promise<string[]>;
/**
* Main export interface extending the promise function with utility exports
*/
interface Export extends GlobPromise {
readonly glob: typeof import('glob');
readonly Glob: typeof import('glob').Glob;
readonly hasMagic: typeof import('glob').hasMagic;
readonly sync: typeof import('glob').sync;
readonly promise: GlobPromise;
}The import('glob').IOptions interface contains all glob options including:
cwd?: string - Current working directory to search fromroot?: string - Root directory to mount the filesystemmark?: boolean - Add a / character to directory matchesnosort?: boolean - Don't sort the resultsdot?: boolean - Include .dot files in normal matchesnoglobstar?: boolean - Disable ** pattern expansionnocase?: boolean - Case insensitive matchingignore?: string | string[] - Glob patterns to ignorefollow?: boolean - Follow symbolic linkssilent?: boolean - Suppress warnings and errorsstrict?: boolean - Continue on errorsmaxLength?: number - Maximum number of file system callsThe promise-based functions will reject with an error in the following cases:
// Error handling example
try {
const files = await glob('**/*.js');
console.log('Files found:', files.length);
} catch (error) {
if (error.message === 'invalid pattern') {
console.error('Pattern must be a non-empty string');
} else {
console.error('Glob operation failed:', error.message);
}
}glob ^8.0.3 as a peer dependency