Is this node error something I'm doing?

I’m seeing this node error:

~> rescript
~> node lib/es6/src/Set/Set.bs.js
(node:40746) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/stu/tmp/rescript/learn/node_modules/@rescript/core/lib/es6/src/Core__List.bs.js:3
import * as Curry from "rescript/lib/es6/curry.js";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1166:20)
    at Module._compile (node:internal/modules/cjs/loader:1210:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1300:10)
    at Module.load (node:internal/modules/cjs/loader:1103:32)
    at Module._load (node:internal/modules/cjs/loader:942:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:168:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)

Node.js v19.5.0

It’s from this (admittedly clumsy, just for learning purposes) ReScript module:

module type Set = {
  type set<'e>

  let empty: unit => set<'e>

  let singleton: 'e => set<'e>

  let size: set<'e> => int
}


module ArraySet: Set = {
  type set<'e> = array<'e>

  let empty = () => []

  let singleton = x => [x]

  let size = xs => Array.length(xs)
}


let aset_1 = ArraySet.singleton(42)
Console.log(aset_1)


module ListSet: Set = {
  type set<'e> = list<'e>

  let empty = () => list{}

  let singleton = x => list{x}

  let size = xs => List.length(xs)
}

let lset_1 = ListSet.singleton(42)
Console.log(lset_1)


module type SetLaws = {
  type set<'a>

  let singleton_size: 'a => bool
}


module SetLawsFor = (S: Set): SetLaws => {
 type set<'a> = S.set<'a>

 let singleton_size = x =>
   S.size(S.singleton(x)) == 1
}



module ArraySetLaws = SetLawsFor(ArraySet)
Console.log(ArraySetLaws.singleton_size(42))


module ListSetLaws = SetLawsFor(ListSet)
Console.log(ListSetLaws.singleton_size(42))

Any advice much appreciated.

Maybe this helps How to fix "cannot use import statement outside a module"

Or change the suffix in your bsconfig to mjs

Thanks – I’d already done that.

That worked, thanks. Should the official documentation be changed to recommend “.mjs” for new ReScript projects?

1 Like

I agree, although I prefer .bs.mjs

While I’m preferring es modules, I had troubles with some libraries (e.g. date-fns). So I don’t know if rescript should use it as default🤷

Thanks, I’ll keep that in mind.

Hello guys, im having similar Syntax error

can someone assist?