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

Deadly 提交于 2019-12-10 10:15:11

问题


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.)


回答1:


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.



来源:https://stackoverflow.com/questions/10723180/sanity-check-rhino-does-not-have-a-require-function-right

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!