public-transport / hafas-client

JavaScript client for HAFAS public transport APIs.
ISC License
263 stars 52 forks source link

ERR_REQUIRE_ESM #309

Closed paha77 closed 6 months ago

paha77 commented 6 months ago

I'm trying to create a client within an nestjs-console command:

import { createClient } from "hafas-client";
import { profile as dbProfile } from "hafas-client/p/db/index.js";
// ...
async testCommand(): Promise<void> {
        const client = createClient(dbProfile, "https://github.com/paha77");
}

but get the following error:

Error [ERR_REQUIRE_ESM]: require() of ES Module <MY PROJECT ROOT>node_modules/hafas-client/index.js from <MY PROJECT ROOT>src/hafas/hafas.console.ts not supported.
Instead change the require of index.js in <MY PROJECT ROOT>src/hafas/hafas.console.ts to a dynamic import() which is available in all CommonJS modules.
derhuerst commented 6 months ago

AFAICT, this is nothing that hafas-client has control over, so I can't help you, sorry.

Using the type: module directive, it denotes itself as an ESM package.

It seems like your code is getting transpiled to require() calls, which then fails because hafas-client is not getting transpiled (and because Node's require() does not support ESM).