Skip to content

Conversation

@shulhi
Copy link
Member

@shulhi shulhi commented Jan 19, 2026

The @rescript/runtime package has "type": "module" in its root package.json, which tells Node.js to treat all .js files as ES modules. However, the files in lib/js/* are actually written in CommonJS format (using require() and exports).

When Node.js tries to load these files via require(), it throws:

  Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported

This breaks any code that runs directly with Node.js (outside of build tools) because the nearest root package.json has its type set to module.

This follows the https://nodejs.org/api/packages.html#dual-commonjses-module-packages. The nested package.json files override the parent's "type" declaration. This adds explicit package.json file with type set to commonjs (similar to what we have in lib/es6/package.json but with module set).

@shulhi shulhi requested a review from cknitt January 19, 2026 03:54
Copy link
Member

@cknitt cknitt left a comment

Choose a reason for hiding this comment

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

Good catch!

Previously, this used to be the other way around. The whole package was common JS, and it was overridden for ES6. That override is still there in https://github.com/rescript-lang/rescript/blob/master/packages/%40rescript/runtime/lib/es6/package.json and could be removed now.

@shulhi shulhi force-pushed the package-json-commonjs branch from af0581f to eeda32d Compare January 19, 2026 08:13
@shulhi shulhi marked this pull request as ready for review January 19, 2026 08:16
@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 19, 2026

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@8194

@rescript/darwin-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@8194

@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@8194

@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@8194

@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@8194

@rescript/runtime

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/runtime@8194

@rescript/win32-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/win32-x64@8194

commit: 25d2315

@cknitt
Copy link
Member

cknitt commented Jan 19, 2026

I wonder why this problem has never surfaced before for v12.

It seems we did not have any kind of test for this after moving tests/tests to esmodule.
Could you add some test setup for this? Maybe in a new folder tests/commonjs_tests?

For the CHANGELOG, could you adapt the wording so that it is clearer what the bug is that is being fixed here?

@cknitt
Copy link
Member

cknitt commented Jan 19, 2026

I am also asking myself if it would be hard to just explicitly use the corresponding file extensions (.mjs, .cjs) instead. (But not in this PR, here we should keep the fix minimal for easy backportiing. We can investigate this later.)

@shulhi shulhi force-pushed the package-json-commonjs branch from 7322601 to 3988236 Compare January 20, 2026 01:01
Co-authored-by: Christoph Knittel <[email protected]>
@shulhi
Copy link
Member Author

shulhi commented Jan 20, 2026

@cknitt can you look at the test implementation whether it is sufficient? I tested the test with/without the fix and it caught the error appropriately.

@cknitt
Copy link
Member

cknitt commented Jan 20, 2026

@cknitt can you look at the test implementation whether it is sufficient? I tested the test with/without the fix and it caught the error appropriately.

@shulhi Looks great, thanks a lot! 👍

@cknitt cknitt merged commit 02b3e5b into rescript-lang:master Jan 20, 2026
26 checks passed
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.

2 participants