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

Node14环境Express+ES6+TypeScript编译失败求助

问题描述

Node 14环境下,在server.ts中使用ES6模块导入语法,运行server命令时出现编译错误,提示Must use import to load ES Module。相关配置文件及代码如下:

package.json

{  
  "name": "api",  
  "version": "1.1.0",  
  "type": "module",  
  "main": "index.js",  
  "author": "Matthew Reynolds",  
  "license": "Proprietary",  
  "private": true,  
  "scripts": {    
    "server": "./node_modules/.bin/ts-node-dev server.ts"  
  },  
  "dependencies": {    
    "@types/express": "^4.16.0",    
    "express": "^4.18.2",    
    "ts-node": "^10.9.1",    
    "ts-node-dev": "^2.0.0",    
    "tslint": "^5.10.0",    
    "typescript": "^4.8.4"  
  }
}

server.ts

import Express from 'express';
import http from 'http';
const app = Express();
const server = http.createServer(app);

server.listen(8080, function() {    
    console.log("Server Ready");
}).on('error', (e: any) => {    
    console.log("Server Error");
});

tsconfig.json

{  
  "compilerOptions": {    
    /* Basic Options */    
    "target": "ES2015",                       
    "module": "es2015",                       
    // "lib": [],                             
    // "allowJs": true,                       
    // "checkJs": true,                       
    // "jsx": "preserve",                     
    // "declaration": true,                   
    // "declarationMap": true,                
    // "sourceMap": true,                     
    // "outFile": "./",                       
    // "outDir": "./",                        
    // "rootDir": "./",                       
    // "composite": true,                     
    // "removeComments": true,                
    // "noEmit": true,                        
    // "importHelpers": true,                 
    // "downlevelIteration": true,            
    // "isolatedModules": true,               

    /* Strict Type-Checking Options */    
    "strict": true,                           
    "noImplicitAny": false,                 
    // "strictNullChecks": true,              
    // "strictFunctionTypes": true,           
    // "strictPropertyInitialization": true,  
    // "noImplicitThis": true,                
    // "alwaysStrict": true,                  

    /* Additional Checks */    
    // "noUnusedLocals": true,                
    // "noUnusedParameters": true,            
    // "noImplicitReturns": true,             
    // "noFallthroughCasesInSwitch": true,    

    /* Module Resolution Options */    
    "moduleResolution": "node",            
    // "baseUrl": "./",                       
    // "paths": {},                           
    // "rootDirs": [],                        
    // "typeRoots": [],                       
    // "types": [],                           
    // "allowSyntheticDefaultImports": true,  
    "esModuleInterop": true                   
    // "preserveSymlinks": true,              

    /* Source Map Options */    
    // "sourceRoot": "./",                    
    // "mapRoot": "./",                       
    // "inlineSourceMap": true,               
    // "inlineSources": true,                 

    /* Experimental Options */    
    // "experimentalDecorators": true,        
    // "emitDecoratorMetadata": true,         
  }
}

错误信息

Compilation error in /Users/workspace/api/server.ts
Error: Must use import to load ES Module: /Users/workspace/api/server.ts    
at Object.<anonymous> (/Users/workspace/api/server.ts:1:7)    
at Module._compile (internal/modules/cjs/loader.js:1085:14)    
at Module._compile (/Users/workspace/api/node_modules/source-map-support/source-map-support.js:568:25)    
at Module.m._compile (/private/var/folders/p0/xbzc_v4x2xgccqxk0rh2f8k00000gp/T/ts-node-dev-hook-5362012710629007.js:69:33)    
at Module._extensions..js (internal/modules/cjs/loader.js:1114:10)    
at require.extensions..jsx.require.extensions..js (/private/var/folders/p0/xbzc_v4x2xgccqxk0rh2f8k00000gp/T/ts-node-dev-hook-5362012710629007.js:114:20)    
at require.extensions.<computed> (/private/var/folders/p0/xbzc_v4x2xgccqxk0rh2f8k00000gp/T/ts-node-dev-hook-5362012710629007.js:71:20)    
at Object.nodeDevHook [as .ts] (/Users/workspace/api/node_modules/ts-node-dev/lib/hook.js:63:13)    
at Module.load (internal/modules/cjs/loader.js:950:32)    
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
[ERROR] 18:27:34 Error: Must use import to load ES Module: /Users/workspace/api/server.ts
解决方案

问题核心是ts-node-dev默认以CommonJS模式处理文件,但你的项目通过package.json的"type": "module"声明了使用ES模块,导致加载逻辑冲突。按以下步骤调整:

  1. 修改package.json的server脚本
    给ts-node-dev添加--esm参数,强制启用ES模块模式:
"scripts": {
  "server": "./node_modules/.bin/ts-node-dev --esm server.ts"
}
  1. 更新tsconfig.json的模块解析配置
    将moduleResolution改为nodenext(Node 14.13.0+支持),确保TypeScript能正确识别ES模块的解析规则:
{
  "compilerOptions": {
    // 保留其他原有配置
    "module": "es2015",
    "moduleResolution": "nodenext"
  }
}
  1. 检查Node版本
    确保你的Node版本在14.13.0及以上,低于这个版本的Node对ES模块的支持存在限制,建议升级到符合要求的版本。

完成以上调整后,重新运行npm run server即可正常编译运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 23:05:30