You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • @Wilberforce... may be left and right got a bit mixed up... did it...

    Things entered on the left side are JavaScript expressions / statements that are immediately executed. Therefore, if you enter something like var mod = require("moduleName");, Espruino tries to executed that, which means: get the module from the Espruino (memory)'s Modules cache. Since it is not there, Espruino tries to look for it on a mounted SD card. If the card is not mounted, 'mount error' is printed and subsequently the 'module not found error'.

    If you really want to use the left side to load a module into the cache, you have to do it differently - as documented - in Modules.addCached("moduleName","moduleSo­urceCode"); (id and moduleName are the interchangeable).

    If you track what the IDE does when uploading code with a require("moduleName");, it does exactly above: The IDE grabs the file from https://espruino.com/modules/moduleName.­min.js as text and executes Modules.addCached("moduleName","moduleSo­urceCodeAsReadFromTheFile.min.js");.

    This though has limitations, because it does not handle nested require(...): if a module source code that you push into the cache in the left side of the IDE, you also have to that recursively for all the included require(...).

    If entered in the right side - editor side - and uploaded using the upload button - all this is taken care of.

    Now there may be the situation that modules are in deed on an SD card and the SD card is mounted on the Espruino board. If you then want to take advantage of that while still uploading code from the Editor side, you would stick the moduleName as string into a variable and then use the variable in the require expression:

    var moduleNameVar = "moduleName";
    var mod = require(moduleNameVar);
    

    Doing it this way, the upload process does not find a literally required module (finding the exact character sequence require("...") and therefore not jump into action a retrieve the module from the Web / or local project/modules/ sandbox folder and upload it to the Modules cache.

    When it then comes to execution, the module is not found in the Modules cache but it would hen find it on the mounted SD card (if the card is mounted...).

    (PS: there is now also an option that modules are stored as functions in Espruino cache... I do though not know how it could be done using the left side... may be the reference doc is not updated yet...).

About

Avatar for allObjects @allObjects started