猿问

node中require导入模块,但是没有定义命名,如何使用调用模块里的函数

require('module1') // 没有定义命名,如何使用?var module2 = require('module2') // 可以使用module2调用模块里的函数

批量导入模块

var models_path = __dirname + '/app/models'var walk = function(path) {
  fs
    .readdirSync(path)
    .forEach(function(file) {      var newPath = path + '/' + file      
    var stat = fs.statSync(newPath)      if (stat.isFile()) {        if (/(.*)\.(js|coffee)/.test(file)) {
          require(newPath)
        }
      }      else if (stat.isDirectory()) {
        walk(newPath)
      }
    })
}
walk(models_path)

这里导入的模块如何调用模块里的方法呢?如果这样无法使用模块那么这样可以批量导入并可以使用模块?


绝地无双
浏览 865回答 1
1回答
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答