In my models
folder I have a lot of files like ..
// Account.js
module.exports = mongoose.model('Account', AccountSchema)
...
// Rules.js
module.exports = mongoose.model('Rules', RulesSchema)
And in my index.js
file ( same folder ./models
). The reason is to lookup all files in ./models
folder, and export as named export
// 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))
})
So in another file main.js
I want to do like that...
import * as Models from './models'
Models.Account
Or
import { Account } from './models'
This is possible?
I would recommend doing something like the following:
// main.js
var glob = require('glob')
, path = require('path');
glob.sync('./models/**/*.js').forEach( function( file ) {
require(path.resolve(file));
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.