Nodemon: Add ability to watch a single file

Created on 2 May 2014  ·  20Comments  ·  Source: remy/nodemon

I might just be missing something, but it looks like I can't tell nodemon to watch a single file. I tried to configure nodemon to watch the following (I'm running nodemon through grunt-nodemon):

[
    "server",
    "!server/views",
    "app.js"
]

Here's is the grunt output:

Running "nodemon:dev" (nodemon) task
Verifying property nodemon.dev exists in config...OK
File: [no files]
Options: restartable="rs", verbose=false, watch=["server","!server/views","app.js"], env={"NODE_ENV":"development"}, ext="js json"

Which, in my particular case, results in:

[nodemon] watching 26,446 files - this might cause high cpu usage. To reduce use "--watch".

When I remove the app.js entry, the warning goes away. I believe the app.js rule ends up watching much more than just app.js.

I added console.log(JSON.stringify(config)); to lib/config/index.js just before ready(config) is called and this is the output:

{
   "run":false,
   "system":{
      "cwd":"/Users/aahardy/dev/totem",
      "useFind":true,
      "useWatch":false,
      "useWatchFile":false
   },
   "required":true,
   "dirs":[
      "/Users/aahardy/dev/totem/server",
      "/Users/aahardy/dev/totem"
   ],
   "timeout":1000,
   "options":{
      "restartable":"rs",
      "verbose":false,
      "watch":[
         "server",
         "!server/views",
         "app.js"
      ],
      "env":{
         "NODE_ENV":"development"
      },
      "ext":"js,json",
      "script":"bin/www",
      "ignore":[
         ".git",
         "node_modules/**/node_modules"
      ],
      "execMap":{
         "py":"python",
         "rb":"ruby"
      },
      "stdin":true,
      "stdout":true,
      "execOptions":{
         "script":"bin/www",
         "exec":"node",
         "ext":"js,json",
         "env":{
            "NODE_ENV":"development"
         },
         "execArgs":[

         ]
      },
      "monitor":[
         "/Users/aahardy/dev/totem/server/**/*",
         "!/Users/aahardy/dev/totem/server/views/**/*",
         "app.js",
         "!/Users/aahardy/dev/totem/.git/**/*",
         "!node_modules/**/node_modules"
      ]
   },
   "lastStarted":0,
   "loaded":[

   ],
   "command":{
      "raw":{
         "executable":"node",
         "args":[
            "bin/www"
         ]
      },
      "string":"node bin/www"
   }
}

The monitor value shows app.js instead of a full path. I looked at rulesToMonitor() in lib/monitor/match.js and it appears to somewhat ignore paths that aren't directories.

I would love to be able to watch single files correctly. Thanks!

bug

Most helpful comment

Any movement on this?

All 20 comments

That's weird, because adding app.js by itself should force it to watch the
individual file.

Can you paste in the output from tree from the directory you're running
nodemon from?

– Remy
On 2 May 2014 16:44, "Aaron Hardy" [email protected] wrote:

