malaporte / nashorn-commonjs-modules

CommonJS modules support for Nashorn
MIT License
108 stars 31 forks source link

Getting "ERROR: An error ocurred: javax.script.ScriptException: TypeError: [object global] is not an Object in <eval> at line number 4" error #15

Closed joquijada closed 7 years ago

joquijada commented 7 years ago

Hello,

Not sure if this is an issue or bad use on my part. I'm trying to into Nashorn a Node.js package, json-rules-enign, located here.

public static void main( String[] args ) { App app = new App(); String moduleFolder = "node_modules/json-rules-engine/dist/"; String filePath = moduleFolder + "index.js"; //String filePath = "index.js"; try { ScriptEngineManager engineManager = new ScriptEngineManager(); ScriptEngine engine = engineManager.getEngineByName(ID_SCRIPT_ENGINE); Folder nodeJsModFolder = ResourceFolder.create(app.getClass().getClassLoader(), moduleFolder, "UTF-8"); Require.enable((NashornScriptEngine) engine, nodeJsModFolder); logDebug("Folder is " + nodeJsModFolder.getPath()); engine.eval(new FileReader(app.loadFile(filePath))); } catch (FileNotFoundException e) { logError(e); } catch (ScriptException e) { logError(e); } }

But when I run it, getting this error on the line "engine.eval(new FileReader(app.loadFile(filePath)));", which is trying to load file "node_modules/json-rules-engine/dist/index.js" from the JSON rules engine Node.js package:

ERROR: An error ocurred: javax.script.ScriptException: TypeError: [object global] is not an Object in at line number 4

In the JavaScript, the error seems to be coming from this line in file "node_modules/json-rules-engine/dist/json-rules-engine.js":

Object.defineProperty(exports, "__esModule", { value: true });

I'm using Java 1.8.0_121, and version 1.0.6 of nashorn-commonjs-modules. Any idea what I might be doing wrong? Thanks.

joquijada commented 7 years ago

I fixed issue in the source code by replacing the line below in method 'Module com.coveo.nashorn_modules.Module.compileJavaScriptModule(Folder parent, String fullPath, String code) throws ScriptException`:

//exports = engine.createBindings();

exports = engine.getBindings(ScriptContext.ENGINE_SCOPE);

This is similar to issue reported here. The original line results in ScriptObjectMirror type object, which Nashorn does not like, where as the new code creates ScriptObject, which goes through fine. If you have a patch for that let me know. In the mean time I'll run with my update.

Thanks!

malaporte commented 7 years ago

I had a look and I could easily reproduce your issue. Sadly, I don't think the fix you suggest is appropriate because this would mean that all modules would use the global scope for their exports. This really seems to be an issue similar to the one you linked... but sadly you get it on a recent version of the JRE. I'll look for a workaround.

malaporte commented 7 years ago

Thanks to the tip & PR from @kipz that issue should be fixed once https://github.com/coveo/nashorn-commonjs-modules/pull/17 is merged.

malaporte commented 7 years ago

New 1.0.7 release contains this fix.

joquijada commented 7 years ago

Great, thanks @malaporte for your time in looking at this issue! If there's any other way I can contribute, I will. Thanks so much.

Best, joquijada.

On Mon, Mar 13, 2017 at 4:24 AM, Martin Laporte notifications@github.com wrote:

New 1.0.7 release contains this fix.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/coveo/nashorn-commonjs-modules/issues/15#issuecomment-286042069, or mute the thread https://github.com/notifications/unsubscribe-auth/AYm72lSMAkW7HwmfTgeHUUl-bLEZ-Wubks5rlP0ygaJpZM4MCOQK .