Skip to content

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

Open

Description

@SunboX

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');
};

Activity

  1. SunboX commented on Jan 23, 2023

    @SunboX
    Author

    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.
    
  2. zN3utr4l commented on Nov 21, 2024

    @zN3utr4l

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions