PICO doesn't load the module

Posted on
  • The error that shows up on occasion in other projects but here it's persistant

    ERROR: SD card must be setup with E.connectSDCard first
    The error occasionally occurs when other modules are required, but usually clears on a 2nd or third send to Espruino.

    The code is an example from @allObjects

    var creds = require("credsXYZ.js");
    //var creds={ id: "XYZ", ssid: "Mine", pw: "123456", ip: "..." };
    console.log(creds);
    
    /* credsXYZ.js */
    //exports = { id: "XYZ", ssid: "Mine", pw: "123456", ip: "..." }; 
    
    var creds1={ id: "XYZ", ssid: "Mine", pw: "123456", ip: "..." };
    console.log("creds1= ");
    console.log(creds1);
    

    The credsXYZ.js module in the sandbox

    /* credsXYZ.js */
    exports = { id: "XYZ", ssid: "Mine", pw: "123456", ip: "..." }; 
    

    The output

     1v92 Copyright 2016 G.Williams
    >ERROR: SD card must be setup with E.connectSDCard first
    WARNING: Module "credsXYZ.js" not found
    undefined
    creds1=
    {
      "id": "XYZ",
      "ssid": "Mine",
      "pw": "123456",
      "ip": "..."
     }
    =undefined
    > 
    
  • Found the problem.
    var creds = require("credsXYZ.js");
    should be
    var creds = require("credsXYZ");

  • Thanks for updating it with the answer - any idea why it worked on the 2nd or 3rd send?

  • This example always fails with "module.js".
    Other code that use just the module name produce the intermittent load problem, that resolves after several attempts. Perhaps one day I will stumble on a simple example that can be used to resolve the issue.

  • Tried in console the following... according to Modules reference:

    >reset()
    =undefined
     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v92 Copyright 2016 G.Williams
    >Modules.removeAllCached();
    =undefined
    >Modules.getCached()
    =[  ]
    >Modules.addCached("credsTest",'{ssid: "ssidVal"}');
    =undefined
    Uncaught SyntaxError: Got ':' expected EOF
     at line 1 col 8
    {ssid: "ssidVal"}
         ^
    >Modules.addCached("credsTest",'{"ssid":­ "ssidVal"}');
    =undefined
    Uncaught SyntaxError: Got ':' expected EOF
     at line 1 col 8
    {"ssid": "ssidVal"}
           ^
    > 
    

    Both - non-JSON and JSON - ways fail... I suspect that the new feature to load modules as a function meddles with the previous workings... may be the issue is with both, upload AND pull ( require("..."); )...

    @Gordon, could it be that the feature of saving modules as functions is doing things differently?

    *** BUT*** *** BUT*** *** BUT*** *** BUT*** *** BUT***

    Doing it in a module:

    // credsTest.js
    exports = { ssid: "ssidVal", pw: "pwValue" };
    

    ...and in the code as follows:

    // credsObjTest.js
    var creds = require("credsTest");
    function onInit() {
      console.log("creds object:");
      console.log(creds);
      console.log("Modules as loaded:");
      console.log(Modules.getCached());
    }
    // for the laxy developer at development time:
    setTimeout(onInit,1000);
    

    ...I get this output on upload:

    >
     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v92 Copyright 2016 G.Williams
    >
    =undefined
    creds object:
    {
      "ssid": "ssidVal",
      "pw": "pwValue"
     }
    Modules as loaded:
    [
      "credsTest"
     ]
    > 
    

    The last line is only that I do not have to enter onInit() in the console. Saving the code without the last line on my PICO I get following output (...because save() invokes onInit() and I'm still connected):

    >save()
    =undefined
    Erasing Flash.....
    Writing.....
    Compressed 81600 bytes to 3066
    Checking...
    Done!
    Running onInit()...
    creds object:
    {
      "ssid": "ssidVal",
      "pw": "pwValue"
     }
    Modules as loaded:
    [
      "credsTest"
     ]
    > 
    

    @ClearMemory041063, I do not know what it fighting you...

    @Gordon, made another try in the console with (modules source code and not just source code):

    >reset()
    =undefined
     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v92 Copyright 2016 G.Williams
    >Modules.removeAllCached();
    =undefined
    >Modules.getCached();
    =[  ]
    >Modules.addCached("credsTest",'exports = {ssid: "ssidVal"};');
    =undefined
    >Modules.getCached();
    =[
      "credsTest"
     ]
    >require("credsTest");
    ={
      "ssid": "ssidVal"
     }
    > 
    

    And it works, works, works,... nothing is broken... (so: @Gordon, never mind my comments from before...)

  • Yes, the issue is that Modules.addCached is expecting a statement, but you were giving it an expression.

    If you typed {ssid: "ssidVal"} at the console it'd fail too, because when it sees { it's expecting a block of code and not JSON.

    To work around it, you can surround the JSON in brackets: ({ssid: "ssidVal"}) - but in this case it's not actually going to have the effect you want.

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

PICO doesn't load the module

Posted by Avatar for ClearMemory041063 @ClearMemory041063

Actions