next.js
next.js copied to clipboard
Turbopack loaders don't work with middleware
Link to the code that reproduces this issue
https://github.com/dlehmhus/turbopack-loader-middleware-bug
To Reproduce
- Run
npm run dev - Open website and see error stacktrace
Current vs. Expected behavior
When using a loader in the turbopack rules like this:
// next.config.ts
turbopack: {
rules: {
"*.ts": {
loaders: [
{
loader: join(__dirname, "./custom-loader.js"),
options: {},
},
],
},
},
},
// custom-loader.js
module.exports = (code) => {
return code;
};
and having a middleware at the same time, the app crashes with the following error:
Error: [turbopack-node]/transforms/transforms.ts:53:10
lint TP1006 path.join(???*0*, (???*2* ? ???*5* : ???*7*)) is very dynamic
51 | }
52 | export const fromPath = (path: string) => {
> 53 | return join(contextDir, sep !== '/' ? path.replaceAll('/', sep) : path)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
54 | }
55 |
56 | // Patch process.env to track which env vars are read
- *0* process.cwd*1*()
⚠️ process.cwd is not specified in the environment
⚠️ This value might have side effects
- *1* process.cwd: The Node.js process.cwd method: https://nodejs.org/api/process.html#processcwd
- *2* (???*3* !== "/")
⚠️ nested operation
- *3* path*4*["sep"]
⚠️ unsupported property on Node.js path module
⚠️ This value might have side effects
- *4* path: The Node.js path module: https://nodejs.org/api/path.html
- *5* ???*6*["replaceAll"]("/", module<path, {}>["sep"])
⚠️ unknown callee object
- *6* arguments[0]
⚠️ function calls are not analysed yet
- *7* arguments[0]
⚠️ function calls are not analysed yet
at <unknown> (Error: [turbopack-node]/transforms/transforms.ts:53:10)
at Object.getCompilationErrors (file:///path/to/project/turbopack-loader-middleware-bug/node_modules/next/dist/server/dev/hot-reloader-turbopack.js:717:55)
at DevBundlerService.getCompilationError (file:///path/to/project/turbopack-loader-middleware-bug/node_modules/next/dist/server/lib/dev-bundler-service.js:39:55)
at DevServer.getCompilationError (file:///path/to/project/turbopack-loader-middleware-bug/node_modules/next/dist/server/dev/next-dev-server.js:663:42)
at DevServer.findPageComponents (file:///path/to/project/turbopack-loader-middleware-bug/node_modules/next/dist/server/dev/next-dev-server.js:633:43)
at async DevServer.renderErrorToResponseImpl (file:///path/to/project/turbopack-loader-middleware-bug/node_modules/next/dist/server/base-server.js:2645:26)
Provide environment information
Node.js v22.9.0
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 24.4.0: Fri Apr 11 18:33:46 PDT 2025; root:xnu-11417.101.15~117/RELEASE_ARM64_T8112
Available memory (MB): 24576
Available CPU cores: 8
Binaries:
Node: 22.9.0
npm: 10.8.3
Yarn: 1.22.22
pnpm: N/A
Relevant Packages:
next: 15.4.0-canary.50 // Latest available version is detected (15.4.0-canary.50).
eslint-config-next: N/A
react: 19.1.0
react-dom: 19.1.0
typescript: 5.8.3
Next.js Config:
output: N/A
Which area(s) are affected? (Select all that apply)
Turbopack, Middleware
Which stage(s) are affected? (Select all that apply)
next dev (local)
Additional context
No response
@timneutkens @unstubbable Is there any process for this issue? Or could you please provide some advice how we should modify our loader?
@zh-lx Like this issue #83613 , waiting for PR merge