This repository has been archived by the owner. It is now read-only.

fs.watchFile not working on windows #1358

Closed
Bonuspunkt opened this Issue Jul 18, 2011 · 24 comments

Comments

Projects
None yet
@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

This comment has been minimized.

Show comment
Hide comment
@asciidisco

asciidisco Jul 22, 2011

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

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

@jinwei233

This comment has been minimized.

Show comment
Hide comment
@jinwei233

jinwei233 Jul 24, 2011

win7 64 sucks too

win7 64 sucks too

@krisnye

This comment has been minimized.

Show comment
Hide comment
@krisnye

krisnye Aug 19, 2011

Same problem.

krisnye commented Aug 19, 2011

Same problem.

@patriksimek

This comment has been minimized.

Show comment
Hide comment
@patriksimek

patriksimek Aug 23, 2011

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

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

This comment has been minimized.

Show comment
Hide comment
@luv2code

luv2code Sep 11, 2011

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:\>

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

This comment has been minimized.

Show comment
Hide comment
@indutny

indutny Sep 11, 2011

Member

This feature atm works only on unix-based systems.

Member

indutny commented Sep 11, 2011

This feature atm works only on unix-based systems.

@luv2code

This comment has been minimized.

Show comment
Hide comment
@luv2code

luv2code Sep 12, 2011

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

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

@deemstone

This comment has been minimized.

Show comment
Hide comment
@deemstone

deemstone Sep 29, 2011

not resolved yet? I just got the same problem.

not resolved yet? I just got the same problem.

@blackhunter

This comment has been minimized.

Show comment
Hide comment
@blackhunter

blackhunter Sep 29, 2011

v0.4+ working well!

v0.4+ working well!

@luv2code

This comment has been minimized.

Show comment
Hide comment
@luv2code

luv2code Sep 30, 2011

@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?

@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?

@catamphetamine

This comment has been minimized.

Show comment
Hide comment
@catamphetamine

catamphetamine Oct 1, 2011

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.

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

This comment has been minimized.

Show comment
Hide comment
@elis

elis Oct 1, 2011

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)```

elis commented Oct 1, 2011

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

This comment has been minimized.

Show comment
Hide comment
@Bonuspunkt

Bonuspunkt Oct 2, 2011

require('fs').watch('file.txt', function(){console.log('x'); });

works for me with v0.5.8 (W7 x64)

require('fs').watch('file.txt', function(){console.log('x'); });

works for me with v0.5.8 (W7 x64)

@catamphetamine

This comment has been minimized.

Show comment
Hide comment
@catamphetamine

catamphetamine Oct 2, 2011

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();
                    }
                });
            });
        });

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

This comment has been minimized.

Show comment
Hide comment
@Bonuspunkt

Bonuspunkt Oct 2, 2011

yeah
v0.5.8 fs.watchFile throws

Error: use fs.watch api instead

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

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 Oct 2, 2011

@nakedslavin

This comment has been minimized.

Show comment
Hide comment
@nakedslavin

nakedslavin Oct 14, 2011

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);
    });
});

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);
    });
});
@catamphetamine

This comment has been minimized.

Show comment
Hide comment
@catamphetamine

catamphetamine Oct 14, 2011

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)
Для продолжения нажмите любую клавишу . . .

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

This comment has been minimized.

Show comment
Hide comment
@bnoordhuis

bnoordhuis Oct 14, 2011

Member

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

Member

bnoordhuis commented Oct 14, 2011

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

@catamphetamine

This comment has been minimized.

Show comment
Hide comment
@catamphetamine

catamphetamine Oct 14, 2011

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)

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)
@catamphetamine

This comment has been minimized.

Show comment
Hide comment
@catamphetamine

catamphetamine Oct 14, 2011

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.

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

This comment has been minimized.

Show comment
Hide comment
@bnoordhuis

bnoordhuis Oct 14, 2011

Member

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.

Member

bnoordhuis commented Oct 14, 2011

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

This comment has been minimized.

Show comment
Hide comment
@nakedslavin

nakedslavin Oct 14, 2011

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

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

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

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

This comment has been minimized.

Show comment
Hide comment
@catamphetamine

catamphetamine Oct 14, 2011

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/

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 subscribe to this conversation on GitHub. Already have an account? Sign in.