n4m : maxAPI error

Dg's icon

After a successful npm init and a mqtt module installation I'm not able to start my script and get this :
ReferenceError : maxAPI is not defined
I've added the npm path to File Preferences but it seems to be something else I can't handle for now.
Is there some specific initialization to do in the node_modules?
Any help is highly appreciated.

Mark Jackson's icon

Have you tried to pull the maxApi library in you code? "const maxApi = require('max-api');"

Dg's icon
Yes of course, here is the code

const maxApi = require("max-api");
const mqtt = require("mqtt");
let client;
maxAPI.addHandler("connect",(url) => {
    client = mqtt.connect(url);
    client.on("connect", () => {
    maxApi.outlet("connected");
    })
});
maxApi.addHandler("publish", (message) => {
    [topic, value] = message.split(" ");
    console.log (topic, value);
});
Mark Jackson's icon

I noticed that you have maxAPI (API all caps) defined right after the "let client". I have found that syntax must be correct. Try changing it to maxApi (not all caps) and see it that fixes the error.

Dg's icon

Wow! You're the # one. I owe you a beer :))

Mark Jackson's icon

I am glad I could help!