I am trying out Graalvm for a use case and I was wondering if I can use import statements in the javascript being called from java. I have tried both:
context.eval("js", "import v4 from 'uuid';var o = v4();");
which gives the error
Expected an operand but found import import v4 from 'uuid';var o = v4();
and
creating a separate script containing
import v4 from 'uuid';
var uu = v4();
then running the script via
InputStream inputStream = Controller.class.getResourceAsStream("/script.js");
String script = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
context.eval("js", script);
which returns the same error: Expected an operand but found import import v4 from 'uuid';
I went through the docs and it seems like it is possible. Can someone please guide me towards the correct approach?
In case somebody is still interested, the code below is tested with Eclipse running on Windows 10. (Note double escaped backslashes) Of course, you should have both GraalVM and GraalJS installed.
Where
foo.mjs
is located in folderC:/Other
with the following content:Output:
Classpath in Eclipse:
Where graal-sdk-xx.x.x.jar is downloaded separately from maven repo.
Docs.
Part 2. In case you want to source Javascript from a file:
See here.