microsoft / kiota

OpenAPI based HTTP Client code generator
https://aka.ms/kiota/docs
MIT License
3.02k stars 210 forks source link

Issue with consuming latest versions of Kiota core libraries in a typescript project having Module / Module Resolution as NodeNext or even CommonJs #4936

Closed sandeeptipparthi closed 4 months ago

sandeeptipparthi commented 4 months ago

What are you generating using Kiota, clients or plugins?

API Client/SDK

In what context or format are you using Kiota?

Source Build

Client library/SDK language

TypeScript

Describe the bug

My requirement is to consume some of these microsoft kiota core libraries like shown below in a typescript project. "@microsoft/kiota-abstractions": "1.0.0-preview.50", "@microsoft/kiota-http-fetchlibrary": "1.0.0-preview.49", "@microsoft/kiota-serialization-form": "1.0.0-preview.39", "@microsoft/kiota-serialization-json": "1.0.0-preview.50", "@microsoft/kiota-serialization-multipart": "1.0.0-preview.28", "@microsoft/kiota-serialization-text": "1.0.0-preview.47",

We have already created a TypeScript client SDK using Kiota Client generator tool for one of our Open API spec. Now we are looking to consume that generated code to make an API call with help of authentication and adapter methods provided by Kiota-Abstractions and http-fetchlibrary packages.

Now the issue is, only if i use the above list of versions for the core libraries the project compiles fine and also i am able to make use of the required methods from the imported libraries and also able to call the API's with no issues.

If i attempt to upgrade the versions anything above this, it starts to complain with ESM error.

Current TS Config i am using for the typescript project in this case is as per kiota's recommentation in microsoft website which is - tsconfig > compilerOptions > esModuleInterop set to "true". tsconfig > compilerOptions > forceConsistentCasingInFileNames set to "true". tsconfig > compilerOptions > lib with an entry of "es2015". tsconfig > compilerOptions > module set to "NodeNext". tsconfig > compilerOptions > moduleResolution set to "NodeNext". tsconfig > compilerOptions > target set to "es2016" or later.

Also we tried with module set to CommonJS but that too fails with "import" statement cannot be used error. Infact i had tried to exactly clone the sample project provided on the microsoft kiota documentation site - > https://github.com/microsoft/kiota-samples/tree/main/get-started/quickstart/typescript

After cloning this project, I tried to build the project and then ran the index.js file and also attempted to run the index.ts without compiling directly using ts-node command. Either way it throws the exact same error as i am getting for this sample repository. Please help in this regard.

Version of Kiota used - 1.15.0 Node version is 20.2.0 Typescript is 5 +

Expected behavior

It should have allowed us to consume any latest versions of kiota core libraries in our typescript to benefit.

How to reproduce

Provided the details already in description.

Open API description file

No response

Kiota Version

1.15.0

Latest Kiota version known to work for scenario above?(Not required)

No response

Known Workarounds

No response

Configuration

No response

Debug output

Click to expand log ``` ```

Other information

No response

baywet commented 4 months ago

Hi @sandeeptipparthi, Thanks for using kiota and for reaching out. This is something we have changed recently, we have switched to esm modules generation. However, this change has not been released as a minor version yet, it will be released in the next couple of days. Have you tried with the latest preview of kiota instead?

sandeeptipparthi commented 4 months ago

@baywet Oh good to know there is new version coming in couple days. I would be very much interested to see esm modules coming through.

So the new Kiota preview available is no longer using NodeNext and moved away to ESM?

baywet commented 4 months ago

This question will bring you along answer. Long story short note next supports both esm modules and common js modules. The difficulty is that it switches from one to another depending on a few complex detection rules. To the upcoming version of kiota will generate code that has the JS extension so it triggers the ESM detection rule for nodeJS. You can read more about this issue here and in the general typescript documentation. https://www.typescriptlang.org/docs/handbook/modules/theory.html#the-module-output-format

sandeeptipparthi commented 4 months ago

@baywet Gone through it, I have now upgraded to 1.16 preview version available for Kiota. Generated the typescript SDK using the upgraded kiota cli. Now in the consuming repository if I use NodeNext I am getting the following error - >

index.ts:1:49 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("@microsoft/kiota-abstractions")' call instead. To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field "type": "module" to 'C:/Users//Downloads/kiota-typescript/package.json'.

1 import { AnonymousAuthenticationProvider } from '@microsoft/kiota-abstractions';

And when I change from NodeNext to commonJS, still the error comes through as below - node ./dist/index.js C:\Users\<>user\Downloads\kiota-typescript\dist\index.js:12 const kiota_abstractions_1 = require("@microsoft/kiota-abstractions"); ^

Error [ERR_REQUIRE_ESM]: require() of ES Module

Please suggest what should be done here to get this resolved.

baywet commented 4 months ago

Thanks for the additional information. The Tsconfig should keep the node next resolution but you should update your package.json to change the type to module. Can you also confirm that for all the relative imports in the generated code and with the .js extensions please?

sandeeptipparthi commented 4 months ago

@baywet I have added the type: module in package.json and attempted to rerun the npm install and also tried to recompile things using tsc. It complained with the following error stating the .js extensions were missing ...

Kiota version used - v1.16.0-preview.202406270001

image

baywet commented 4 months ago

can you try with the latest version that was just released please? (1.16, non preview) it's possible that preview didn't have the change.

sandeeptipparthi commented 4 months ago

It works now with the new version of Kiota. Thank you so much @baywet.

The way how it works right now is with the compiled distributable index.js file when i run it works as expected.

Although if i attempt to run the index.ts file directly it says unrecognized .ts extension. Like if i run using "npx ts-node index.ts"

baywet commented 4 months ago

Thanks for confirming, closing