Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix createRequire so it works for local node_modules. #23516

Merged
merged 1 commit into from
Jan 28, 2025

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Jan 28, 2025

For most uses of require() in emscripten this does not matter since we load mostly system libraries. However for the ws module it is needed. This bug was being masked by the fact that we were setting NODE_PATH in our socket test running. This is no longer needed now that we run (non-parallel) tests in the emscripten tree (in out/test).

This is followup to #23265 which itself was an attempt to revert #23169.

Fixes: #23503

@@ -105,7 +105,7 @@ if (ENVIRONMENT_IS_NODE) {
// We need to use `createRequire()` to construct the require()` function.
const { createRequire } = await import('module');
/** @suppress{duplicate} */
var require = createRequire('/');
var require = createRequire(import.meta.url);
Copy link
Member

Choose a reason for hiding this comment

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

I don't know enough about support for this - didn't we have an issue with it breaking in some bundler or environment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I believe you are thinking of the use of import.meta.url of in new Worker. This usage is perfectly fine AFAIK. See #23169 which removed it prematurely.

@sbc100 sbc100 force-pushed the fix_createRequire branch from c80239f to 946733f Compare January 28, 2025 19:42
For most uses of `require()` in emscripten this does not matter since
we load mostly system libraries.  However for the `ws` module it is
needed.  This bug was being masked by the fact that we were setting
`NODE_PATH` in our socket test running.  This is no longer needed now
that we run (non-parallel) tests in the emscripten tree (in out/test).

This is followup to emscripten-core#23265 which itself was an attempt to revert 23169.

Fixes: emscripten-core#23503
@sbc100 sbc100 force-pushed the fix_createRequire branch from 946733f to 3c07854 Compare January 28, 2025 22:07
@sbc100 sbc100 enabled auto-merge (squash) January 28, 2025 22:07
@sbc100 sbc100 merged commit 0e9d6f6 into emscripten-core:main Jan 28, 2025
29 checks passed
@sbc100 sbc100 deleted the fix_createRequire branch January 28, 2025 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression in "require" in ES6 modules. Fails to find "ws" module.
2 participants