How Mozilla Rhino can use nodejs "fs" module?
Asked Answered
M

2

1

Let's say I have my.js file that has dependency on nodejs fs module. Like:

var fs = require('fs');

The question: How can I load Core NodeJS module like "FS" in My Mozilla Rhino Java Code to be able to run my.js that depends on it ?

Monophagous answered 3/7, 2014 at 17:56 Comment(0)
Z
3

I don't think you can do this. fs module just a wrapper for native bindings built into node.js itself. There are some thoughts flying in the sky of node.js community about being able to use different JS engines inside node.js but I don't think we will see this in the near future.

Zhao answered 3/7, 2014 at 18:12 Comment(2)
some related links I'm looking at: github.com/nodyn/jvm-npm nodyn.ioMonophagous
Looks promising but, again, you can't load native node.js modules, you can only require them if they were implemented in jvm-npm/nodyn.io. They are not modules in regular way. BTW, there is ticket for that: github.com/nodyn/nodyn/issues/26Zhao
S
0

You can also write your own fs.js, only implementing the functions you need. Here is a start:

exports.readFile = function(file, enc, callback) {
  try {
    text = readFile(file, enc);
    callback(null, text);
  }
  catch (e) {
    callback(e, null);
  }
}

I've used this together with jvm-npm.

Sergei answered 23/1, 2015 at 21:8 Comment(3)
How would that work when fs.js requires Node's built-in native functions for file access?Humidistat
The fs module is Node's native file access. That's not available on Rhino, so you have to write your own.Sergei
fs.js is not written in native code.Humidistat

© 2022 - 2024 — McMap. All rights reserved.