Error: require() of ES Module #373

Closed
opened 2026-03-13 07:43:47 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @wagihmohamed on GitHub (Dec 9, 2024).

Describe the bug
When try to run express server after doing the configuration I get the following error. I think there is a compatibility issue with both CommonJS and ES modules.

`Error [ERR_REQUIRE_ESM]: require() of ES Module \node_modules\oslo\dist\index.js from \node_modules\better-auth\dist\index.cjs not supported.
Instead change the require of index.js in \node_modules\better-auth\dist\index.cjs to a dynamic import() which is available in all CommonJS modules.

Error: require() of ES Module \node_modules\oslo\dist\index.js from \node_modules\better-auth\dist\index.cjs not supported.
Instead change the require of index.js in \node_modules\better-auth\dist\index.cjs to a dynamic import() which is available in all CommonJS modules.`

To Reproduce
I just followed the docs with the express section but here is my code

src/index.ts

import express from "express";
import cors from "cors";
import dotenv from "dotenv";
import { toNodeHandler } from "better-auth/node";
import { auth } from "./lib/auth";

dotenv.config();

const app = express();
const PORT = process.env.PORT || 5000;

app.use(
  cors({
    origin: true,
    credentials: true,
  })
);
app.use(express.json());

app.all("/api/auth/*", toNodeHandler(auth));

app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

package.json
{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node dist/index.js",
    "dev": "ts-node-dev --respawn src/index.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@prisma/client": "^6.0.1",
    "better-auth": "^1.0.14",
    "cors": "^2.8.5",
    "dotenv": "^16.4.7",
    "express": "^4.21.2",
    "prisma": "^6.0.1"
  },
  "devDependencies": {
    "@types/cors": "^2.8.17",
    "@types/express": "^5.0.0",
    "@types/node": "^22.10.1",
    "ts-node-dev": "^2.0.0",
    "typescript": "^5.7.2"
  }
}

