nextjs - ✅(Solved) Fix `__dirname` is `/ROOT/...` instead of real path in Turbopack [2 pull requests, 1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#86476Fetched 2026-04-08 02:10:48
View on GitHub
Comments
1
Participants
2
Timeline
11
Reactions
6
Participants
Timeline (top)
subscribed ×4cross-referenced ×2referenced ×2commented ×1

Fix Action

Fix / Workaround

  1. require() a cjs module, for example in getServerSideProps or in a server component. Import location does not matter.
  2. Inside of the module, try to access __dirname - it is /ROOT/... instead of the real path. import.meta.url works correctly though - this is not a valid workaround though, since this normally does not work in CommonJS.

PR fix notes

PR #86480: fix(turbopack): correct __dirname and __filename in CJS modules

Description (problem / solution / changelog)

This commit fixes an issue where __dirname and __filename were not being correctly resolved in CommonJS modules when using Turbopack. The issue was that the origin of the requiring module was being used to resolve these variables, instead of the origin of the CJS module itself.

To fix this, I've introduced a new CjsPath variant to the FreeVarReference enum. This new variant is used to represent __dirname and __filename in CJS modules. The handle_free_var_reference function in urbopack/crates/turbopack-ecmascript/src/references/mod.rs has been updated to handle this new variant and resolve the path correctly using the origin of the CJS module.

This change ensures that __dirname and __filename have the correct values in CJS modules, which is important for compatibility with existing Node.js code.

Fixes #86476

<!-- 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 ### 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 # -->

Changed files

  • turbopack/crates/turbopack-core/src/compile_time_info.rs (modified, +9/-0)
  • turbopack/crates/turbopack-ecmascript/src/references/mod.rs (modified, +29/-6)

PR #9: Fix Vercel deployment and dev server issues

Description (problem / solution / changelog)

Summary

This PR fixes critical Vercel deployment failures and local development server issues related to Prisma, Turbopack, and pnpm configuration.

Issues Resolved

1. Vercel Build Failures

  • Fixed ERR_PNPM_OUTDATED_LOCKFILE by regenerating pnpm-lock.yaml
  • Fixed Prisma generation failures during build
  • Removed unnecessary prisma.config.ts that was requiring DATABASE_URL during build

2. Turbopack Workspace Root Detection

  • Fixed "Next.js inferred your workspace root, but it may not be correct" error
  • Worked around Next.js 16.0.4 bug where __dirname resolves to /ROOT/... in Turbopack
  • Removed conflicting package-lock.json that confused Turbopack's root detection
  • Added proper outputFileTracingRoot and turbopack.root configuration

3. Prisma Module Resolution with pnpm

  • Fixed "Cannot find module .prisma/client/default" error
  • Configured pnpm to use hoisted node-linker for proper module resolution
  • Removed custom Prisma output path in favor of standard location

Changes Made

Configuration Files

  • .npmrc (new): Configure pnpm to use hoisted node-linker for Prisma compatibility
  • next.config.mjs (converted from .ts): Added Turbopack and Prisma configuration
    • Hardcoded projectRoot to work around Turbopack __dirname bug
    • Added serverExternalPackages for Prisma
    • Synced outputFileTracingRoot with turbopack.root
  • vercel.json (new): Force Vercel to use custom build command
  • package.json: Updated build script to run prisma generate && next build

Prisma Configuration

  • prisma/schema.prisma: Removed custom output path
  • lib/prisma.ts: Updated to import from standard @prisma/client
  • Deleted prisma.config.ts: Unnecessary file causing build failures

Cleanup

  • Removed conflicting package-lock.json (using pnpm exclusively)
  • Removed old app/generated/prisma directory
  • Cleared .next cache

Technical Details

Root Cause Analysis

  1. Turbopack Bug: Next.js 16.0.4 has a known bug where __dirname resolves incorrectly in Turbopack (Issue #86476)
  2. pnpm Isolation: pnpm's content-addressable store isolates dependencies, preventing Turbopack from finding .prisma/client at the expected location (Issue #25833)
  3. Multiple Lockfiles: Having both npm and pnpm lockfiles confused Turbopack's workspace detection

Solutions Applied

  1. Hardcoded absolute path in next.config.mjs as workaround for __dirname bug
  2. Hoisted node-linker in .npmrc to make pnpm use flat structure like npm
  3. Synchronized configuration between outputFileTracingRoot and turbopack.root (required per Next.js source code)

Testing

  • ✅ Local dev server starts successfully with npm run dev
  • ✅ Prisma client imports correctly
  • ✅ Turbopack workspace root detection working
  • ✅ All changes committed and pushed

Next Steps

Once merged:

  1. Vercel deployment should succeed
  2. When Next.js releases fix for __dirname bug, can replace hardcoded path with dynamic resolution

Related Issues & Documentation

🤖 Generated with Claude Code

Changed files

  • .npmrc (added, +1/-0)
  • next.config.mjs (added, +25/-0)
  • next.config.ts (removed, +0/-15)

Code Example

Node: 20.12.1
  npm: 10.5.0
  Yarn: 1.22.19
  pnpm: 8.15.6
Relevant Packages:
  next: 16.0.2-canary.34 // Latest available version is detected (16.0.2-canary.34).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: N/A
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/angry-lalande-98zyn5

To Reproduce

  1. require() a cjs module, for example in getServerSideProps or in a server component. Import location does not matter.
  2. Inside of the module, try to access __dirname - it is /ROOT/... instead of the real path. import.meta.url works correctly though - this is not a valid workaround though, since this normally does not work in CommonJS.

Current vs. Expected behavior

__dirname should work the same as when using Webpack / be consistent with data reported from import.meta

Provide environment information

Node: 20.12.1
  npm: 10.5.0
  Yarn: 1.22.19
  pnpm: 8.15.6
Relevant Packages:
  next: 16.0.2-canary.34 // Latest available version is detected (16.0.2-canary.34).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: N/A

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

Turbopack

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

next dev (local), next build (local), next start (local)

Additional context

No response

extent analysis

TL;DR

The issue with __dirname resolving to /ROOT/... instead of the real path in a cjs module within Next.js may be related to how Turbopack handles module resolution, and a potential fix could involve adjusting how modules are required or using a different approach to determine the directory path.

Guidance

  • Investigate how Turbopack's configuration or defaults might be influencing the resolution of __dirname and consider if there are settings that can be adjusted to achieve the expected behavior.
  • Since import.meta.url works correctly, explore if there's a way to utilize this in conjunction with other Node.js APIs (like url or path) to derive the correct directory path as a potential workaround.
  • Review the documentation and issues related to Next.js and Turbopack to see if there are known issues or solutions related to module resolution and __dirname.
  • Consider testing the behavior with a different version of Next.js or Turbopack to see if the issue persists, which could help in identifying if it's a version-specific problem.

Notes

The provided information does not specify a clear, direct fix, and the issue seems to be related to the interaction between Next.js, Turbopack, and how CommonJS modules are handled. Therefore, any solution might require further investigation into the specifics of these technologies and their configurations.

Recommendation

Apply workaround: Utilize import.meta.url in conjunction with Node.js path manipulation APIs as a temporary solution until a more direct fix for __dirname resolution in cjs modules within Next.js and Turbopack is found.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

nextjs - ✅(Solved) Fix `__dirname` is `/ROOT/...` instead of real path in Turbopack [2 pull requests, 1 comments, 2 participants]