Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

fs.watchFile not working on windows #1358

Closed
Bonuspunkt opened this Issue · 24 comments
@Bonuspunkt

official node.exe

D:\node\bin>node -v
v0.5.1
D:\node\bin>node --eval "require('fs').watchFile('node.exe', function() {})"

fs.js:611
    statWatchers[filename] = new binding.StatWatcher();
                             ^
TypeError: undefined is not a function
    at CALL_NON_FUNCTION_AS_CONSTRUCTOR (native)
    at Object.watchFile (fs.js:611:30)
    at Object. (eval at  (eval:1:90))
    at Object. (eval:1:78)
    at Module._compile (module.js:411:26)
    at startup (node.js:90:23)
    at node.js:474:3
@asciidisco

Same experience.
Using Windows XP Service Pack 3...

@WeweTom

win7 64 sucks too

@krisnye

Same problem.

@patriksimek

Same here, W7 64

PS C:\node> .\node -v
v0.5.4
PS C:\node> .\node --eval "require('fs').watchFile('node.exe', function() {})"

fs.js:593
  this._handle = new binding.StatWatcher();
                 ^
TypeError: undefined is not a function
    at new StatWatcher (fs.js:593:18)
    at Object.watchFile (fs.js:642:30)
    at Object. (eval at  (eval:1:75))
    at Object. (eval:1:63)
    at Module._compile (module.js:416:26)
    at startup (node.js:91:14)
    at node.js:511:3
@luv2code

same problem: W7 x64

C:\> node -v
v0.5.6

C:\> node --eval "require('fs').watchFile('node.exe', function() {})"

fs.js:596
  this._handle = new binding.StatWatcher();
                 ^
TypeError: undefined is not a function
    at new StatWatcher (fs.js:596:18)
    at Object.watchFile (fs.js:645:30)
    at Object.<anonymous> (eval at <anonymous> (eval:1:75))
    at Object.<anonymous> (eval:1:63)
    at Module._compile (module.js:416:26)
    at startup (node.js:91:14)
    at node.js:512:3

C:\>
@indutny
Owner

This feature atm works only on unix-based systems.

@luv2code

It should work on windows systems too. windows has support for file watchers. just needs to be implemented in nodejs.

@deemstone

not resolved yet? I just got the same problem.

@blackhunter

v0.4+ working well!

@luv2code

@blackhunter how did you get v0.4 to run on windows? I thought windows compatibility wasn't introduced until v0.5.

also, where do you hunt blacks?

@halt-hammerzeit

Also having this on Windows with official Node 0.5
I've tried the link above, but child_process.spawn('node', 'path') doesn't work since 'path' is resolved like in Linux.

@elis

Same problem here.
Using version 0.5.8

Call to fs.watchFile dumps the following error:

  this._handle = new binding.StatWatcher();
                 ^
