module not found: Can't resolve 'crypto' #122

Closed
opened 2026-03-13 07:34:20 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @goodchengwang on GitHub (Oct 22, 2024).

Describe the bug
use bearer plugin causes an error module not found: Can't resolve 'crypto'

To Reproduce
Steps to reproduce the behavior:
Using the bearer plugin causes an error

import { betterAuth } from 'better-auth'
import { drizzleAdapter } from 'better-auth/adapters/drizzle'
import { bearer } from 'better-auth/plugins'

import { db } from '@acme/db/client' // your drizzle instance

import { env } from '../env'

export const auth = betterAuth({
  database: drizzleAdapter(db, {
    provider: 'sqlite',
  }),
  socialProviders: {
    github: {
      clientId: env.GITHUB_CLIENT_ID,
      clientSecret: env.GITHUB_CLIENT_SECRET,
    },
  },
  plugins: [bearer()],
})

Expected behavior
no error

Screenshots
image

Did you mean './crypto'?
Requests that should resolve in the current directory need to start with './'.
Requests that start with a name are treated as module requests and resolve within module directories (node_modules, /home/goodcheng/.volta/tools/shared, /home/goodcheng/Development/SelfCode/collectionbox/apps/web).
If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too.

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
../../packages/auth/src/auth.ts
../../packages/auth/src/index.ts
./src/app/api/auth/[...all]/route.ts
../../node_modules/next/dist/build/webpack/loaders/next-edge-app-route-loader/index.js?absolutePagePath=%2Fhome%2Fgoodcheng%2FDevelopment%2FSelfCode%2Fcollectionbox%2Fapps%2Fweb%2Fsrc%2Fapp%2Fapi%2Fauth%2F%5B...all%5D%2Froute.ts&page=%2Fapi%2Fauth%2F%5B...all%5D%2Froute&appDirLoader=bmV4dC1hcHAtbG9hZGVyP25hbWU9YXBwJTJGYXBpJTJGYXV0aCUyRiU1Qi4uLmFsbCU1RCUyRnJvdXRlJnBhZ2U9JTJGYXBpJTJGYXV0aCUyRiU1Qi4uLmFsbCU1RCUyRnJvdXRlJmFwcFBhdGhzPSZwYWdlUGF0aD1wcml2YXRlLW5leHQtYXBwLWRpciUyRmFwaSUyRmF1dGglMkYlNUIuLi5hbGwlNUQlMkZyb3V0ZS50cyZhcHBEaXI9JTJGaG9tZSUyRmdvb2RjaGVuZyUyRkRldmVsb3BtZW50JTJGU2VsZkNvZGUlMkZjb2xsZWN0aW9uYm94JTJGYXBwcyUyRndlYiUyRnNyYyUyRmFwcCZwYWdlRXh0ZW5zaW9ucz10c3gmcGFnZUV4dGVuc2lvbnM9dHMmcGFnZUV4dGVuc2lvbnM9anN4JnBhZ2VFeHRlbnNpb25zPWpzJnJvb3REaXI9JTJGaG9tZSUyRmdvb2RjaGVuZyUyRkRldmVsb3BtZW50JTJGU2VsZkNvZGUlMkZjb2xsZWN0aW9uYm94JTJGYXBwcyUyRndlYiZpc0Rldj10cnVlJnRzY29uZmlnUGF0aD10c2NvbmZpZy5qc29uJmJhc2VQYXRoPSZhc3NldFByZWZpeD0mbmV4dENvbmZpZ091dHB1dD0mcHJlZmVycmVkUmVnaW9uPSZtaWRkbGV3YXJlQ29uZmlnPWUzMCUzRCE%3D&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D!

Desktop (please complete the following information):

"better-auth": "0.5.3-beta.1"

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

