简体   繁体   中英

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 )

  • version ("gtfs": "^3.0.4")

Importing the library this way

import { importGtfs } from 'gtfs';

But due to no TS support I m facing this error

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';

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

This issue is caused by the "type": "module" property in package.json of the gtfs version you're using

在此处输入图片说明

type = module would expect es6 syntax however somewhere commonjs require is used.

this is a known issue with node v12.11.0 which was resolved in v12.11.1

you could downgrade the gtfs version to 2.4.4 which doesn't have this type:module property.

If this is a dependency of dependency then add it to resolutions

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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