Runtime.ImportModuleError with mqttjs when deployed

Hello, I am using netlify’s amazing functions to make a bridge between a webapp and an MQTT broker. The idea is to send a POST req to it and lamda does the talking and then ends the connection. The code is pretty simple and works locally using netlify-lambda.

I am using these settings : netlify-lambda build functions --config ./config/webpack.functions.js
as i read them here.

When I deploy it i get an error on the functions console -
ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'mqtt'\nRequire stack:\n- /var/task/test.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","stack":["Runtime.ImportModuleError: Error: Cannot find module 'mqtt'","Require stack:","- /var/task/test.js","- /var/runtime/UserFunction.js","- /var/runtime/index.js"," at _loadUserApp (/var/runtime/UserFunction.js:100:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:955:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)"," at Module.load (internal/modules/cjs/loader.js:811:32)"," at Function.Module._load (internal/modules/cjs/loader.js:723:14)"," at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)"," at internal/main/run_main_module.js:17:11"]}

I think somehow its not getting the mqtt.js dependency. What am I missing?
I followed this tutorial to get started.

my script is like this.
import mqtt from ‘mqtt’

exports.handler=function(event, context, callback){
console.log(event.body)
const params= JSON.parse(event.body);
console.log(params)
const mqttTopic = params.mqttTopic+‘/cardID’;
const cardID= params.id;

var options={
    clientId:"middleWare",
    username:"xxxxx",
    password:"xxxxxx",
    port: xxxxxx};

//Send back response to user
const send = body =>
{
callback(null,{
statusCode: 200,
headers: {
‘Access-Control-Allow-Origin’:‘*’,
‘Access-Control-Allow-Headers’ :‘Origin,X-Requested-With,Content-Type,Accept’
},
body : JSON.stringify({ msg : mqttTopic, data: cardID})
});
}

//Send unlock command to the HUD
const sendData =()=>
{
var client = mqtt.connect(“mqtt://xxxxx.cloudmqtt.com”,options)
client.on(“connect”,function()
{
console.log(“connected”)
client.publish(String(mqttTopic),String(cardID));
client.end();
})
client.on(“error”,function(error)
{
console.log(error)
})

send("1");

}

//Check if its a valid response
if(event.httpMethod == ‘POST’)
{
sendData();
}

}

Thanks a lot :slight_smile:

Hi @IndianTinker, it looks like you are using netlify-lambda and are bundling your function. Could you try using our buildbot’s built-in zip-it-and-ship-it feature instead? To use it, you’ll remove the neltify-lambda build command and just make sure you npm i inside your function’s folder if you have a package.json file there. For more information on how zip-it-and-ship-it works, you can go here: GitHub - netlify/zip-it-and-ship-it: Intelligently prepare Node.js Lambda functions for deployment (just note that this is already built in to our builtbot and you don’t have to do any installing to use it).

Let me know if that helps.