Loading modules from a webserver

Posted on
  • I have my own nodejs webserver and want to load my modules from this server. When I enter the following code on the right side of the Web IDE everything works hwever I get error messages like "Unexpected reserved word in":­

    Transmit = require("­smitClass.js");
    Wireless = require("­less.js");
    Switch = require("­chClass.js");
    oWl = new Wireless('myAp', '', new Switch());

    when I enter the command on the left side, the file is not found:

    Transmit = require("­smitClass.js")
    Uncaught Error: Module­.js not found
     at line 1 col 65

    The same happens, when I require such a module within the module:

      doTransmit() {
        console.log('Call Transmit');
        var url = 'http://'+this.wServer+':8081/transmitCl­ass.js';
        Transmit = require(url);
        this.oTr = new Transmit(this.wServer, this.mThing);

    I do not understand this behaviour. Could somebody explain this to me?

    Thanks for all,

  • Modules in the right, are resolved using your computer internet connection, and loaded with your code which is sent over serial/bluetooth to espruino device. In the left, you're on the espruino board itself and modules are not loaded. Indeed you've no guarantee of internet on some boards. Similarly, I woudn't expect you could require and resolve http addressed modules at runtime as you appear to be doing. "Unexpected reserved word" I don't know about.

  • Unexpected reserved word in ....

    Could be a syntax error in the module code.
    Copy & paste the code into right site for a syntax check.

  • Just to add to this: The IDE scans your code on the right hand side for mentions of require(url) - however it isn't smart enough to dynamically load module URLs that are specified in a variable.

    Espruino itself can't easily load modules via require because an HTTP request to get them would take a certain amount of time. Instead, if you want to load the module yourself do something like:

    function loadModule(moduleName, url, callback) {
      require("http").get(url, function(res) {
        var contents = "";
        res.on('data', function(data) { contents += data; });
        res.on('close', function() {Modules.addCached(moduleName, contents); callback(); });
    loadModule("transmitClass", "­s.js", functtion() {
      Transmit = require("transmitClass");
  • Tue 2019.01.15


    Add to Tips-and-Tricks?

  • Thanks for this tip! This brought me to the Idea to store the modules to the flash using Storage. This would allow to load the modules later also offline.
    My application has multiple modules. This approch would allow me to work on a smaller module without the need to load all the time all modules.

    However I came over a problem with the storage module, i'll put this in another thread.

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

Loading modules from a webserver

Posted by Avatar for fanThomas @fanThomas