You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Next.js项目中无法使用顶级await的问题求助

Next.js项目中无法使用顶级await的问题求助

各位大佬好,我在Next.js项目里尝试使用顶级await(比如写let styles = await import('path'))的时候,碰到了下面这个报错:

Module parse failed: The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enabled it)
Error: The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enabled it.

错误截图

我已经在next.config.js里做了相关配置,文件内容如下:

/* eslint-disable import/no-extraneous-dependencies */

const withBundleAnalyzer = require("@next/bundle-analyzer")({
  enabled: process.env.ANALYZE === "true",
});

module.exports = withBundleAnalyzer({
  eslint: {
    dirs: ["."],
    ignoreDuringBuilds: true,
  },
  poweredByHeader: false,
  trailingSlash: true,
  basePath: "",
  // The starter code load resources from `public` folder with `router.basePath` in React components.
  // So, the source code is "basePath-ready".
  // You can remove `basePath` if you don't need it.
  reactStrictMode: true,
  experimental:{
    topLevelAwait: true,
  },
  webpack: (config, { isServer }) => {
    // enable top-level-await experiment
    if (!isServer) {
      config.experiments = { topLevelAwait: true };
    }
    return config;
  }
});

另外我也把tsconfig.json里的module字段改成了ES2022,但还是没能解决这个问题,有没有大佬能帮我看看问题出在哪呀?

备注:内容来源于stack exchange,提问作者Nitin Dutta

火山引擎 最新活动