Does the top-level-await error mean Webpack is already installed?

huangapple go评论59阅读模式
英文:

Does the top-level-await error mean Webpack is already installed?

问题

I'm getting the error
'模块解析失败:未启用顶级等待实验(设置experiments.topLevelAwait: true以启用它)'

After some research, I created a 'webpack.config.js' file in the same location as my 'package.json' file. Here is the content

module.exports = {
    experiments: {
        topLevelAwait: true
    }
};

This had no effect. I still get the error.

Do I need to explicitly install Webpack and then create the config file? That seems odd since the original error itself implies Webpack is already there.

Here are the dependencies from my 'package.json' file. Webpack isn't there.

"dependencies": {
    "@aws-amplify/ui-react": "^4.6.2",
    "@nextui-org/react": "^1.0.0-beta.12",
    "aws-amplify": "^5.2.1",
    "next": "^13.4.2",
    "react": "18.2.0",
    "react-dom": "18.2.0"
}
  1. 我是否需要安装Webpack?
  2. 或者,如果我不需要安装它,我需要更改'webpack.config.js'文件的哪些内容才能使其正常工作?
英文:

I'm getting the error
Module parse failed: The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enabled it)

After some research, I created a webpack.config.js file in the same location as my package.json file. Here is the content

module.exports = {
    experiments: {
        topLevelAwait: true
    }
};

This had no effect. I still get the error.

Do I need to explicitly install Webpack and then create the config file? That seems odd since the original error itself implies Webpack is already there.

Here are the dependencies from my package.json file. Webpack isn't there.

"dependencies": {
    "@aws-amplify/ui-react": "^4.6.2",
    "@nextui-org/react": "^1.0.0-beta.12",
    "aws-amplify": "^5.2.1",
    "next": "^13.4.2",
    "react": "18.2.0",
    "react-dom": "18.2.0"
  }
  1. Do I need to install Webpack anyway?
  2. Or, if I don't need to install it, what do I need to change about the webpack.config.js file to get this working?

答案1

得分: 1

NextJS内部嵌入了Webpack,因此已安装Webpack。

此外,启用顶级等待的方法是在 next.config.js 文件中添加注释,如下所示:

module.exports = {
    webpack: (config) => {
        // this will override the experiments
        config.experiments = {...config.experiments, topLevelAwait: true};
        // this will just update topLevelAwait property of config.experiments
        // config.experiments.topLevelAwait = true
        return config;
    },
};
英文:

It turns out NextJS has Webpack embedded in it. Therefore Webpack is installed.

Also, the way to enable top-level-await is to annotate the next.config.js file like this:

module.exports = {
    webpack: (config) => {
        // this will override the experiments
        config.experiments = {...config.experiments, topLevelAwait: true};
        // this will just update topLevelAwait property of config.experiments
        // config.experiments.topLevelAwait = true
        return config;
    },
};

huangapple
  • 本文由 发表于 2023年5月30日 00:33:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定