[genType] Curry.js error when exporting a rescript function with more than 1 parameter

When exporting a function with more than 1 parameter (2 or more) it throws the following error, which basically says there is an issue with the way we import curry.js. I am attaching an example and package versions below.


Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: path/to/node_modules/rescript/lib/es6/curry.js
require() of ES modules is not supported.
require() of /path/to/node_modules/rescript/lib/es6/curry.js from /path/to/src/demo.gen.ts is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename curry.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /path/to/node_modules/rescript/lib/es6/package.json.

at new NodeError (node:internal/errors:329:5)
    at Module._extensions..js (node:internal/modules/cjs/loader:1109:13)
    at Object.require.extensions.<computed> [as .js] (/path/to/node_modules/ts-node/src/index.ts:851:44)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Module.require (node:internal/modules/cjs/loader:996:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at Object.<anonymous> (/path/to/src/demo.gen.ts:6:1)
    at Module._compile (node:internal/modules/cjs/loader:1092:14)
    at Module.m._compile (/path/to/node_modules/ts-node/src/index.ts:858:23)


let helloWord = (firstName: string, lastName: string): unit => {
  Js.log("Hello " ++ firstName ++ " " ++ lastName)
// Generated by ReScript, PLEASE EDIT WITH CARE
'use strict';

function helloWord(firstName, lastName) {
  console.log("Hello " + firstName + " " + lastName);

exports.helloWord = helloWord;
/* No side effect */

/* TypeScript file generated from demo.res by genType. */
/* eslint-disable import/first */

// @ts-ignore: Implicit any on import
import * as Curry__Es6Import from 'rescript/lib/es6/curry.js';
const Curry: any = Curry__Es6Import;

// @ts-ignore: Implicit any on import
import * as demoBS__Es6Import from './demo.bs';
const demoBS: any = demoBS__Es6Import;

export const helloWord: (firstName:string, lastName:string) => void = function (Arg1: any, Arg2: any) {
  const result = Curry._2(demoBS.helloWord, Arg1, Arg2);
  return result
    "ts-node": "^10.2.1",
    "gentype": "^4.1.0",
    "rescript": "^9.1.4",
    "typescript": "^3.9.6"

Node Version

$ node -v


Manjaro Linux

Based on the output demo.bs.js, it seems your project is set to use CommonJS modules. Check Build System Configuration | ReScript Language Manual to change it to ES6 modules.

it is a nodejs app (CLI) so commonjs is the right setup. Changing it to ES6 will break a lot of stuff

It looks like gentype is generating es6 but you’re using commonjs.

Some things to check:

Do you have commonjs configured in your bsconfig.js file?

"package-specs": [
    "module": "commonjs",
    "in-source": true

I believe you can also specify commonjs in your gentypeconfig, but not sure if that’s still required:

"gentypeconfig": {
  "language": "typescript",
  "module": "commonjs",
  ... etc

Lastly, from the error message, it looks like in your package.json you might be configured for ES modules:

"type": "module",

You might need to switch this to commonjs as well:

"type": "commonjs",

After taking a look at this file in genType: genType/Config_.ml at fb6201266558a64d62441f7ac0d8d6652456397a · rescript-association/genType · GitHub

I found the option you mentioned

"gentypeconfig": {
  "module": "commonjs",

setting that solves the issue, however, I belive someone should add documentation about each setting in the website Getting Started | ReScript GenType


Same happened to me, thank you for posting the solution :smiley:

Added an issue to include docs:

1 Like