Retrieving files from Directory Node Js

前端 未结 2 609
刺人心
刺人心 2021-02-06 21:08

I am using readDirSync to get the files from a Diretory. PLease find the code and error as following.

var fs = require(\'fs\');
var files = fs.readdirSync(\'./ap         


        
2条回答
  •  猫巷女王i
    2021-02-06 21:11

    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]);
    }
    

提交回复
热议问题