Question about require()

Posted on
  • I have a question, lets say i have a big function called bigFunction() that is being called inside Testing module in connect() function. If I packaged this function as a module and called it something like bigFunction and instead of calling this function from my Testing module I do a require("bigFunction").

    When will the require do its job and grab the file? when i require the Testing module or when I call the function that requires it?

    require("Testing") <--- Will it be loaded here?
    require("Testing").connect() <--- includes require("bigFunction") or only here?

    What I want is to only grab the big function if I required it to save space and memory

  • It depends on whether you're using the Web IDE to load modules, or have them on an SD card.

    • If it's an SD card then require("Testing") will load everything into memory.
    • If it's the Web IDE, the module will be loaded up even before your program code is. require("Testing") itself will do nothing.

    In both cases, the .connect() won't load anything extra (unless something in that function allocates a bunch of stuff).

  • I am using the Web IDE, so that means that the Web IDE will grab all required modules first and will just discard which function is calling it. I guess this wont change anything then and my big function will be loaded whenever I call my module

  • Yes, absolutely. Potentially a module is slightly worse as nothing in it can get freed in case the module is used again. If you did something like:

    function onInit() {
      myBigFunction();
      delete myBigFunction;
    }
    

    you could explicitly remove the function. You could actually do delete require("myModule").bigFunction too, but it's pretty nasty :)

  • hi,
    i have a question regarding the require mechanism and the project handling in the webIDE.

    i created a simple wrapper for the 'HC-SR04' which abstracts the low level handling.
    this wrapper of course uses the 'HC-SR04' module from espruino.com
    i want to reuse this class on several places so i put it into the 'modules' folder of the configured project directory in the webIDE.

    when i 'require(...)' this file i get the error message that the modul can not be found.

    it seems to me that it is not possible for a module (from the modules directory) to require another module regardless if it's a local one or from the web?

  • Ahh. I'll have a look at this today, it may have come up before. It could well be a bug in the module handling that happens for the 'projects' code in the Web IDE.

    For now, I guess you could just pass it in:

    var mymodule = require("mymodule").connect(require("HC-SR04"), ...)
    
  • Looking at this now - seems everything is working but there's an issue with the ordering of module initialisation. Easiest solution for now is to just do:

    require("HC-­SR04");
    var mymodule = require("mymodule")...;
    

    ... Or, I guess you're doing something like this in your module:

    var foo = require("HC-­SR04");
    
    exports.abc = function() {
    };
    

    But if you did this, it'd all work:

    exports.abc = function() {
      var foo = require("HC-­SR04");  
    };
    

    ... just to add, I don't think there's anything different with the projects code at all - all modules are handled the same right now. It's just that if modules use other modules, they're generally written not to use them in the global scope.

  • Ok, just fixed this - but it won't update until I push the next version of the Web IDE I'm afraid.

  • Hi, thank you for looking into this!

    Will test this out, the intermediate solution above is fine!
    What are the plans for releasing th next version of the WebIDE?

    Thanks for doing an amazing job!
    The espruino made my entry into hardware tinkering very easy. :)

  • I'm not sure - I might try and do a release next week? I like to try and use it quite a bit here first, just in case any bugs got introduced :)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Question about require()

Posted by Avatar for sameh.hady @sameh.hady

Actions