我查看了有关此错误的其他帖子,但找不到解决我的特定问题的帖子
我正在遵循《 Node.js 8正确的方式》一书中的示例,在该示例中,我编写了一个监视文件中更改的简单程序。
watcher-spawn.js
'use strict';
const fs = require('fs');
const spawn = require('child_process').spawn();
const filename = process.argv[2];
if(!filename)
throw Error('A file must be specified');
fs.watch(filename, ()=> {
const ls = spawn('ls', ['l','h',filename]);
ls.stdout.pipe(process.stdout)
});
我跑步时
node watcher-spawn.js target.txt
我收到错误
TypeError: "file" argument must be a non-empty string
at normalizeSpawnArguments (child_process.js:380:11)
at Object.exports.spawn (child_process.js:487:38)
at Object.<anonymous> (/Users/BorisGrunwald/Desktop/filesystem/watcher-spawn.js:4:40)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
at startup (bootstrap_node.js:158:16)
知道可能是什么问题吗?
答案 0 :(得分:3)
您正在执行spawn
函数,而不是在此行中要求它:
const spawn = require('child_process').spawn();
此行应为:
const spawn = require('child_process').spawn;