apigee / trireme

Embed Node.js inside a Java Virtual Machine
Other
479 stars 48 forks source link

Execute Trireme with NPM dependencies in Jar-File #206

Open hmlimkokhoe opened 2 years ago

hmlimkokhoe commented 2 years ago

Hello,

we have a Java Maven project that loads the Node module ECharts to output charts. It works locally with this NodeScript instantiation: NodeScript echartsScript = env.createScript("index.js", new File("src/main/resources/echarts/src/index.js"), params);

Our Node project contains a package.json with ECharts as dependency. Both the script and node files are located here: src/main/resources/echarts. After building the project with Maven, the dependency is moved to the root of the project like this: my-jar-file.jar/echarts.

Inside our JavaScript, we simply use var echarts = require("echarts") to import ECharts.

Problem

Now, we want to achieve the same thing, when executing our Jar file. As Maven is able to resolve files under the resources directory itself, we replaced our file path with this: NodeScript echartsScript = env.createScript("index.js", new File("/echarts/src/index.js"), params);

I you run java -jar target/trireme-java-1.0-SNAPSHOT-jar-with-dependencies.jar com.mycompany.app.App with the JavaScript import mentioned above, you'll get the error message as below.

Trireme can load ECharts flawlessly if using an absolute path var echarts = require("/home/lim/Projects/trireme-java/src/main/resources/echarts/node_modules/echarts"); for the import. But obviously this is not the correct way... What would you suggest us to fix this problem?


SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Launching ECharts on port 8080
Exception in thread "main" java.util.concurrent.ExecutionException: org.mozilla.javascript.JavaScriptException: Error: Cannot find module '/echarts/src/index.js' (module.js#340)
Caused by: org.mozilla.javascript.JavaScriptException: Error: Cannot find module '/echarts/src/index.js' (module.js#340)
        at io.apigee.trireme.node10.node.module._c_anonymous_15(module.js:340)
        at io.apigee.trireme.node10.node.module.call(module.js)
        at org.mozilla.javascript.optimizer.OptRuntime.call2(OptRuntime.java:42)
        at io.apigee.trireme.node10.node.module._c_anonymous_14(module.js:280)
        at io.apigee.trireme.node10.node.module.call(module.js)
        at org.mozilla.javascript.optimizer.OptRuntime.callN(OptRuntime.java:52)
        at io.apigee.trireme.node10.node.module._c_anonymous_26(module.js:497)
        at io.apigee.trireme.node10.node.module.call(module.js)
        at org.mozilla.javascript.optimizer.OptRuntime.callProp0(OptRuntime.java:85)
        at io.apigee.trireme.node10.main.trireme._c_startup_2(trireme.js:142)
        at io.apigee.trireme.node10.main.trireme.call(trireme.js)
        at org.mozilla.javascript.optimizer.OptRuntime.callName0(OptRuntime.java:74)
        at io.apigee.trireme.node10.main.trireme._c_anonymous_1(trireme.js:923)
        at io.apigee.trireme.node10.main.trireme.call(trireme.js)
        at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:405)
        at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3508)
        at io.apigee.trireme.node10.main.trireme.call(trireme.js)
        at io.apigee.trireme.core.internal.ScriptRunner.runScript(ScriptRunner.java:762)
        at io.apigee.trireme.core.internal.ScriptRunner$4.run(ScriptRunner.java:702)
        at org.mozilla.javascript.Context.call(Context.java:544)
        at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:515)
        at io.apigee.trireme.core.internal.ScriptRunner.call(ScriptRunner.java:697)
        at io.apigee.trireme.core.ScriptFuture.run(ScriptFuture.java:183)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:834```