Node.js Require

Discussion in 'Learning Javascript' started by Kino, Aug 3, 2017.

  1. Kino

    Kino EIS Game Dev Veteran

    Messages:
    515
    Likes Received:
    672
    Location:
    United States
    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.

    [​IMG]

    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.

    [​IMG]

    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
     
    #1
    Zeriab, Clock Out, Sarlecc and 2 others like this.
  2. Sarlecc

    Sarlecc Veteran Veteran

    Messages:
    454
    Likes Received:
    209
    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
     
    #2
    Kino likes this.
  3. Kino

    Kino EIS Game Dev Veteran

    Messages:
    515
    Likes Received:
    672
    Location:
    United States
    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.
     
    #3
    Sarlecc likes this.
  4. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,577
    Likes Received:
    1,353
    Location:
    Canada / Qu├ębec
    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:
     
    #4
    Kino likes this.

Share This Page