Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Running in ESM (*.mjs) returns "ERROR : use init before" #186

Open
1 of 2 tasks
SunboX opened this issue Jan 23, 2023 · 2 comments
Open
1 of 2 tasks

[Bug]: Running in ESM (*.mjs) returns "ERROR : use init before" #186

SunboX opened this issue Jan 23, 2023 · 2 comments

Comments

@SunboX
Copy link

SunboX commented Jan 23, 2023

Expected behavior

getting a wifi networks list

Current behavior

Error thrown: ERROR : use init before

Which are the affected features

  • node API
  • CLI

Which is your operating system?

Linux

Environment

OpenEmbedded / Yocto / Linux kernel 5.4.88 running on i.MX6X with ath6k and bcm43236b drivers

Version of node-wifi

2.0.16

Steps to Reproduce

app.mjs

import * as wifi from 'node-wifi'

export class App {

    static async start() {
        console.log(process.platform)

        wifi.init({
            debug: true,
            iface: null // network interface, choose a random wifi interface if set to null
        })

        wifi
            .scan()
            .then(networks => {
                console.log(networks)
            })
            .catch(e => {
                console.log(e)
            })
    }
}

App.start()
$ node app.mjs

linux
ERROR : use init before

Solutions

maybe related to

exports.scan = () => {
  throw new Error('ERROR : use init before');
};
@SunboX
Copy link
Author

SunboX commented Jan 23, 2023

Running it on macOS:

$ node app.mjs 

darwin
(node:12343) UnhandledPromiseRejectionWarning: Error: ERROR : use init before
    at Module.exports.scan (/xxx/node_modules/node-wifi/src/wifi.js:77:9)
    at Function.start (file:///xxx/app.mjs:14:14)
    at file:///xxx/app.mjs:24:5
    at ModuleJob.run (internal/modules/esm/module_job.js:183:25)
    at async Loader.import (internal/modules/esm/loader.js:178:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:12343) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:12343) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

@zN3utr4l
Copy link

When using type: "module", importing a CommonJS such as node-wifi requires special attention. node-wifi does not natively support ESM, so you must use the createRequire method of Node.js.

import { createRequire } from 'module';
const require = createRequire(import.meta.url);

const wifi = require('node-wifi');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants