require() of ES Module nanostores not supported when using better-auth in a Turbo Repo package #640

Closed
opened 2026-03-13 07:58:14 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @ahsanfarooq210 on GitHub (Feb 8, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Description:

When using better-auth in a Turbo Repo package, I encounter an error related to the require() of an ES Module (nanostores). The error occurs specifically when better-auth tries to require the nanostores package, which is an ES Module. This issue does not occur when using better-auth directly in a standalone Node.js/Express app.

The error suggests that better-auth is attempting to use require() to import an ES Module, which is not supported. Instead, the error recommends using dynamic import(). However, since I'm working within a Turbo Repo setup, switching to "type": "module" is not feasible as it breaks other parts of the monorepo.


Error Message:

[ERROR] 14:34:46 Error: require() of ES Module /Users/ahsanfarooq/Desktop/Projects/open source/EchoLog/node_modules/.pnpm/nanostores@0.11.3/node_modules/nanostores/index.js from /Users/ahsanfarooq/Desktop/Projects/open source/EchoLog/node_modules/.pnpm/better-auth@1.1.16/node_modules/better-auth/dist/chunk-XAK7GFTD.cjs not supported.
Instead change the require of index.js in /Users/ahsanfarooq/Desktop/Projects/open source/EchoLog/node_modules/.pnpm/better-auth@1.1.16/node_modules/better-auth/dist/chunk-XAK7GFTD.cjs to a dynamic import() which is available in all CommonJS modules.

Steps to Reproduce:

  1. Set up a Turbo Repo with a package that depends on better-auth.
  2. Add better-auth as a dependency to the package.
  3. Export better-auth functions from the package's entry point (e.g., index.ts).
  4. Try to build or run the package using ts-node-dev or similar tools.

Code Snippets:

Package JSON (Turbo Repo Package):

{
  "name": "@workspace/auth",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "dev": "echo 'Add dev script here'",
    "build": "echo 'Add build script here'",
    "test": "echo 'Add test script here'",
    "lint": "echo 'Add lint script here'"
  },
  "dependencies": {
    "@workspace/db": "workspace:*",
    "better-auth": "^1.1.16"
  },
  "devDependencies": {
    "@types/node": "^22.13.1",
    "@workspace/eslint-config": "workspace:*",
    "@workspace/typescript-config": "workspace:*"
  },
  "exports": {
    ".": "./src/index.ts"
  }
}

TypeScript Config:

{
  "extends": "@workspace/typescript-config/base.json",
  "compilerOptions": {
    "tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json",
    "incremental": true,
    "types": ["node"],
    "module": "NodeNext",
    "moduleResolution": "nodenext",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  },
  "include": ["src", "*.ts"],
  "exclude": ["node_modules"]
}

Index File (src/index.ts):

export * from './lib/auth';
export { toNodeHandler, fromNodeHeaders } from "better-auth/node";
export { createAuthClient } from "better-auth/react";

Auth File (src/lib/auth.ts):

import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { prisma } from "@workspace/db";
import { jwt, bearer } from "better-auth/plugins";

export const auth = betterAuth({
  trustedOrigins: ["http://localhost:3000"],
  database: prismaAdapter(prisma, {
    provider: "postgresql",
  }),
  emailAndPassword: {
    enabled: true,
  },
  socialProviders: {
    google: {
      clientId: process.env.GOOGLE_CLIENT_ID as string,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
    },
  },
  plugins: [jwt()],
});

Additional Context:

  • Turbo Repo Setup: The issue occurs within a Turbo Repo monorepo setup. The package using better-auth is part of a larger monorepo with shared configurations for TypeScript, ESLint, etc.
  • No Issue in Standalone Node.js/Express App: When using better-auth directly in a standalone Node.js/Express app (outside of Turbo Repo), the issue does not occur. This suggests that the problem is related to how Turbo Repo or its tooling interacts with better-auth and its dependencies.
  • **Avoiding "type": "module": Switching to "type": "module" is not a viable solution as it breaks other parts of the monorepo that rely on CommonJS.

Proposed Solutions:

  1. Update better-auth to Use Dynamic Imports:
    Replace require() calls with dynamic import() for ES Modules like nanostores.

  2. Provide a CommonJS-Compatible Build:
    Ensure better-auth distributes a CommonJS-compatible build alongside the ES Module build.

  3. Documentation for Turbo Repo Users:
    Add documentation or examples for using better-auth in monorepo setups like Turbo Repo.


Environment:

  • Node.js Version: v20.x
  • Package Manager: pnpm
  • OS: macOS
  • better-auth Version: 1.1.16
  • Turbo Repo Version: Latest

Let me know if you need further details or clarification! I’d be happy to assist in debugging or testing potential fixes.


### Current vs. Expected behavior

#### **Current Behavior**:  
When using `better-auth` in a Turbo Repo package, the build or runtime process fails with the following error:  

```plaintext
[ERROR] 14:34:46 Error: require() of ES Module /Users/ahsanfarooq/Desktop/Projects/open source/EchoLog/node_modules/.pnpm/nanostores@0.11.3/node_modules/nanostores/index.js from /Users/ahsanfarooq/Desktop/Projects/open source/EchoLog/node_modules/.pnpm/better-auth@1.1.16/node_modules/better-auth/dist/chunk-XAK7GFTD.cjs not supported.
Instead change the require of index.js in /Users/ahsanfarooq/Desktop/Projects/open source/EchoLog/node_modules/.pnpm/better-auth@1.1.16/node_modules/better-auth/dist/chunk-XAK7GFTD.cjs to a dynamic import() which is available in all CommonJS modules.

This error occurs because better-auth is attempting to use require() to import the nanostores package, which is an ES Module. This is not supported in CommonJS environments, such as the one used in my Turbo Repo setup.

Expected Behavior:

better-auth should work seamlessly in a Turbo Repo package without throwing errors related to ES Module imports. Specifically:
3. The package should function correctly in a Turbo Repo monorepo setup, just as it does in a standalone Node.js/Express app.


Additional Notes:

  • When using better-auth directly in a standalone Node.js/Express app (outside of Turbo Repo), the issue does not occur. This indicates that the problem is specific to the Turbo Repo environment or how better-auth interacts with it.
  • Switching to "type": "module" in the package.json is not a viable solution, as it breaks other parts of the monorepo that rely on CommonJS.

What version of Better Auth are you using?

1.1.16

Provide environment information

Node.js Version: v20.x
Package Manager: pnpm
OS: macOS
better-auth Version: 1.1.16
Turbo Repo Version: Latest

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { prisma } from "@workspace/db";
import { jwt, bearer } from "better-auth/plugins";

export const auth = betterAuth({
  trustedOrigins: ["http://localhost:3000"],
  database: prismaAdapter(prisma, {
    provider: "postgresql", // or "mysql", "postgresql", ...etc
  }),
  emailAndPassword: {
    enabled: true,
  },
  socialProviders: {
    google: {
      clientId: process.env.GOOGLE_CLIENT_ID as string,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
    },
  },
  plugins: [jwt()],
});

Additional context

here is the link of the commit. you can use run the code so see for your self, use pnpm, go to the backend app and run pnpm dev

Github Repo link for relevant commit
No response

Originally created by @ahsanfarooq210 on GitHub (Feb 8, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce ### **Description**: When using `better-auth` in a Turbo Repo package, I encounter an error related to the `require()` of an ES Module (`nanostores`). The error occurs specifically when `better-auth` tries to `require` the `nanostores` package, which is an ES Module. This issue does **not** occur when using `better-auth` directly in a standalone Node.js/Express app. The error suggests that `better-auth` is attempting to use `require()` to import an ES Module, which is not supported. Instead, the error recommends using `dynamic import()`. However, since I'm working within a Turbo Repo setup, switching to `"type": "module"` is not feasible as it breaks other parts of the monorepo. --- ### **Error Message**: ```plaintext [ERROR] 14:34:46 Error: require() of ES Module /Users/ahsanfarooq/Desktop/Projects/open source/EchoLog/node_modules/.pnpm/nanostores@0.11.3/node_modules/nanostores/index.js from /Users/ahsanfarooq/Desktop/Projects/open source/EchoLog/node_modules/.pnpm/better-auth@1.1.16/node_modules/better-auth/dist/chunk-XAK7GFTD.cjs not supported. Instead change the require of index.js in /Users/ahsanfarooq/Desktop/Projects/open source/EchoLog/node_modules/.pnpm/better-auth@1.1.16/node_modules/better-auth/dist/chunk-XAK7GFTD.cjs to a dynamic import() which is available in all CommonJS modules. ``` --- ### **Steps to Reproduce**: 1. Set up a Turbo Repo with a package that depends on `better-auth`. 2. Add `better-auth` as a dependency to the package. 3. Export `better-auth` functions from the package's entry point (e.g., `index.ts`). 4. Try to build or run the package using `ts-node-dev` or similar tools. --- ### **Code Snippets**: #### **Package JSON** (Turbo Repo Package): ```json { "name": "@workspace/auth", "version": "0.0.0", "private": true, "scripts": { "dev": "echo 'Add dev script here'", "build": "echo 'Add build script here'", "test": "echo 'Add test script here'", "lint": "echo 'Add lint script here'" }, "dependencies": { "@workspace/db": "workspace:*", "better-auth": "^1.1.16" }, "devDependencies": { "@types/node": "^22.13.1", "@workspace/eslint-config": "workspace:*", "@workspace/typescript-config": "workspace:*" }, "exports": { ".": "./src/index.ts" } } ``` #### **TypeScript Config**: ```json { "extends": "@workspace/typescript-config/base.json", "compilerOptions": { "tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json", "incremental": true, "types": ["node"], "module": "NodeNext", "moduleResolution": "nodenext", "allowSyntheticDefaultImports": true, "esModuleInterop": true }, "include": ["src", "*.ts"], "exclude": ["node_modules"] } ``` #### **Index File** (`src/index.ts`): ```typescript export * from './lib/auth'; export { toNodeHandler, fromNodeHeaders } from "better-auth/node"; export { createAuthClient } from "better-auth/react"; ``` #### **Auth File** (`src/lib/auth.ts`): ```typescript import { betterAuth } from "better-auth"; import { prismaAdapter } from "better-auth/adapters/prisma"; import { prisma } from "@workspace/db"; import { jwt, bearer } from "better-auth/plugins"; export const auth = betterAuth({ trustedOrigins: ["http://localhost:3000"], database: prismaAdapter(prisma, { provider: "postgresql", }), emailAndPassword: { enabled: true, }, socialProviders: { google: { clientId: process.env.GOOGLE_CLIENT_ID as string, clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, }, }, plugins: [jwt()], }); ``` --- ### **Additional Context**: - **Turbo Repo Setup**: The issue occurs within a Turbo Repo monorepo setup. The package using `better-auth` is part of a larger monorepo with shared configurations for TypeScript, ESLint, etc. - **No Issue in Standalone Node.js/Express App**: When using `better-auth` directly in a standalone Node.js/Express app (outside of Turbo Repo), the issue does **not** occur. This suggests that the problem is related to how Turbo Repo or its tooling interacts with `better-auth` and its dependencies. - **Avoiding `"type": "module"`: Switching to `"type": "module"` is not a viable solution as it breaks other parts of the monorepo that rely on CommonJS. --- ### **Proposed Solutions**: 1. **Update `better-auth` to Use Dynamic Imports**: Replace `require()` calls with `dynamic import()` for ES Modules like `nanostores`. 2. **Provide a CommonJS-Compatible Build**: Ensure `better-auth` distributes a CommonJS-compatible build alongside the ES Module build. 3. **Documentation for Turbo Repo Users**: Add documentation or examples for using `better-auth` in monorepo setups like Turbo Repo. --- ### **Environment**: - **Node.js Version**: v20.x - **Package Manager**: pnpm - **OS**: macOS - **better-auth Version**: 1.1.16 - **Turbo Repo Version**: Latest --- Let me know if you need further details or clarification! I’d be happy to assist in debugging or testing potential fixes. ``` ### Current vs. Expected behavior #### **Current Behavior**: When using `better-auth` in a Turbo Repo package, the build or runtime process fails with the following error: ```plaintext [ERROR] 14:34:46 Error: require() of ES Module /Users/ahsanfarooq/Desktop/Projects/open source/EchoLog/node_modules/.pnpm/nanostores@0.11.3/node_modules/nanostores/index.js from /Users/ahsanfarooq/Desktop/Projects/open source/EchoLog/node_modules/.pnpm/better-auth@1.1.16/node_modules/better-auth/dist/chunk-XAK7GFTD.cjs not supported. Instead change the require of index.js in /Users/ahsanfarooq/Desktop/Projects/open source/EchoLog/node_modules/.pnpm/better-auth@1.1.16/node_modules/better-auth/dist/chunk-XAK7GFTD.cjs to a dynamic import() which is available in all CommonJS modules. ``` This error occurs because `better-auth` is attempting to use `require()` to import the `nanostores` package, which is an ES Module. This is not supported in CommonJS environments, such as the one used in my Turbo Repo setup. #### **Expected Behavior**: `better-auth` should work seamlessly in a Turbo Repo package without throwing errors related to ES Module imports. Specifically: 3. The package should function correctly in a Turbo Repo monorepo setup, just as it does in a standalone Node.js/Express app. --- #### **Additional Notes**: - When using `better-auth` directly in a standalone Node.js/Express app (outside of Turbo Repo), the issue does **not** occur. This indicates that the problem is specific to the Turbo Repo environment or how `better-auth` interacts with it. - Switching to `"type": "module"` in the `package.json` is not a viable solution, as it breaks other parts of the monorepo that rely on CommonJS. --- ### What version of Better Auth are you using? 1.1.16 ### Provide environment information ```bash Node.js Version: v20.x Package Manager: pnpm OS: macOS better-auth Version: 1.1.16 Turbo Repo Version: Latest ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import { prismaAdapter } from "better-auth/adapters/prisma"; import { prisma } from "@workspace/db"; import { jwt, bearer } from "better-auth/plugins"; export const auth = betterAuth({ trustedOrigins: ["http://localhost:3000"], database: prismaAdapter(prisma, { provider: "postgresql", // or "mysql", "postgresql", ...etc }), emailAndPassword: { enabled: true, }, socialProviders: { google: { clientId: process.env.GOOGLE_CLIENT_ID as string, clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, }, }, plugins: [jwt()], }); ``` ### Additional context here is the link of the commit. you can use run the code so see for your self, use pnpm, go to the backend app and run pnpm dev [Github Repo link for relevant commit](https://github.com/ahsanfarooq210/EchoLog/tree/3a81bc8780c4bfe5acb3ae2f76271332c7efab4a) _No response_
GiteaMirror added the bug label 2026-03-13 07:58:14 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jul 23, 2025):

Hi, @ahsanfarooq210. I'm Dosu, and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • Issue with better-auth package in a Turbo Repo setup.
  • Error occurs due to require() importing ES Module nanostores.
  • Suggested solution is using dynamic import().
  • Issue not present in standalone Node.js/Express apps.
  • Changing project to "type": "module" is not viable.

Next Steps:

  • Is this issue still relevant to the latest version of the better-auth repository? If so, please comment to keep the discussion open.
  • Otherwise, this issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (Jul 23, 2025): Hi, @ahsanfarooq210. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale. **Issue Summary:** - Issue with `better-auth` package in a Turbo Repo setup. - Error occurs due to `require()` importing ES Module `nanostores`. - Suggested solution is using `dynamic import()`. - Issue not present in standalone Node.js/Express apps. - Changing project to `"type": "module"` is not viable. **Next Steps:** - Is this issue still relevant to the latest version of the better-auth repository? If so, please comment to keep the discussion open. - Otherwise, this issue will be automatically closed in 7 days. Thank you for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#640