rescript-core icon indicating copy to clipboard operation
rescript-core copied to clipboard

Option module loading error

Open kswope opened this issue 2 years ago • 2 comments

Something is weird about the core Option module, when run with just node

but seems to work just fine with create-react-app (webpack I think)

works

Array.at([1, 2, 3], 0)->Console.log

doesn't work

Option.equal(Some(1), Some(1), (v1, v2) => {v1 == v2})->Console.log
[nodemon] starting `node src/Index.bs.js`
(node:21031) 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/kevin/Dev/ReScript/rescript-notes/node_modules/@rescript/core/src/Core__Option.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:1195:20)
    at Module._compile (node:internal/modules/cjs/loader:1239:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
    at Module.load (node:internal/modules/cjs/loader:1133:32)
    at Module._load (node:internal/modules/cjs/loader:972:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:168:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)

If I sneak a "type":"module" into package.js inside @rescript/core package it starts working

kswope avatar Nov 16 '23 16:11 kswope

Try using .bs.mjs suffix. I don't recommend setting type: "module for ReScript packages, since it'll lead to a whole lot of other problems. There's somewhere a post on the forum about it.

DZakh avatar Nov 17 '23 11:11 DZakh

Try using .bs.mjs suffix. I don't recommend setting type: "module for ReScript packages, since it'll lead to a whole lot of other problems. There's somewhere a post on the forum about it.

Its awkward that this is the default for

npx rescript init
  "package-specs": {
    "module": "es6",
    "in-source": true
  },
  "suffix": ".bs.js"

making running with node fail as your first beginner experience

But I still cant figure out what it was only the Option module and not Array module that gave me this error. Something is different between them.

kswope avatar Nov 19 '23 16:11 kswope

create-rescript-app now defaults to .res.mjs so this should not happen anymore.

tsnobip avatar Mar 07 '25 11:03 tsnobip