sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

[Next.js] Figure out a way to silence the `import/require-in-the-middle` warnings for Turbopack

Open lforst opened this issue 10 months ago • 20 comments

Description

Seems like Turbopack is more strict in what it will do when there would be 2 different versions of a package it could bundle. Currently it will refuse to build (?). Maybe we can iron out the kinks here.

 ⚠ ./node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/instrumentation/build/esm/platform/node
Package import-in-the-middle can't be external
The request import-in-the-middle matches serverExternalPackages (or the default list).
The request could not be resolved by Node.js from the project directory.
Packages that should be external need to be installed in the project directory, so they can be resolved from the output files.
Try to install it into the project directory by running npm install import-in-the-middle from the project directory.

lforst avatar Jan 20 '25 08:01 lforst

Can these be ignored?

benjick avatar Feb 17 '25 21:02 benjick

Which warnings are these and what do they look like? Do these warnings still exist now that in v9 we use import-in-the-middle in a mode where it doesn't attempt to instrument everything?

timfish avatar Feb 17 '25 22:02 timfish

These are the ones I get:

 ○ Compiling /login ...
 ✓ Compiled /login in 1488ms
 ⚠ ./node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/instrumentation/build/esm/platform/node
Package import-in-the-middle can't be external
The request import-in-the-middle matches serverExternalPackages (or the default list).
The request could not be resolved by Node.js from the project directory.
Packages that should be external need to be installed in the project directory, so they can be resolved from the output files.
Try to install it into the project directory by running npm install import-in-the-middle from the project directory.


 ⚠ ./node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/instrumentation/build/esm/platform/node
Package import-in-the-middle can't be external
The request import-in-the-middle matches serverExternalPackages (or the default list).
The request could not be resolved by Node.js from the project directory.
Packages that should be external need to be installed in the project directory, so they can be resolved from the output files.
Try to install it into the project directory by running npm install import-in-the-middle from the project directory.


 ⚠ ./node_modules/.pnpm/@[email protected]/node_modules/@sentry/node/build/cjs/sdk
Package import-in-the-middle can't be external
The request import-in-the-middle matches serverExternalPackages (or the default list).
The request could not be resolved by Node.js from the project directory.
Packages that should be external need to be installed in the project directory, so they can be resolved from the output files.
Try to install it into the project directory by running npm install import-in-the-middle from the project directory.


 ⚠ ./node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/instrumentation/build/esm/platform/node
Package require-in-the-middle can't be external
The request require-in-the-middle matches serverExternalPackages (or the default list).
The request could not be resolved by Node.js from the project directory.
Packages that should be external need to be installed in the project directory, so they can be resolved from the output files.
Try to install it into the project directory by running npm install require-in-the-middle from the project directory.


 ⚠ ./node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/instrumentation/build/esm/platform/node
Package require-in-the-middle can't be external
The request require-in-the-middle matches serverExternalPackages (or the default list).
The request could not be resolved by Node.js from the project directory.
Packages that should be external need to be installed in the project directory, so they can be resolved from the output files.
Try to install it into the project directory by running npm install require-in-the-middle from the project directory.

benjick avatar Feb 18 '25 06:02 benjick

having same issues, two different versions of import-in-the-middle in @sentry/[email protected] causing the warnings ⚠ ./node_modules/@opentelemetry/instrumentation/build/esm/platform/node Package import-in-the-middle can't be external The request import-in-the-middle matches serverExternalPackages (or the default list). The package resolves to a different version when requested from the project directory (1.13.1) compared to the package requested from the importing module (1.11.2). Make sure to install the same version of the package in both locations.

novelnet avatar Mar 24 '25 13:03 novelnet

@novelnet Yes, this is fixed by adding an override to your package manager to install one specific version. In the case of import-in-the-middle, the later version is usually the better choice.

lforst avatar Mar 24 '25 13:03 lforst

@lforst thank you, but means to keep track on all sentry updates which may change package versions. Would be nice to fix this on sentry's side

novelnet avatar Mar 24 '25 14:03 novelnet

This is not 100% fixable on our side. We are not in control of other dependencies and what they install.

lforst avatar Mar 24 '25 14:03 lforst

What should we do with the OP's case? (Not the version mismatch).

