Error while trying to import mqtt_client
saurabh-mhaisekar opened this issue · 4 comments
saurabh-mhaisekar commented
Hi,
I am trying to follow the example from the Readme file for Node.js but when i try to import the mqtt_client
I get following error
import mqtt_client from 'u8-mqtt/esm/node/index.mjs'
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1033:15)
at Module._compile (node:internal/modules/cjs/loader:1069:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Module._load (node:internal/modules/cjs/loader:827:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
shanewholloway commented
The good news is this problem is common to NodeJS new style modules, so there are lots of google-able material on it available. The bad news is there are a myriad of different solutions.There are several ways to fix it — you can rename your file to have an ‘.mjs’ extension. You can change your ‘package.json’ to be a module. You can use ‘require()’. And probably a few more. See the NodeJS official docs:
https://nodejs.org/api/packages.html
And for some outside article context:
https://sabe.io/blog/node-syntaxerror-cannot-use-import-statement-outside-a-module
shanewholloway commented
Email reply broke my links — fixed in GitHub issue thread
shanewholloway commented
Did this resolve your question?
saurabh-mhaisekar commented
Hey @shanewholloway sorry.. I forgot to reply you back.. Yep... it did solve the problem. Thanks for help and keeping the library alive.