swagger-typescript-api: type: module & moduleResolution: NodeNext fails if the Typescript dependency version is 4.7+

Node version: 16.15.0 OS: Windows

Code to reproduce: package.json

{
  "name": "swagger-typescript-api-bug",
  "version": "1.0.0",
  "description": "",
  "type": "module",
  "main": "index.js",
  "scripts": {
    "fetch-types": "swagger-typescript-api -p https://my-website-api.com"
  },
  "license": "ISC",
  "dependencies": {
    "swagger-typescript-api": "^9.3.1"
  }
}

tsconfig.json

{
  "include": ["./src/**/*"],
  "exclude": [],
  "compilerOptions": {
    "esModuleInterop": true,
    "module": "ES6",
    "moduleResolution": "NodeNext",
    "target": "ES6"
  },
   "ts-node": {
      "transpileOnly": true
    }
}

running the fetch-types script raises the following stack:

TypeError: host.fileExists is not a function
    at getPackageJsonInfo (D:\Projects\WebstormProjects\testing-tools\node_modules\typescript\lib\typescript.js:43798:37)
    at Object.getPackageScopeForPath (D:\Projects\WebstormProjects\testing-tools\node_modules\typescript\lib\typescript.js:43765:23)
    at lookupFromPackageJson (D:\Projects\WebstormProjects\testing-tools\node_modules\typescript\lib\typescript.js:116488:28)
    at Object.getImpliedNodeFormatForFile (D:\Projects\WebstormProjects\testing-tools\node_modules\typescript\lib\typescript.js:116482:200)
    at acquireOrUpdateDocument (D:\Projects\WebstormProjects\testing-tools\node_modules\typescript\lib\typescript.js:136051:47)
    at Object.acquireDocumentWithKey (D:\Projects\WebstormProjects\testing-tools\node_modules\typescript\lib\typescript.js:136026:20)
    at getOrCreateSourceFileByPath (D:\Projects\WebstormProjects\testing-tools\node_modules\typescript\lib\typescript.js:165211:41)
    at Object.getOrCreateSourceFile [as getSourceFile] (D:\Projects\WebstormProjects\testing-tools\node_modules\typescript\lib\typescript.js:165158:24) 
    at findSourceFileWorker (D:\Projects\WebstormProjects\testing-tools\node_modules\typescript\lib\typescript.js:118298:29)
    at findSourceFile (D:\Projects\WebstormProjects\testing-tools\node_modules\typescript\lib\typescript.js:118196:26)
error Command failed with exit code 1.

No error is raised if the dependency is Typescript 4.6.2

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 21
  • Comments: 34 (4 by maintainers)

Commits related to this issue

Most upvoted comments

If you use yarn you can workaround this issue by using yarn resolutions in your package.json.

swagger-typescript-api@^10.0.1 has a dependency of typescript@^4.7.4 which resolves to typescript@4.8.2 for me, which causes this error.

Adding a resolution of "typescript": "4.7.4" ensures the breaking version of TypeScript isn’t used. image

For those using NPM, which lacks package resolution overrides like yarn, you may “pin” the version of your own typescript dependency as @fabsenet said above.

In my case I could have just changed typescript@^4.1.2 to typescript@4.1.2, so removing the caret symbol. So by running npm i -D typescript@4.1.2. However, the package.json of swagger-typescript-api specifies a minimum compatible version of typescript@4.7.4, so I do this at my own risk. I could also pin to typescript@4.7.4 and do an application-wide upgrade.

The next version will pin the TypeScript version to 4.7.X with #401.

This solution, which only overrides swagger-typescript-api, works for me using npm:

  package.json
  
  "overrides": {
    "swagger-typescript-api": {
      "typescript": "4.7.4"
    }
  }

how correct use “overrides” for npm? I added this in my package.json but it’s not work(

@Misinin make sure to be using npm@8.3 or higher: https://docs.npmjs.com/cli/v8/configuring-npm/package-json#overrides

{
  "overrides": {
    "swagger-typescript-api": {
      "typescript": "4.7.4"
    }
  }
}

Helps with yarn

"resolutions": {
	"swagger-typescript-api/**/typescript": "4.7.4"
}

Same issue, after a lot of testing it seems that swagger-typescript-api simply does not work with typescript 4.8.2 (released 16 hours ago). When locking typescript to 4.7.4 (using e.g. yarn resolutions) it works.

Since swagger-typescript-api requests "typescript: "^4.7.4" it will automatically pick up typescript 4.8.2, so unless you got an existing lockfile that resolves typescript ^4.7.4 to 4.7.4 (the no longer latest version) swagger-typescript-api won’t run.

@js2me Any progress? Can you solve it first, my test failed:

Failures:
1) Schematic: sta should be working
  Message:
    Error: Swagger schema parse error!
     TypeError: host.fileExists is not a function
  Stack:
    Error: Swagger schema parse error!
     TypeError: host.fileExists is not a function
        at /home/vsts/work/1/s/node_modules/swagger-typescript-api/src/index.js:257:17
  Message:
    Error: Generate error: TypeError: host.fileExists is not a function
  Stack:
    Error: Generate error: TypeError: host.fileExists is not a function
        at /home/vsts/work/1/s/dist/schematics-test/sta/index.js:167:23

