Using this in CJS environment
lavolp3 opened this issue ยท 3 comments
I'd love to use this module but I'm having issues using this in a (partial) node.js environment.
I would like to write a MagicMirror module that uses this library. MagicMirror is partially based on Node.js.
When I try to import this module into my app MMM-Picnic like described I get an error message:
[15.02.2021 16:30.57.935] [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
node.js is updated to 14.15
Can this be easily transferred to CJS?
Or do you know anything else that needs to be done to get it to work?
I looked at this two weeks ago but couldn't find a 'quick fix' to make the project both compatible with CommonJS and the ES6 Module style.
If you want a quick fix on your end then you can make your nodejs app an ES6 Module by adding "type": "module"
to package.json.
However, I will rewrite the project with maybe webpack and babel or something like that to support both types, since I agree with you that this should not be forced onto the end user.
I'll probably get started with this by the end of the day ๐
Great! Looking forward to it and happy to test it out.
I have tried your suggestions before but the MagicMirror environment being set on top of electron and rather complex did not allow that. But I am not experienced at all in this so I may have done sth wrong.
I just released v1.3.0 which changes the library from ESM (ES6 Modules) to CJS (CommonJS), which makes both of these possible:
import PicnicClient, { CountryCodes, ImageSizes, HttpMethods } from "picnic-api";
// or
const PicnicClient = require("picnic-api");
const { CountryCodes, ImageSizes, HttpMethods } = PicnicClient;
Please let me know if you're experiencing issues ๐