MonstraG avatar Apr 03 '25 08:04 MonstraG

@MonstraG maybe what the error message suggests?

Try to install it into the project directory by running npm install import-in-the-middle from the project directory.

lforst avatar Apr 03 '25 08:04 lforst

Is this unsolvable? It makes developing so annoying

benjick avatar May 22 '25 12:05 benjick

Hey @benjick have you tried adding

public-hoist-pattern[]=*import-in-the-middle*
public-hoist-pattern[]=*require-in-the-middle*

to .npmrc?

or

pnpm add import-in-the-middle require-in-the-middle?

chargome avatar May 23 '25 10:05 chargome

Using yarn, I get a different error more specific to the versioning of import-in-the-middle:


 ⚠ ./node_modules/@sentry/node/node_modules/@opentelemetry/instrumentation/build/esm/platform/node
Package import-in-the-middle can't be external
The request import-in-the-middle matches serverExternalPackages (or the default list).
The package resolves to a different version when requested from the project directory (1.11.2) compared to the package requested from the importing module (1.14.0).
Make sure to install the same version of the package in both locations.

Kief5555 avatar May 31 '25 16:05 Kief5555

The request import-in-the-middle matches serverExternalPackages (or the default list).

@Kief5555 could you try an override so you just get the 1.14.0 version?

chargome avatar Jun 02 '25 08:06 chargome

@chargome ~~that worked for me, thanks a lot!~~

edit: it actually brought on other errors, like eslint not being found by packages in the workspace

benjick avatar Jun 02 '25 10:06 benjick

The request import-in-the-middle matches serverExternalPackages (or the default list).

@Kief5555 could you try an override so you just get the 1.14.0 version?

It caused a lot more issues.

Kief5555 avatar Jun 02 '25 15:06 Kief5555

I assume you already tried just installing the package in your project?

chargome avatar Jun 03 '25 07:06 chargome

I assume you already tried just installing the package in your project?

Yes. Met with a lot more errors

Kief5555 avatar Jun 12 '25 17:06 Kief5555

If anyone can provide a minimal reproduction for this, we'd greatly appreciate it, thanks!

Lms24 avatar Jun 13 '25 14:06 Lms24

https://github.com/MonstraG/sentry-in-the-middle-bug

Steps:

  1. clone
  2. pnpm i
  3. pnpm run dev
  4. open localhost:3000
  5. check terminal where you did pnpm run dev

MonstraG avatar Jun 13 '25 15:06 MonstraG

Thanks @MonstraG as always :) we'll look into it.

andreiborza avatar Jun 16 '25 08:06 andreiborza

How's it going? Debugging other issues in dev mode with this issue on-going is incredibly annoying. Will it ever get fixed?

benjick avatar Aug 21 '25 11:08 benjick

Taking a look at this, thanks for the repro and the patience!

chargome avatar Aug 25 '25 07:08 chargome

So Turbopack expects external packages to be resolvable from the project root, but pnpm's hoisting mechanics isolate dependencies in directories that Turbopack (maybe?) cannot traverse. This is an internal hoisting mechanics issue between two external tools (pnpm and Turbopack) which is why we can't fix this in the SDK.

Our documentation provides two working solutions (also in the case of the repro):

  • Direct installation: pnpm add import-in-the-middle require-in-the-middle
  • Opt-out of hoisting via .npmrc:
public-hoist-pattern[]=*import-in-the-middle*
public-hoist-pattern[]=*require-in-the-middle*

Both solutions resolve the warnings by making these packages available at the project root where Turbopack can find them.

Closing this as this is currently not actionable for us, but I will reach out to the Vercel team in case they can help out here!

chargome avatar Aug 25 '25 08:08 chargome

Tried the solution from troubleshooting documentation but did not solve the problem.

What seems to work for me is:

  1. delete pnpm-lock.yaml
  2. update the next.config.ts with
const nextConfig: NextConfig = {
  transpilePackages: ["require-in-the-middle"],
  /* config options here */
};

using versions

"@sentry/nextjs": "^10.15.0",
"next": "15.3.3"

Also the approach reported in https://github.com/getsentry/sentry-javascript/issues/15209#issuecomment-2864028787 installing require-in-the-middle as devDependency worked.

dnhyde avatar Oct 20 '25 17:10 dnhyde