Is it possible to include Node.js modules from npm?
codefoster opened this issue · 4 comments
Is it currently possible using ScriptCraft to require modules from npm? I am new to ScriptCraft and believe I read that it's not actually Node, but Rhino? I tried a require
, but it doesn't work. I'm wondering if anyone has had need to use an npm module and found a simple enough workaround workflow - maybe adding a module loading system like System JS or something?
Is it currently possible using ScriptCraft to require modules from npm?
I don't see why not, as long as the npm module doesn't use any Node.js-specific API, or uses language features that aren't implemented in your Java version's JS engine.
You might have to manually specify the node_modules
directory, however (i.e. require('./node_modules/thing')
instead of require('thing')
. I'm not sure if the require()
resolver looks for it.
I am new to ScriptCraft and believe I read that it's not actually Node, but Rhino?
Node is a runtime with the V8 JS engine, and SC is a runtime on top of the Java runtime, which uses the Nashorn JS engine for Java 8+ (Rhino for Java 7)
Thanks for the info @SEAPUNK, I think the require keyword doesn't work at all in Nashorn, so if I'm not mistaken, most npm modules are going to fail if they have any dependencies (which most do).
Im not good at Java at all, but what about this article? http://source.coveo.com/2016/06/15/nashorn-require/
Could it work?
It would be awesome to be able ot use require in ScriptCraft.
Of course, Nashorn doesn’t support the Node APIs so most modules simply won’t work, but you can still use it to download portable libraries such as Underscore.
This is basically what scriptcraft's built-in require
does too. It's been why I'm reluctant to support a node_modules
folder though - supporting this folder would imply that all node modules are supported - which is not true.