MQTT.js icon indicating copy to clipboard operation
MQTT.js copied to clipboard

vite support

Open jahnli opened this issue 4 years ago • 18 comments

Currently used on vite, node variables such as global are shown to be wrong

Will Vue Vite be supported

jahnli avatar Apr 16 '21 02:04 jahnli

You can already use this library with these changes to your code (ts):

  • Use import * as mqtt from 'mqtt/dist/mqtt.min';
  • Add "skipLibCheck": true to your tsconfig.json
  • Create a shims-mqtt.d.ts with
    declare module 'mqtt/dist/mqtt.min' {
      import MQTT from 'mqtt'
      export = MQTT
    }
    

chartinger avatar May 14 '21 13:05 chartinger

It works for js environment (Vite + vue3 + mqtt). import * as mqtt from 'mqtt/dist/mqtt.min';

Thank you.

Jairos2015 avatar Jun 26 '21 02:06 Jairos2015

declare module 'mqtt/dist/mqtt.min' { import MQTT from 'mqtt' export = MQTT }

It works.Before this, shown "global is not defined"

waytoviva avatar Jul 30 '21 05:07 waytoviva

You can already use this library with these changes to your code (ts):

  • Use import * as mqtt from 'mqtt/dist/mqtt.min';
  • Add "skipLibCheck": true to your tsconfig.json
  • Create a shims-mqtt.d.ts with
    declare module 'mqtt/dist/mqtt.min' {
      import MQTT from 'mqtt'
      export = MQTT
    }
    

@chartinger these are good instructions. Do you know how to test that vite would be supported automatically, i.e. adding it to our Github Actions testing process? If so, we could more formally support vite for this package.

YoDaMa avatar Sep 29 '21 02:09 YoDaMa

You can already use this library with these changes to your code (ts):

  • Use import * as mqtt from 'mqtt/dist/mqtt.min';
  • Add "skipLibCheck": true to your tsconfig.json
  • Create a shims-mqtt.d.ts with
    declare module 'mqtt/dist/mqtt.min' {
      import MQTT from 'mqtt'
      export = MQTT
    }
    

Great!!! Thanks. It works.

ReneeDress avatar Oct 19 '21 03:10 ReneeDress

You can already use this library with these changes to your code (ts):

  • Use import * as mqtt from 'mqtt/dist/mqtt.min';
  • Add "skipLibCheck": true to your tsconfig.json
  • Create a shims-mqtt.d.ts with
    declare module 'mqtt/dist/mqtt.min' {
      import MQTT from 'mqtt'
      export = MQTT
    }
    

i use this for my project, but i got error mqtt.min.js:1 Uncaught TypeError: n.createConnection is not a function when i use connect api, who can help me?

chenzesam avatar Oct 30 '21 15:10 chenzesam

You can already use this library with these changes to your code (ts):

  • Use import * as mqtt from 'mqtt/dist/mqtt.min';
  • Add "skipLibCheck": true to your tsconfig.json
  • Create a shims-mqtt.d.ts with
    declare module 'mqtt/dist/mqtt.min' {
      import MQTT from 'mqtt'
      export = MQTT
    }
    

i use this for my project, but i got error mqtt.min.js:1 Uncaught TypeError: n.createConnection is not a function when i use connect api, who can help me?

oh, this is my error, i send the wrong url argument like htpp:// so mqtt use net to connect...

chenzesam avatar Oct 30 '21 16:10 chenzesam

Anyone know of an equivalent workaround for async-mqtt?

nerdyman avatar Nov 03 '21 19:11 nerdyman

i use this for my project, but i got error mqtt.min.js:1 Uncaught TypeError: n.createConnection is not a function when i use connect api, who can help me?

oh, this is my error, i send the wrong url argument like htpp:// so mqtt use net to connect...

For anyone else running into this, it appears this library always tries to use the node net library when connecting with the mqtt protocol (selected by default if you have an http address). I worked around the issue by connecting with a ws:// protocol and adding an appropriate listener to my broker (mosquitto):

listener 8883
protocol websockets

BlakeTheAwesome avatar Nov 04 '21 01:11 BlakeTheAwesome

i just added resolve: { alias: { mqtt: 'mqtt/dist/mqtt.js', }, }, to my vite config, and it works!

if you are using webtest runner, then add it in the webtest runner config too:

import { fromRollup } from '@web/dev-server-rollup';
import pluginAlias from '@rollup/plugin-alias';

