MZ Can I use require function (from NodeJS)?

Hello people!

I have two JS files: quests, where I export a test object like this export default {quest: 'abc'} (located in ./plugins/data), and my loaded plugin where I attempted to load the quests file like this: const quests = require('./data/quests.js'); (located in the plugins folder).

Whenever I try starting up the game I get this error: Cannot find module './data/quests.js' Require stack: - C:\Users\Felipe\Documents\RMMZ\PreviewMZ\/index.html . When opening up the plugin window, I've seen this observation at the bottom:

1598830392536.png


Is there any way I can use the require function to load modules separately? If so, will my game work on any device?
[Bonus Question] When I build the final game, will it package my quests.js file in ./plugins/data folder normally?

Thank you a lot!
Hola, Felipe.

I'm currently develop a plugin and I can use require. For example:
JavaScript:
getJSON(lang) {
    return require(`./lang/${lang}.json`);
}
This function retrieve the correct data json from .json file.

Try to TURN OFF all your Plugins (if you have) except yours and try again. Or just, simply press F8 during playtest to open DevTools and type the require function:
JavaScript:
const quests = require('./data/quests.js');
And then check if "const" has value.


Bonus Question: Yes, your plugin will deploy too. If you want to know, just go to File -> Deployment, deploy and then go to the output folder and check the "/plugins" folder. Your .js should be there.

:LZSsmile:
Hola, Felipe.

I'm currently develop a plugin and I can use require. For example:
JavaScript:
getJSON(lang) {
    return require(`./lang/${lang}.json`);
}
This function retrieve the correct data json from .json file.

Try to TURN OFF all your Plugins (if you have) except yours and try again. Or just, simply press F8 during playtest to open DevTools and type the require function:
JavaScript:
const quests = require('./data/quests.js');
And then check if "const" has value.


Bonus Question: Yes, your plugin will deploy too. If you want to know, just go to File -> Deployment, deploy and then go to the output folder and check the "/plugins" folder. Your .js should be there.

:LZSsmile:

Olá, estrangeiro! Nice to know it is possible. I just need to figure out why my /data folder is being ignored:

1598836006372.png


It is currently inside the plugins folder on my file system. Can you tell me where have you put this lang folder of yours, please?
Well, I can always edit my main.js file, forcing it to load my data folder with all in it, but I think RMMZ has a native approach to that.
You probably need to change the path, with the current path you put in, its looking for a data folder within the plugins folder. ./ mean look in the folder the script is being run from, which is the plugin directory. You will need to use ../../data/quests.js the ../ will move back 1 directory, we do it twice to finally get to the root of the project where data folder is located.
  • / is the root of the current drive;
  • ./ is the current working directory;
  • ../ is the parent of the current working directory.

Edit: Sorry I must've missed that you had a data folder in the plugins folder. I assumed you were attempting to read from the data folder.
I've double checked and now I'm also getting
Uncaught ReferenceError: exports is not defined
at quests.js:1

Isn't NodeJS pre-built with RM?
Yes for sure, the window it playtests from is nwjs which is loaded with node, to me there should be no reason your require is not working.

Uncaught ReferenceError: exports is not defined
at quests.js:1

I assume its referring to module.exports?

In the console check to see if you can require('fs'), if you can't then there is something wrong with your environment
.
1598839381404.png
Yes for sure, the window it playtests from is nwjs which is loaded with node, to me there should be no reason your require is not working.



I assume its referring to module.exports?

In the console check to see if you can require('fs'), if you can't then there is something wrong with your environment
. View attachment 158662
Yeah, I'm being able to require 'fs'. I'm just confused I'm not being able to export either. When I try module.exports.quests = [{bla:'bla'}], I get:

quests.js:1 Uncaught ReferenceError: module is not defined
at quests.js:1
I just tried it myself and everything appears to work fine for me, the only thing is the plugins actually run from the root of the project and not the plugins directory. So I used "./js/plugins/data/quests.js" to require the quests, got no errors and the console log I made displayed the modules object with the quests array
within.
Spoiler: My test setup
1598840494685.png
Thanks, @LTN Games ! My relative path was messed up. I was setting it up from the plugin folder ('./data/quests'), instead of the index.html folder. Since we don't have webpack, gulp, or any other package manager to correct that, we have to do it ourselves.

Thank you a lot, my friend!
Oops, I'm late... :LZSjoy:

Good to know you've can figured out... that was an easy but frustrating error, lol.
If you have another question don't doubt in post it.

Have a nice codding 🤘

Original thread: https://forums.rpgmakerweb.com/threads/can-i-use-require-function-from-nodejs.126476/