【问题标题】:Typescript Support for JS Library (node-gtf) in a Express ProjectExpress 项目中对 JS 库 (node-gtf) 的 Typescript 支持
【发布时间】:2021-07-01 11:30:16
【问题描述】:

拥有一个 Typescript Express 服务器,用于使用 GTFS 库 (https://github.com/BlinkTagInc/node-gtfs) 使用 GTFS 数据

  • 版本(“gtfs”:“^3.0.4”)

以这种方式导入库

import { importGtfs } from 'gtfs';

但由于没有 TS 支持,我面临这个错误

require() of ES modules is not supported.
require() of <Project-path>/node_modules/gtfs/index.js from <Project-path>/src/index.ts is an ES module file as it is a .js file whose nearest parent package.
json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from <Project-path>/node_modules/gtfs/package.json.

以这种方式使用它 import { importGtfs } from 'gtfs';

为 GTFS 找到了一个类型库 https://www.npmjs.com/package/gtfs-types 但无法找到将这些类型包含在项目中的解决方案

【问题讨论】:

  • 您使用的是哪个版本的 gtfs?
  • @deechris27 "gtfs": "^3.0.4"

标签: node.js typescript express gtfs


【解决方案1】:

此问题是由您正在使用的 gtfs 版本的 package.json 中的 "type": "module" 属性引起的

type = module 期望使用 es6 语法,但是在某处使用了 commonjs require。

这是节点 v12.11.0 的一个已知问题,已在 v12.11.1 中解决

您可以将 gtfs 版本降级到没有此 type:module 属性的 2.4.4。

如果这是依赖项的依赖项,则将其添加到解决方案中

"resolutions":{
   "gtfs": "^2.4.4"
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-12-13
  • 2019-05-05
  • 1970-01-01
  • 2015-08-17
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
  • 2019-06-06
相关资源
最近更新 更多