node.js - 使用 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
解决方案
在 genType 中查看此文件后:https ://github.com/rescript-association/genType/blob/fb6201266558a64d62441f7ac0d8d6652456397a/src/Config_.ml
我找到了这个选项
{
"gentypeconfig": {
"module": "commonjs",
}
}
解决问题的设置 :D 不幸的是,我在 rescript docs 的其他任何地方都找不到它 :(
推荐阅读
- dc.js - 如何在不破坏过滤器的情况下在 crossfilter.js 中向前移动组的值
- android-studio - 从 Android Studio 构建时使用修改后的 gradlew
- javascript - RxJS createEpicMiddleware 无法读取未定义的属性“应用”
- mysql - 查找替换变量整数 wordpress
- algorithm - 去块方法如果我们可以控制“块”工件出现的位置
- c# - Else vs new IF - 可读性和性能
- github - 从 Github 历史记录中完全删除文件,包括 API 历史记录
- python-sphinx - Sphinx 错误 - 警告“列表表”宽度与表中的列数不匹配 (1)
- java - 如何为 DAO 实现创建 Spring Boot 共享库
- javascript - WORDPRESS 错误:未捕获的类型错误:无法读取未定义的属性“替换”