Originally created by @goodchengwang on GitHub (Oct 22, 2024). **Describe the bug** use bearer plugin causes an error module not found: Can't resolve 'crypto' **To Reproduce** Steps to reproduce the behavior: Using the bearer plugin causes an error ```ts import { betterAuth } from 'better-auth' import { drizzleAdapter } from 'better-auth/adapters/drizzle' import { bearer } from 'better-auth/plugins' import { db } from '@acme/db/client' // your drizzle instance import { env } from '../env' export const auth = betterAuth({ database: drizzleAdapter(db, { provider: 'sqlite', }), socialProviders: { github: { clientId: env.GITHUB_CLIENT_ID, clientSecret: env.GITHUB_CLIENT_SECRET, }, }, plugins: [bearer()], }) ``` **Expected behavior** no error **Screenshots** ![image](https://github.com/user-attachments/assets/007d4c88-8267-4980-94e5-65807b8e2c23) ```js Did you mean './crypto'? Requests that should resolve in the current directory need to start with './'. Requests that start with a name are treated as module requests and resolve within module directories (node_modules, /home/goodcheng/.volta/tools/shared, /home/goodcheng/Development/SelfCode/collectionbox/apps/web). If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too. https://nextjs.org/docs/messages/module-not-found Import trace for requested module: ../../packages/auth/src/auth.ts ../../packages/auth/src/index.ts ./src/app/api/auth/[...all]/route.ts ../../node_modules/next/dist/build/webpack/loaders/next-edge-app-route-loader/index.js?absolutePagePath=%2Fhome%2Fgoodcheng%2FDevelopment%2FSelfCode%2Fcollectionbox%2Fapps%2Fweb%2Fsrc%2Fapp%2Fapi%2Fauth%2F%5B...all%5D%2Froute.ts&page=%2Fapi%2Fauth%2F%5B...all%5D%2Froute&appDirLoader=bmV4dC1hcHAtbG9hZGVyP25hbWU9YXBwJTJGYXBpJTJGYXV0aCUyRiU1Qi4uLmFsbCU1RCUyRnJvdXRlJnBhZ2U9JTJGYXBpJTJGYXV0aCUyRiU1Qi4uLmFsbCU1RCUyRnJvdXRlJmFwcFBhdGhzPSZwYWdlUGF0aD1wcml2YXRlLW5leHQtYXBwLWRpciUyRmFwaSUyRmF1dGglMkYlNUIuLi5hbGwlNUQlMkZyb3V0ZS50cyZhcHBEaXI9JTJGaG9tZSUyRmdvb2RjaGVuZyUyRkRldmVsb3BtZW50JTJGU2VsZkNvZGUlMkZjb2xsZWN0aW9uYm94JTJGYXBwcyUyRndlYiUyRnNyYyUyRmFwcCZwYWdlRXh0ZW5zaW9ucz10c3gmcGFnZUV4dGVuc2lvbnM9dHMmcGFnZUV4dGVuc2lvbnM9anN4JnBhZ2VFeHRlbnNpb25zPWpzJnJvb3REaXI9JTJGaG9tZSUyRmdvb2RjaGVuZyUyRkRldmVsb3BtZW50JTJGU2VsZkNvZGUlMkZjb2xsZWN0aW9uYm94JTJGYXBwcyUyRndlYiZpc0Rldj10cnVlJnRzY29uZmlnUGF0aD10c2NvbmZpZy5qc29uJmJhc2VQYXRoPSZhc3NldFByZWZpeD0mbmV4dENvbmZpZ091dHB1dD0mcHJlZmVycmVkUmVnaW9uPSZtaWRkbGV3YXJlQ29uZmlnPWUzMCUzRCE%3D&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D! ``` **Desktop (please complete the following information):** "better-auth": "0.5.3-beta.1" **Smartphone (please complete the following information):** - Device: [e.g. iPhone6] - OS: [e.g. iOS8.1] - Browser [e.g. stock browser, safari] - Version [e.g. 22] **Additional context** Add any other context about the problem here.
Author
Owner

@firstaxel commented on GitHub (Oct 25, 2024):

You need to remove the export runtime = "edge" from your API auth route [...all]/route.ts cause better Auth is not yet supported on the edge
That is why you are getting the crypto is not found on the edge cause edge doesn't support crypto or fs
So remove the export runtime = "edge" from the API route

@firstaxel commented on GitHub (Oct 25, 2024): You need to remove the export runtime = "edge" from your API auth route [...all]/route.ts cause better Auth is not yet supported on the edge That is why you are getting the crypto is not found on the edge cause edge doesn't support crypto or fs So remove the `export runtime = "edge" ` from the API route
Author
Owner

@goodchengwang commented on GitHub (Oct 26, 2024):

I use cloudflare d1 database and I want to deploy it to the cloudflare, if I remove the export const runtime = "edge", it will not work.

@goodchengwang commented on GitHub (Oct 26, 2024): I use cloudflare d1 database and I want to deploy it to the cloudflare, if I remove the `export const runtime = "edge"`, it will not work.
Author
Owner

@goodchengwang commented on GitHub (Oct 26, 2024):

maybe can use the web api like the next-auth https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API

@goodchengwang commented on GitHub (Oct 26, 2024): maybe can use the web api like the next-auth https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API
Author
Owner

@goodchengwang commented on GitHub (Oct 26, 2024):

using cloudflare worker deploy can solve this problem, but it builds large 1MB files, not good for free plan

@goodchengwang commented on GitHub (Oct 26, 2024): using cloudflare [worker deploy](https://developers.cloudflare.com/workers/frameworks/framework-guides/nextjs/) can solve this problem, but it builds large 1MB files, not good for free plan
Author
Owner

@Bekacru commented on GitHub (Nov 17, 2024):

this should be resolved by #456

@Bekacru commented on GitHub (Nov 17, 2024): this should be resolved by #456
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#122