最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SyntaxError: Unexpected reserved word "await" - Stack Overflow

programmeradmin8浏览0评论

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, "../../mands/")

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

        for (const file of mandFiles) {
            const mand = await import(join(dir, cat, file));
            clientmands.set(mand.default.name, mand.default);
        }
    }
}

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

Thanks.

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, "../../mands/")

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

        for (const file of mandFiles) {
            const mand = await import(join(dir, cat, file));
            client.mands.set(mand.default.name, mand.default);
        }
    }
}

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

Thanks.

Share Improve this question asked Apr 27, 2022 at 18:34 nikoszznikoszz 1721 gold badge4 silver badges16 bronze badges 9
  • For me this code doesn't have any syntax errors... – CherryDT Commented Apr 27, 2022 at 18:39
  • I might be wrong and I can't check now, but I don't think you can await and assign to a constant. – Aioros Commented Apr 27, 2022 at 18:39
  • @Aioros You can! Otherwise it wouldn't be very useful, if you couldn't use its return value... – CherryDT Commented Apr 27, 2022 at 18:40
  • I am not sure why it happens either, when it drops the error, mands stop importing, leaving only 6-7 mands out of the 25+ mands imported... – nikoszz Commented Apr 27, 2022 at 18:41
  • 1 Did you check the full stack? My assumption is that the syntax error is actually in a module you import, not on the import expression itself! So trying to import that bad module fails with the syntax error because that error was raised while parsing the module. (You could also try logging the filename before importing the file so you know which one caused the error.) – CherryDT Commented Apr 27, 2022 at 18:42
 |  Show 4 more ments

1 Answer 1

Reset to default 6

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.

发布评论

评论列表(0)

  1. 暂无评论