• The following code works just fine, but provides me twice with a warning Module ABC not found when uploading to the board. Since I provide the module id and code, I would not expect any warnings on .addCached() nor on require().

    For 1V71:

    Modules.addCached("ABC"
      , "exports.name = 'Espruino'; "
       +"exports.spelled = function() { return exports.name.split('').join(' - '); }; "
      );
    
    var m;
    console.log((m = require("ABC")).name); // --> Espruino
    console.log(require("ABC").spelled()); // --> E - s - p - r - ...
    m.name = "espruino";
    console.log(require("ABC").spelled()); // --> e - s - p - r - ...
    console.log(m.name); // --> espruino
    console.log(m.spelled()); // --> e - s - p - r - ...
    

    Above code proves that repeated require() return the very same module (object).

    For *1v72 - which supports generic exports for more obvious object-oriented programming style with singletons and classes defined in/as modules - I use this code, and get the same complaints about Module XYZ not found:

    Modules.addCached("XYZ" // Singleton
      , "exports = "
       +"{ name: 'Espruino' "
       +", spelled: function() { "
       +"      return this.name.split('').join(' - '); } "
       +"}; "
      );
    
    console.log(require("XYZ").name); // --> Espruinus
    console.log(require("XYZ").spelled()); // --> E - s - p - r - ...
    require("XYZ").name = "espruino"; // modifying module / singleton
    console.log(require("XYZ").name); // --> espruino
    console.log(require("XYZ").spelled()); // --> e - s - p - r - ...
    
    Modules.addCached("Person" // Class
      , "var Person = function(name) { "
       +"    this.name = name; "
       +"  }; "
       +"Person.prototype.spelled = function() { "
       +"    return this.name.split('').join(' - '); }; "
       +"exports = Person; "
     );
    
    // var Person = require("Person");
    // var person = new Person("Espruinus");
    var person = new (require("Person"))("Espruinus");
    console.log(person.name); // --> Espruinus
    console.log(person.spelled()); // --> E - s - p - r - ...
    person.name = "Scriptus";
    console.log(person.spelled()); // --> S - c - r - i - ...
    require("Person").prototype.spelled 
      = function() { return this.name.split('').join(' = '); };
    console.log(person.spelled()); // --> S = c = r = i = ...
    
  • Hi - it's actually the Web IDE that's complaining about 'module not found'. It has a list of modules that are built in and checks against that, but if it thinks that module isn't built into Espruino and can't find it then it'll complain.

    I'm not quite sure what you're trying to accomplish here, but if you want to load the module in some other way then it might be worth modifying the Web IDE's source. It's got a plugin structure so shouldn't be too much of a pain to do.

  • Can't you confuse the IDE, to work around this? I thought you could do this and the IDE would let it through (I remember doing something like this before the IDE could load modules from local filesystem)...

    
    var x="ABC";
    require(x).connect(...);
    
    

    In an ideal world, I think this should pop up a dialog "Module XYZ not found, send anyway?" with a check box to not show it again, but I would consider this pretty unimportant, since it's such an exotic use case, and there's a workaround.

  • @Gordon, I came across this while working on returning a 'class' definition as module (see How to create a module that is a 'Class' - aka a function usable with new - or: How to make require() return a function and not an object and 'playing' with the Modules object (modules cache) to make it happen. So no real-world goal at first sight... but keep reading.

    Now thinking about the sequencing, it makes sense: on code upload, IDE parses the source looking for all the require()... Since the module is nowhere to be found because it will be created on execution dynamically by the code itself and the IDE is not executing code, pushing out a warning is the best - and most friendly - the IDE can do...

    Using @DrAzzy's approach - using a variable for the module name - 'hides' the module for the IDE... and the IDE is not checking for the module... (variable name in the source is not a literal String).

    @DrAzzy, since IDE's parse is obviously a simple implementation, no variable is required (which in the above 1v72 Person class example costs 4 variable slots). Putting the string in parenthesis - as an expression - works just as fine and does not give away any variable slots:
    ...require(("Person"))...
    is all it needs... ;-)

    What I try to achieve - in general - is get rid also of warnings... Warnings are not really an issue, but they are a great (gut) indicator that something is about to go bad... ;-). Adding some sort of directives/annotations for the uploader could help here to get rid of the warning... - @Gordon: this is the second-to-last thing on your to-do list, in case it even makes it on the list... the last thing is still be discovered ;-)

  • After posting, I recalled that the uploader already understands at least one directive: "compiled"; ;-)

  • I'd totally forgotten about the require(var) thing. Your bracket solution is much neater though - thanks for posting it up!

    The dialog solution is much neater - and probably not too painful to implement either... It could be useful for several other classes of warnings too.

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

Added module code to Modules cache - but (still) getting 'Module XYZ' not found

Posted by Avatar for allObjects @allObjects

Actions