lucia-auth / lucia

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

[Bug]: 'ERR_REQUIRE_ESM' #794

Closed rajbirjohar closed 1 year ago

rajbirjohar commented 1 year ago

Package

lucia-auth

Describe the bug

I've tried numerous ways to get this library working with babel, express, and mongoose but I keep getting this error. My code is structured as follows, I created a file containing:

import mongoose from "mongoose";
import Key from "../models/Key";
import User from "../models/User";
import { express } from "lucia-auth/middleware";
import adapter from "@lucia-auth/adapter-mongoose";
import lucia from "lucia-auth";

const auth = lucia({
  User,
  Key,
  adapter: adapter(mongoose),
  env: process.env.NODE_ENV ?? "DEV",
  getUserAttributes: (user: any) => {
    return {
      email: user.email
    };
  },
  middleware: express(),
  csrfProtection: true,
  requestOrigins: ["http://localhost:3000"],
  sessionCookie: {
    sameSite: "strict",
    path: "/",
    domain: "localhost"
  },
  sessionExpiresIn: {
    activePeriod: 1000 * 60 * 60 * 24 * 30, // 1 month
    idlePeriod: 0 // disable session renewal
  },
  experimental: {
    debugMode: "DEV" ? true : false
  }
});

export type Auth = typeof auth;

export default auth;

by following the documentation. Then in an API endpoint, I am using it like so:

import { Request, Response } from "express";
import auth from "auth/lucia";

export async function signUp(req: Request, res: Response) {
  // TODO ...
  const { email, password } = req.body;

  const user = await auth.createUser({
    primaryKey: {
      providerId: "username",
      providerUserId: email,
      password
    },
    // ...
    attributes: {
      email
    }
  });

  if (!user) {
    return res
      .status(500)
      .json({ message: "Unable to create user. Please try again." });
  }

  res.status(200).json({ message: "User created successfully." });
}

When I comment out the snippet calling auth.createUser, my server runs without any hiccups. The moment I uncomment it, it throws this error:

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/...repo.../node_modules/.pnpm/lucia-auth@1.8.0/node_modules/lucia-auth/middleware/index.js from /Users/...repo.../src/auth/lucia.ts not supported.
Instead change the require of index.js in /Users/...repo.../src/auth/lucia.ts to a dynamic import() which is available in all CommonJS modules.

My tsconfig is set to use "ESNext".

Anyone got any insights at all what could be causing this?

pilcrowOnPaper commented 1 year ago

Is your project a module type (see package.json)?

rajbirjohar commented 1 year ago

@pilcrowOnPaper It is. I added "type": "module" to my package.json but then it throws a new error:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/...repo.../src/index.ts

Edit: Removing it throws the original error up above.

pilcrowOnPaper commented 1 year ago

Are you using something like ts-node?

rajbirjohar commented 1 year ago

@pilcrowOnPaper Original I was using nodemon but I tried a clean install using ts-node and both are giving me the same errors. For reference, I followed Fireship's tutorial.

rajbirjohar commented 1 year ago

I'm at a loss right now. The starter code on the documentation doesn't exactly help with these sort of obstacles and these issues don't seem to be prevalent with other users.

pilcrowOnPaper commented 1 year ago

ts-node doesn't work with ESM by default. tsconfig.json:

{
    "compilerOptions": {
        "module": "ESNext" // or ES2015, ES2020
    },
    "ts-node": {
        "esm": true
    }
}

If you use ESM, you also need to explicitly define the fine extension when importing modules:

import auth from "auth/lucia.js"; // yes, .js, not .ts
// ...
rajbirjohar commented 1 year ago

Let me try that.

pilcrowOnPaper commented 1 year ago

Also, the improved documentation for v2 will be more comprehensive

pilcrowOnPaper commented 1 year ago

btw you might need to define the compiler option (updated the original comment)

rajbirjohar commented 1 year ago

Do you have an example for a starter repo using the mongoose adapter and typescript? You have one using prisma.

pilcrowOnPaper commented 1 year ago

Yeah need to create one, if you're having trouble setting up Express + TypeScript, I could quickly make an example repo?

rajbirjohar commented 1 year ago

That would be lovely yes.

pilcrowOnPaper commented 1 year ago

https://github.com/pilcrowOnPaper/esm-express-ts

I used tsx instead of ts-node since it just works with ESM. I didn't add mongoose since my experience with that is minimal.

rajbirjohar commented 1 year ago

@pilcrowOnPaper You're the best. I will definitely contribute to v2's documentation when I can.

pilcrowOnPaper commented 1 year ago

@rajbirjohar Keep in mind that tsx doesn't do type checking, so for prod builds I recommend running tsc --noEmit before tsx. It should be fine for development tho.

adnandothussain commented 1 month ago

@pilcrowOnPaper im using NestJS and it internally uses ts-node with commonJS module. Im getting the same error Error [ERR_REQUIRE_ESM]: require() of ES Module but i cannot shift to tsx or ESNext as NestJS compiles everything in commonJS.