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

如何配置TypeScript适配Node.js 14及ES2020与ES模块特性?

Hey there! Let's break down your two TypeScript configuration questions one by one, tailored to Node.js's capabilities:

1. 哪种TypeScript配置可生成最贴近Node.js 14能力的输出?

Node.js 14 natively supports almost all ES2020 syntax features and uses CommonJS as its default module system. To generate output that matches its capabilities as closely as possible, you want to minimize unnecessary transpilation while ensuring full compatibility. Here's an optimized tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "CommonJS",
    "moduleResolution": "Node",
    "lib": ["ES2020"],
    "skipLibCheck": true,
    "outDir": "./dist",
    "strict": true // Optional, but recommended for type safety
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

Let's walk through the key settings:

  • target: "ES2020": This tells TypeScript to only transpile syntax that Node.js 14 doesn't support (which is almost none), so the output JS stays as close to modern syntax as possible.
  • module: "CommonJS": Aligns with Node.js 14's default module system, ensuring require/exports work exactly like native Node.js.
  • moduleResolution: "Node": Matches how Node.js resolves CommonJS modules, so your imports behave predictably.
  • lib: ["ES2020"]: Lets TypeScript use ES2020 standard library APIs without needing polyfills—Node.js 14 already includes these natively.
  • skipLibCheck: true: Skips type checking for TypeScript's standard libraries to speed up compilation (no impact on output quality).

2. 随着Node.js更新支持ES2020并新增ES模块支持,如何配置TypeScript以生成能充分利用这些新特性的JavaScript代码?

Starting from Node.js 14.13.0, you can use ES Modules (ESM) natively (with import/export syntax instead of require). To fully leverage this, you'll need to configure TypeScript to output ESM, and adjust your project settings to match Node.js's ESM rules.

Pure ESM Project Configuration

First, update your package.json to mark the project as ESM:

{
  "type": "module",
  "scripts": {
    "build": "tsc"
  }
}

Then use this tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "lib": ["ES2020"],
    "outDir": "./dist",
    "esModuleInterop": true,
    "strict": true // Optional but recommended
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

Key explanations:

  • module: "NodeNext" & moduleResolution: "NodeNext": These settings let TypeScript align with Node.js's dual module system. It will output ESM syntax when package.json has "type": "module", and automatically handle resolution of both ESM and CommonJS dependencies.
  • esModuleInterop: true: Smooths out compatibility between ESM and CommonJS modules, so you can import CommonJS packages (like most npm libraries) using import syntax without issues.
  • "type": "module" in package.json: Tells Node.js to treat all .js files (including TypeScript-compiled ones) as ESM, so native import/export works as expected.

Dual Module Support (ESM + CommonJS)

If you need your project to support both module systems, add an exports field to package.json:

{
  "type": "module",
  "exports": {
    ".": {
      "import": "./dist/index.js",
      "require": "./dist/index.cjs"
    }
  }
}

Then adjust your tsconfig.json to compile two versions (you can use separate config files or a script to run tsc twice with different settings—one targeting ESM, another targeting CommonJS).

内容的提问来源于stack exchange,提问作者Linus Unnebäck

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 11:07:29