SWI-Prolog/npm-swipl-wasm

Example doesn't work with newer node

Jean-Luc-Picard-2021 opened this issue · 2 comments

I am having problems with your examples. Take this here:

~/myproj$ cat script.js
import SWIPL from "swipl-wasm";

async function main() {
  const swipl = await SWIPL({ arguments: ["-q"] });
  console.log(swipl.prolog.query("member(X, [a, b, c]).").once().X);
}

main();

I get this error when trying to run it with node script.js:

$ node script.js
(node:260) 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)
/home/rburs/myproj/script.js:1
import SWIPL from "swipl-wasm";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1288:20)
    at Module._compile (node:internal/modules/cjs/loader:1340:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49

Node.js v20.10.0

So I need to manually edit package.json ?
Does npm init have an option to create a module project?
Or maybe I should just use the .mjs extension unlike your docu?

jeswr commented

Indeed import ... from ... is ECMAScript syntax.

If you want to stay with plain JavaScript you can either use .mjs as you suggest or use commonjs syntax here which takes the form const SWIPL = require("swipl-wasm").

I personally prefer to use typescript which also accepts the ECMAScript syntax.

Note that I've opened #433 to avoid further confusion in the docs.