Cannot use 'import.meta' outside a module when using next.config.ts
Link to the code that reproduces this issue
https://github.com/hrougier/next15
To Reproduce
- Start the application in development (next dev)
- See error in the console
- Open
next.config.tsto 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"
}
SWC has preserveImportMeta
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)
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.
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.