const alias = fromRollup(pluginAlias);
export default /** @type {import("@web/test-runner").TestRunnerConfig} */ ({
  plugins: [
  alias({
    entries: {
      mqtt: '.vite/mqtt.js',
    }
  }),
],
})

after these changes, you can use mqtt: import mqtt from 'mqtt'

note: before running test, you should run the vite optimize command. it could be handy if you have CI/CD pipeline

icsaba avatar Dec 02 '21 17:12 icsaba

How to use mqtt in nuxt.js

shba007 avatar Jun 05 '22 11:06 shba007

I not understand why mqtt.min.js:1 Uncaught TypeError: n.createConnection is not a function happened in 4.3.7 version but success in 4.0.1.

donkeylmx avatar Jun 06 '22 06:06 donkeylmx

Hi, I have the same problem

selinp avatar Jun 15 '22 06:06 selinp

after 4.01 use ws:// or wss:// for your server url not mqtt.

bfritscher avatar Jul 27 '22 21:07 bfritscher

Hi @everyone and @chartinger !

I could not active tô running your comments usina React.

Please could you help me?

Thanks!

rogeriocassares avatar Aug 08 '22 12:08 rogeriocassares

Hi @everyone and @chartinger !

I could not active tô running your comments usina React.

Please could you help me?

Thanks!

Hi @rogeriocassares

  1. Install mqtt and precompiled-mqtt using npm or yarn
  2. Copy the lib folder from node_modules/mqtt to node_modules/precompiled-mqtt
  3. Use import { connect } from "precompiled-mqtt" to use mqtt

This is the easiest way to get started with mqtt. Other possible way is to install mqtt and compile manually. The instructions are given here Webpack Bundling

shba007 avatar Aug 08 '22 13:08 shba007

You can already use this library with these changes to your code (ts):

  • Use import * as mqtt from 'mqtt/dist/mqtt.min';
  • Add "skipLibCheck": true to your tsconfig.json
  • Create a shims-mqtt.d.ts with
    declare module 'mqtt/dist/mqtt.min' {
      import MQTT from 'mqtt'
      export = MQTT
    }
    

vite + vue 2.7.10 + ts + pnpm

other than the above ,we can try

pnpm i --save mqtt-packet

onepisYa avatar Sep 02 '22 00:09 onepisYa

Hey! I am going to try the mqtt-packet!

rogeriocassares avatar Sep 06 '22 21:09 rogeriocassares

i use this for my project, but i got error mqtt.min.js:1 Uncaught TypeError: n.createConnection is not a function when i use connect api, who can help me?

oh, this is my error, i send the wrong url argument like htpp:// so mqtt use net to connect...

For anyone else running into this, it appears this library always tries to use the node net library when connecting with the mqtt protocol (selected by default if you have an http address). I worked around the issue by connecting with a ws:// protocol and adding an appropriate listener to my broker (mosquitto):

listener 8883
protocol websockets

After doing that, I get ReferenceError: WebSocket is not defined... I'm using NUXT 3.

timstrasser avatar Nov 07 '22 15:11 timstrasser

any updates on this? Using Nuxt3 gives errors

FrodoTrash avatar Mar 10 '23 19:03 FrodoTrash

You can already use this library with these changes to your code (ts):

  • Use import * as mqtt from 'mqtt/dist/mqtt.min';
  • Add "skipLibCheck": true to your tsconfig.json
  • Create a shims-mqtt.d.ts with
    declare module 'mqtt/dist/mqtt.min' {
      import MQTT from 'mqtt'
      export = MQTT
    }
    

It's useful. Thank you!

touxing avatar May 31 '23 07:05 touxing

MQTT 5.0.0 BETA is now available! Try it out and give us feedback: npm i mqtt@beta. It may fix your issues

robertsLando avatar Jul 21 '23 14:07 robertsLando

I have fixed Browser docs by adding webpack and vite setup. Check them out

robertsLando avatar Jul 31 '23 15:07 robertsLando

@robertsLando With your examples in Vite, it works when you run dev, but it does not work when you run build.

[plugin:vite:resolve] Module "url" has been externalized for browser compatibility, imported by "C:/Users/setpi/Documents/information-display/node_modules/mqtt/build/src/lib/connect/index.js". See http://vitejs.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.

setpixel avatar Aug 05 '23 16:08 setpixel

@setpixel Check vite-webpack in examples folder, that example works for both

robertsLando avatar Aug 05 '23 16:08 robertsLando