繁体   English   中英

TypeScript 与 Node.js ESM 不一致

[英]TypeScript with Node.js ESM disagreement

Node.js ESM 的工作方式似乎与 TypeScript 不同。

它导致程序通过类型检查但在运行时崩溃。

package.json

{
  "dependencies": {
    "@types/lodash": "^4.0.0",
    "lodash": "^4.0.0",
    "typescript": "^4.0.0"
  },
  "type": "module"
}

tsconfig.json

{
  "compilerOptions": {
    "module": "es2015"
  }
}

a.ts

import { partition } from "lodash";

partition([1, 2, 3, 4], n => n % 2);

npm install
node_modules/.bin/tsc -p .
node index.js

程序崩溃:

file:///home/paul/test44/index.js:1
import { partition } from "lodash";
         ^^^^^^^^^
SyntaxError: Named export 'partition' not found. The requested module 'lodash' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'lodash';
const { partition } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:179:5)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
    at async handleMainPromise (node:internal/modules/run_main:63:12)

如何将 TypeScript 和 Node.js ESM 带入 alignment,以便类型检查与运行时行为相匹配?


编辑:

我尝试将 typescript@next 与"module": "nodenext"使用。

发生同样的错误。

问题是你混合了 ES 和 ComonJS 模块; 它在错误消息的文本中。 这仍然是一个相对较新的概念,当您期望模块......应该......正常工作时很容易错过。

TypeScript 仅提供import语句,然后它们将转译到您选择的模块系统; 在 ESM 的情况下,由于它们在语法上相似,所以没有太多需要转换的地方。

由于您要转译为 ESM,因此问题不在于 TypeScript; 它是底层 Node.js 引擎以及它如何处理混合模块系统。 您已经在 ESM 中编写了一些内容(授予,使用 TypeScript)并且您正在尝试导入用 CommonJS 编写的其他内容。

为了根据 CommonJS 模块执行代码,您需要:

  1. 使用您的依赖项的 ESM 版本(并非所有依赖项都提供一个!)
  2. 使用module.createRequire()以可用的形式导入 CommonJS 模块

资料来源:

https://nodejs.org/api/esm.html#no-require-exports-or-moduleexports https://nodejs.org/api/module.html#modulecreaterequirefilename

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM