Sanity check: Rhino does not have a require function, right?
Asked Answered
C

1

3

I'm using rhino from the JVM with the jaxax.script interfaces. I'm trying to evaluate JavaScript that contains 'require' calls. This does not work because there is no definition of 'require'. Is require just a v8 thing? Or maybe just a node.js thing? Are there ways around this on the JVM? Options I can think of are

  • rewrite the js files by manually including the required code
  • call out to a separate node.js process

(I'm not necessarily recommending the approaches.)

Colloidal answered 23/5, 2012 at 15:29 Comment(0)
P
5

require is a CommonJS spec that node.js (and other frameworks, like RingoJS) use. The newest version of Rhino does support require, but the one that ships with the JVM does not.

To work around it, you could either use Rhino directly instead of the javax.script interface, or implement the CommonJS module system yourself. You might be able to drop in the newer Rhino version in place of the JVM's, but I have no idea if that would work or not.

You might need to use something like RingoJS though, since there are other CommonJS specs that are probably assumed to be there and aren't supported by Rhino natively. Of course, there would still be issues with libraries that depend on node.js-specific features.

Percolator answered 23/5, 2012 at 15:47 Comment(1)
I found a small Rhino Require Project github.com/micmath/Rhino-Require that works for my small application... but if this is working in a larger scope.... I don't knowOversubtlety

© 2022 - 2024 — McMap. All rights reserved.