• So I'm creating some modules for my project, one of them being a class. This class is in the Modules folder, and is being require'd in. Here's the opening code snippet:

    class Potentiometer {
        constructor(config, PID) {
            this.config = config;
            this.PID = PID;
            this.registeredCallbackTimerId = undefined;
            this.setValueTimerId = undefined;
            this.currentValue = 0;
    exports = Potentiometer;

    ...and of course I'm doing this in the main code:

    let Potentiometer = require('potentiometer');
    let pot = new Potentiometer(config, PID);

    When I run the code, I get the 'constructor' is not defined error. I'm on fw 1.93 if that matters. I could use a little help.

  • Mon 2019.07.30

    'I'm on fw 1.93 if that matters'

    Ahhhhh, finally an easy one. Glad you included that tid bit.


    See section under ES6 when classes were introduced.

    Flash away . . . .

  • Whew! Glad to hear that...because I will have lots more questions coming down the pike.

    PS - I've played with NetDuino (but not Arduino), Raspberry Pi, and BeagleBone Black prior to Espruino. Hands down, Espruino is best!

  • @Robin, the firmware update did indeed solve the problem, so thanks a bunch for the help. I'm going to hijack my own convo thread rather than create a new post...

    With both the old 1.93 fw and the current 2.04 fw, I've had intermittent errors loading the module I showed above. I was able to load this module in 1.93 when it was a function rather than an ES6 class. After class conversion and fw upgrade, I was once again able to load the module.

    However, intermittently, the load would simply fail with "module potentiometer not found". Screenshot attached, showing load failure followed by load success like 5 seconds later. Note that this is the ONLY file that intermittently fails to load (and that it is last in the require list), the others all load 100%.

    1 Attachment

    • Potentiometer-not-found.png
  • Glad you got it sorted!

    I think this is actually something @Robin may have experience with as well. Basically the 'Projects' plugin for the IDE uses a pretty simplistic way of requesting module files - it tries to find one and then waits 1/4 second to see if it can get a result.

    I've literally never had one failure with this, but then I'm using Linux with an SSD. I think if you have Windows with a hard disk and a virus checker then it seems it really can take over 250ms to load a 2kb file sometimes!

    Unfortunately it's not quite as easy as just raising the delay, because that delay impacts literally everyone who is using 'projects' when they load a module that is not on their hard disk. If I raise it to 500ms then if you require 4 modules that exist on the Espruino site, that's 2 seconds of waiting around for every upload.

    The code is here if you're interested: https://github.com/espruino/EspruinoWebI­DE/blob/gh-pages/js/plugins/project.js#L­46

    While it should be possible for it to be fixed properly (eg. without a delay), Google's been threatening to close the Google Web App Store for years, in which case all this code would have to disappear anyway so it wouldn't be high priority.

    I'd say if you definitely want it to work reliably, for now it might be better for you to start a local webserver and then require("http://localhost/myfile.js") - and that will work reliably.

  • Thanks for the detailed explanation, @Gordon, I understand the sticky wicket you're in. I'll consider a local webserver if things get worse - how long does a static node webserver take to write anyway, like an hour maybe? For the record, I'm on a 2009 Mac Pro (big aluminum tower) with Zeon processors and spinning hard drives. Virus checker? What's that?

  • Strange - maybe it's the seek time on the hard drives that's the biggest issue then? I always assumed 250ms would be plenty, but obviously not.

  • Wed 2019.07.31

    fr @Gordon #5 'actually something @Robin may have experience'

    Yes, a year ago. This link may contain answers to some questions that may arise. For what it's worth, disabling the virus checker actually didn't change the situation I had then.


    Is the same #4 error the one we are currently dealing with @indianajones?

    Although the error (presented in 2pt font for ;-) obscurity) shows a property not found, the abbreviated #1 snippet doesn't have the balance of the class, that property may indeed be missing, so unable to definitively answer here.

    But I did notice that the exports statement uses a capital 'P' designating the class name. The require statement uses a lower case 'p'. When I tried the #1 snippets, I get a module not found error, until I changed the case of that leading character.

    A topic (re: exports - require) I never quite understood all the rules to, but might be something to check.

  • @Robin, my understanding of the case issues with require is to get the case of the filename right, and my filename is all lowercase. The errors you see are what happens after you get undefined back from require. If the module loads, and it does 90% of the time, I get no errors and my code runs as expected. Note that if this is merely an issue with the chrome extension, and not a "real" issue, I can live with it. Though I wonder what a 300ms wait would do, since I appear to be on the fence.

  • Would developing in a monolithic file until all the fine detail is worked out be of interest @indianajones? Speeds module development. I could dig up some links.

  • Well, to be honest, I like the module configuration I have going right now. I'm humming along very nicely in the web IDE environment, and this loading error is a minor issue. I'm doing all my coding in VSCode, which is then loaded using require. Works great. As for the loading issue, I know that it's not a bug in my code, and I'm able to continue just fine because it's intermittent.

    I want to reiterate my thanks for all the help you guys have provided on my project, which BTW is an 8-channel MIDI-over-WiFi control surface for the Allen & Heath Qu-SB mixer. One Pico to control each channel, and one Wifi as the Master and communicator with the actual mixer and with the 8 Picos.

  • 'I'm able to continue just fine because it's intermittent'

    On an extreme unique project a year ago, when I got to 4 modules ~1000 lines each, the intermittent turned into each upload. To save my sanity, I gave up and switched to a monolithic file. Then when done, created the individual modules for final testing. Deploying modules to a remote server such as GiHub worked reliably also.

    'One Pico to control each channel'

    Does the design actually require a dedicated Pico for each channel?

    Has the MDBT42Q with more pins and BLE been considered? (my favorite - wireless programming)


    Which is being used for comm. SPI, I2C or USART? I don't see why one Pico couldn't handle more than one dedicated task. Lot's of free space between pulses when viewing with a logic analyzer. MUX the pins perhaps?

    Sounds like a fun project. Only wish I had more time to get back to my synth practice. Rick Wakeman look out!!

    'Though I wonder what a 300ms wait would do'

    For me, but on a Windows10 PC, the upload will hang. Closing the WebIDE and relaunching is the only option. (I've found so far)

    It is interesting to check what is going on under the hood. If this hasn't been discovered yet:

    WebIDE >> Settings >> Console

  • This project will grow obviously, but I think I'm going to use @Gordon's idea of a local webserver to serve them up. Heck, I could just use my bitbucket URLs to the files.

    I initially wanted one Wifi to manage the whole thing. Too few pins. So I went with a Pico per channel. I considered a Pico for every 2 channels or 4 channels, which I still might do. It's not like I've sent out for PCBs yet! But I'm starting at 1 each and will go from there.

    I haven't checked out the MDBT42Q, I'll do that.

    USART for comms, because as I learned from other forums postings of mine, I2C and SPI are master-only. I2C is ideal because it's got built-in multi-device support, but if the Picos can't be I2C slaves, then I'm sunk. Though I found that bridge chip which is cool. But I'm going to first try Gordon's approach using the USART. Because it doesn't require more chips.

  • This sounds great - Thanks for sharing what you're up to!

    Since MIDI is basically just UART you could potentially use software serial on the Espruino Wifi for each channel (if you're sending only) - although at 115200 I don't know how reliable that'd be.

    There are always I2C to UART converters like https://www.embeddedadventures.com/i2c_t­o_uart_mod-1025.html but to be honest if you have something working and the budget isn't a problem, using Picos sounds great and gives you loads of flexibility for doing more stuff later.

  • Thanks, I'll keep everyone updated with my progress, because I'll continue to have questions. I've just started the project a week ago, but am reading/moving the potentiometer, and just ordered a Wifi to start working on comms with the mixer (via WiFi) and the Pico that controls the pot. Those comms are the hard part of this entire project. Not getting them to work, but getting them to work and feel real-timey.

    I think I'll also purchase a MDBT42Q and see how it works as a Pico replacement, though I wouldn't use bluetooth. One step at a time.

  • Instead of local Web Server, you can also mount an SD card with your modules and require the module from there. To avoid require to be resolved by the upload, you have to put the module name into a variable and use the variable in the require.

    var mn = "moduleName", moduleNameModule = require(mn);

    ...even this may work - since a regex is used to search for modules, such as 'require("moduleName") ':

    var mn; // define module name variable once at the beginning of the code
    // .....
    // ...
    // .
    var moduleNameModule = require(mn="moduleName");
    // .....
    // ...
    // .
    var moduleName2Module = require(mn="moduleName2");
    // .....
    // ...
    // .

    Using that approach let's you quickly update all your devices by just replacing the SD card... The code that you upload to Espruino is just a boot strapper that mounts the SD card, makes the initial require for the initial module - like a main / init module - and invokes the startup method.

    Of course, the is one more piece of hardware you need per Espruino... but since you go modules, why not to it all the way with modules...

    To make sure the that things work as expected, you need to put the boot strapper code into the onInit() method.

  • Thanks for the suggestion, @allObjects! For some reason, the intermittent load issue has abated, I haven't seen it in the last couple hundred uploads. I haven't shrunken the file size, but I'm also not growing it either.

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

Uncaught ReferenceError: 'constructor' is not defined

Posted by Avatar for indianajones @indianajones