Regarding modules, the upload for every piece of code happens in two steps:
parse the code (or module) to upload for every instance of require("noduleName");. for each found module do 1.
upload - code or module - for which the (nested) required modules have just been loaded.
Note: you can 'escape' step one by using a variable... But in order that the code has what it needs to run successfully, the module has to be uploaded separately.
var moduleName = "myModule";
require(moduleName);
Above code will fail with already on upload because it is all in top-level/level 0 and interpreted when arriving at Espruino with this message:
1v86 Copyright 2016 G.Williams
>echo(0);
ERROR: SD card must be setup with E.connectSDCard first
WARNING: Module "myModule" not found
=undefined
>
You notice two things:
2. ```WARNING: Module "myModule" not found```
Bottom line the 2nd item is what matters. Interestingly though is that ***when you have an SD memory card connected and mounted*** and "myModule.js" or "myModule.min.js" (minified version of "myModule.js") on it, the module is found on the SD memory card, loaded and stored *evaluated* in the cache.
You can actually ask Espruino which modules are cached. Enter in the console [Modules.getCached()](http://www.espruino.com/Reference#l_Modules_getCached) (command, reference):
Modules.getCached();
What you would expect is (according to [reference documentation](http://www.espruino.com/Reference#l_Modules_getCached)):
=[
]
***but*** you get:
=[
"myModule"
]
You will get what you expected with 'empty' Espruino (after you enter command ```reset()```. But for what ever reason, @Gordon decided to make the (map) entry anyway on require() but put the value undefined... and unfortunately there is *NO* (easy and) quiet (programmatic) way to figure out if the module is actually there, successfully loaded... *NOT even with a a try catch block* (Upload below code - copy-pasted into editor - and enter command ```onInit()```:
var moduleName = "myModule";
var myModule = "The module was not found";
function onInit() {
try {
}
var namesOfCachedModules = Modules.getCached();
console.log("Cached modules:");
console.log(namesOfCachedModules);
if (typeof myModule === "string") {
console.log("myModule = " + myModule);
} else {
console.log("myModule was set to " + myModule);
}
}
@Gordon, ***can that be fixed? ...backward compatible?*** Not successfully loaded modules should not show in the list or there should be an easy way to get to that info about the module - without Espruino throwing a fit / writing to console (and may be mess up if console not connected...) - or, worst case, with cumbersome try-catch-block that actually works... Try-catch is obviously not working as can be seen by the output produced (or not produces as expected by the catch-block), but sets the variable ```myModule```.
1v86 Copyright 2016 G.Williams
echo(0);
=undefined
onInit()
ERROR: SD card must be setup with E.connectSDCard first
WARNING: Module "myModule" not found
Cached modules:
[
"myModule"
]
=undefined
(Since ```Modules``` is natively implemented, there is no access to the 'map' that could just return ```null``` for not uploaded modules and ```not-null/module object``` for successfully uploaded modules.)
Back to hat I wanted to continue with (Sorry for this (even me) surprising detour...): Enter following commands and you get empty list of cached modules.
reset();
Modules.getCached();
Now we 'hand'-fix the issue by entering the issue by entering the following in the console (copy-paste):
Modules.addCached(
"myModule"
, "exports = \"a simple String value: 'Hi!'\";");
Then we enter in console (copy-paste):
Modules.getCached();
var myModule = require("myModule");
console.log("myModules is: " + myModule);
And the expected output is:
Modules.getCached();
=[
"myModule"
]
="a simple String value: 'Hi!'"
console.log("myModules is: " + myModule);
myModules is: a simple String value: 'Hi!'
=undefined
We could not directly upload and execute previous code, because first thing of upload is ```reset()``` which just wipes out our 'hand'-fix. But we can add the 'hand'-fix to the code as first thing and then proceed as before.
Modules.addCached(
"myModule"
, "exports = \"a simple String value: 'Hi!'\";");
var moduleName = "myModule";
var myModule = "The module was not found";
function onInit() {
try {
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Regarding modules, the upload for every piece of code happens in two steps:
parse the code (or module) to upload for every instance of
require("noduleName");
. for each found module do 1.upload - code or module - for which the (nested) required modules have just been loaded.
Note: you can 'escape' step one by using a variable... But in order that the code has what it needs to run successfully, the module has to be uploaded separately.
Above code will fail with already on upload because it is all in top-level/level 0 and interpreted when arriving at Espruino with this message:
You notice two things:
I guess this should put to peace many of the questions in regard of modules.
...another 2 cents from my part.
PS: Read through related Struggling with modules conversation.