2016-05-02 8 views
0

nodejsfsモジュール。ファイルが存在しない場合、fs.watchにエラーハンドラがありますか?

const fs = require('fs'); 
fs.watch('target.txt', function() { 
console.log("File 'target.txt' just changed!"); 
}); 

ファイルが存在しない場合、私は得る:

fs.js:1172 
    throw errnoException(err, 'watch'); 
     ^
Error: watch ENOENT 
    at exports._errnoException (util.js:746:11) 
    at FSWatcher.start (fs.js:1172:11) 

は、ファイルが存在しない場合にはそこerrコールバックではないでしょうか?ここでエラーを処理する適切な方法は何ですか?

+0

どのノードのバージョンを使用していますか? – amanpurohit

+0

@amanpurohit v0.12.6 – Jas

答えて

1

これをラップすると、既存のファイルが存在するかどうかを調べることができます。

if (fs.existsSync('target.txt')) { 
     fs.watch('target.txt', function() { 
      console.log("File 'target.txt' just changed!"); 
     }); 
    } 
関連する問題