next.js: Module not found: Can't resolve 'crypto'

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

❯ npx --no-install next info

    Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.4.0: Mon Mar  6 20:59:28 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6000
    Binaries:
      Node: 19.1.0
      npm: 8.19.3
      Yarn: 3.3.0
      pnpm: 7.24.3
    Relevant packages:
      next: 13.4.5-canary.0
      eslint-config-next: 13.4.2
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.0.4

Which area(s) of Next.js are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue or a replay of the bug

To Reproduce

/demo/page.tsx:

import { authOptions } from '@/lib/next-auth';
import { getServerSession } from 'next-auth/next';

export default async function Page() {
  const session = await getServerSession(authOptions);

  return (
    <>
      <pre>{JSON.stringify(session)}</pre>
    </>
  );
}

export const dynamic = 'force-dynamic';

Describe the Bug

works on 13.3.1

image

but when i tried 13.3.4-13.4.4 or canary

image

13.3.2,13.3.3 when started, cannot open localhost:3000 always loading.

Expected Behavior

Which browser are you using? (if relevant)

Brave 113

How are you deploying your application? (if relevant)

Vercel

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 23 (3 by maintainers)

Most upvoted comments

(please refrain from mass tagging maintainers in the future)

This is correct, crypto does not need to be imported.

The current version of next-auth is not edge compatible yet anyway, but the upcoming version will be.

So this error is currently expected.

I’m having the same issue. I tried using the latest 13.4.5-canary.3 and the error is still there.

It can be reproduced by just creating a simple route:

import { randomUUID } from "crypto"

export const runtime = "edge"

export function GET() {
  return new Response(JSON.stringify({ uuid: randomUUID() }), { status: 200 })
}

It works by removing the runtime export.

Also, I’m not sure if there is anything to do with it, but i’m using nvm.

I added the following in package.json and it was fixed:

{
  "browser": {
    "crypto": false,
    "stream": false
  }
}
image
- event compiled client and server successfully in 885 ms (1020 modules)
- error TypeError: Cannot read properties of undefined (reading 'substring')
    at __webpack_require__ (/Users/v0/Projects/Temp/crypto-bug-mvp/.next/server/edge-runtime-webpack.js:37:33)
    at fn (/Users/v0/Projects/Temp/crypto-bug-mvp/.next/server/edge-runtime-webpack.js:281:21)
    at __webpack_require__ (/Users/v0/Projects/Temp/crypto-bug-mvp/.next/server/edge-runtime-webpack.js:37:33)
    at fn (/Users/v0/Projects/Temp/crypto-bug-mvp/.next/server/edge-runtime-webpack.js:281:21)
- error TypeError: Cannot read properties of undefined (reading 'substring')
    at __webpack_require__ (/Users/v0/Projects/Temp/crypto-bug-mvp/.next/server/edge-runtime-webpack.js:37:33)
    at fn (/Users/v0/Projects/Temp/crypto-bug-mvp/.next/server/edge-runtime-webpack.js:281:21)
    at __webpack_require__ (/Users/v0/Projects/Temp/crypto-bug-mvp/.next/server/edge-runtime-webpack.js:37:33)
    at fn (/Users/v0/Projects/Temp/crypto-bug-mvp/.next/server/edge-runtime-webpack.js:281:21)
digest: "1179593499"

nodejs: v19.1.0

Clone this repo: https://github.com/wshow/crypto-bug-mvp

set ENV:

GITHUB_ID=xxx
GITHUB_SECRET=xxx

github oauth app redirect set http://localhost:3000/api/auth/callback/github

npm run dev

then login.