Read the statement by Michael Teeuw here.
Integrating an npm module that uses import
-
HI all,
I am trying to write a module around the npm module picnic-api that uses the
import
function instead ofrequire
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? -
@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
-
@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 -
-
@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.
-
@lavolp3 sometimes u get to do things that arent perfect. but get to move on.
-
- rename
node_helper.js
tonode_helper.mjs
worked in my test (did not do anything with the imported axios, but mm starts).
- rename
-
@karsten13 was node_helper loaded? i doubt it. its optional. so this doesnt help
-
@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/
-
@karsten13 in node… but we have a custom environment, that is not node pure…