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

如何解决Express中的"TypeError: (0 , _web.default) is not a function"错误

Express运行报错:TypeError: (0 , _web.default) is not a function

问题代码

import express from "express";
import bodyParser from "body-parser";
import { configViewEngine } from "./config/viewEngine.js";
import { initWebRoutes } from './route/web.js';
import connectDB from "./config/connectDB.js";
import cors from 'cors';
require('dotenv').config();
let app = express();
app.use(cors({ origin: true }));
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
configViewEngine(app);
initWebRoutes(app);
connectDB();
let port = process.env.PORT || 7070;

//if port is undefined, default to current 7070
app.listen(port, () => {
    //callback
    console.log("BE NodeJS is running on the port" + port);
})

报错信息

"TypeError: (0 , _web.default) is not a function"

解决方案

这个错误是导入与导出语法不匹配导致的——你在主文件中使用了命名导入,但./route/web.js里的initWebRoutes大概率是默认导出的,以下两种方法二选一即可:

方法1:修改主文件的导入方式(改为默认导入)

将主文件中的导入代码替换为:

import initWebRoutes from './route/web.js';

方法2:修改web.js的导出方式(改为命名导出)

在./route/web.js中,确保initWebRoutes以命名导出的方式对外暴露:

// 示例:web.js中的正确命名导出
export const initWebRoutes = (app) => {
  // 这里写你的路由配置逻辑
};

同时检查web.js中是否正确定义了initWebRoutes函数,确保它是一个能接收app参数的有效函数,无语法错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 17:45:31