我正在通過書Node.js the Right Way瞭解Node.js。我正在嘗試運行以下示例,以查看與.js
文件位於同一目錄中的名爲target.txt
的文件的更改。當看到文件被修改時,spawn ls ENOENT
"use strict";
const
fs = require('fs'),
spawn = require('child_process').spawn,
filename = process.argv[2];
if (!filename) {
throw Error("A file to watch must be specified!");
}
fs.watch(filename, function() {
let ls = spawn('ls', ['-lh', filename]);
ls.stdout.pipe(process.stdout);
});
console.log("Now watching " + filename + " for changes...");
我收到以下錯誤,當我更改文本文件並將其保存:
events.js:160 throw er; // Unhandled 'error' event ^
Error: spawn ls ENOENT at exports._errnoException (util.js:1018:11) at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32) at onErrorNT (internal/child_process.js:367:16) at _combinedTickCallback (internal/process/next_tick.js:80:11) at process._tickCallback (internal/process/next_tick.js:104:9)
Node.js的版本:v6.11.0
IDE:Visual Studio代碼1.13.1
OS:Windows 10 64x
對不起,我改變了'require'行。你可以用'cmd'替換「watch」部分嗎?我得到的TypeError說「產卵不是一個功能」。謝謝。 – Disasterkid
@Disasterkid我不確定我是否理解你的問題,但請看看答案上的編輯。 – Danziger
這導致'let dir = spawn('dir',[filename],{shell:true}); ^ TypeError:在FSWatcher中,spawn不是函數 。 (events.js:191:00)處的emitTwo(events.js:106:13) ((在C:\ Users \ Pedram \ Documents \ Node \ hello \ watcher- spawn.js:10:15 ) ) 7) at FSEvent.FSWatcher._handle.onchange(fs。js:1426:12)' –
Disasterkid