lucia-auth / lucia

Authentication, simple and clean
https://lucia-auth.com
MIT License
9.04k stars 465 forks source link

[Bug]: ts-node throws errors when being used in CommonJS module #1200

Closed BeBoRE closed 11 months ago

BeBoRE commented 11 months ago

Package

​@lucia-auth/adapter-postgresql

Describe the bug

Because @lucia-auth/adapter-postgresql is of type "module" I am unable to use it in my project. Trying to import the adapter it will throw the error:

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/before/ei-noah-bot/node_modules/@lucia-auth/adapter-postgresql/dist/index.js from /home/before/ei-noah-bot/packages/lucia/auth.ts not supported.
Instead change the require of index.js in /home/before/ei-noah-bot/packages/lucia/auth.ts to a dynamic import() which is available in all CommonJS modules.

I have not been able to convert my packages to type "module" without running into other issues.

Can you consider using tsup to build both an ESM and CJS bundle and removing type: "module"?

BeBoRE commented 11 months ago

I am now using tsx and that seems to have fixed the issues. But I do still think that you should consider using tsup to support ts-node when using CommonJS modules.

pilcrowOnPaper commented 11 months ago

We don't intend to support CommonJS

benkingcode commented 8 months ago

@pilcrowOnPaper The Prisma adapter seems to not work for me because of this? I get ERR_REQUIRE_ESM

Rykuno commented 7 months ago

Those those utilizing lucia with NestJS or commonJS, you can utilize dynamic imports to get around this issue.

const dynamicImport = async (packageName: string) =>
  new Function(`return import('${packageName}')`)();
@Module({
  providers: [
    {
      provide: AUTH,
      async useFactory() {
        return (await dynamicImport('./lucia')).lucia;
      },
    },
  ],
  exports: [AUTH],
})
export class AuthModule {}
mmontacer commented 2 months ago

Those those utilizing lucia with NestJS or commonJS, you can utilize dynamic imports to get around this issue.

const dynamicImport = async (packageName: string) =>
  new Function(`return import('${packageName}')`)();
@Module({
  providers: [
    {
      provide: AUTH,
      async useFactory() {
        return (await dynamicImport('./lucia')).lucia;
      },
    },
  ],
  exports: [AUTH],
})
export class AuthModule {}

you lose type safety though