Skip to content

fix(cjs-build): enable evaluating import.meta in cjs build#1236

Merged
styfle merged 3 commits intovercel:mainfrom
nicotsx:main
Sep 18, 2025
Merged

fix(cjs-build): enable evaluating import.meta in cjs build#1236
styfle merged 3 commits intovercel:mainfrom
nicotsx:main

Conversation

@nicotsx
Copy link
Contributor

@nicotsx nicotsx commented Nov 19, 2024

Purpose

A while ago, a PR was merged to disable import.meta evaluation altogether to support new versions of Webpack. This sometimes causes issues in CJS projects.

In the comments, there were some discussions about whether we should have a conditional and keep the previous behavior for CJS builds. This PR addresses exactly this by translating import.meta.url to a CJS compatible statement which results to the same output.

I have created a comparative build between two projects using the same code and a dependency (@sentry/node) that uses import.meta.url. You can see the difference in the output index.js

Before the change:

const importMetaUrl = typeof import.meta.url !== 'undefined' ? import.meta.url : undefined;

After:

const importMetaUrl = typeof require("url").pathToFileURL(__filename).href !== 'undefined' ? require("url").pathToFileURL(__filename).href : undefined;

Closes #1019

@nicotsx nicotsx requested review from Timer and styfle as code owners November 19, 2024 19:33
Copy link
Member

@styfle styfle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

Please add a test to confirm this is working as expected.

@nicotsx
Copy link
Contributor Author

nicotsx commented Sep 18, 2025

@styfle Sorry for taking this long to add the unit tests, it should work as expected now

Copy link
Member

@styfle styfle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, thanks!

@styfle styfle merged commit e72d34d into vercel:main Sep 18, 2025
9 checks passed
@github-actions
Copy link

🎉 This PR is included in version 0.38.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot use 'import.meta' outside a module on 0.36.0 (not 0.34.0)

2 participants