I might just be missing something, but it looks like I can't tell nodemon
to watch a single file. I tried to configure nodemon to watch the following
(I'm running nodemon through grunt-nodemon):

[
"server",
"!server/views",
"app.js"
]

Here's is the grunt output:

Running "nodemon:dev" (nodemon) task
Verifying property nodemon.dev exists in config...OK
File: [no files]
Options: restartable="rs", verbose=false, watch=["server","!server/views","app.js"], env={"NODE_ENV":"development"}, ext="js json"

Which, in my particular case, results in:

[nodemon] watching 26,446 files - this might cause high cpu usage. To reduce use "--watch".

When I remove the app.js entry, the warning goes away. I believe the
app.js rule ends up watching much more than just app.js.

I added console.log(JSON.stringify(config)); to lib/config/index.js just
before ready(config) is called and this is the output:

{
"run":false,
"system":{
"cwd":"/Users/aahardy/dev/totem",
"useFind":true,
"useWatch":false,
"useWatchFile":false
},
"required":true,
"dirs":[
"/Users/aahardy/dev/totem/server",
"/Users/aahardy/dev/totem"
],
"timeout":1000,
"options":{
"restartable":"rs",
"verbose":false,
"watch":[
"server",
"!server/views",
"app.js"
],
"env":{
"NODE_ENV":"development"
},
"ext":"js,json",
"script":"bin/www",
"ignore":[
".git",
"node_modules/**/node_modules"
],
"execMap":{
"py":"python",
"rb":"ruby"
},
"stdin":true,
"stdout":true,
"execOptions":{
"script":"bin/www",
"exec":"node",
"ext":"js,json",
"env":{
"NODE_ENV":"development"
},
"execArgs":[

     ]
  },
  "monitor":[
     "/Users/aahardy/dev/totem/server/**/*",
     "!/Users/aahardy/dev/totem/server/views/**/*",
     "app.js",
     "!/Users/aahardy/dev/totem/.git/**/*",
     "!node_modules/**/node_modules"
  ]

},
"lastStarted":0,
"loaded":[

],
"command":{
"raw":{
"executable":"node",
"args":[
"bin/www"
]
},
"string":"node bin/www"
}
}

The monitor value shows app.js instead of a full path. I looked at
rulesToMonitor() in lib/monitor/match.js and it appears to somewhat
ignore paths that aren't directories.

I would love to be able to watch single files correctly. Thanks!


Reply to this email directly or view it on GitHubhttps://github.com/remy/nodemon/issues/334
.

I've posted it here: http://aaronhardy.com/transfer/tree.txt
You might have to open it in an editor for it to look right.

Thanks for helping out. I have a decent workaround so don't pull any late nights over it.

Okay, got a lead on this issue, by including the app.js instead of watching the single file, nodemon is saying "hey, this is in the cwd, so let's watch everything inside that" which includes your full node_modules directory. Definitely a bug.

I had to write a utility to generate your tree file into a local directory structure too! https://github.com/remy/eert

Look at you! Thanks for your help. Again, don't rush anything for me. We're rolling fine with what we have.

Update: so the server watch path is causing the issue, though I'm unsure why. It's basically saying watch everything, which includes all your node_modules - which is odd, because it's supposed to be ignored...

Oh no. Ignore me.

Thanks Remy. I'll give this a whirl.

I'm getting this too, but I have a feeling I'm not using it properly.

❯ nodemon server.js --watch
22 May 15:12:46 - [nodemon] v1.0.17
22 May 15:12:46 - [nodemon] to restart at any time, enter `rs`
22 May 15:12:46 - [nodemon] watching: *.*
22 May 15:12:46 - [nodemon] starting `node server.js`
22 May 15:12:47 - [nodemon] watching 32,178 files - this might cause high cpu usage. To reduce use "--watch".

Why is it watching everything?

@kevinSuttle without the output from --dump I can't say why...

Sorry, I wasn't aware of that flag.

❯ nodemon server.js --dump
22 May 15:24:16 - [nodemon] v1.0.17
22 May 15:24:16 - [nodemon] to restart at any time, enter `rs`
22 May 15:24:16 - [nodemon] watching: *.*
--------------
node: v0.10.28
nodemon: v1.0.17
command: node /usr/local/bin/nodemon server.js --dump
cwd: /Users/kevinsuttle/Code/myApp
OS: darwin x64
--------------
{ run: false,
  system:
   { cwd: '/Users/kevinsuttle/Code/myApp',
     useFind: true,
     useWatch: false,
     useWatchFile: false },
  required: false,
  dirs: [ '/Users/kevinsuttle/Code/myApp' ],
  timeout: 1000,
  options:
   { scriptPosition: 0,
     dump: true,
     script: 'server.js',
     args: [],
     ignore:
      [ '.git',
        'node_modules/**/node_modules',
        re: /\.git|node_modules/.*.*/node_modules/ ],
     watch: [ '*.*', re: /.*\..*/ ],
     restartable: 'rs',
     execMap: { py: 'python', rb: 'ruby' },
     stdin: true,
     verbose: false,
     stdout: true,
     execOptions:
      { script: 'server.js',
        exec: 'node',
        nodeArgs: undefined,
        ext: 'js',
        env: {},
        execArgs: [] },
     ext: 'js',
     monitor:
      [ '*.*',
        '!/Users/kevinsuttle/Code/myapp/.git/**/*',
        '!node_modules/**/node_modules' ] },
  load: [Function],
  reset: [Function: reset],
  lastStarted: 0,
  loaded: [],
  command:
   { raw: { executable: 'node', args: [ 'server.js' ] },
     string: 'node server.js' } }
--------------

@kevinSuttle can you update nodemon to latest? 1.0.19? You're running on mac, so this is a different issue I think.

Same situation. :/

❯ nodemon server.js --watch
22 May 15:31:45 - [nodemon] v1.0.19
22 May 15:31:45 - [nodemon] to restart at any time, enter `rs`
22 May 15:31:45 - [nodemon] watching: *.*
22 May 15:31:45 - [nodemon] starting `node server.js`
22 May 15:31:47 - [nodemon] watching 57,782 files - this might cause high cpu usage. To reduce use "--watch".
^C

❯ nodemon server.js --dump
22 May 15:32:08 - [nodemon] v1.0.19
22 May 15:32:08 - [nodemon] to restart at any time, enter `rs`
22 May 15:32:08 - [nodemon] watching: *.*
--------------
node: v0.10.28
nodemon: v1.0.19
command: node /usr/local/bin/nodemon server.js --dump
cwd: /Users/kevinsuttle/Code/myApp
OS: darwin x64
--------------
{ run: false,
  system:
   { cwd: '/Users/kevinsuttle/Code/myApp',
     useFind: true,
     useWatch: false,
     useWatchFile: false },
  required: false,
  dirs: [ '/Users/kevinsuttle/Code/myApp' ],
  timeout: 1000,
  options:
   { scriptPosition: 0,
     dump: true,
     script: 'server.js',
     args: [],
     ignore:
      [ '.git',
        'node_modules/**/node_modules',
        re: /\.git|node_modules/.*.*/node_modules/ ],
     watch: [ '*.*', re: /.*\..*/ ],
     restartable: 'rs',
     execMap: { py: 'python', rb: 'ruby' },
     stdin: true,
     verbose: false,
     stdout: true,
     execOptions:
      { script: 'server.js',
        exec: 'node',
        args: [],
        nodeArgs: undefined,
        ext: 'js',
        env: {},
        execArgs: [] },
     ext: 'js',
     monitor:
      [ '*.*',
        '!/Users/kevinsuttle/Code/myApp/.git/**/*',
        '!node_modules/**/node_modules' ] },
  load: [Function],
  reset: [Function: reset],
  lastStarted: 0,
  loaded: [],
  command:
   { raw: { executable: 'node', args: [ 'server.js' ] },
     string: 'node server.js' } }
--------------

@kevinSuttle okay, looking at this config, it says that it's running server.js which is in myApp, which is being monitored for .js. If you run find . | wc in the myApp directory, what kind of numbers do you get? (though this will include the node_modules/**/node_modules directories).

I'm hitting the same issue.

"ignore": ["**/node_modules/*", "**/.git/*"]

seems to help.

@remy

❯ find . | wc
   80956   81081 12707823

@realyze Where do you place this? (I'm using gulp-nodemon btw)

Same issue here.

I personally still see this issue even with newest nodemon 1.17.5. If I tell it to watch one single file, w/ verbose mode it tells me it is watching 3000 files:

nodemon --verbose --inspect=6860 ./dist/app.js --watch

watches 5000+ files

nodemon --verbose --inspect=6860 ./dist/app.js --watch ./dist/app.js

watches 3000+ files.

I have tried removing configuration from nodemon.json to make sure sure ext isn't competing here, with same results. It must be looking in the dist/ directory completely, not just the dist/app.js file

@remy do you want me to open this as a separate issue since this one is so old?

Any movement on this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Mohammad-Quanit picture Mohammad-Quanit  ·  5Comments

robboerman picture robboerman  ·  3Comments

olalonde picture olalonde  ·  3Comments

giacomorebonato picture giacomorebonato  ·  5Comments

Bastorx picture Bastorx  ·  5Comments