Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我可以从FS导出模块吗?_Javascript_Node.js_Systemjs_Commonjs - Fatal编程技术网

Javascript 我可以从FS导出模块吗?

Javascript 我可以从FS导出模块吗?,javascript,node.js,systemjs,commonjs,Javascript,Node.js,Systemjs,Commonjs,在我的模型文件夹中我有很多文件,如 // Account.js module.exports = mongoose.model('Account', AccountSchema) ... // Rules.js module.exports = mongoose.model('Rules', RulesSchema) 在我的index.js文件中(同一文件夹/models)。 原因是要查找/models文件夹中的所有文件,并将其导出为命名导出 // index.js const mathJSF

在我的
模型
文件夹中我有很多文件,如

// Account.js
module.exports = mongoose.model('Account', AccountSchema)
...
// Rules.js
module.exports = mongoose.model('Rules', RulesSchema)
在我的
index.js
文件中(同一文件夹
/models
)。 原因是要查找
/models
文件夹中的所有文件,并将其导出为命名导出

// index.js
const mathJSFiles = /^[^index].*\.js$/gmi;
fs.readdirSync('.')
  .filter(file => file.search(mathJSFiles) >= 0)
  .forEach(file => {
    file = path.basename(file, '.js')
    exports[file] = require(join(models, file))
  })
所以在另一个文件
main.js
I中,我想这样做

import * as Models from './models'
Models.Account


这是可能的?

我建议您执行以下操作:

// main.js
var glob = require('glob')
  , path = require('path');

glob.sync('./models/**/*.js').forEach( function( file ) {
  require(path.resolve(file));
});

什么是
*作为
?您只是将ES6与Commonjs模块混合在一起吗?实际上,它应该使用普通的
constmodels=require('./models')
是的,对不起。我改变了我的答案。
// main.js
var glob = require('glob')
  , path = require('path');

glob.sync('./models/**/*.js').forEach( function( file ) {
  require(path.resolve(file));
});