簡體   English   中英

Express 項目中對 JS 庫 (node-gtf) 的 Typescript 支持

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

有一個 Typescript Express Server,用於使用 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 版本的 package.json 中的 "type": "module" 屬性引起的

在此處輸入圖片說明

type = module 需要 es6 語法,但是在某處使用了 commonjs require。

這是節點 v12.11.0 的已知問題,已在 v12.11.1 中解決

你可以將 gtfs 版本降級到 2.4.4,它沒有這個 type:module 屬性。

如果這是依賴項的依賴項,則將其添加到分辨率中

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM