Rescript runtime error with commonjs

So I have been trying to upgrade my projects to Rescript v12 from v11. I’ve created a new project but still see the same issue.

package.json:

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "rescript": "^12.0.1"
  }
}

rescript.json:

{
  "name": "test",
  "sources": [
    {
      "dir": "src",
      "subdirs": true
    }
  ]
}

src/Index.res:

Console.log(Some(1)->Option.map(x => x + 1))

I encounter this issue:

❯ node src/Index.js
file:///tmp/test/node_modules/@rescript/runtime/lib/js/Stdlib_Option.js:3
let Stdlib_JsError = require("./Stdlib_JsError.js");
                     ^

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/tmp/test/node_modules/@rescript/runtime/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///tmp/test/node_modules/@rescript/runtime/lib/js/Stdlib_Option.js:3:22
    at ModuleJobSync.runSync (node:internal/modules/esm/module_job:514:37)
    at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:425:47)
    at loadESMFromCJS (node:internal/modules/cjs/loader:1578:24)
    at Module._compile (node:internal/modules/cjs/loader:1743:5)
    at Object..js (node:internal/modules/cjs/loader:1893:10)
    at Module.load (node:internal/modules/cjs/loader:1481:32)
    at Module._load (node:internal/modules/cjs/loader:1300:12)
    at TracingChannel.traceSync (node:diagnostics_channel:328:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:245:24)

Node.js v24.11.1

If I set package specs to es6 and use type module in my project package.json, it runs.
Is the runtime broken with commonjs or am I missing something?

1 Like

What is your node version?
Did you try changing your suffix to .cjs as that error mentions?

If the compiled code is commonjs, then running it is not a rescript matter anymore.

Node.js v24.11.1

The error specifically refers to the @rescript/runtime dependency. I don’t see how can I fix the file extensions in a dependency without forking it and using my own version?

It seems to me that the @rescript/runtime dependency, by specifying "type": "module" makes it impossible to use it from a commonjs project because rescript compiles it (the dependency) to commonjs output and then node interprets that output as es module because of the package.json “type”.

Oh, interesting, I see what you mean.
Yes, that could be a problem.

Is it hard for you to move to esm?

For me personally, I’ve been pursuing converting over to esm, but this does make updating to rescript 12 harder.

The other thing is that if you look at my rescript.json in the OP I didn’t specify which and so commonjs seems to be the default, thus I’m imaging this is easy to hit as a new user since commonjs seems to be the default when not specified.

Maybe something can be tweaked in here to support both commonjs and esm.

Feel free to explore.