Search code examples
javascriptnode.jsdiscorddiscord.js

SyntaxError: Unexpected reserved word "await"


I keep getting this error even thought I am in a asynchronous function...

import { dirname, join } from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

export async function importer(client) {
    const dir = join(__dirname, "../../commands/")

    const commandCategories = readdirSync(dir)
    for (let cat of commandCategories) {
        const commandFiles = readdirSync(join(dir, cat)).filter(files => files.endsWith(".js"));

        for (const file of commandFiles) {
            const command = await import(join(dir, cat, file));
            client.commands.set(command.default.name, command.default);
        }
    }
}

Is there something I am missing? I definitely need the await part in import in const command. It imports a few commands then it drops the mentioned error in title on the console output.

Thanks.


Solution

  • The problem isn't the importer code, it's in one of the modules you attempted to import (and import crashes on parsing it)!

    It's a bug in node that such a non-helpful error is thrown when this syntax error is encountered during dynamic import.

    Try logging the filenames before importing them so you can see at which file it crashes. Once you know which file failed to parse, do a static import of it for testing, so you can get a proper error that tells you which line has the issue.