I have the following function:
export default async function getController() {
const directoryPath = path.join(__dirname, '@presentation/controller');
let controller = [];
await fs.readdir(directoryPath, async (err, files) => {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
for (let file of files) {
const fullPath = path.join(directoryPath, file);
if (path.extname(fullPath) === '.ts') {
try {
const module = await import(fullPath);
controller.push(module.default);
} catch (err) {
console.log('Erro ao importar o módulo:', err);
}
}
}
});
return controller
}
When I try to call the function in another file like this:
const controllers = await getController();
I get the error:
ReferenceError: await is not defined
My tsconfig looks like this:
{
"compilerOptions": {
"module": "Node16",
"target": "es2017",
...
}
I tried changing tsconfig settings I've also tried changing the way the function is exported by throwing it in a variable, however I just got a different error.