TypeError: undefined is not a function
    at new StatWatcher (fs.js:596:18)
    at Object.watchFile (fs.js:648:37)
    at Object.C360Module [as Module] (d:\dev\Nodejs\c360\server\company360.js:355:8)
    at Object.requestAsync (d:\dev\Nodejs\c360\server\company360.js:83:20)
    at Object.<anonymous> (d:\dev\Nodejs\c360\server\server.js:48:15)
    at Server.<anonymous> (d:\dev\Nodejs\c360\server\server.js:171:24)
    at Server.emit (events.js:70:17)
    at HTTPParser.onIncoming (http2.js:1451:12)
    at HTTPParser.onHeadersComplete (http2.js:108:31)
    at Socket.ondata (http2.js:1347:22)```
@Bonuspunkt
require('fs').watch('file.txt', function(){console.log('x'); });

works for me with v0.5.8 (W7 x64)

@halt-hammerzeit

I've downloaded Node 0.5.8, and now it seemed to work, but I have this error:

fs.js:596
  this._handle = new binding.StatWatcher();
                 ^
TypeError: undefined is not a function
    at new StatWatcher (fs.js:596:18)
    at Object.watchFile (fs.js:648:37)
    at C:\work\sociopathy\code\develop.js:63:20
    at Array.forEach (native)
    at C:\work\sociopathy\code\develop.js:61:19
    at ChildProcess.exithandler (child_process_uv.js:147:7)
    at ChildProcess.emit (events.js:70:17)
    at maybeExit (child_process_uv.js:227:16)
    at Process.onexit (child_process_uv.js:262:5)

The code is:

    "watchFiles": function() {
        var that = this;

        child_process.exec('dir /s /b "*.js" "*.coffee"', function(error, stdout, stderr) {
            var files = stdout.trim().split("\n");

            files.forEach(function(file) {
                that.files.push(file);
                fs.watchFile(file, {interval : 500}, function(curr, prev) {
                    if (curr.mtime.valueOf() != prev.mtime.valueOf() || curr.ctime.valueOf() != prev.ctime.valueOf()) {
                        sys.debug('DEVSERVER: Restarting because of changed file at ' + file);
                        dev_server.restart();
                    }
                });
            });
        });
@Bonuspunkt

yeah
v0.5.8 fs.watchFile throws

Error: use fs.watch api instead

seems like fs.watch isn't yet in the documentation

@Bonuspunkt Bonuspunkt closed this
@nakedslavin

just tried it now, its a bit different from what we used to, but works.

fs.watch([full or local path], function(action,fileName) {
   fs.stat(fileName,function(err,stats) {
        console.log(stats.mtime);
    });
});
@halt-hammerzeit

Just tried 0.5.9 - no effect:

DEBUG: DEVSERVER: Starting server

fs.js:596
  this._handle = new binding.StatWatcher();
                 ^
TypeError: undefined is not a function
    at new StatWatcher (fs.js:596:18)
    at Object.watchFile (fs.js:648:37)
    at C:\work\sociopathy\code\develop.js:63:20
    at Array.forEach (native)
    at C:\work\sociopathy\code\develop.js:61:19
    at ChildProcess.exithandler (child_process_uv.js:147:7)
    at ChildProcess.emit (events.js:70:17)
    at maybeExit (child_process_uv.js:227:16)
    at Process.onexit (child_process_uv.js:262:5)
Для продолжения нажмите любую клавишу . . .
@bnoordhuis
Owner

@kuchumovn: fs.watchFile() will be gone before 0.6.0, it's superseded by fs.watch().

@halt-hammerzeit

oh, really, I didn't read the code other people posted, so I didn't notice that.
now it says:

DEBUG: DEVSERVER: Starting server

fs.js:625
    throw errnoException(errno, 'watch');
          ^
Error: watch UNKNOWN
    at errnoException (fs.js:598:11)
    at FSWatcher.start (fs.js:625:11)
    at Object.watch (fs.js:653:11)
    at C:\work\sociopathy\code\develop.js:63:20
    at Array.forEach (native)
    at C:\work\sociopathy\code\develop.js:61:19
    at ChildProcess.exithandler (child_process_uv.js:254:7)
    at ChildProcess.emit (events.js:70:17)
    at maybeExit (child_process_uv.js:335:16)
    at Process.onexit (child_process_uv.js:370:5)
@halt-hammerzeit

I've made a little research, and found out that node.exe doesn't support windows paths here:

var fs = require('fs')

fs.watch('c:\work\test.txt', function(action, fileName) 
{
    fs.stat(fileName, function(error, stats) 
    {
        console.log(stats.mtime)
    })
})

outputs "watch UNKNOWN", while this works:

var fs = require('fs')

fs.watch('test.txt', function(action, fileName) 
{
    fs.stat(fileName, function(error, stats) 
    {
        console.log(stats.mtime)
    })
})

And this outputs "watch ENOENT":

var fs = require('fs')

fs.watch('./test.txt', function(action, fileName) 
{
    fs.stat(fileName, function(error, stats) 
    {
        console.log(stats.mtime)
    })
})

So, we need it to work with windows-style paths now.

@bnoordhuis
Owner

It's the escape sequence in your path: 'c:\work\test.txt' becomes 'c:work\test.txt' (try it in the REPL).

The UNKNOWN error message is a pending issue.

@nakedslavin

@kuchumovn it's not a node issue. just use double bslash:

fs.watch('c:\\work\\test.txt', function(action, fileName) { }
@halt-hammerzeit

Thanks to all of you.
I finally made it work.
Here is the Windows script to make Node.js refresh the application code "on the fly" (when the files get modified).
https://github.com/kuchumovn/node-js-development-mode

I've adapted it from this Linux script:
http://dracoblue.net/dev/hot-reload-for-nodejs-servers-on-code-change/173/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.