microsoft / kiota-typescript

TypeScript libraries for Kiota-generated API clients.
https://aka.ms/kiota/docs
MIT License
37 stars 27 forks source link

The bundling configuration & output seem incorrect #1207

Closed yasaichi closed 3 months ago

yasaichi commented 4 months ago

I found that there has been a package import error since 1.0.0-preview.52 of kiota-abstractions was released. It would be related to drop of cjs bundling. Here is the minimum code to reproduce the issue:

Setup

index.js

import abstractions from '@microsoft/kiota-abstractions';
const { apiClientProxifier } = abstractions;
console.log(apiClientProxifier);

package.json

{
  "dependencies": {
    "@microsoft/kiota-abstractions": "^1.0.0-preview.51"
  },
  "type": "module"
}

Execution result

1.0.0-preview.51

$ node index.js 
[Function: apiClientProxifier]

1.0.0-preview.52 or later

$ node index.js 
(node:14796) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/path/to/node_modules/@microsoft/kiota-abstractions/dist/es/src/index.js:7
export * from "./apiClientBuilder";
^^^^^^

SyntaxError: Unexpected token 'export'
    at wrapSafe (node:internal/modules/cjs/loader:1350:18)
    at Module._compile (node:internal/modules/cjs/loader:1379:20)
    at Module._extensions..js (node:internal/modules/cjs/loader:1518:10)
    at Module.load (node:internal/modules/cjs/loader:1249:32)
    at Module._load (node:internal/modules/cjs/loader:1065:12)
    at cjsLoader (node:internal/modules/esm/translators:318:15)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:258:7)
    at ModuleJob.run (node:internal/modules/esm/module_job:262:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:475:24)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:109:5)

Node.js v22.2.0
andrueastman commented 4 months ago

@koros @musale Any chance you can confirm if this is expecte/related to the changes at https://github.com/microsoft/kiota-typescript/pull/1102

Just to confirm @yasaichi, does this error occur with the latest versions(1.0.0-preview.53) as well?

yasaichi commented 4 months ago

@andrueastman Yes. Unfortunately, it also occurs in the latest version (preview.53).

yasaichi commented 4 months ago

In my understanding, the build output does not conform ES Modules specifications at some points, one of which is that import and export do not come with file extensions.

koros commented 4 months ago

Reopening the issue it seems from the latest release 1.0.0-preview.54 the issue isn't fully fixed especially for pure javascript files in node.js environment

koros commented 4 months ago

@yasaichi This is what I have on my end:

tsconfig.json

{
    "compilerOptions": {
      "target": "ESNext",                // Target the latest JavaScript features
      "module": "ESNext",                // Use ESNext module syntax (ESM)
      "moduleResolution": "node",        // Node module resolution strategy
      "esModuleInterop": true,           // Enable interoperability between CommonJS and ESM
      "skipLibCheck": true,              // Skip type checking of declaration files
      "strict": true,                    // Enable all strict type-checking options
      "forceConsistentCasingInFileNames": true,
      "outDir": "./dist"                 // Output directory for compiled files
    },
    "include": ["src"],                  // Include all files in the src directory
    "exclude": ["node_modules", "dist"]  // Exclude node_modules and dist directories
  }

index.ts

import { parseGuidString } from "@microsoft/kiota-abstractions";
console.log(parseGuidString("3f2e8b1e-66ae-4f2d-bc8e-367f2e1b2e94"));

npm i -D tsx

npx tsx src/index.ts

It might not work for your case since you're using .js, so maybe only option is to bring back cjs support

yasaichi commented 4 months ago

@koros Thank you for your thoughts. With your tsconfig.json and tsx, I could run index.ts that you also gave. However, when it comes to the build output, it is still not working. I don't think everyone uses tsx or something like this to run a backend application in production, that's why we could say there is still an issue to solve, unfortunately.

// index.ts
import { parseGuidString } from "@microsoft/kiota-abstractions";
console.log(parseGuidString("3f2e8b1e-66ae-4f2d-bc8e-367f2e1b2e94"));
// ./dist/index.js (tsc output)
import { parseGuidString } from "@microsoft/kiota-abstractions";
console.log(parseGuidString("3f2e8b1e-66ae-4f2d-bc8e-367f2e1b2e94"));
$ node dist/index.js
node:internal/modules/esm/resolve:260
    throw new ERR_MODULE_NOT_FOUND(
          ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/yuichigoto/kiota-sample/node_modules/@microsoft/kiota-abstractions/dist/es/src/apiClientBuilder' imported from /Users/yuichigoto/kiota-sample/node_modules/@microsoft/kiota-abstractions/dist/es/src/index.js
    at finalizeResolution (node:internal/modules/esm/resolve:260:11)
    at moduleResolve (node:internal/modules/esm/resolve:920:10)
    at defaultResolve (node:internal/modules/esm/resolve:1119:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:542:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:511:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:241:38)
    at ModuleJob._link (node:internal/modules/esm/module_job:126:49) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///Users/yuichigoto/kiota-sample/node_modules/@microsoft/kiota-abstractions/dist/es/src/apiClientBuilder'
}

Node.js v22.2.0
koros commented 4 months ago

I agree

yasaichi commented 4 months ago

@koros I cannot imagine how hard it is, but one of the most possible solutions is to append .js extension to all import statements. This does not need to add an additional build tool other than tsc.

baywet commented 3 months ago

closing since #1213 got merged. Let us know if you see further issues.