MorneZaayman
MorneZaayman

Reputation: 25

Import MQTT NPM module into NativeScript

I'm trying to use MQTT npm package as part of a NativeScript application I'm building.

I'm running into a problem when I try to require it.

My code uses the var mqtt = require('mqtt'); as the example indicates, but when trying to compile the NativeScript application, I get the following error :

com.tns.NativeScriptException: Failed to find module: "mqtt", relative to :/app/tns_modules.

I've verified that the mqtt folder is in my node_modules folder, and I tried creating a tns_modules folder and copying the mqtt folder there as well.

However I'm still getting the error. I tried loading the knock-knock-jokes package and that loads perfectly.

I'm using the latest versions of Node and NativeScript as 13 April 2016 (4.4.0 for Node and 1.7.1 for NativeScript).

Upvotes: 0

Views: 571

Answers (1)

Nathanael
Nathanael

Reputation: 5399

Ok, I tried this out to see why this is happening.

The very first line of mqtt.js is

#!/bin/node...

Which is invalid JavaScript code. Node has been programmed to ignore it; but NativeScript does not. (Might be worth a enhancement request..)

So when it loads this file to parse it, it fails to parse and returns that it isn't available. (Not always the best error on parse issues)

HOWEVER, if you fix that error you will run into MORE issues. It will then complain about not finding the "net" library. NativeScript does not have a net library built in. To my knowledge no one has yet created a net replacement for NativeScript. So, in a lot of cases you can use node modules as is; but if they call anything that depends on a built in node library; they unfortunately will not currently work in NativeScript as their is no equivalent library existing.

Just looking at the dependencies of the mqtt library; your odds of getting this working on NativeScript looks at a glance pretty slim.

Upvotes: 1

Related Questions