driver icon indicating copy to clipboard operation
driver copied to clipboard

[bluetooth-adapter] - TypeError: Noble__default is not a constructor

Open aargon007 opened this issue 6 months ago • 1 comments

I am using @node-escpos/core, @node-escpos/network-adapter, @node-escpos/bluetooth-adapter in my electron.js, react project. network adapter working fine. but when i call Bluetooth it give me the following error.

TypeError: Noble__default is not a constructor at Object. (E:\project\node_modules.pnpm@[email protected][email protected]\node_modules@node-escpos\bluetooth-adapter\dist\index.cjs:11:15) at Module._compile (node:internal/modules/cjs/loader:1738:14) at Module._extensions..js (node:internal/modules/cjs/loader:1904:10) at Module.load (node:internal/modules/cjs/loader:1472:32) at Module._load (node:internal/modules/cjs/loader:1289:12) at c._load (node:electron/js2c/node_init:2:18013) at TracingChannel.traceSync (node:diagnostics_channel:322:14) at wrapModuleLoad (node:internal/modules/cjs/loader:242:24) at Module.require (node:internal/modules/cjs/loader:1494:12) at require (node:internal/modules/helpers:135:16)

here is my implementation on main process

import Bluetooth from '@node-escpos/bluetooth-adapter'; ipcMain.handle('bt-print', async (_event, { port }) => { try { const device = new Bluetooth(port, {});

} catch (error) { console.log(error);

}

});

aargon007 avatar Aug 04 '25 16:08 aargon007

Hi @aargon007! I'm Dosu and I’m helping the driver team.

The error happens because the Bluetooth adapter tries to use new Noble(), but the @abandonware/noble library is a singleton module, not a constructor. This means you should use the imported Noble object directly, not instantiate it with new—the current adapter code is fundamentally incompatible with how noble is meant to be used, which leads to the TypeError: Noble__default is not a constructor in Electron and Node.js environments. This is a known issue with the adapter's implementation and not something you can fix with import settings or TypeScript config alone.
More details: bluetooth-adapter source

To work around this, you would need to patch the adapter code to use the Noble object as a singleton (remove new Noble(...) and use Noble directly). There is no official workaround or fix published yet. This kind of native module and ESM/CJS interop issue is also common in Electron environments, especially with hardware libraries like noble.
Related discussion: issue 77

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

dosubot[bot] avatar Aug 04 '25 16:08 dosubot[bot]