Home > Back-end >  How to exclude node_modules from glob pattern
How to exclude node_modules from glob pattern

Time:11-04

I'm trying to build a script which will search for a specific text in my projects directory.

Here is an example of searching all the files in current directory, including node_modules.

const glob = require('glob')

async function searchFiles(directory) {
  return new Promise((resolve, reject) => {
    glob(`${directory}/**/*`, { ignore: ['./node_modules/*'] }, (error, files) => {
      if (error) reject(error)
      resolve(files)
    })
  })
}

async function start() {
  const files = await searchFiles('./').catch(error => console.log(error))
  console.log(files)
}

start()

The output I get in the console is the following

[
  './app.js',
  './code.js',
  './node_modules',
  './node_modules/balanced-match/index.js',
  './node_modules/balanced-match/LICENSE.md',
  './node_modules/balanced-match/package.json',
  './node_modules/balanced-match/README.md',
  './node_modules/bindings/bindings.js',
  './node_modules/bindings/LICENSE.md',
  './node_modules/bindings/package.json',
  './node_modules/bindings/README.md',
  './node_modules/brace-expansion/index.js',
  './node_modules/brace-expansion/LICENSE',
  './node_modules/brace-expansion/package.json',
  './node_modules/brace-expansion/README.md',
  './node_modules/fast-string-search/benchmark',
  './node_modules/fast-string-search/benchmark/benchmark.js',
  './node_modules/fast-string-search/binding.gyp',
  './node_modules/fast-string-search/build',
  './node_modules/fast-string-search/build/binding.sln',
  './node_modules/fast-string-search/build/config.gypi',
  './node_modules/fast-string-search/build/fast-string-search.vcxproj',
  './node_modules/fast-string-search/build/fast-string-search.vcxproj.filters',
  './node_modules/fast-string-search/build/Release',
  './node_modules/fast-string-search/build/Release/fast-string-search.exp',
  './node_modules/fast-string-search/build/Release/fast-string-search.iobj',
  './node_modules/fast-string-search/build/Release/fast-string-search.ipdb',
  './node_modules/fast-string-search/build/Release/fast-string-search.lib',
  './node_modules/fast-string-search/build/Release/fast-string-search.node',
  './node_modules/fast-string-search/build/Release/fast-string-search.pdb',
  './node_modules/fast-string-search/build/Release/obj',
  './node_modules/fast-string-search/build/Release/obj/fast-string-search',
  './node_modules/fast-string-search/build/Release/obj/fast-string-search/fast-str.B35216B5.tlog',
  './node_modules/fast-string-search/build/Release/obj/fast-string-search/fast-str.B35216B5.tlog/CL.command.1.tlog',
  './node_modules/fast-string-search/build/Release/obj/fast-string-search/fast-str.B35216B5.tlog/CL.read.1.tlog',
  './node_modules/fast-string-search/build/Release/obj/fast-string-search/fast-str.B35216B5.tlog/CL.write.1.tlog',
  './node_modules/fast-string-search/build/Release/obj/fast-string-search/fast-str.B35216B5.tlog/fast-string-search.lastbuildstate',
  './node_modules/fast-string-search/build/Release/obj/fast-string-search/fast-str.B35216B5.tlog/fast-string-search.write.1u.tlog',
  './node_modules/fast-string-search/build/Release/obj/fast-string-search/fast-str.B35216B5.tlog/link.command.1.tlog',
  './node_modules/fast-string-search/build/Release/obj/fast-string-search/fast-str.B35216B5.tlog/link.read.1.tlog',
  './node_modules/fast-string-search/build/Release/obj/fast-string-search/fast-str.B35216B5.tlog/link.write.1.tlog',
  './node_modules/fast-string-search/build/Release/obj/fast-string-search/fast-string-search.node.recipe',
  './node_modules/fast-string-search/build/Release/obj/fast-string-search/src',
  './node_modules/fast-string-search/build/Release/obj/fast-string-search/src/fast-string-search.obj',
  './node_modules/fast-string-search/build/Release/obj/fast-string-search/win_delay_load_hook.obj',
  './node_modules/fast-string-search/index.js',
  './node_modules/fast-string-search/LICENSE',
  './node_modules/fast-string-search/package.json',
  './node_modules/fast-string-search/README.md',
  './node_modules/fast-string-search/src',
  './node_modules/fast-string-search/src/fast-string-search.c',
  './node_modules/fast-string-search/test',
  './node_modules/fast-string-search/test/test.js',
  './node_modules/file-uri-to-path/History.md',
  './node_modules/file-uri-to-path/index.d.ts',
  './node_modules/file-uri-to-path/index.js',
  './node_modules/file-uri-to-path/LICENSE',
  './node_modules/file-uri-to-path/package.json',
  './node_modules/file-uri-to-path/README.md',
  './node_modules/file-uri-to-path/test',
  './node_modules/file-uri-to-path/test/test.js',
  './node_modules/file-uri-to-path/test/tests.json',
  './node_modules/fs.realpath/index.js',
  './node_modules/fs.realpath/LICENSE',
  './node_modules/fs.realpath/old.js',
  './node_modules/fs.realpath/package.json',
  './node_modules/fs.realpath/README.md',
  './node_modules/glob/common.js',
  './node_modules/glob/glob.js',
  './node_modules/glob/LICENSE',
  './node_modules/glob/package.json',
  './node_modules/glob/README.md',
  './node_modules/glob/sync.js',
  './node_modules/inflight/inflight.js',
  './node_modules/inflight/LICENSE',
  './node_modules/inflight/package.json',
  './node_modules/inflight/README.md',
  './node_modules/inherits/inherits_browser.js',
  './node_modules/inherits/inherits.js',
  './node_modules/inherits/LICENSE',
  './node_modules/inherits/package.json',
  './node_modules/inherits/README.md',
  './node_modules/minimatch/lib',
  './node_modules/minimatch/lib/path.js',
  './node_modules/minimatch/LICENSE',
  './node_modules/minimatch/minimatch.js',
  './node_modules/minimatch/package.json',
  './node_modules/minimatch/README.md',
  './node_modules/once/LICENSE',
  './node_modules/once/once.js',
  './node_modules/once/package.json',
  './node_modules/once/README.md',
  './node_modules/wrappy/LICENSE',
  './node_modules/wrappy/package.json',
  './node_modules/wrappy/README.md',
  './node_modules/wrappy/wrappy.js',
  './package-lock.json',
  './package.json'
]

How can i exclude node_modules from search pattern?

Tried searching but couldn't find the pattern that would exclude exclusively node_modules directory

any help is appreciated

CodePudding user response:

You are missing a double asterisk in your ignore pattern, so you should refactor your searchFiles this way:

async function searchFiles(directory) {
  return new Promise((resolve, reject) => {
    glob(`${directory}/**/*`, { ignore: ['./node_modules/**'] }, (error, files) => {
      if (error) reject(error)
      resolve(files)
    })
  })
}

the double asterisk (**) literally means "everything (files and folders) downwards"

  • Related