Getting node.js and require working in JS outside of game engine

I'm trying to write a little utility script that doesn't need to be run in the engine, but I need to use require('fs') so I can save the results.  Just can't seem to get it working.


I know node.js is a part of the MV framework, but I can't see any reference to it being required/used in either the MV files or in my project files, so I can't follow how it knows to use it when I run index.html


I've even installed node.js and npm separately and I still can't get it to work.


What's the trick?  Do I need to add something specific to my html file, to my js file, do I need to put the project in a particular folder?  The utility runs by opening the html file, which then calls the script.  I do NOT want to run it from within node itself.
I won't even pretend I understand it - I'm just pretty good at hack-job code, but when I did my saving in local data for desktop, I did this...


var gui = require('nw.gui');
return gui.App.dataPath;


Perhaps that will help, I don't know.
I think you're looking for node-WebKit which is what MV is wrapped in, I'm pretty sure this is what makes all of the node.js methods available which would be where you get your  require('fs') , I'm not 100% sure I'm still rather new to Javascript myself. It's pretty simple to setup the app you just zip it and change filetype then compile it, there may be a better way because compiling it every time you want to test your script would be a nuisance


Node-Webkit.


https://github.com/nwjs/nw.js
You may be right.


For now, I'm going to work in MV, as I've managed to modify index.html, ripped out all the scripts and put my own utility script in, and it's working like I want it to when I play test.  This will at least let me continue while I'm waiting for other ideas of what to do.  Having the whole thing in MV for what should be a simple 2 or 3 file tool is a bit overkill.
@Shaz Have you tried similar to what I used? Something like 


var fs = require('fs');


may allow you to use whatever functions you're looking for by using dot properties (or functions) on the fs variable. From the look of this, that would be the way to do it.
yes.  It tells me 'fs' isn't loaded
Odd. I just did some quick and dirty testing, and while I didn't get it to load the test file's text (probably just doing it wrong), I definitely got the fs object loaded in.

Spoiler
FSTest.png

Apparently it's got so many functions they don't all fit on my screen!


Here's the code I used to accomplish it (including my not-quite-working load a text file stuff):


(function() {
var getDataFromTestText = function() {
var data = null;
var fs = require('fs');
console.log(fs);
var filePath = window.location.pathname.replace(/(\/www|)\/[^\/]*$/, '/save/') + "testtext.txt";
if (fs.existsSync(filePath)) {
data = fs.readFileSync(filePath, { encoding: 'utf8' });
}
console.log(data);
}

getDataFromTestText();
}) ();


I do hope it helps. I mean, it does appear to be functional, it just needs to be wielded by somebody competent (ie. not me).
I think you've misread/misinterpreted me.


This is a small tool that I'm making that doesn't need to run in MV, so I don't want to write it in MV.  I just want to write it in JS and call it from HTML.  It doesn't work.


It works fine if I do it in MV, so that's how I'm doing it for the time being, because otherwise I'm stopped dead in my tracks.  But I want to know how to get it working outside of MV.
Oh, I see! I'm sorry. Well, hopefully the link I left earlier can point you in the right direction. It seems very helpfully step-by-step.
What link?  I only see one in LTNGames' post, not in any of yours.  I'll check it out - step-by-step is good 🙂

Original thread: https://forums.rpgmakerweb.com/threads/getting-node-js-and-require-working-in-js-outside-of-game-engine.66650/