-
• #2
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.
-
• #3
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.
-
• #4
@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 ;-)
-
• #5
After posting, I recalled that the uploader already understands at least one directive: "compiled"; ;-)
-
• #6
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.
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:
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: