googleapis / google-api-nodejs-client

Google's officially supported Node.js client library for accessing Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included.
https://googleapis.dev/nodejs/googleapis/latest/
Apache License 2.0
11.27k stars 1.91k forks source link

Any option for ESM support in current release? #3397

Open nlwillia opened 7 months ago

nlwillia commented 7 months ago

I'm attempting to port a node TypeScript project to use the ESM support in newer versions of node and TypeScript.

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 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 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().

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.

sofisl commented 7 months ago

Hi @nlwillia, we don't have plans on making this library ESM. However, as a workaround, you could try esm.sh for now. I haven't played too much with attempting to use this library in ESM, so I'll have to keep this issue open and think about it some more.