Help requiring modules on Espruino-wifi board.

Posted on
  • I have tried a few ways to put a module in my local project. In the web ide I have set a folder for a project, this created a bunch of folders, one of them modules - I have tried to NPM install a module gsap there but I cannot require this with:

    var gsap = require('gsap');
    

    I thought maybe it doesn't need to be in node_modules folder so I took out the gsap folder and only that is in modules, still can't find it.

    I copied the min.js file to modules folder, still cannot require it there.

    Both of these examples work if I use the cmd to run node and the require('gsap'). What is the correct way to require a module from modules folder in a project?

  • Sun 2018.10.14

    Please post the following:

    What PC and OS is the IDE running on?

    What is the version of IDE? native or Chrome? Settings >> About

    Version of Espruino? process.env

    Are you using an SD card?

    File Path to the sandbox folder? Settings >> Project


    Your technique and syntax appears fine.

    I'm sure this is your case as was explained, but just in case:
    Verify the module 'gsap.js' is in a sister 'modules' folder adjacent to the code file in the 'projects' folder. Both children of the sandbox parent folder.



    I ran into a similar issue on Windows10 and played with this for quite a while before finding a solution. See #19

    http://forum.espruino.com/conversations/­325378/#comment14450197



    FYI @Gordon, here is another user with a 'node_folders' folder see #17 #15 in above link, and #19 for 'tilde' reference response to #17

  • Hi Robin,

    Thanks for the reply, here's my answers to your questions...

    What PC and OS is the IDE running on? -Desktop AMD 1700x custom built - Windows 10 professional

    What is the version of IDE? native or Chrome? Settings >> About -Web IDE version 0.70.4 chrome app.

    Version of Espruino? process.env - VERSION": "1v99",
    "GIT_COMMIT": "f0d66ba",
    "BOARD": "ESPRUINOWIFI",
    "FLASH": 524288, "RAM": 131072,
    "SERIAL": "29004900-0c513532-39333638",
    "CONSOLE": "USB",
    "MODULES": "Flash,Storage,fs," ... ",neopixel,Wifi,AT",
    "EXPTR": 536871156

    Are you using an SD card? No sir

    File Path to the sandbox folder? Settings >> Project - C:\git\espruino\leds

    Here's the console messages:

    Firmware >1.43 supports faster writes over USB
    Set Slow Write = false
    FIRMWARE: Current 1v99, Available 1v99
    Device found {"bitrate":9600,"bufferSize":4096,"conne­ctionId":12,"ctsFlowControl":false,"data­Bits":"eight","name":"","parityBit":"no"­,"paused":false,"persistent":false,"rece­iveTimeout":0,"sendTimeout":0,"stopBits"­:"one"}
    [success] Connected to COM7

    Connected to COM7
    loadModule(gsap)
    ERROR: getURL("https://www.espruino.com/modules/gsap.mi­n.js") error : Not Found
    ERROR: getURL("https://www.espruino.com/modules/gsap.js­") error : Not Found
    WARNING: [notify_warn] Module gsap not found
    Received a prompt after sending newline... good!
    Splitting for reset(), delay 250

  • Sun 2018.10.14

    Thanks for the data posting. That confirms and nearly identical to the path I was on. The difference 'native' vs 'Chrome' flavor of IDE.

    From console output:

    re 'ERROR: getURL("https://www.espruino.com/modules/gsap.mi­n.js") error : Not Found'

    If you read #1 from the link I previously posted in #2, this is the normal sequence. Unfortunately no local path indication the IDE is searching for module fetch.


    Just to confirm, module 'gsap' is one you created? (as it isn't present at)

    http://www.espruino.com/modules/

    If this is the case, sounds like the same issue I had. I would read over what I went through. The only suggestion I have (I'm an end user just like yourself) is to create a new folder (the sandbox) in the root of drive 'C'. Change the sandbox location (Settings >> Project) to match that folder. Then using the 'projects' button on the IDE, accept that default in order for the IDE to create it's own mappings. Then using Windows, go back and patch the code file and place the module file in the IDE created folders.

  • Just to confirm, module 'gsap' is one you created? (as it isn't present at)

    No I want to use gsap from here, well specifically i will want TweenLite as I may run into memory problems as gsap could be too big I think.

    https://www.npmjs.com/package/gsap

    I tried a new folder at the C root, still no joy, it appears to me that it doesn't even attempt to look in the modules folder at all according to the console output...

  • OK so this is now working, thank you for pointing me in the right direction with the question if I had written this module myself!!!

    I created a quick module to check and that was indeed loaded correctly, which made me think ahhhh I need to rename the downloaded module so that the filename matches what the Espruino is looking for, this isn't necessary using node_modules as it uses the folder name as the mapping I believe.

    So it is now loading the module as expected.. Sorry for the confusion and thanks for the support.

  • re: ' it appears to me that it doesn't even attempt to look in the modules folder at all according to the console output...'

    As I indicated in #4 above, please see #1 at link provide above dated Sun 2018.09.09 for reference to the order of locations looked at by IDE



    Thanks for the link to gsap. Learn something new everyday.

    'GSAP is a JavaScript library for creating high-performance animations that work in every major browser.'

    As it appears that TweenLite wasn't written for Espruino, there may be issues (that might be correctable re-writing parts) straight out of the box. Caveat Emptor

    EDIT
    Post #6 stepped on during creating this #7 - flip for best reading . . .

  • Sun 2018.10.14

    'thank you for pointing me in the right direction'

    Glad it was an easy fix, having just been through something remarkably similar.

    @Coder2012 for @Gordon reference, where did you pick up on, or where are you using 'node_modules' or was that the content viewed as an error in the IDE?

    Are you still using folder: ' C:\git\espruino\leds ' or did you use a single folder in the drive root? e.g. just a file name issue and not also a folder mapping issue



    Gordon, make sure you see #2 response

  • I just renamed the 'TweenLite.min.js' to 'gsap' so I could do:

    var gsap = require('gsap');
    

    Didn't use node_modules anywhere. It works fine regardless of where my sandbox folder is in the two cases mentioned previously.

    I think you're right about not being able to use this on Espruino though as I can't seem to call any functions on it.

    I was hoping to use it with the WS2811 lights, I used to do all my anims manually with setInterval but I've extensive knowledge of this library from doing frontend dev over the years and had an idea to Tween values using this as it's so powerful, maybe it will work out or maybe it won't...

    PS. It can be used on nodejs so maybe there's a way to get it to play with Espruino.

  • This might have also worked:

    var gsap = require('TweenLite');
    


    ' as I can't seem to call any functions on it.'

    Remember that you might be able to use those functions if you edit that file using the exports keyword statement to expose them.

    http://www.espruino.com/Writing+Modules

    EDIT -Yikes!! Just peeked as source and it will be challenging . . .




    re 'Didn't use node_modules anywhere'

    Do you recall how you learned about 'node_modules' or used it (see #1) for this post?

    Just trying to provide those that work on the edits of the IDE more information. In my case it was an error that popped up, despite never using, or creating, or even having any knowledge of that folder word. I'm sure they would be curious how a separate unique user had the same occur on their system, doing nothing more than attempting to access a module.

  • Ah so originally when I wanted to use the 'gsap' module, I thought I'd install it in the modules folder that was created by the sandbox project thingy. To do this all you need to do (as long as you have nodejs installed of course) is:

    npm install gsap
    

    This creates the 'node_modules' folder with another folder called gsap inside, inside this is all the files associated with the gsap module.

    Then in node at least, you just require it and off you go...

    What I hadn't realised (but do now after reading the forums) is that there was a beta NPM setup but that was dropped, possibly unrelated to my issue anyway (I'm digressing here) but I thought maybe the Espruino expected local modules to reside in a 'node_modules' folder, but it doesn't.

    Hope that clears up any confusion I've caused?

  • Hope that clears up any confusion I've caused?

    Yep, thanks, it does for me, no confusion on your part. Ahhhh, now we have the needed missing bit of information. I also, at some distant time past, installed NPM. So, maybe there is a conflict with the IDE somehow, relative pathing when using modules. Food for thought @Gordon, and yet another item to add to your all ready lengthening to do list . . . see #8 #2 response

  • The https://www.npmjs.com/package/gsap documentation in its very first sentence says:

    GSAP is a JavaScript library for creating high-performance animations that work *** in every major browser ***.

    The code runs in the browser and the node.js / Web server. Even though Espruino (-Wifi) can work as a a Web server, it is not a node.js server.

    If you implement a Web server on your Espruino-Wifi - based on https://www.espruino.com/WebServer - and implement the pulling of files according to the http(s) request request urls from the SD card, you may get something working (in your browser). You have to used the SD card because Espruno's memory is just to small to keep all the files in memory. Espruino is then the server for the resources the browser needs. Even if you get node.js basics server-side - on Espruino-Wifi - going, it just lacks the resources...

    It would be help full to explain what you want to achieve from a birds point of view.

  • Yes thank you @allObjects. I wasn't aware of 'gsap' or it being a publicly available source and at that (#2 above) time made the assumption that 'gsap'(an unusual name) was created by Coder2012 and was just having difficulty loading the module. We cleared up usage in #7 above, and @Coder2012 is aware of it's limitations.

  • There is information available about using node.js / npm modules. After all, Espruino follows the node.js style of handling modules. Second one is very insightful conversation with explanations from @Gordon.

    Not directly related, but interesting to know is that the Web IDE can run as a node.js application on any device that can run a node.js... for example, raspberry pi:

    https://github.com/espruino/EspruinoWebI­DE

  • It would be help full to explain what you want to achieve from a birds point of view.

    So when I first got the original Espruino I created animations for my LED lights using setInterval functions.

    The code quickly became verbose as I added more patterns. I experimented back then trying to use gsap but there wasn't enough memory.

    When I decided to look again and was amazed that the Espruino WiFi version was out with more memory I thought my idea could work out.

    Gsap is designed for the browser as it transitions elements in the DOM but can also transition values on objects you create yourself, therefore you're not restricted to just DOM objects or the window object in the browser.

    I've since looked at smaller libraries that could work as I want to interpolate between values to use on these lights, the power of a tween library is that it has various helper functions like repeat, reverse and yo-yo to aid with creativity.

  • @Coder2012, very interesting and good to know. Actually I was looking for something like that for similar purposes, like driving multiple chains of RGB LED strings and RGB LED cubes:

    Gsap is designed for the browser as it transitions elements in the DOM but can also transition values on objects you create yourself, therefore you're not restricted to just DOM objects or the window object in the browser.

    May be taking gsap apart and use only the pieces that are needed, could be a starting point. From other experiences though I know that pretty quickly Espruino runs out of steam... and a minimum setup is more in the realm of recent model of Raspberry Pi, even though some stuff can be 'native-d down into Espruino guts' using compiled. Most likely, you will end up writing it natively and add it to the Espruino firmware with control-APIs for the JS side, like GPUs in an end point device have APIs exposed thru the Browser w/ JavaScript language binding (for feeding data and commands and ('source') code).

  • @Coder2012 glad you got this sorted - kinda. When you require something in Espruino it just grabs that single file as you named it. In something like Node.js it'll look in node_modules, find the folder with that name, then look in package.json and then load the file that it references - so dragging something directly off NPM is super unlikely to work.

    Sadly the removal of NPM support in the IDE was because the vast, vast majority of modules on NPM are just too big to be usable. It was just super-confusing for everyone. It may end up getting re-enabled with some tree-shaking support that would hopefully be able to strip things back down a bit though.

    Crazily it looks like TweenLite is actually bigger than Tween. On Espruino you can pull in Tween as-is - you just require some simple wrapper functions:

    var TWEEN = require("https://cdnjs.cloudflare.com/aj­ax/libs/tween.js/17.2.0/Tween.min.js");
    process.hrtime = function() {
      var t = getTime();
      var s = 0|t;
      return [s, 0|((t-s)*1000000000)];
    };
    function requestAnimationFrame(cb) {
      setTimeout(cb,0);
    }
    
    function animate(time) {
        requestAnimationFrame(animate);
        TWEEN.update(time);
    }
    requestAnimationFrame(animate);
    
    var coords = { x: 0, y: 0 }; // Start at (0, 0)
    var tween = new TWEEN.Tween(coords) // Create a new tween that modifies 'coords'.
            .to({ x: 300, y: 200 }, 1000) // Move to (300, 200) in 1 second.
            .easing(TWEEN.Easing.Quadratic.Out) // Use an easing function to make the animation smooth.
            .onUpdate(function() { // Called after tween.js updates 'coords'.
              console.log(coords);
            })
            .start(); // Start the tween immediately.
    

    @Robin I think this is basically unrelated to your problems. NPM doesn't interfere with the IDE or anything like that. As far as I can tell from the issues you posted to date, your problems were either with how you used the modules you defined, or your use of ES6 classes which while supported by Espruino aren't handled properly by the minifier the IDE uses yet.

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

Help requiring modules on Espruino-wifi board.

Posted by Avatar for Coder2012 @Coder2012

Actions