- KEYWORDS: Modules,Libraries
In Espruino, Modules are pieces of pre-written code (libraries) that perform common tasks, such as interfacing to different bits of hardware.
They can currently be used in a few different ways:
If you're using the Espruino Web IDE, simply write require("modulename")
on the right-hand side - as you would have seen in the reference pages. When you click the Send to Espruino button, the Web IDE will automatically look online for minified versions of the modules you need, download them, and load them onto the board. You don't need an SD card or an internet connection to the Espruino board itself.
If you are using the Web IDE as is, the modules will be loaded from http://www.espruino.com/modules/. This URL can be changed in Web IDE settings.
To save space, most modules are provided as a minified version and the Web IDE tries to load minified versions first with default configuration.
For example, using require("ADNS5050");
will make the Web IDE loading the minified module from http://www.espruino.com/modules/ADNS5050.min.js.
For now, as you can type a URL into require, you can actually just pull a module right off GitHub. For instance you could look at the history of something on GitHub, and could then just require a specific version of that file with:
require("https://github.com/espruino/EspruinoDocs/blob/411be7a72bc55b4a51d05f3df5bc8fa80017f3b0/devices/PCD8544.js");
The URL comes from clicking <>
by the commit you were interested in.
If you activate this option in Web IDE, you can load modules from the NPM repository. Right now it:
- only loads the latest version there.
- only works if the module contains a single file.
- can cause some confusion with Espruino's modules, for instance clock.
For example using require("async");
will make the Web IDE loading the tar.gz file (with automatic extraction) of the module from http://registry.npmjs.org/async.
If you are using a local project folder, the Web IDE will automatically create an empty modules folder inside. Put a module there and you can load it with require("myCustomModule");
.
With default Web IDE configuration, it will look for modules following this order:
- local minified
- online minified
- local normal
- online normal
If your own module has the same name as one of the existing ones, the Web IDE will use the minified version from online first.
If you need it anyway, you can provide a local minified version or you can change the Web IDE configuration from .min.js|.js
to .js|.min.js
or even myCustomModule.js|.min.js|.js
to get it working.
If you have an Espruino with an SD card (but you're not using the Web IDE), you can copy the modules you need into a directory called 'node_modules' on the SD card. Now, whenever you write require("modulename")
the module will be used.
We're working on this - but soon: If you have a WiFi-enabled Espruino and it is connected to the internet, writing require("mymodule")
will cause it to look on the internet for the module with the name you have given.
- APPEND_KEYWORD: Module
Espruino also contains many built-in modules and classes that provide a lot of functionality:
- APPEND_KEYWORD: Built-In
You don't need a module to be able to interface to hardware - sometimes it just makes it easier. If you want to interface to a device that isn't listed here, please check out the [[Tutorials]] page, or [[Search]] for it.
Why don't modules work when typing require
on the left-hand side of the Web IDE (or from a terminal window)?
When you type require("modulename")
on the right-hand side and click Send to Espruino, the Espruino Web IDE scans your code for require
statements and loads the relevant modules off the internet. Because the left-hand side of the Web IDE (or a terminal window) sends each character direct to Espruino, by the time you have pressed enter to execute your command, it's then too late to load the module.
Instead, Espruino defaults to what is mentioned under the Stand-alone Espruino heading above - it looks on an SD card (if inserted) for the module. This is why you might get a ERROR: Unable to read file : NOT_READY
error written to the console.
No. As long as you have used require('module')
at least once for each module before you type save()
, all the information that is needed will be cached inside Espruino.
Yes. By default each module that is loaded will be cached (to avoid loading modules twice). However you can call Modules.removeCached('modulename')
which will remove the module from the cache and free the memory that it uses.
It's easy! See the [[Writing Modules]] page...