Retrieving files from Directory Node Js

If you are using relative path when calling readdirSync, make sure it is relative to process.cwd().
However, “require” should be relative to the current script.

For example, given the following structure

server.js (node process)
/lib/importer.js (the current script)
/lib/application/models/

you may need to write importer.js as:

var fs = require('fs');
var files = fs.readdirSync('./lib/application/models/');
for (var i in files) {
  var definition = require('./application/models/' + files[i]).Model;
  console.log('Model Loaded: ' + files[i]);
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)