Boris Grunwald
Boris Grunwald

Reputation: 2712

Node.js TypeError: "file" argument must be a non-empty string

I've looked at the other posts regarding this error, but can't find one that solves my particular problem

I am following an example from the book "Node.js 8 the right way" where I am making a simple program that watches for changes in a file.

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)

});

when I run

node watcher-spawn.js target.txt

I get the error

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)

Any idea what the problem could be?

Upvotes: 4

Views: 5859

Answers (1)

Thomas Dondorf
Thomas Dondorf

Reputation: 25230

You are executing the spawn function instead of requiring it in this line:

const spawn = require('child_process').spawn();

This line should be:

const spawn = require('child_process').spawn;

Upvotes: 5

Related Questions