next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Cannot use 'import.meta' outside a module when using next.config.ts

Open hrougier opened this issue 1 year ago • 2 comments

Link to the code that reproduces this issue

https://github.com/hrougier/next15

To Reproduce

  1. Start the application in development (next dev)
  2. See error in the console
  3. Open next.config.ts to see the failing code.

Current vs. Expected behavior

Following the steps from the previous section, I expected import.meta.dirname to be printed in the console (works when using next.config.js in ESM format instead of next.config.ts).

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
Binaries:
  Node: 20.18.0
  npm: 10.8.2
  Yarn: 1.22.22
  pnpm: 9.12.2
Relevant Packages:
  next: 15.0.1 // Latest available version is detected (15.0.1).
  eslint-config-next: 15.0.1
  react: 19.0.0-rc-69d4b800-20241021
  react-dom: 19.0.0-rc-69d4b800-20241021
  typescript: 5.6.3
Next.js Config:
  output: N/A

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

Developer Experience, TypeScript

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

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

Additional context

Using next.config.ts with a package.json containing:

{
  "type": "module"
}

hrougier avatar Oct 23 '24 08:10 hrougier

SWC has preserveImportMeta

kdy1 avatar Oct 23 '24 09:10 kdy1

Running next build throws:

ReferenceError: exports is not defined

whenever I use import.meta.dirname in my next.config.ts (this worked correctly under next.config.js)

controversial avatar Oct 24 '24 18:10 controversial

It would be nice to fail more gracefully than what currently happens. If import.meta.dirname is used, the ReferenceError: exports is not defined appears and it's extremely difficult to figure out the cause of that error. Thanks @controversial, very useful comment.

romgrk avatar Apr 09 '25 07:04 romgrk

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

github-actions[bot] avatar Sep 22 '25 12:09 github-actions[bot]