所以,最近我一直遇到麻烦。使用名为Enmap
的增强Map模块,我可以将模块加载到其中并访问和使用条目。
我正在加载的模块:
export const name = 'foo';
export const run = async (bar, baz) => {
};
这就是我将其登录到控制台时的显示方式:
[Module] {
name: 'foo',
run: [AsyncFunction: run]
}
现在,这就是我检索这些并将它们加载到enmap中的方式:
// Loading them in main.js
import Enmap from 'enmap';
import { readdirSync } from 'fs';
const myEnmap = new Enmap();
const files = readdirSync('./folder/');
// No await in a .forEach() function
for (let i = 0; i < files.length; i++){
const file = await import(`./folder/${files[i]}`);
myEnmap.set(file.name, file);
}
// Retrieving in otherFile.js
// Let's assume I have it imported here as well
const input = userInputHere;
const entry = myEnmap.get(input);
// Check to see if the user input is a name of one of the modules,
// and if not, doing nothing
if(!entry) return;
entry.run(bar, baz);
这是当我收到错误消息时,指出entry.run
不是一个函数。它应该是一个函数,但是我不确定为什么会发生这种情况。