首页 > 解决方案 > ES6 import not working correctly in next.js application

问题描述

I'm working on a tiny next.js project, but the issue that i'm facing is that ES6 module import only works in React components.The 2 following code are some Database configurations that are used in routes inside API folder. The code below is the one that doesn't work using ES6 modules Code with ES6 module system

Here is the code that works fine with common js Code using common JS

Also when i use the debugger, with the code using ES6 modules, i cannot capture values form imported modules and i only capture values when using common js.

I've tried to solve the problem with this github issue : Support ES module in next.config.js but id did not help. Have any idea?

标签: reactjsnext.jses6-modulescommonjs

解决方案


ES6 导入不起作用,因为这是服务器端渲染。

我建议你使用 requirejs。

但是如果你想在 NodeJS 中使用 ES6 导入,它可以在新版本中使用,现在它是一个实验性功能(你可以在这里阅读更多

尝试解决方案:在 NodeJS 上更新到最新版本


推荐阅读