首页 > 解决方案 > 使用 genType 导出具有超过 1 个参数的 rescript 函数时出现 Curry.js 错误

问题描述

当导出具有超过 1 个参数(2 个或更多)的函数时,它会引发以下错误,这基本上表示我们 import 的方式存在问题curry.js。我在下面附上了一个示例和软件包版本。

错误:

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)

例子

演示.res
@genType
let helloWord = (firstName: string, lastName: string): unit => {
  Js.log("Hello " ++ firstName ++ " " ++ lastName)
}
演示.bs.js
// Generated by ReScript, PLEASE EDIT WITH CARE
'use strict';


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

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

演示.gen.ts
/* 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
};
包.json
{
    "ts-node": "^10.2.1",
    "gentype": "^4.1.0",
    "rescript": "^9.1.4",
    "typescript": "^3.9.6"
}

节点版本

$ node -v
v15.11.0

操作系统

Manjaro Linux

标签: node.jstypescriptreasonbucklescriptrescript

解决方案


在 genType 中查看此文件后:https ://github.com/rescript-association/genType/blob/fb6201266558a64d62441f7ac0d8d6652456397a/src/Config_.ml

我找到了这个选项

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

解决问题的设置 :D 不幸的是,我在 rescript docs 的其他任何地方都找不到它 :(


推荐阅读