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

3

7

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

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.

Indistinguishable answered 3/4, 2017 at 16:6 Comment(0)
T
3

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.

Trisoctahedron answered 3/4, 2017 at 16:24 Comment(6)
Should but doesn't. In my latest attempt I am using the following in nodemon.json: {"ignoreRoot: [".git", ".nyc_output", ".sass-cache", "bower-components", "coverage"], "ignore": ["node_modules/!(my-watched-module)", "node_modules/my-watched-module/node_modules"]} It's correctly watching my-watched-module and not watching my-watched-module's node_modules, but it's still watching other node modules...Indistinguishable
I edited my question with a link to a repo that reproduces the problem if you're interestedIndistinguishable
It's pretty strange since ignoreRoot clearly blacklists directories...Indistinguishable
Your welcome! And yeah I agree, it would be more intuitive if ignore applied to directories. But hey at least you're free to fork and add that featureTrisoctahedron
Damn it, this doesn't work in the non-simplified context. I will post again when I manage to reproduce the actual problemIndistinguishable
I think I found out why this isn't working so I modified my question to add this inIndistinguishable
L
0

I was able to solve this problem by using NPM's built-in workspaces feature.

  1. Update NPM to the latest version. NPM must be on at least version 7.x to use workspaces.
  2. If a ./node_modules directory doesn't already exist, create it in your project's root folder.
  3. Create a directory outside of ./node_modules (Nodemon ignores this directory by default) to house your under-development dependency module(s).

For example, assuming Nodmon is running in the same folder as your project's package.json file, you could create a new folder named, ./node_modules_custom, in the same directory.

  1. Give each module ① its own package.json file and ② its own folder. NPM will rely on these package.json files to distinguish between your module(s) and anything else that may exist in the parent directory.

    My_Example_Node_Project/
    ├─ package.json/
    ├─ node_modules_custom/
    │  ├─ my_custom_module_1/
    │  │  ├─ package.json
    │  │  ├─ non_standard_main_file.js
    │  ├─ my_custom_module_2/
    │  │  ├─ package.json
    │  │  ├─ index.js
    ├─ node_modules/
    ├─ index.js
    ├─ .git/
    ├─ .gitignore
    ├─ .gitattributes
    
  2. Designate the folder containing your under-development dependency modules as a workspace by adding the following three lines to package.json, where node_modules_custom is the name of the directory created in Step 3.

    "workspaces": [
      "node_modules_custom/*"
    ]
    

    Your full package.json file should now look something like this:

    {
      "name": "my_example_node_project",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "repository": {
        "type": "git",
        "url": "git+https://github.com/USER/my_example_node_project.git"
      },
      "author": "",
      "license": "ISC",
      "bugs": {
        "url": "https://github.com/USER/my_example_node_project/issues"
      },
      "homepage": "https://github.com/USER/my_example_node_project#readme",
      "workspaces": [
        "node_modules_custom/*"
      ]
    }
    
  3. Run npm install in your project's root directory. NPM will scan-through any directories designated as a workspaces (Step 5) and create symbolic links for the module folders therein.

  4. If the process was completed successfully ./node_modules will now contain your under-development dependency module folders as symbolic links. When the contents of any of these folders is altered, Nodemon will automatically detect it and update accordingly. Any non-symbolically-linked files or folders will continue to be ignored.

Loewi answered 19/8, 2021 at 22:6 Comment(0)
C
0

To watch a specific dir under node_modules while continuing to ignore the rest of node_modules, add the negation flag to the node_modules path in ignoreRoot. Specifying ignoreRoot overrides the default, so include the other files that are part of the default.

{
  "watch": ["src", "node_modules/(package_to_watch)"],
  "ignoreRoot": [
    ".git",
    ".log",
    ".nyc_output",
    ".sass-cache",
    ".yarn",
    "bower_components",
    "coverage",
    "node_modules/!(package_to_watch)"
  ]
}

Thanks @strider for cluing me in on the negation flag.

Calque answered 3/6, 2022 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.