lucia: [Bug]: 'ERR_REQUIRE_ESM'

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?

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

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