You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js后端项目报错:找不到default导出,求解决方法

解决模块导出导入不匹配的报错

你的报错核心是ES模块默认导入和CommonJS导出的匹配问题:

  • server.js用import app from './src/app.js'(ES模块默认导入)
  • app.js用module.exports = app(CommonJS导出)

以下两种方法任选其一即可解决:

方法1:统一用ES模块规范

修改app.js的导出语句,替换成ES模块的默认导出:

// 替换 app.js 最后一行的 module.exports = app
export default app;

方法2:统一用CommonJS规范

修改server.js的导入语句,改用CommonJS的require语法:

const app = require('./src/app.js');

app.listen(process.env.SERVER_LISTENING_PORT, () => {
  console.log("Server is listening on port : " + process.env.SERVER_LISTENING_PORT);
});

额外注意

如果你的package.json里设置了"type": "module",项目会默认使用ES模块,此时尽量全用import/export语法;如果没有设置,默认是CommonJS,用require/module.exports更稳妥,避免混合语法导致的兼容问题。

内容的提问来源于stack exchange,提问作者Mongalmoy Karmakar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 07:17:42