61 specs, 1 failure
Finished in 7.686 seconds

If you use yarn you can workaround this issue by using yarn resolutions in your package.json.

swagger-typescript-api@^10.0.1 has a dependency of typescript@^4.7.4 which resolves to typescript@4.8.2 for me, which causes this error.

Adding a resolution of "typescript": "4.7.4" ensures the breaking version of TypeScript isn’t used. image

👍 Or Typescript 4.6.2 if using moduleResolution = Node16 or NodeNext

Indeed, thanks for the heads-up 😃

same error

{
  "compilerOptions": {
    "target": "ES2020",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "ES2020",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "allowSyntheticDefaultImports": true,
    "typeRoots": ["types", "./node_modules/@types"],
    "useUnknownInCatchVariables": true
  }
}
{
  "scripts": {
    "generate-api": "swagger-typescript-api -r --unwrap-response-data --single-http-client --modular --module-name-index=1 -p http://localhost:5000/swagger/v1/swagger.json -o ./packages/queries/src/api"
  },
  "devDependencies": {
    "swagger-typescript-api": "^10.0.1",
    "typescript": "^4.8.2"
  }
}
TypeError: host.fileExists is not a function
    at Object.fileExists (C:\src\Bygghemma.ECP.Web\Clients\node_modules\typescript\lib\typescript.js:167586:63)
    at host.fileExists (C:\src\Bygghemma.ECP.Web\Clients\node_modules\typescript\lib\typescript.js:117448:47)
    at tryFileLookup (C:\src\Bygghemma.ECP.Web\Clients\node_modules\typescript\lib\typescript.js:44293:28)
    at tryFile (C:\src\Bygghemma.ECP.Web\Clients\node_modules\typescript\lib\typescript.js:44285:20)
    at tryExtension (C:\src\Bygghemma.ECP.Web\Clients\node_modules\typescript\lib\typescript.js:44277:24)
    at tryAddingExtensions (C:\src\Bygghemma.ECP.Web\Clients\node_modules\typescript\lib\typescript.js:44255:32)
    at loadModuleFromFile (C:\src\Bygghemma.ECP.Web\Clients\node_modules\typescript\lib\typescript.js:44188:45)
    at nodeLoadModuleByRelativeName (C:\src\Bygghemma.ECP.Web\Clients\node_modules\typescript\lib\typescript.js:44112:36)
    at tryResolve (C:\src\Bygghemma.ECP.Web\Clients\node_modules\typescript\lib\typescript.js:44069:34)
    at C:\src\Bygghemma.ECP.Web\Clients\node_modules\typescript\lib\typescript.js:44032:69
error Command failed with exit code 1.```

@vanslly Thank you so much that helped me!

I couldn’t get the resolution stuff to work in yarn berry, so used @jnig’s PR linked above until merged

"swagger-typescript-api": "github:jnig/swagger-typescript-api.git#8fba36a790fae21ef073154dab2c9da79908de17",

src/formatFileContent.js

class LanguageServiceHost { … fileExists(file) { return ts.sys.fileExists(file); } …

Thank you for adding the above.

For those using NPM, which lacks package resolution overrides like yarn, you may “pin” the version of your own typescript dependency as @fabsenet said above. In my case I could have just changed typescript@^4.1.2 to typescript@4.1.2, so removing the caret symbol. So by running npm i -D typescript@4.1.2. However, the package.json of swagger-typescript-api specifies a minimum compatible version of typescript@4.7.4, so I do this at my own risk. I could also pin to typescript@4.7.4 and do an application-wide upgrade. The next version will pin the TypeScript version to 4.7.X with #401.

This solution, which only overrides swagger-typescript-api, works for me using npm:

  package.json
  
  "overrides": {
    "swagger-typescript-api": {
      "typescript": "4.7.4"
    }
  }

This worked for me. Thanks @matsgm!

We’re having the same problem. Thanks to @treet who explained this very well -

Since swagger-typescript-api requests "typescript: “^4.7.4” it will automatically pick up typescript 4.8.2 (and…) swagger-typescript-api simply does not work with typescript 4.8.2

This means that people interacting with this generator via npx like so:

npx swagger-typescript-api -p http://localhost:3001/swagger-json

Are not going to be able to escape the error by going back to an earlier version of this library

npx swagger-typescript-api@9.3.1 -p http://localhost:3001/swagger-json

Because, both 9.3.1 and the latest release 10.0.1 pull in typescript 4.8.2 due to the typescript package.json dependency using the minor version bump requirement^4.x in both versions.

So even though it was recently bumped from 4.2.4 to 4.7.4 in the latest 10.0 release, both will still pull in 4.8.2.

It seems that this library swapped to using typescript ^4.x as far back as the 2.0 release

Actually sorry got it working thanks everyone for help

Having the same issue. @vanslly’s solution fixes the issue for me. Thanks!