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 does not resolve serverExternalPackages #65828

Open
CHC383 opened this issue May 15, 2024 · 1 comment
Open

Turbopack does not resolve serverExternalPackages #65828

CHC383 opened this issue May 15, 2024 · 1 comment
Labels
bug Issue was opened via the bug report template. linear: turbopack Confirmed issue that is tracked by the Turbopack team. Module Resolution Module resolution (CJS / ESM, module resolving) Turbopack Related to Turbopack with Next.js.

Comments

@CHC383
Copy link

CHC383 commented May 15, 2024

Link to the code that reproduces this issue

https://github.com/CHC383/nextjs-nextlogger-config-bug-example/tree/turbopack-bug

To Reproduce

Start the application in dev mode (npm run dev) and check the console output

Current vs. Expected behavior

  • Current behavior: Error: An error occurred while loading instrumentation hook: Cannot find module as expression is too dynamic
  • Expected behavior: The application could start and load the next-logger config

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.5.0: Wed May  1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000
  Available memory (MB): 32768
  Available CPU cores: 8
Binaries:
  Node: 21.6.2
  npm: 10.2.4
  Yarn: 1.22.21
  pnpm: 8.14.3
Relevant Packages:
  next: 14.3.0-canary.63 // Latest available version is detected (14.3.0-canary.63).
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

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

Module Resolution, Turbopack

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

next dev (local)

Additional context

Original issue of loading next-logger since 14.2 is describe here: #64802. After finding out a workaround to add next-logger to experimental.serverComponentsExternalPackages (14.2) or serverExternalPackages (14.3), the module was able to be included and loaded by Webpack, but Turbopack doesn't work.

  • To reproduce the error: Run npm run dev
  • To see the results of Webpack: Remove --turbo in package.json and run npm run dev

Full error log:

❯ npm run dev

> dev
> next dev --turbo

  ▲ Next.js 14.3.0-canary.63 (turbo)
  - Local:        http://localhost:3000
  - Experiments (use with caution):
    · instrumentationHook

 ✓ Starting...
Error: An error occurred while loading instrumentation hook: Cannot find module as expression is too dynamic
    at /Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/node_modules_595211._.js:5595:19
    at module.exports.[project]/node_modules/import-fresh/index.js [instrumentation] (ecmascript).module.exports (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/node_modules_595211._.js:5598:7)
    at loadJsSync (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/node_modules_595211._.js:4897:12)
    at #loadConfiguration (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/node_modules_595211._.js:5208:24)
    at #readConfiguration (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/node_modules_595211._.js:5195:74)
    at search (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/node_modules_595211._.js:5166:63)
    at emplace (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/node_modules_595211._.js:4746:20)
    at ExplorerSync.search (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/node_modules_595211._.js:5189:42)
    at Object.<anonymous> (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/node_modules_595211._.js:10105:30)
    at [project]/node_modules/next-logger/lib/logger.js [instrumentation] (ecmascript) (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/node_modules_595211._.js:10121:3)
    at instantiateModule (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/[turbopack]_runtime.js:520:23)
    at getOrInstantiateModuleFromParent (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/[turbopack]_runtime.js:572:12)
    at commonJsRequire (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/[turbopack]_runtime.js:136:20)
    at Object.<anonymous> (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/node_modules_595211._.js:10125:16)
    at [project]/node_modules/next-logger/lib/patches/next.js [instrumentation] (ecmascript) (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/node_modules_595211._.js:10155:3)
    at instantiateModule (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/[turbopack]_runtime.js:520:23)
    at getOrInstantiateModuleFromParent (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/[turbopack]_runtime.js:572:12)
    at commonJsRequire (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/[turbopack]_runtime.js:136:20)
    at Object.<anonymous> (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/node_modules_595211._.js:10191:1)
    at [project]/node_modules/next-logger/presets/all.js [instrumentation] (ecmascript) (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/node_modules_595211._.js:10194:3)
    at instantiateModule (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/[turbopack]_runtime.js:520:23)
    at getOrInstantiateModuleFromParent (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/[turbopack]_runtime.js:572:12)
    at commonJsRequire (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/[turbopack]_runtime.js:136:20)
    at Object.<anonymous> (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/node_modules_595211._.js:10197:1)
    at [project]/node_modules/next-logger/index.js [instrumentation] (ecmascript) (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/node_modules_595211._.js:10199:3)
    at instantiateModule (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/[turbopack]_runtime.js:520:23)
    at getOrInstantiateModuleFromParent (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/[turbopack]_runtime.js:572:12)
    at commonJsRequire (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/[turbopack]_runtime.js:136:20)
    at Module.register (/Users/xxxx/workspaces/next-logger-bug-example/.next/server/chunks/instrumentation_ts_ca8ae5._.js:13:15)
    at async DevServer.runInstrumentationHookIfAvailable (/Users/xxxx/workspaces/next-logger-bug-example/node_modules/next/dist/server/dev/next-dev-server.js:437:17)
    at async Span.traceAsyncFn (/Users/xxxx/workspaces/next-logger-bug-example/node_modules/next/dist/trace/trace.js:157:20)
    at async DevServer.prepareImpl (/Users/xxxx/workspaces/next-logger-bug-example/node_modules/next/dist/server/dev/next-dev-server.js:214:9)
    at async NextServer.prepare (/Users/xxxx/workspaces/next-logger-bug-example/node_modules/next/dist/server/next.js:161:13)
    at async initializeImpl (/Users/xxxx/workspaces/next-logger-bug-example/node_modules/next/dist/server/lib/render-server.js:98:5)
    at async initialize (/Users/xxxx/workspaces/next-logger-bug-example/node_modules/next/dist/server/lib/router-server.js:424:22)
    at async Server.<anonymous> (/Users/xxxx/workspaces/next-logger-bug-example/node_modules/next/dist/server/lib/start-server.js:250:36) {
  code: 'MODULE_NOT_FOUND',
  filepath: '/Users/xxxx/workspaces/next-logger-bug-example/next-logger.config.cjs'
}
@CHC383 CHC383 added the bug Issue was opened via the bug report template. label May 15, 2024
@github-actions github-actions bot added Module Resolution Module resolution (CJS / ESM, module resolving) Turbopack Related to Turbopack with Next.js. labels May 15, 2024
@kwonoj kwonoj added the linear: turbopack Confirmed issue that is tracked by the Turbopack team. label May 16, 2024
@kwonoj
Copy link
Contributor

kwonoj commented May 16, 2024

This is not due to serverExternalPackages config. next-logger internally have some dynamic cjs require to workaround node.js module cache, and it's form is currently not analyzable by turbopack's resolution

delete require.cache[filePath]; // Delete module from cache

const parent = require.cache[parentPath]; 

return parent === undefined ? require(filePath) : parent.require(filePath);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. linear: turbopack Confirmed issue that is tracked by the Turbopack team. Module Resolution Module resolution (CJS / ESM, module resolving) Turbopack Related to Turbopack with Next.js.
Projects
None yet
Development

No branches or pull requests

2 participants