简体   繁体   English

Express 项目中对 JS 库 (node-gtf) 的 Typescript 支持

[英]Typescript Support for JS Library (node-gtf) in a Express Project

Have a Typescript Express Server for consuming GTFS data using the GTFS library ( https://github.com/BlinkTagInc/node-gtfs )有一个 Typescript Express Server,用于使用 GTFS 库( https://github.com/BlinkTagInc/node-gtfs )使用 GTFS 数据

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

Importing the library this way以这种方式导入库

import { importGtfs } from 'gtfs';

But due to no TS support I m facing this error但由于没有 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.

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

Found a type Library for GTFS https://www.npmjs.com/package/gtfs-types But unable to find a solution to include these types in the Project为 GTFS https://www.npmjs.com/package/gtfs-types找到了一个类型库但无法找到将这些类型包含在项目中的解决方案

This issue is caused by the "type": "module" property in package.json of the gtfs version you're using此问题是由您使用的 gtfs 版本的 package.json 中的 "type": "module" 属性引起的

在此处输入图片说明

type = module would expect es6 syntax however somewhere commonjs require is used. type = module 需要 es6 语法,但是在某处使用了 commonjs require。

this is a known issue with node v12.11.0 which was resolved in v12.11.1这是节点 v12.11.0 的已知问题,已在 v12.11.1 中解决

you could downgrade the gtfs version to 2.4.4 which doesn't have this type:module property.你可以将 gtfs 版本降级到 2.4.4,它没有这个 type:module 属性。

If this is a dependency of dependency then add it to resolutions如果这是依赖项的依赖项,则将其添加到分辨率中

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

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

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