Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell nodemon to ignore all node_modules except for one directory

Tags:

nodemon

I have a Node.js server I am developing and I'm using nodemon to have the server restarted when the source code changes.

This server has a dependency on another module, which I am also developing, so it would be nice if changes to that module also restarted the server.

Best attempt yet (Thanks, bitstrider!)

I made a repo to reproduce this problem with minimal code if anyone wants to check it out: https://github.com/shawninder/nodemon-troubles

The highlights are in nodemon.json:

{
  "ignoreRoot": [
    ".git",
    ".nyc_output",
    ".sass-cache",
    "bower-components",
    "coverage"
  ],
  "ignore": [
    "node_modules/!(is-node)",
    "node_modules/is-node/node_modules"
  ]
}
  • ignoreRoot is the same as the defaults, but without node_modules.
  • ignore is where I'm trying to tell it to ignore node_modules, but not is-node.

This almost works (editing is-node restarts the server) but it's watching all the node modules, not only is-node.

How can I tell nodemon to ignore all node modules except for one?

Details

  • I'm using the latest version of nodemon, 1.11.0
  • I also tried with https://github.com/remy/nodemon/pull/922

Explaining the problem

Nodemon takes the ignores and combines them into one big regular expression before passing this along to chokidar. It's a custom parsing algorithm based on regular expressions and supports neither glob negation nor regular expression syntax. For example, the example above produces the following regular expression:

/\.git|\.nyc_output|\.sass\-cache|bower\-components|coverage|node_modules\/!\(is\-node\)\/.*.*\/.*|node_modules\/is\-node\/node_modules\/.*.*\/.*/

Notice the negation character ! end up as a literal ! in the resulting regular expression. Using negative look-ahead syntax also doesn't work because the control characters are transformed into literal characters.

like image 656
Shawn Avatar asked Oct 18 '22 14:10

Shawn


1 Answers

Since nodemon uses minimatch for matching, you should be able to use negation with the symbol !, according to the documentation.

So in your case, try:

{
  "ignore" : "node_modules/!(my-watched-module)/**/*"
}

This should ignore all module files except those within my-watched-module

NOTE: The ignore rule here only targets files, not directories. This behavior makes sense in the context of nodemon because its watching for file changes.

like image 89
strider Avatar answered Oct 21 '22 07:10

strider