首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用genType导出具有多个参数的rescript函数时出现Curry.js错误

使用genType导出具有多个参数的rescript函数时出现Curry.js错误
EN

Stack Overflow用户
提问于 2021-08-25 22:54:47
回答 1查看 76关注 0票数 1

当导出一个包含多个参数(2个或更多)的函数时,它会抛出以下错误,这基本上说明我们导入curry.js的方式有问题。我附上了一个例子和下面的包版本。

错误:

代码语言:javascript
复制
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)

示例

demo.res

代码语言:javascript
复制
@genType
let helloWord = (firstName: string, lastName: string): unit => {
  Js.log("Hello " ++ firstName ++ " " ++ lastName)
}

demo.bs.js

代码语言:javascript
复制
// Generated by ReScript, PLEASE EDIT WITH CARE
'use strict';


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

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

demo.gen.ts

代码语言:javascript
复制
/* 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
};

package.json

代码语言:javascript
复制
{
    "ts-node": "^10.2.1",
    "gentype": "^4.1.0",
    "rescript": "^9.1.4",
    "typescript": "^3.9.6"
}

节点版本

代码语言:javascript
复制
$ node -v
v15.11.0

操作系统

Manjaro Linux

EN

回答 1

Stack Overflow用户

发布于 2021-08-26 03:13:29

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

我找到了这个选项

代码语言:javascript
复制
{
  "gentypeconfig": {
    "module": "commonjs",
  }
}

设置解决了这个问题:D不幸的是,我在rescript文档中的其他地方找不到它:(

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68930696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档