Writing modular code using "require"

Posted on
  • I understand the purpose of require("modulename") but I was wondering if this approach could be used to load my own modules and to help structure my own code.

    If I place my own modules in a specific folder can the Web IDE be configured to look for them there or does it always look online for minified versions of the Espruino modules?

    (Even if the Web IDE cannot access my local file system it would be good if it could access custom modules via my locally hosted web server)

  • In options of WebIDE is a point PROJECT(SANDBOX)
    With this you can assign a local directory to be used as a sandbox in WebIDE.
    There is a tour in help to show the options.
    WebIDE searches in sandbox first, before going to Espruino server.

  • Yes, absolutely. Just follow the 'Project Tour' that's accessible from 'Project' in the Settings menu. It'll let you set up a directory that can have your own modules in.

    You can also just type require("http://foo.bar/hello.js") (you can even link to the 'raw' file on GitHub).

    Hope that helps!

  • You can do both.

    You can set a folder for the web ide to look in. There's an option to set the sandbox folder in the settings, and it will look in the modules subfolder there. You might have to enable this as well.

    Also, you can do require("http://yoursite.com/path/to/module.js")

    Lol, beaten... Was it really an hour between when I loaded the page and wrote the reply?

  • Excellent! I should be receiving my board next week and in the meantime have been putting together some code - migrating a central heating timer from delphi to javascript - this will be in its own module.

    It may be of use to others (for anything that needs triggered regularly and repeats on a weekly cycle) so I will share it (with documentation) when finished.

    I find the following site very useful for developing/testing javascript:


    (hopefully the code I have written will run ok on espruino - I'm doing nothing fancy!)

  • For reliable timing, you'll need a crystal (32.768khz) to solder onto the board (see http://www.espruino.com/Clocks ) or (probably better, if you're controlling your heat with it ;-) ) an external RTC module (with a battery, so even when the espruino resets, it'll keep time) - there's already a module for the DS3231, used in the most common RTC boards.

    Espruino runs normal javascript - but you have to be much more aware of memory usage, since it's running on a microcontroller with 48k of ram, some of that used for the interpreter and stack. The length of the code actually becomes relevant.

  • Assuming I'm connected to the internet could I regularly update the inbuilt clock?

  • Yeah - you could use the Date class and rely on regular updates of the time, definitely.

  • ...like that and just took advantage of the Project-Sanbox feature for my Resistive Touchscreen directly (no touch controller) (sub) project at http://forum.espruino.com/conversations/­256122

    Thanks @DaveNI / / @JumJum for bringing that up and to my attention as a posting subject... (...and same time shame on me not having worked through all tutorials (yet)). - :(< ... :-)

    #require #module #localmodule

  • for reliable timing, you'll need a crystal (32.768khz) to solder onto the board (see espruino.com/Clocks )

    @DaveNI where did you order your board from? There's a new revision (1v4) of the board that's slowly working its way out to distributors, and that one has the crystal pre-installed.

  • @Gordon

    It was shipped from digitalmeans this afternoon. Hopefully it's the new version!

  • I just checked and I think you might be out of luck... I'm sending Digitalmeans some 1v4 boards in the next few days, but they still have the 1v3 in stock.

    Adding the crystal isn't a big deal though - they're really cheap, or you can probably scavenge one from an old watch or clock if you have one kicking around.

  • @Gordon

    The package just arrived - really great service, ordered on sunday night!

    Its the 1v3 but I don't mind - if I enjoy using it I'll probably get another and ensure its a 1v4.

    (I may even get a DS3231 RTC module for battery backup)

    Now to get playing :)

  • Great - have fun with it!

    If you have any questions just ask on here, I'm sure someone (or sometimes 3 people!) will be happy to help :)

    The next version of the firmware (1v71) will actually remember the internal RTC's date even after a reset - so if you keep a battery plugged into the JST connector then it should 'just work'.

  • If have setup the sandbox but it only allows a single projects and module setup. I have also tried pulling files from Google Drive via a link but it doesn't seem to work. Not sure if its because it's https or because it doesn't reference a .js file directly or if I'm just doing it wrong.

    Any possibility of supporting relative local references like NodeJS, such as the line below. I like to organize long code blocks into project specific modules that I don't necessarily want in a modules library or folder.

    var onPage = require('./myTestPages').onPage;

    NodeJS also supports reading JSON files that I find handy for loading configurations and data, such as the line below.

    var cfg = require('./config.json');
  • I think that is due to the aggressive limits on chrome webapps. They are really locked down, with regard to accessing system resources.

  • The espruino command line now supports require() as per node . It implements the logic of node's require().resolve according to the rules set out at https://nodejs.org/dist/latest-v5.x/docs­/api/modules.html#modules_all_together (with the exceptions of not supporting node binaries *.node and only looking in the modules folder in the current working directory, not recursively following back up to the path to the root)

    See this post

    That includes files, folders, sub-folders, .json, index.js and package.json support.

    This hasn't made its way to the WebIDE and I don't know if it ever will.

  • have also tried pulling files from Google Drive via a link

    Do you have an example link?

    If it really is a link to a text file then it should work.

    var onPage = require('./myTestPages').onPage;

    Does var onPage = require('myTestPages').onPage; work? I understand you might want to use the local path, but for now it seems like an easy workaround?

    This hasn't made its way to the WebIDE and I don't know if it ever will.

    As @DrAzzy says, the WebIDE is pretty locked down, although once a sandbox is set up this should be possible (it's just not implemented yet).

    I think the issue is that @Snerkle's module search code only works specifically with node.js at the moment. I think there would need to be a bit of refactoring for it to work in the Web IDE - but if that was done it would be fine.

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

Writing modular code using "require"

Posted by Avatar for DaveNI @DaveNI