schema.prisma

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}```
Originally created by @wagihmohamed on GitHub (Dec 9, 2024). **Describe the bug** When try to run express server after doing the configuration I get the following error. I think there is a compatibility issue with both CommonJS and ES modules. `Error [ERR_REQUIRE_ESM]: require() of ES Module \node_modules\oslo\dist\index.js from \node_modules\better-auth\dist\index.cjs not supported. Instead change the require of index.js in \node_modules\better-auth\dist\index.cjs to a dynamic import() which is available in all CommonJS modules. Error: require() of ES Module \node_modules\oslo\dist\index.js from \node_modules\better-auth\dist\index.cjs not supported. Instead change the require of index.js in \node_modules\better-auth\dist\index.cjs to a dynamic import() which is available in all CommonJS modules.` **To Reproduce** I just followed the docs with the express section but here is my code ``` src/index.ts import express from "express"; import cors from "cors"; import dotenv from "dotenv"; import { toNodeHandler } from "better-auth/node"; import { auth } from "./lib/auth"; dotenv.config(); const app = express(); const PORT = process.env.PORT || 5000; app.use( cors({ origin: true, credentials: true, }) ); app.use(express.json()); app.all("/api/auth/*", toNodeHandler(auth)); app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); }); package.json { "name": "server", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "node dist/index.js", "dev": "ts-node-dev --respawn src/index.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "@prisma/client": "^6.0.1", "better-auth": "^1.0.14", "cors": "^2.8.5", "dotenv": "^16.4.7", "express": "^4.21.2", "prisma": "^6.0.1" }, "devDependencies": { "@types/cors": "^2.8.17", "@types/express": "^5.0.0", "@types/node": "^22.10.1", "ts-node-dev": "^2.0.0", "typescript": "^5.7.2" } } schema.prisma generator client { provider = "prisma-client-js" } datasource db { provider = "postgresql" url = env("DATABASE_URL") }```
Author
Owner

@wagihmohamed commented on GitHub (Dec 9, 2024):

If anyone is facing this issue @Bekacru told me that cjs isn't supported. so I changed the

package.json
type:"module"

and changed tsconfig to use modules and its working fine.

@wagihmohamed commented on GitHub (Dec 9, 2024): If anyone is facing this issue @Bekacru told me that cjs isn't supported. so I changed the ``` package.json type:"module" ``` and changed tsconfig to use modules and its working fine.
Author
Owner

@drshika commented on GitHub (Dec 19, 2024):

Hey! I think we have implemented the changes that were mentioned in the thread but we are running into the same error.

Can you propose a fix?

Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/nanostores/index.js from .../node_modules/better-auth/dist/react.cjs not supported.
Instead change the require of index.js in .../node_modules/better-auth/dist/react.cjs to a dynamic import() which is available in all CommonJS modules.

tsconfig.json

{
  "compilerOptions": {
    "target": "es2017",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "module": "esnext", // relevant
    "moduleResolution": "bundler", // relevant 
    "allowJs": true,
    "checkJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "noUncheckedIndexedAccess": true,
    "baseUrl": ".",
    "paths": {
      "~/*": [
        "./src/*"
      ],
      "@/*": [
        "./src/*"
      ]
    },
    "plugins": [
      {
        "name": "next"
      }
    ]
  },
  "include": [
    ".eslintrc.cjs",
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    "**/*.cjs",
    "**/*.mjs",
    "next-i18next.config.mjs",
    "src/app/api/chat/openaiFunctionCall",
    ".next/types/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

package.json

{
  "name": "uiuc-chat-frontend",
  "version": "0.1.0",
  "private": true,
  "type": "module", // relevant
  "scripts": {
    "build": "next build",
    "bd": "infisical run --env=dev -- next build",
    "dev": "infisical run --env=dev -- next dev",
    "lint": "next lint",
    "start": "next start",
    "format": "prettier --check --ignore-path .gitignore .",
    "format:fix": "prettier --write --ignore-path .gitignore .",
    "analyze": "cross-env ANALYZE=true next build"
  },
// rest
@drshika commented on GitHub (Dec 19, 2024): Hey! I think we have implemented the changes that were mentioned in the thread but we are running into the same error. Can you propose a fix? ``` Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/nanostores/index.js from .../node_modules/better-auth/dist/react.cjs not supported. Instead change the require of index.js in .../node_modules/better-auth/dist/react.cjs to a dynamic import() which is available in all CommonJS modules. ``` tsconfig.json ```json { "compilerOptions": { "target": "es2017", "lib": [ "dom", "dom.iterable", "esnext" ], "module": "esnext", // relevant "moduleResolution": "bundler", // relevant "allowJs": true, "checkJs": true, "skipLibCheck": true, "strict": true, "forceConsistentCasingInFileNames": true, "noEmit": true, "esModuleInterop": true, "resolveJsonModule": true, "isolatedModules": true, "jsx": "preserve", "incremental": true, "noUncheckedIndexedAccess": true, "baseUrl": ".", "paths": { "~/*": [ "./src/*" ], "@/*": [ "./src/*" ] }, "plugins": [ { "name": "next" } ] }, "include": [ ".eslintrc.cjs", "next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.cjs", "**/*.mjs", "next-i18next.config.mjs", "src/app/api/chat/openaiFunctionCall", ".next/types/**/*.ts" ], "exclude": [ "node_modules" ] } ``` package.json ```json { "name": "uiuc-chat-frontend", "version": "0.1.0", "private": true, "type": "module", // relevant "scripts": { "build": "next build", "bd": "infisical run --env=dev -- next build", "dev": "infisical run --env=dev -- next dev", "lint": "next lint", "start": "next start", "format": "prettier --check --ignore-path .gitignore .", "format:fix": "prettier --write --ignore-path .gitignore .", "analyze": "cross-env ANALYZE=true next build" }, // rest ```
Author
Owner

@drshika commented on GitHub (Dec 20, 2024):

fixed the issue, moved the BetterAuth login page to the /app router instead of /pages .

@drshika commented on GitHub (Dec 20, 2024): fixed the issue, moved the BetterAuth login page to the /app router instead of /pages .
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#373