I am trying to use experimental ES module support from Grall.js. I use following scripts: ES module "lib"
export const sqrt = Math.sqrt;
export function square(x) {
return x * x;
}
export function diag(x, y) {
return sqrt(square(x) + square(y));
}
and main script "script"
import { square, diag } from 'lib';
console.log('square(11)=' + square(11));
console.log('diag(4,3)=' + diag(4, 3));
I use graalvm-ce-19.2.1 and I use Polyglot inside JVM via JSR 223 to run the main script. Instead of trying to load lib from some place on disk it throws:
javax.script.ScriptException: org.graalvm.polyglot.PolyglotException: SyntaxError: script:1:0 Expected an operand but found import
import { square, diag } from 'lib';
^
script:1:30 Expected ; but found lib
import { square, diag } from 'lib';
^
at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:348)
at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:323)
What's wrong?
There is a convention on file names which trigger treating them as ES modules - file must end with
.mjs
. Alternatively it is possible to use (unofficial) mime typeapplication/javascript+module
onorg.graalvm.polyglot.Source
object. Without thisimport
statement is not allowed 1.This is how it looks when using Polyglot Source/Context API:
and this is for JSR-223 API:
It seems that there also was an older convention - using
module:
prefix, but this doesn't seem to work anymore.