Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ehsanrezaeee opened this issue Apr 14, 2024 · 8 comments · Fixed by #65171
Labels
bug Issue was opened via the bug report template. linear: turbopack Confirmed issue that is tracked by the Turbopack team. locked Pages Router Related to Pages Router. Runtime Related to Node.js or Edge Runtime with Next.js. Turbopack Related to Turbopack with Next.js.

Comments

@ehsanrezaeee
Copy link

ehsanrezaeee commented Apr 14, 2024

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 ehsanrezaeee added the bug Issue was opened via the bug report template. label Apr 14, 2024
@github-actions github-actions bot added Pages Router Related to Pages Router. Runtime Related to Node.js or Edge Runtime with Next.js. labels Apr 14, 2024
@ehsanrezaeee
Copy link
Author

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

@timneutkens timneutkens added linear: turbopack Confirmed issue that is tracked by the Turbopack team. Turbopack Related to Turbopack with Next.js. labels Apr 14, 2024
@ehsanrezaeee
Copy link
Author

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

@ftognetto
Copy link

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
Copy link
Member

timneutkens commented Apr 29, 2024

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

timneutkens added a commit that referenced this issue Apr 30, 2024
## What?

Ensures just importing `crypto` does not error, only when it is used it
shows an error in the edge runtime. This matches webpack behavior. The
`crypto` module was missing the list of unsupported packages in the
Next.js Turbopack integration.

Fixes #64464
Fixes PACK-2954

## TODO

While adding tests for this issue I found another bug that only happens
with webpack.

Specifically these 4 packages are accidentally being polyfilled even
when they're not set up to be polyfilled. i.e. there's no npm package
installed for polyfilling them through aliasing or such. Even in that
case `punycode`, `process`, `querystring`, and `string_decorder` get
polyfilled regardless, this causes the newly added test to fail.

Removing the polyfills would be potentially breaking so we'll want to
change it in Next.js 15 instead.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-3252
@timneutkens
Copy link
Member

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
Copy link
Author

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
Copy link
Member

Thanks for confirming!

Copy link
Contributor

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 15, 2024
ForsakenHarmony pushed a commit that referenced this issue Aug 14, 2024
Ensures just importing `crypto` does not error, only when it is used it
shows an error in the edge runtime. This matches webpack behavior. The
`crypto` module was missing the list of unsupported packages in the
Next.js Turbopack integration.

Fixes #64464
Fixes PACK-2954

While adding tests for this issue I found another bug that only happens
with webpack.

Specifically these 4 packages are accidentally being polyfilled even
when they're not set up to be polyfilled. i.e. there's no npm package
installed for polyfilling them through aliasing or such. Even in that
case `punycode`, `process`, `querystring`, and `string_decorder` get
polyfilled regardless, this causes the newly added test to fail.

Removing the polyfills would be potentially breaking so we'll want to
change it in Next.js 15 instead.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

Closes NEXT-
Fixes #

-->

Closes NEXT-3252
ForsakenHarmony pushed a commit that referenced this issue Aug 15, 2024
## What?

Ensures just importing `crypto` does not error, only when it is used it
shows an error in the edge runtime. This matches webpack behavior. The
`crypto` module was missing the list of unsupported packages in the
Next.js Turbopack integration.

Fixes #64464
Fixes PACK-2954

## TODO

While adding tests for this issue I found another bug that only happens
with webpack.

Specifically these 4 packages are accidentally being polyfilled even
when they're not set up to be polyfilled. i.e. there's no npm package
installed for polyfilling them through aliasing or such. Even in that
case `punycode`, `process`, `querystring`, and `string_decorder` get
polyfilled regardless, this causes the newly added test to fail.

Removing the polyfills would be potentially breaking so we'll want to
change it in Next.js 15 instead.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-3252
ForsakenHarmony pushed a commit that referenced this issue Aug 16, 2024
## What?

Ensures just importing `crypto` does not error, only when it is used it
shows an error in the edge runtime. This matches webpack behavior. The
`crypto` module was missing the list of unsupported packages in the
Next.js Turbopack integration.

Fixes #64464
Fixes PACK-2954

## TODO

While adding tests for this issue I found another bug that only happens
with webpack.

Specifically these 4 packages are accidentally being polyfilled even
when they're not set up to be polyfilled. i.e. there's no npm package
installed for polyfilling them through aliasing or such. Even in that
case `punycode`, `process`, `querystring`, and `string_decorder` get
polyfilled regardless, this causes the newly added test to fail.

Removing the polyfills would be potentially breaking so we'll want to
change it in Next.js 15 instead.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-3252
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. linear: turbopack Confirmed issue that is tracked by the Turbopack team. locked Pages Router Related to Pages Router. Runtime Related to Node.js or Edge Runtime with Next.js. Turbopack Related to Turbopack with Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants