swagger-typescript-api icon indicating copy to clipboard operation
swagger-typescript-api copied to clipboard

swagger-typescript-api does not work with TypeScript >=4.8.0

Open martis347 opened this issue 3 years ago • 1 comments

The issue I've had

I've recently ran into issues getting swagger-typescript-api to work in my project even though it worked a few months ago. The issue that I've ran into kept throwing me an exception:

TypeError: host.fileExists is not a function
    at Object.fileExists (C:\Users\<MyUsername>\AppData\Local\npm-cache\_npx\ad2f6472e51cc62f\node_modules\typescript\lib\typescript.js:167586:63)
    at Object.host.fileExists (C:\Users\<MyUsername>\AppData\Local\npm-cache\_npx\ad2f6472e51cc62f\node_modules\typescript\lib\typescript.js:117448:47)
    at getPackageJsonInfo (C:\Users\<MyUsername>\AppData\Local\npm-cache\_npx\ad2f6472e51cc62f\node_modules\typescript\lib\typescript.js:44458:37)
    at loadModuleFromSpecificNodeModulesDirectory (C:\Users\<MyUsername>\AppData\Local\npm-cache\_npx\ad2f6472e51cc62f\node_modules\typescript\lib\typescript.js:45000:27)
    at loadModuleFromImmediateNodeModulesDirectory (C:\Users\<MyUsername>\AppData\Local\npm-cache\_npx\ad2f6472e51cc62f\node_modules\typescript\lib\typescript.js:44981:58)
    at C:\Users\<MyUsername>\AppData\Local\npm-cache\_npx\ad2f6472e51cc62f\node_modules\typescript\lib\typescript.js:44971:39
    at Object.forEachAncestorDirectory (C:\Users\<MyUsername>\AppData\Local\npm-cache\_npx\ad2f6472e51cc62f\node_modules\typescript\lib\typescript.js:8370:26)
    at loadModuleFromNearestNodeModulesDirectoryWorker (C:\Users\<MyUsername>\AppData\Local\npm-cache\_npx\ad2f6472e51cc62f\node_modules\typescript\lib\typescript.js:44965:19)
    at loadModuleFromNearestNodeModulesDirectory (C:\Users\<MyUsername>\AppData\Local\npm-cache\_npx\ad2f6472e51cc62f\node_modules\typescript\lib\typescript.js:44957:16)
    at tryResolve (C:\Users\<MyUsername>\AppData\Local\npm-cache\_npx\ad2f6472e51cc62f\node_modules\typescript\lib\typescript.js:44052:34)

Findings

After digging for a few hours I've managed to trace that it was caused by a later version of TypeScript. It stops working if version >=4.8.0 is used.

This issue is not reproduced by cloning the repository and doing an npm i due to the package-lock.json file explicitly stating, that typescript 4.7.4 should be used. However package-lock.json is not included as part of an npm package. This and the fact, that the typescript dependency is with a caret (^) symbol installs the latest version of typescript for new projects depending on this package.

Reproduction

To reproduce this error you can simply:

  1. git clone https://github.com/acacode/swagger-typescript-api.git
  2. npm i [email protected]
  3. npm i

This will yield an error during the tests run.

How to fix it on my machine?

If you're just a passer-by like me who stumbled across this issue on their project - you can resolve it like this:

If the package is in your package.json

  1. npm i [email protected]

Please keep in mind that this will affect your whole project.

If you're using npx

  1. Find npx cache for swagger-typescript-api mine was in C:\Users\<MyUsername>\AppData\Local\npm-cache\_npx\ad2f6472e51cc62f
  2. Add "typescript": "4.7.4" to the package.json
  3. npm i

martis347 avatar Sep 01 '22 11:09 martis347

Duplicate of #370

vanslly avatar Sep 01 '22 18:09 vanslly

Hi @martis347 , I think this problem fixed in 11.* release. Because of this I'm closing this issue, please reopen if you are still occurring with this bug

js2me avatar Nov 05 '22 18:11 js2me