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

Next.js+Electron环境下启用esModuleInterop后仍出现TS1259错误的问题求助

Next.js+Electron环境下启用esModuleInterop后仍出现TS1259错误的问题求助

各位大佬好,我现在在做一个Next.js+Electron的项目,遇到了一个很头疼的TypeScript错误:明明已经在tsconfig.json里开启了esModuleInterop: true,但导入express的时候还是触发了TS1259错误,提示只能用esModuleInterop标志才能默认导入这个模块。

先贴一下具体的错误信息:

[1] server/express.ts(1,8): error TS1259: Module '"C:/Users/pepo/Documents/Aprendizagem própria/pdv-raro-electron/node_modules/@types/express/index"' can only be default-imported using the 'esModuleInterop' flag

我的项目配置和代码如下:

1. tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}

2. server/express.ts

import express, { Request, Response } from "express";

const app = express();
const PORT = 4000;

app.get("/api/hello", (req: Request, res: Response) => {
  res.json({ message: "Hello from Express!" });
});

export function startExpressServer() {
  app.listen(PORT, () => {
    console.log(`In running on: http://localhost:${PORT}`);
  });
}

3. main.ts (Electron主进程文件)

import { app, BrowserWindow } from 'electron'
import { startExpressServer } from './server/express'

function createWindow() {
  const win = new BrowserWindow({
    width: 1024,
    height: 768,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false
    }
  })

  win.loadURL('http://localhost:3000')
}

app.whenReady().then(() => {
  startExpressServer()
  createWindow()
})

4. package.json中的运行脚本

{
  "scripts": {
    "electron:dev": "concurrently \"npm run dev\" \"npm run electron:watch\"",
    "electron:watch": "tsc main.ts --outDir dist && nodemon --watch main.ts --exec \"electron .\""
  }
}

我运行npm run electron:dev的时候就会触发这个错误,明明已经严格按照提示开启了esModuleInterop,但问题还是存在,有没有大佬能帮我分析一下这是哪里出问题了?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.08 11:54:29