"Expected an operand but found const" when trying to run a JavaScript script using ScriptEngine

0

I'm trying to execute a javascript method from a java project. The logic I used I picked up on the internet and it looks like this:

final String javascript = "/home/douglas/workspace/node/nintendo-switch-eshop/index.js";
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        engine.eval(Files.newBufferedReader(Paths.get(javascript), StandardCharsets.UTF_8));
        Invocable inv = (Invocable) engine;
        System.out.println(inv.invokeFunction("getShopsAsia"));

But it gives error in the 4th line when it will read the js. The error is as follows:

Exception in thread "main" javax.script.ScriptException: <eval>:1:0 Expected an operand but found const
const request = require("request");
^ in <eval> at line number 1 at column number 0
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)
    at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:537)
    at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:524)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
    at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:150)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249)
    at App.main(App.java:17)
Caused by: jdk.nashorn.internal.runtime.ParserException: <eval>:1:0 Expected an operand but found const
const request = require("request");
^
    at jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:294)
    at jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:279)
    at jdk.nashorn.internal.parser.Parser.unaryExpression(Parser.java:3182)
    at jdk.nashorn.internal.parser.Parser.expression(Parser.java:3282)
    at jdk.nashorn.internal.parser.Parser.expressionStatement(Parser.java:1150)
    at jdk.nashorn.internal.parser.Parser.statement(Parser.java:967)
    at jdk.nashorn.internal.parser.Parser.sourceElements(Parser.java:773)
    at jdk.nashorn.internal.parser.Parser.program(Parser.java:709)
    at jdk.nashorn.internal.parser.Parser.parse(Parser.java:283)
    at jdk.nashorn.internal.parser.Parser.parse(Parser.java:249)
    at jdk.nashorn.internal.runtime.Context.compile(Context.java:1284)
    at jdk.nashorn.internal.runtime.Context.compileScript(Context.java:1251)
    at jdk.nashorn.internal.runtime.Context.compileScript(Context.java:627)
    at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:535)
    ... 5 more

Does anyone know how I can resolve?

    
asked by anonymous 11.12.2017 / 19:29

0 answers