MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    Integrating an npm module that uses import

    Scheduled Pinned Locked Moved Development
    13 Posts 4 Posters 4.8k Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • lavolp3L Offline
      lavolp3 Module Developer
      last edited by sdetweil

      HI all,

      I am trying to write a module around the npm module picnic-api that uses the import function instead of require

      import axios from "axios";
      

      This does not work within the Magicmirror environment, I get an error message when I start the mirror.

      [13.02.2021 07:21.43.938] [ERROR] /home/pi/MagicMirror/modules/MMM-Picnic/node_modules/picnic-api/index.js:1
      import axios from "axios";
      ^^^^^^
      
      SyntaxError: Cannot use import statement outside a module
      

      Does anyone have a solution for me?
      Is there a way to use it or do I need to rewrite the npm module?

      How to troubleshoot modules
      MMM-soccer v2, MMM-AVStock

      strawberry 3.141S 1 Reply Last reply Reply Quote 0
      • strawberry 3.141S Offline
        strawberry 3.141 Project Sponsor Module Developer @lavolp3
        last edited by

        @lavolp3 which version of nodejs are you running? import is a feature of es6 modules. You can try to enable it like this https://stackoverflow.com/a/45854500

        Please create a github issue if you need help, so I can keep track

        lavolp3L 1 Reply Last reply Reply Quote 1
        • lavolp3L Offline
          lavolp3 Module Developer @strawberry 3.141
          last edited by

          @strawberry-3-141 said in Integrating an npm module that uses import:

          @lavolp3 which version of nodejs are you running? import is a feature of es6 modules. You can try to enable it like this https://stackoverflow.com/a/45854500

          Thanks for the tip. Upgraded to node.js 14.15 but did not work yet.
          Seeing "type": "module in my package.json also did not help.
          I will have to have a closer look at the link

          How to troubleshoot modules
          MMM-soccer v2, MMM-AVStock

          S karsten13K 2 Replies Last reply Reply Quote 0
          • S Do not disturb
            sdetweil @lavolp3
            last edited by sdetweil

            @lavolp3 yiu can also use

            const ax = require('axios')
            

            in node_helper.js
            after npm install axios

            Sam

            How to add modules

            learning how to use browser developers window for css changes

            lavolp3L 1 Reply Last reply Reply Quote 0
            • lavolp3L Offline
              lavolp3 Module Developer @sdetweil
              last edited by lavolp3

              @sdetweil that’s not sufficient. There are two more modules imported by the picnic-api module. I could require them all in node_helper but that looks too of much a workaround for me. There should be another way.

              How to troubleshoot modules
              MMM-soccer v2, MMM-AVStock

              S 1 Reply Last reply Reply Quote 0
              • S Do not disturb
                sdetweil @lavolp3
                last edited by

                @lavolp3 sometimes u get to do things that arent perfect. but get to move on.

                Sam

                How to add modules

                learning how to use browser developers window for css changes

                1 Reply Last reply Reply Quote 0
                • karsten13K Offline
                  karsten13 @lavolp3
                  last edited by

                  @lavolp3

                  • rename node_helper.js to node_helper.mjs

                  worked in my test (did not do anything with the imported axios, but mm starts).

                  S 1 Reply Last reply Reply Quote 0
                  • S Do not disturb
                    sdetweil @karsten13
                    last edited by

                    @karsten13 was node_helper loaded? i doubt it. its optional. so this doesnt help

                    Sam

                    How to add modules

                    learning how to use browser developers window for css changes

                    karsten13K 1 Reply Last reply Reply Quote 0
                    • karsten13K Offline
                      karsten13 @sdetweil
                      last edited by

                      @sdetweil no :crying_face:

                      may using esm library is one approach as described e.g. here https://blog.logrocket.com/how-to-use-ecmascript-modules-with-node-js/

                      S 1 Reply Last reply Reply Quote 0
                      • S Do not disturb
                        sdetweil @karsten13
                        last edited by sdetweil

                        @karsten13 in node… but we have a custom environment, that is not node pure…

                        Sam

                        How to add modules

                        learning how to use browser developers window for css changes

                        1 Reply Last reply Reply Quote 0
                        • S Do not disturb
                          sdetweil
                          last edited by

                          i helped someone with a similar problem but the n module was wrutten in typescript and down compiled to js

                          https://github.com/Mitica/public-holidays-js

                          Sam

                          How to add modules

                          learning how to use browser developers window for css changes

                          lavolp3L 1 Reply Last reply Reply Quote 0
                          • lavolp3L Offline
                            lavolp3 Module Developer @sdetweil
                            last edited by

                            @sdetweil @strawberry-3-141
                            I can’t get it working.
                            node 14.15 installed.

                            When I require the “picnic-api” npm module the usual way I get the error message inside the "picnic-api module.

                            import axios from "axios";
                            ^^^^^^
                            
                            SyntaxError: Cannot use import statement outside a module
                            

                            When I do the import like described in the readme the error originates in my node_helper.js

                                    import PicnicClient, { CountryCodes, ImageSizes, HttpMethods } from "picnic-api";
                                    ^^^^^^
                            
                            SyntaxError: Cannot use import statement outside a module
                            

                            I’m lost. At least the environment of the picnic-api module should be a “pure node” environment, why does the import not work at least in that module?

                            Changing the index.js inside picnic-api to an .mjs does not help.

                            Any more ideas?

                            How to troubleshoot modules
                            MMM-soccer v2, MMM-AVStock

                            S 1 Reply Last reply Reply Quote 0
                            • S Do not disturb
                              sdetweil @lavolp3
                              last edited by sdetweil

                              @lavolp3 i do not know. i consider myself a node rookie, and a typescript noobie.

                              because i am away from my systems i am unable to try different solutions… at least a few more weeks

                              if u try w

                              node serverOnly 
                              

                              does it make a difference?

                              there the node engine is running the code, vs the engine thru electron…

                              Sam

                              How to add modules

                              learning how to use browser developers window for css changes

                              1 Reply Last reply Reply Quote 0

                              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                              With your input, this post could be even better 💗

                              Register Login
                              • 1 / 1
                              • First post
                                Last post
                              Enjoying MagicMirror? Please consider a donation!
                              MagicMirror created by Michael Teeuw.
                              Forum managed by Sam, technical setup by Karsten.
                              This forum is using NodeBB as its core | Contributors
                              Contact | Privacy Policy