Erik Waters Erik Waters - 5 months ago 21
Node.js Question

Node / statSync issues

I'm trying to log an array of numerically named sub-directories in "e:\subdirectory\" by using fs.statSynch but I keep getting the error "module.exports" is not a function; to my understanding, this is exactly how I'm supposed to export the data

I'm using the synchronous version because I want the array to finish populating before it's exported.

this is for a "proof of concept", I plan on serving an html doc and pushing this array to an input field

here is the code...

checke.js



var fs = require('fs');

function checkE() {
for (var i = 1, accts = [], path = "e:\\subdirectory\\"; i <10000; i++ ) {
var target = fs.statSynch(path + i.toString())
if (target.isDirectory()) { accts.push(i) }
}
}

module.exports(checkE)


init.js



var checke = require('./checke.js')

console.log(checke)

Answer

You need to assign something to module.exports not call it like a function. module.exports is an object

checke.js

var fs = require('fs');

module.exports = {
    checkE: function checkE() {
       var accts = [];
       var path = 'e:\\subdirectory\\';

       for (var i = 1; i <10000; i++ ) {
           var target = fs.statSync(path + i.toString())
           if (target.isDirectory())
               accts.push(i);
       }

       return accts;
   }
}

init.js

var checke = require('./checke.js');
checke.checkE();