Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 opened this issue Jul 4, 2024 · 9 comments
Labels
Needs: Attention 👋 type:question An issue that's a question TypeScript Pull requests that update Javascript code

Comments

@sandeeptipparthi
Copy link

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 ```
</details>


### Other information

_No response_
@sandeeptipparthi sandeeptipparthi added status:waiting-for-triage An issue that is yet to be reviewed or assigned type:bug A broken experience labels Jul 4, 2024
@github-project-automation github-project-automation bot moved this to Needs Triage 🔍 in Kiota Jul 4, 2024
@msgraph-bot msgraph-bot bot added the TypeScript Pull requests that update Javascript code label Jul 4, 2024
@baywet
Copy link
Member

baywet commented Jul 4, 2024

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?

@baywet baywet added question status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close type:question An issue that's a question and removed type:bug A broken experience status:waiting-for-triage An issue that is yet to be reviewed or assigned labels Jul 4, 2024
@baywet baywet moved this from Needs Triage 🔍 to Waits for author 🔁 in Kiota Jul 4, 2024
@sandeeptipparthi
Copy link
Author

@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?

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: Attention 👋 and removed status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close labels Jul 4, 2024
@baywet
Copy link
Member

baywet commented Jul 4, 2024

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

@baywet baywet added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed Needs: Attention 👋 labels Jul 4, 2024
@sandeeptipparthi
Copy link
Author

@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.

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: Attention 👋 and removed status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close labels Jul 4, 2024
@baywet
Copy link
Member

baywet commented Jul 4, 2024

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?

@baywet baywet added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed Needs: Attention 👋 labels Jul 4, 2024
@sandeeptipparthi
Copy link
Author

sandeeptipparthi commented Jul 5, 2024

@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

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: Attention 👋 and removed status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close labels Jul 5, 2024
@fey101 fey101 removed the question label Jul 5, 2024
@baywet
Copy link
Member

baywet commented Jul 5, 2024

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.

@baywet baywet added the status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close label Jul 5, 2024
@sandeeptipparthi
Copy link
Author

sandeeptipparthi commented Jul 5, 2024

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"

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: Attention 👋 and removed status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close labels Jul 5, 2024
@baywet
Copy link
Member

baywet commented Jul 5, 2024

Thanks for confirming, closing

@baywet baywet closed this as completed Jul 5, 2024
@github-project-automation github-project-automation bot moved this from Waits for author 🔁 to Done ✔️ in Kiota Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs: Attention 👋 type:question An issue that's a question TypeScript Pull requests that update Javascript code
Projects
Archived in project
Development

No branches or pull requests

3 participants