openapi-ts icon indicating copy to clipboard operation
openapi-ts copied to clipboard

Module not found: Can't resolve './client'

Open cheehong1030 opened this issue 10 months ago • 6 comments

Description When using code generated by @hey-api/openapi-ts, I'm encountering a module resolution error. The generated code imports from a ./client module that doesn't exist.

Steps to reproduce Configure @hey-api/openapi-ts with the provided config file Run the openapi-ts to generate sdk Import and use the generated code in a project run the project

Error

Module not found: Can't resolve './client'
  1 | // This file is auto-generated by @hey-api/openapi-ts
  2 |
> 3 | import { type Config, type ClientOptions as DefaultClientOptions, createClient, createConfig } from './client';
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  4 | import type { ClientOptions } from './types.gen';
  5 |
  6 | /**

Configuration

// openapi-ts.config.ts
import { defaultPlugins, defineConfig } from '@hey-api/openapi-ts';

export default defineConfig({
    input: 'http://localhost/api/swagger/openapi.json',
    output: {
        format: 'prettier',
        lint: 'eslint',
        path: './src/client',
    },
    plugins: [
        ...defaultPlugins,
        {
            bundle: true,
            name: '@hey-api/client-axios',
            runtimeConfigPath: './src/requestSetting.ts',
        },
        {
            enums: 'typescript',
            name: '@hey-api/typescript',
        },
        {
            name: '@hey-api/schemas',
            type: 'json',
        },
        {
            asClass: true,
            serviceNameBuilder: '{{name}}',
            name: '@hey-api/sdk',
        },
        '@hey-api/sdk',
        '@tanstack/react-query',
    ],
});

Image

Please correct me if there is any wrong configuration

cheehong1030 avatar Apr 21 '25 17:04 cheehong1030

@cheehong1030 I see the folder in your screenshot, is it empty?

mrlubos avatar Apr 21 '25 17:04 mrlubos

No, The contents of the folder are normal and there are no typescript errors. But seem he can't find the ./client folder. Is this error about typescript configuration?

cheehong1030 avatar Apr 21 '25 17:04 cheehong1030

Probably! Please create a reproducible example if you want me to have a look

mrlubos avatar Apr 21 '25 17:04 mrlubos

Try this http://github.com/cheehong1030/example-openapi-ts

Image

cheehong1030 avatar Apr 22 '25 04:04 cheehong1030

Can you try with bundleSource_EXPERIMENTAL?

mrlubos avatar Apr 22 '25 04:04 mrlubos

New error Seems this error from the bundle configuration

Image

cheehong1030 avatar Apr 22 '25 05:04 cheehong1030

I observed that when I manually edited the source of the generated client and changed line 4:

import { type Config, type ClientOptions as DefaultClientOptions, createClient, createConfig } from './client'

into

import { type Config, type ClientOptions as DefaultClientOptions, createClient, createConfig } from './client/index.cjs'

or

import { type Config, type ClientOptions as DefaultClientOptions, createClient, createConfig } from './client/index.d'

then I was able to use the functions. However I have no idea if this is correct or just some pure accident - I don't write typescript code on a daily basis

I did notice that inside the client directory there was index.d.ts (that's why I used it) which was a bit surprising as I was expecting an index.ts file

toudi avatar May 25 '25 21:05 toudi

@toudi Hey if it works, it works. I definitely need to improve this feature, just haven't prioritised it

mrlubos avatar May 25 '25 22:05 mrlubos

I'm going to close this issue as part of 0.73.0 where clients will be bundled by default. Please let me know if you still run into any problems!

mrlubos avatar Jun 14 '25 08:06 mrlubos