David J.
David J.

Reputation: 1913

why isn't this simple node program printing contents of current directory?

I have a folder with a few js files in it:

admin$ ls

filterfiles.js  filterfiles.js~ program.js  program.js~

program.js is a node program with the following contents:

var dir = process.argv[2]

var fs = require('fs')
fs.readdir(dir, function(results){console.log(results)})

When I do the following, why do I get null, instead of a list of the files in the directory?

admin$ node program.js './' 
null

Upvotes: 0

Views: 57

Answers (1)

Diasiare
Diasiare

Reputation: 755

The first argument of the callback for fs.readdir is the error, the result is in argument 2. This is standard practice for node callbacks.

You want:

fs.readdir(dir, function(err,results){console.log(results)})

Upvotes: 5

Related Questions