vercel / vercel/next.js

import.meta.dirname + import.meta.filename `undefined` in page

Open
#60,879 12 comments 11 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug linear: turbopack Upstream
Dominant language
JavaScript
Stars
142k
Forks
32.4k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/import-meta-dirname-reproduction-vj3gfq?file=%2Fapp%2Fpage.tsx%3A1%2C1

To Reproduce
  1. Open the reproduction
  2. Observe import.meta.dirname is undefined in the webpack-compiled page.tsx (but is a dir path in Node.js)

Screenshot 2024-01-19 at 11 45 54

Changes to repro template:

  • upgrade to Node.js 20.11.0 via dev container config
  • switch to "type": "module" to enable running ESM with Node.js (and change module.exports = to export default in next.config.js)
  • add file import-meta.dirname.js with console.log() of import.meta.dirname
  • add console.log() of import.meta.dirname to app/page.tsx
Current vs. Expected behavior

I expected import.meta.dirname and import.meta.filename, introduced in Node.js 20.11.0 to not be undefined in Next.js pages in the App Router, but it is undefined

Verify canary release
  • I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
Binaries:
  Node: 20.11.0
  npm: 10.2.4
  Yarn: 1.22.19
  pnpm: 8.14.1
Relevant Packages:
  next: 14.1.1-canary.1
  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 (CJS / ESM, module resolving)

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

next dev (local), next build (local)

Additional context

No response

PACK-2953

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked CodeSandbox reproduction and the webpack-compiled app/page.tsx, then trace how Next.js handles import.meta during the App Router's next dev and next build stages. Done means import.meta.dirname and import.meta.filename are no longer undefined in the affected page and match the expected Node.js behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nextjs, nodejs, typescript, webpack
Domain
backend, build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.