Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
562
Reaction score
805
First Language
English
Primarily Uses
RMMV
Node.js Require
The "require" function is used often in Node.js applications. Node allows you to require modules from different files and use their content in other parts of your application. The "require" function also maintains a cache. Now, most would not touch the cache. But today, let's talk about the cache and how we can use it to improve our workflow.



The "Require" Cache
The "require" function maintains a cache that contains all the contents that were loaded into the running program. This is for efficiency reasons; loading modules repeatedly would only serve to slow down a program in the long run. However, the information on the cache is useful to us, because we know now the code is maintained within the loaded module.



Now, we know how the cache works. But, how can we leverage this in our program? The answer is managing the cache; by doing so we can reload/update modules at different points in our application. Now, let's go into cache management.



Cache Management
As a developer, I'm sure a cache has caused you issue from time to time (Google Chrome Developer tools cache anyone?). A cache is useful, but being able to reload services in our program can be beneficial to development as a whole. Imagine updating your business services without taking down your application. That can be extremely beneficial when people rely on your services. This deserves an example.



Here's an example of cache management from a Discord side project of mine.

DiscordExample1.png


In this example, we created a function that can run commands once the path to the module(where it is on the computer system) is resolved. Once the path is resolved, it requires the module and runs the 'run' function within the context of our application. The actual module is now maintained in the cache and will not be reloaded from the underlying file system. Now, let's look at a function that helps manage the cache.

Here's an example of the reload function that removes the cache contents in the same project.

DiscordExample2.png


In this case, reload takes a set of parameters and deletes the file's module from the cache. We simply use the delete keyword in JavaScript to do so. Note, the module, in this case, is the file name and is usually the case in modules that aren't in your node_modules folder. Now, this code relates to our previous example.

Now, because in our previous example we require the module before executing the code in the context of our program, require will reload a new version of our module when the command is called allowing us to maintain and improve our services dynamically. Awesome!

Personally, I think this shows a lot of the power behind server side work. As a developer, I don't often think of using the underlying file system in this way when building programs. But, the idea can help build out a more robust architecture in our code that uses not only our coding tools but the platform(machine) we're working on. So, in the future think of the places, your program will run and consider the solution that covers the lowest common denominator.



Sources
Node.js Modules

Discord.js Command Handler
 

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
453
Reaction score
211
First Language
English
Primarily Uses
RMMV
Interesting post @Kino. :)
This gives me an idea for a new command in something I am working on. ^_^
Something like this at the command line:
Code:
nodejs /index.js

update myModule.js
or:
Code:
nodejs /index.js

update all
 

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
562
Reaction score
805
First Language
English
Primarily Uses
RMMV
That would be great; being able to update components is so much fun. It's the cornerstone of my bot.
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,634
Reaction score
1,452
First Language
French
Primarily Uses
RMMV
Thank you infinitely, it would be great, if you will be able to lay a small topic on the EventListener
inside rmmv.
I'm at this stage of coding development in my game, with a lot of interaction, and I will be delighted to have a small topic with good advice about event and the best way to use it.
This is only a suggestion. :rock-left::guffaw:
 

Latest Threads

Latest Profile Posts

i want to make videogame, though i am burned out on games with RPG elements, but the only gamedev tool i know how to use is rpg maker. :/
Rakon wrote on xizorandy's profile.
Hello! I found a post of lightning strike pictures you posted. You said you made them, am I allowed to use them in my commercial project. Would be really cool!^^
evhht2zr.png

Have a nice day!
It's almost 3 month and this piece of drawing is still not finished yet.
aaaa.jpg

Really busy with other stuffs like commission and coping with personal problem.
Stella & Elyune, maybe used for promotional art if my game project finished one day.

Forum statistics

Threads
112,253
Messages
1,066,834
Members
145,858
Latest member
signcentre12
Top