Search code examples
node.jsnode-glob

node glob pattern include all js except in certain folders


I have a project that looks like this

ls foo/
- file0.js
- a/file1.js
- b/file2.js
- c/file3.js
- d/file4.js

How do I write a glob pattern to exclude the c & d folder but get all other javascript files? I have looked here for an example, but cannot get anything to work.

I imagine the solution would look similar to this:

glob('+(**/*.js|!(c|d))', function(err, file) {
  return console.log(f);
});

I am wanting back

- file0.js
- a/file1.js
- b/file2.js

Solution

  • There is an ignore option I glazed over in the readme:

    glob('**/*.js', { ignore: '{c,d}/**' }, cb)
    

    This will exclude both c + d folders from the match. More here