I generated an Axios Typescript client for the PokeAPI using OpenAPI Generator with the following command:
npx openapi-generator-cli generate -i https://raw.githubusercontent.com/PokeAPI/pokeapi/master/openapi.yml -g typescript-axios -o ./src
It generated a folder, containing an index.ts
file which exports everything:
export * from './api';
export * from './configuration';
I added the index.ts
to be the main
in my package.json
of the custom Turborepo package:
{
"name": "@repo/poke-client",
"version": "1.0.0",
"main": "./src/index.ts",
"scripts": {
"codegen:pokeapi": "openapi-generator-cli generate -i https://raw.githubusercontent.com/PokeAPI/pokeapi/master/openapi.yml -g typescript-axios -o ./src"
},
"devDependencies": {
"@openapitools/openapi-generator-cli": "^2.13.4"
}
}
Importing also looks good in the IDE:
But when I run it (using tsx
), I get the following error:
backend:dev: SyntaxError: The requested module '@repo/poke-client' does not provide an export named 'PokemonApi'
backend:dev: at ModuleJob._instantiate (node:internal/modules/esm/module_job:131:21)
backend:dev: at async ModuleJob.run (node:internal/modules/esm/module_job:213:5)
backend:dev: at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
backend:dev: at async loadESM (node:internal/process/esm_loader:34:7)
backend:dev: at async handleMainPromise (node:internal/modules/run_main:66:12)
I did manage to find a workaround, using it like this:
import * as pokeClient from '@repo/poke-client';
const api = new pokeClient.default.PokemonApi();
But it leads to weird TypeScript and ESlint errors on every line that uses this (although compiling and working):
backend:lint: 2:7 error 'api' is assigned a value but never used @typescript-eslint/no-unused-vars
backend:lint: 3:7 error 'POKEMONS_COUNT' is assigned a value but never used @typescript-eslint/no-unused-vars
backend:lint: 13:19 error Unreachable code no-unreachable
Any ideas on how to fix it?
Apparently, the problem was that in the package.json
of my internal package in Turborepo.
I added this:
"type": "module"
And it worked!