How to include multiple file extensions with 'glob.sync' in NodeJS?

21.6k Views Asked by At

Can anyone please suggest how to add multiple file extensions with the glob.sync method.

Something like:

const glob = require('glob');
let files = glob.sync(path + '**/*.(html|xhtml)');

Thank you :)

1

There are 1 best solutions below

2
On

You can use this (which most shells support as well):

glob.sync(path + '**/*.{html,xhtml}')

Or this one:

glob.sync(path + '**/*.@(html|xhtml)')

EDIT: I initially also suggested this pattern:

glob.sync(path + '**/*.+(html|xhtml)')

However, this will also match files that have .htmlhtml as extension (plus any other combination of html and xhtml, in single or multiple occurrences), which is incorrect.