vercel / next.js

The React Framework
https://nextjs.org
MIT License
124.28k stars 26.49k forks source link

turbopack : module not found: can't resolve crypto --> jsonwebtoken package uses crypto module #64464

Closed ehsanrezaeee closed 3 months ago

ehsanrezaeee commented 4 months ago

Link to the code that reproduces this issue

https://github.com/ehsanrezaeee/test-jwt

To Reproduce

  1. Change the package.json to use "next dev --turbo"
  2. in you app install jsonwebtoken package with npm i jsonwebtoken
  3. run the app with npm run dev and the error shows up

Current vs. Expected behavior

i expected to redirected to a page in my app with middleware logic. however i encounter the following error:

Failed to compile

Next.js (14.2.1) (turbo)
./node_modules/jsonwebtoken/verify.js:9:55
Module not found: Can't resolve 'crypto'
   7 | const PS_SUPPORTED = require('./lib/psSupported');
   8 | const jws = require('jws');
>  9 | const {KeyObject, createSecretKey, createPublicKey} = require("crypto");```

### Provide environment information

```bash
Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:11:05 PDT 2024; root:xnu-10063.101.17~1/RELEASE_X86_64
  Available memory (MB): 16384
  Available CPU cores: 12
Binaries:
  Node: 21.6.1
  npm: 10.5.1
  Yarn: 1.22.21
  pnpm: N/A
Relevant Packages:
  next: 14.2.1 // Latest available version is detected (14.2.1).
  eslint-config-next: 14.0.4
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.4.4
Next.js Config:
  output: standalone

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

App Router, Data fetching (gS(S)P, getInitialProps), Middleware / Edge (API routes, runtime)

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

next dev (local), next build (local), next start (local), Other (Deployed)

Additional context

i tested my app with nextjs v14.2.1 latest version

PACK-2954

ehsanrezaeee commented 4 months ago

i use jsonwebtoken package in the middleware. i think the turbopack cant work with modules belong to node js environment exclusively

ehsanrezaeee commented 4 months ago

if anyone wants a workaround, you can use "jose" instead of "jsonwebtoken" npm package

ftognetto commented 4 months ago

Hello, I'm using crypto directly

import crypto from 'crypto';
export const crypt = (password: string): string =>
  crypto.createHash('md5').update(crypto.createHash('sha1').update(password).digest('hex')).digest('hex');

Inside the authorized callback in next-auth, and I have the same issue with--turbo

Error: Cannot find module 'crypto'
    at <unknown> (./src/lib/crypt.ts:0:0)
    at <unknown> (./src/lib/crypt.ts:0:0)
    at [project]/src/lib/crypt.ts [middleware] (ecmascript) (./file:///Users/fabriziotognetto/.../.next/server/edge/chunks/ssr/src_7cd4a5._.js:18:3)
    at <unknown> (./[turbopack]/dev/runtime/base/runtime-base.ts:347:21)
    at runModuleExecutionHooks (./[turbopack]/dev/runtime/base/runtime-base.ts:403:5)
    at instantiateModule (./[turbopack]/dev/runtime/base/runtime-base.ts:345:5)
    at getOrInstantiateModuleFromParent (./[turbopack]/dev/runtime/base/runtime-base.ts:451:10)
    at esmImport (./[turbopack]/shared/runtime-utils.ts:215:18)
    at <unknown> (./src/app/(auth)/auth.ts:0:0)
    at [project]/src/app/(auth)/auth.ts [middleware] (ecmascript) (./file:///Users/fabriziotognetto/.../.next/server/edge/chunks/ssr/src_7cd4a5._.js:147:3) {
  digest: undefined
}
  1 | import crypto from 'crypto';
  2 | export const crypt = (password: string): string =>
  3 |   crypto.createHash('md5').update(crypto.createHash('sha1').update(password).digest('hex')).digest('hex');
timneutkens commented 3 months ago

Thanks for the report with a great reproduction! I've opened a fix here: #65171.

timneutkens commented 3 months ago

Verified the reproduction correctly compiles on next@canary (which holds the fix in #65171) 🎉

Sandbox: https://codesandbox.io/p/github/ehsanrezaeee/test-jwt/csb-pgkx4v/draft/gallant-monad?file=%2Fsrc%2Fmiddleware.ts%3A8%2C20

ehsanrezaeee commented 3 months ago

Verified the reproduction correctly compiles on next@canary (which holds the fix in #65171) 🎉

Sandbox: https://codesandbox.io/p/github/ehsanrezaeee/test-jwt/csb-pgkx4v/draft/gallant-monad?file=%2Fsrc%2Fmiddleware.ts%3A8%2C20

now works properly. thanks

timneutkens commented 3 months ago

Thanks for confirming!

github-actions[bot] commented 3 months ago

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.