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

tsconfig设为node16仍编译为require致ERR_REQUIRE_ESM,求正确配置

问题描述

我发布了一个用于NPM的Node.js概念验证包,将其导入应用编译时抛出错误:

[ERR_REQUIRE_ESM]: require() of ES Module

查看编译后的lib/index.js,发现TSC将代码中的import编译成了require语句。我的包已在package.json中设置"type": "module",当前tsconfig.json配置如下:

{
  "compilerOptions": {
    "target": "es6",
    "sourceMap": true,
    "module": "node16",
    "esModuleInterop": false,
    "forceConsistentCasingInFileNames": true,
    "strict": false,
    "outDir": "lib",
    "removeComments": true,
    "noLib": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "declaration": true,
    "moduleResolution": "Node",
    "allowSyntheticDefaultImports": false
  }
}

进展更新:将包的module和target改为"esnext"后,编译出的index.js使用了import {}语句,但消费该包的NestJS应用仍报错。尝试修改NestJS应用的target/module为esnext会导致核心模块报错,此方案不可行。

解决方案

1. 调整TS配置以正确输出ESM代码

修正tsconfig.json配置,确保TS编译后输出符合ESM规范的代码,同时适配Node.js模块解析:

{
  "compilerOptions": {
    "target": "es2020", // 选择支持ESM特性的较新目标版本
    "sourceMap": true,
    "module": "ESNext", // 强制输出原生ESM的import/export语句
    "moduleResolution": "NodeNext", // 匹配module配置,遵循Node.js最新模块解析规则
    "esModuleInterop": true, // 提升对CommonJS模块的兼容度
    "forceConsistentCasingInFileNames": true,
    "strict": false,
    "outDir": "lib",
    "removeComments": true,
    "noLib": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "declaration": true,
    "allowSyntheticDefaultImports": true, // 允许从CommonJS模块导入默认导出
    "skipLibCheck": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "lib"]
}

关键配置说明:

  • module: "ESNext":避免TS将ESM语法转换为CommonJS的require语句
  • moduleResolution: "NodeNext":配合package.json的"type": "module",遵循Node.js原生模块解析逻辑
  • target: "es2020":防止因目标版本过低,触发不必要的语法降级
  • esModuleInterop与allowSyntheticDefaultImports:优化跨模块规范的导入体验

2. 适配NestJS(CommonJS环境)的消费问题

NestJS默认使用CommonJS模块系统,直接导入纯ESM包会触发错误。无法修改NestJS配置时,可通过以下两种方式解决:

方式一:为包添加双模式支持(同时输出ESM和CommonJS)

通过两个TS配置文件分别编译两种模块版本:

  1. 创建tsconfig.esm.json用于ESM编译:
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "NodeNext",
    "outDir": "lib/esm"
  }
}
  1. 创建tsconfig.cjs.json用于CommonJS编译:
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "module": "CommonJS",
    "moduleResolution": "Node",
    "outDir": "lib/cjs",
    "target": "es6"
  }
}
  1. 修改package.json声明双入口:
{
  "type": "module",
  "main": "./lib/cjs/index.js", // CommonJS入口,供NestJS使用
  "module": "./lib/esm/index.js", // ESM入口,供其他ESM环境使用
  "types": "./lib/esm/index.d.ts",
  "scripts": {
    "build": "tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json"
  }
}

这样不同环境会自动匹配对应模块版本,实现双向兼容。

方式二:在NestJS中动态导入ESM包

若不想修改包的构建配置,可在NestJS中使用动态导入绕开CommonJS的require限制:

// 替代静态导入:import { xxx } from 'package-for-dave'
async function usePackage() {
  const { xxx } = await import('package-for-dave');
  // 调用包内功能
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 20:20:18