googleapis / googleapis/google-api-nodejs-client

Any option for ESM support in current release?

オープン
#3,397 コメント 1 件 リアクション 3 件 担当者 0 名 GitHub で見る
priority: p3 type: question
主要言語
TypeScript
スター
12.2k
フォーク
2k
平均マージ
1日 9時間
マージ済み PR(30日)
24

説明

I'm attempting to port a node TypeScript project to use the ESM support in newer versions of node and [TypeScript](https://www.typescriptlang.org/tsconfig#module).

TypeScript has muddied the water historically by supporting ESM style import/export syntax but still emitting CommonJS format modules for the node ecosystem. Moving things forward means increased consciousness of output format, understanding the format of libraries and falling back to [dynamic imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) when needed to pull in things that are only still available as CommonJS.

The only dependency that I'm running into a wall with for this project is `@googleapis/sheets`. It's written in TypeScript, but published as CommonJS with types bundled. That worked when my code was also CommonJS under the hood, but I can't figure out an equivalent way to reference what I need from it in ESM TypeScript.

Attached is an [example project](https://github.com/googleapis/google-api-nodejs-client/files/13532286/googleapis-esm.zip) showing various approaches. Note the `"type": "module"` in _package.json_ and the `"module": "NodeNext"` in _tsconfig.json_. The intent is to write TypeScript in ESM format, emit verbatim ESM and pull in CommonJS where necessary via `import()`.
- `base.mts` - A base class to force other modules to integrate with something in explicitly ESM format.
- `classic.ts` - How we would have used the sheets API historically. But this fails when we try to pull in base because using sheets forces us to CommonJS under the hood so we can't use pure ESM directly. `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(  ./base.mjs )' call instead.`
- `commonjs.cts` - The same scenario/problem as classic, just using CommonJS explicitly instead of TypeScript's sugared import/export syntax.
- `esm.mts` - An explicitly ESM module. This compiles because it's only dependent on sheets at the type level, and that all gets stripped out in the build. I'm not sure if it would work with more substantial integration since it would be producing ESM style imports for a CommonJS module which wouldn't resolve at runtime.
- `implicit.ts` - An implicitly ESM module because the package is ESM and because of the tsconfig. It's trying to use dynamic import but runs into problems because things like interfaces aren't part of the build structure and the classes can only be referenced with `typeof`.

I see #3335 and #3337 to improve ESM support, but I'm interested in any workaround I'm overlooking with the _current_ version that would make this usable in an ESM project. At this point sandboxing it as `.cts` and converting all the ESM imports it needs to dynamic imports seems most equivalent, but it's non-trivial because all of those then have to be dealt with as async.

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

まず、添付されたサンプルプロジェクトで報告された動作を再現します。特に base.mts、classic.ts、commonjs.cts、esm.mts、implicit.ts を、記載されている package.json と tsconfig.json の設定を使って確認してください。再現結果とあわせて issues #3335 と #3337 を読みます。現在の ESM プロジェクトで @googleapis/sheets を使用するための、文書化および検証済みの方法が得られるか、必要なサポートの範囲が明確に定義されれば、作業は完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
node.js, typescript
領域
api
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。