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

ExpressJS项目集成TypeScript遇ES模块加载错误求助

问题描述

在现有ExpressJS项目中集成TypeScript,使用Node.js v20.12.1版本,相关配置及报错如下:

tsconfig.json

"compilerOptions": {
  "forceConsistentCasingInFileNames": true, 
  "esModuleInterop": true, 
  "strict": true,  
  "target": "ES6",  
  "module": "ESNext",
  "skipLibCheck": true
}

package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "ts-node-dev app.ts"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "ts-node-dev": "^2.0.0",
    "typescript": "^5.4.4"
  },
  "dependencies": {
    "nodemon": "^3.1.0"
  }
}

运行npm run dev时触发编译错误:

[INFO] 03:37:15 Reinitializing TS compilation
[INFO] 03:37:15 Restarting: D:\test\tsconfig.json has been modified
Compilation error in D:\test\app.ts
Error: Must use import to load ES Module: D:\test\app.ts
    at Object.<anonymous> (D:\test\app.ts:1:7)
    at Module._compile (node:internal/modules/cjs/loader:1369:14)
    at Module._compile (D:test\node_modules\source-map-support\source-map-support.js:568:25)
    at Module.m._compile (C:\User\AppData\Local\Temp\ts-node-dev-hook-836053039101899.js:69:33)
    at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
    at require.extensions..jsx.require.extensions..js (C:\AppData\Local\Temp\ts-node-dev-hook-836053039101899.js:114:20)
    at require.extensions.<computed> (C:\AppData\Local\Temp\ts-node-dev-hook-836053039101899.js:71:20)
    at Object.nodeDevHook [as .ts] (D:\ts-node-dev\lib\hook.js:63:13)
    at Module.load (node:internal/modules/cjs/loader:1206:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1022:12)
[ERROR] 03:37:15 Error: Must use import to load ES Module: D:\test\app.ts

已尝试调整Node.js版本、tsconfig中的target和module选项,问题仍未解决,当前app.ts仅保留console.log()语句。


解决方法

1. 完善tsconfig.json配置

补充ES模块兼容的关键配置项:

{
  "compilerOptions": {
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop": true,
    "strict": true,
    "target": "ES6",
    "module": "ESNext",
    "moduleResolution": "NodeNext", // 匹配Node.js的ES模块解析逻辑
    "skipLibCheck": true,
    "outDir": "./dist" // 指定编译输出目录
  },
  "include": ["app.ts", "src/**/*"], // 明确需要编译的文件范围
  "exclude": ["node_modules"]
}

2. 更新package.json配置

调整脚本参数并补充必要依赖:

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "dist/app.js", // 指向编译后的入口文件
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "ts-node-dev --esm --respawn app.ts", // 添加--esm启用ES模块支持
    "build": "tsc" // 新增编译脚本
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/express": "^4.17.21", // Express类型定义
    "@types/node": "^20.12.7", // Node.js类型定义
    "ts-node-dev": "^2.0.0",
    "typescript": "^5.4.4"
  },
  "dependencies": {
    "express": "^4.19.2", // 确保安装Express(若项目未引入)
    "nodemon": "^3.1.0"
  }
}

3. 确认app.ts的模块语法

确保文件使用ES模块语法,避免CommonJS写法:

// app.ts
console.log("TypeScript + Express running!");

4. 安装依赖并测试

执行命令安装新增依赖:

npm install

启动开发服务:

npm run dev

关键说明
  • --esm是ts-node-dev适配ES模块的核心参数,因为项目package.json中设置了"type": "module",必须明确启用该模式
  • moduleResolution: "NodeNext"让TypeScript遵循Node.js的ES模块解析规则,避免导入错误
  • 安装@types/express和@types/node是为了让TypeScript识别对应API的类型,提供编译检查和代码提示

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 12:28:17