[英]Export all files from a folder in es module
我有以下腳本,它試圖從特定文件夾中導出每個文件,但我被困在它的導出部分。
以下是我嘗試過的:
索引.js
function exportAllFiles() {
fs.readdir(filesDir, (err, files) => {
if (err) {
console.log(err);
return;
}
files.forEach(async file => {
const module = await import(file);
//'export module.default' ;export statement is not working for me here, how can I make it work here?
});
})
}
exportAllFiles();
如上所示導入每個文件后,我不知道如何導出它,因為導出語句在該塊中似乎對我不起作用。
任何想法將不勝感激。
謝謝
如果您將此代碼放入“index.js”,那么它會選擇該目錄中的任何其他 JS 模塊並將它們作為子模塊公開。
var fs = require('fs');
fs.readdirSync('./dir/path').forEach(function(file){
if ( file.indexOf(".js") > -1 && file != "index.js" )
exports[ file.replace('.js','') ] = require('./'+file);
});
請務必將./dir/path
替換為目錄的path
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.