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: "Cannot find module" when package require() a package installed via the optionalDependencies field in package.json #63850

Closed
pilcrowOnPaper opened this issue Mar 28, 2024 · 8 comments · Fixed by #65204
Labels
bug Issue was opened via the bug report template. linear: turbopack Confirmed issue that is tracked by the Turbopack team. locked Turbopack Related to Turbopack with Next.js.

Comments

@pilcrowOnPaper
Copy link

pilcrowOnPaper commented Mar 28, 2024

Link to the code that reproduces this issue

https://github.com/pilcrowOnPaper/repro-nextjs-turbopack-optional-dependencies

To Reproduce

  1. Start dev server with pnpm dev

Current vs. Expected behavior

I expect the dev server to not crash

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.4.0: Wed Feb 21 21:44:54 PST 2024; root:xnu-10063.101.15~2/RELEASE_ARM64_T6030
  Available memory (MB): 36864
  Available CPU cores: 11
Binaries:
  Node: 21.7.0
  npm: 10.5.0
  Yarn: N/A
  pnpm: 8.15.4
Relevant Packages:
  next: 14.2.0-canary.48 // Latest available version is detected (14.2.0-canary.48).
  eslint-config-next: 14.1.0
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.4.3
Next.js Config:
  output: N/A

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

Package manager (npm, pnpm, Yarn), Turbopack (--turbo)

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

next dev (local)

Additional context

No response

PACK-2925

@pilcrowOnPaper pilcrowOnPaper added the bug Issue was opened via the bug report template. label Mar 28, 2024
@pilcrowOnPaper pilcrowOnPaper changed the title Turbopack: "Cannot find module" when package require() a package installed via the "optionalDependecies" field in package.json Turbopack: "Cannot find module" when package require() a package installed via the optionalDependecies field in package.json Mar 28, 2024
@github-actions github-actions bot added the Turbopack Related to Turbopack with Next.js. label Mar 28, 2024
@pilcrowOnPaper pilcrowOnPaper changed the title Turbopack: "Cannot find module" when package require() a package installed via the optionalDependecies field in package.json Turbopack: "Cannot find module" when package require() a package installed via the optionalDependencies field in package.json Mar 28, 2024
@rwieruch

This comment was marked as off-topic.

@padmaia padmaia added the linear: turbopack Confirmed issue that is tracked by the Turbopack team. label Apr 9, 2024
@timneutkens
Copy link
Member

timneutkens commented Apr 30, 2024

Just double checked and it seems that this isn't specific to Turbopack? I can see the same error when running with webpack.

I.e. changing the dev script to next dev: https://codesandbox.io/p/github/pilcrowOnPaper/repro-nextjs-turbopack-optional-dependencies/csb-78d4k5/draft/youthful-diffie?file=%2Fpackage.json%3A15%2C23

Seems @node-rs/argon2 just can't be bundled and needs to be added to the list of external packages, argon2 is already in there: https://github.com/vercel/next.js/blob/canary/packages/next/src/lib/server-external-packages.json#L15, maybe it was changed to @node-rs/ recently. Will open a PR to add it.

Here's an example with serverComponentsExternalPackages manually added: https://codesandbox.io/p/github/pilcrowOnPaper/repro-nextjs-turbopack-optional-dependencies/csb-p9q66w/draft/tender-rain?file=%2Fnext.config.mjs

@timneutkens
Copy link
Member

timneutkens commented Apr 30, 2024

Opened #65204 to address this. Added additional details and made sure oslo is also in there, that way Lucia should work automatically, you'll no longer have to edit next.config.js when adding Lucia 👍

@pilcrowOnPaper
Copy link
Author

You seem to be correct - apologies for that!

The error was similar to the ones I got when when importing oslo/password (which imports @node-rs/argon2) even with serverComponentsExternalPackages, so I just assumed it was an issue with @node-rs/argon2 and Turbopack. I'll try to see what's causing the issues with oslo/password and Turbopack.

ijjk pushed a commit that referenced this issue Apr 30, 2024
…65204)

## What?

Initially added `@node-rs/argon2` to the list as `argon2` was already in
the list to make the provided reproduction pass, however, when digging
deeper into Lucia I found that `oslo` also uses `@node-rs/bcrypt`.

I also looked at Lucia example
[1](https://github.com/lucia-auth/examples/blob/main/nextjs-app/username-and-password/next.config.js#L2-L6)
and
[2](https://github.com/lucia-auth/examples/blob/main/nextjs-app/username-and-password/next.config.js#L2-L6)
which show that this configuration is always added:
`serverComponentsExternalPackages: ["oslo"]`.

That shouldn't be needed as we keep a list of packages that can't be
bundled in Next.js itself, so I've updated the list to add `oslo`,
`@node-rs/argon2`, and `@node-rs/bcrypt`.

This makes it so that you don't have to add additional configuration to
use Lucia.

Fixes #63850


<!-- 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-3264
@timneutkens
Copy link
Member

Thanks for checking @pilcrowOnPaper, can you confirm it's fixed with next@canary? Just to double check you no longer need custom configuration 👍

@pilcrowOnPaper
Copy link
Author

Thanks, that seems to have fixed the issue! Though, bad timing since we're planning to remove oslo/password due to various issues (including this) so I think I'll send a PR removing oslo from the list when we release a new major version. Still, having @node-rs/argon2 excluded by default is super helpful!

@timneutkens
Copy link
Member

Awesome, thanks for checking! Yeah it's fine to remove it later, overall it doesn't really hurt to have it external and the older version of Lucia people use would still have the version of oslo included so probably fine to leave it for a few months.

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 16, 2024
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 Turbopack Related to Turbopack with Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants