song940/node-escpos

TypeError: usb.on is not a function

fillahfirdausyah opened this issue · 11 comments

Hello, i'v tried and use example code, and igot error like this

TypeError: usb.on is not a function.

My code :
const escpos = require("escpos");
escpos.USB = require('escpos-usb');

const device = new escpos.USB(1046, 20497);
// const device = new escpos.RawBT();
// const device = new escpos.Network('localhost');
// const device = new escpos.Serial('/dev/usb/lp0');
const printer = new escpos.Printer(device);

console.log(device)

device.open(function (err) {
printer
.font("a")
.align("ct")
.style("bu")
.size(1, 1)
.text("The quick brown fox jumps over the lazy dog")
.text("敏捷的棕色狐狸跳过懒狗")
.barcode("1234567", "EAN8")
.qrimage("https://github.com/song940/node-escpos", function (err) {
this.cut();
this.close();
});
});

this happens on windows 10

  • on linux - ubuntu it's working fine.

if you're in a rush like me, a hacky-temporary workaround would be to go into the node_modules/escpos-usb/index.js and comment out usb.on ... part.

Then you'll run into another problem, saying usb.removeAllListeners is not a function. I commented that part out as well. Then it works ok-ish.

I had already made an app with electron + express and stuff on Linux about a month ago. And just about 3 days ago I tested on a windows machine for the first time, and got these same errors...
I still got another problem to work with, which is the image I generate for it to print seems to "not fit" all of a sudden..

It seems to be somewhat related with this.. idk MadLittleMods/node-usb-detection#77

Anyways.. good luck

This actually seem to be related with a major release of usb [1]

From what I checked, this project didn't pin the dependency [2], and it was working fine with version 1.9.2

Using yarn set resolution usb@npm:* 1.9.2 --save for now fixed this for me

[1] https://www.npmjs.com/package/usb
[2] https://github.com/song940/node-escpos/blob/v3/packages/usb/package.json#L10

this happens on windows 10

  • on linux - ubuntu it's working fine.

if you're in a rush like me, a hacky-temporary workaround would be to go into the node_modules/escpos-usb/index.js and comment out usb.on ... part.

Then you'll run into another problem, saying usb.removeAllListeners is not a function. I commented that part out as well. Then it works ok-ish.

I had already made an app with electron + express and stuff on Linux about a month ago. And just about 3 days ago I tested on a windows machine for the first time, and got these same errors... I still got another problem to work with, which is the image I generate for it to print seems to "not fit" all of a sudden..

It seems to be somewhat related with this.. idk MadLittleMods/node-usb-detection#77

Anyways.. good luck

oh wow, for now it's working. thanks

For npm using folks:
As of NPM v8.3 you can override the version in your package.json by adding an overrides section:

"overrides": {
  "usb": "^1.9.2"
}

For earlier versions we noticed you can also install usb as a direct dependency.

npm install usb@1.9.2

(Using version 1.9.2 as romaia advised)

Same issue and using usb@1.9.2 did not help :/

Same problem here...
No solution at all.

Look like usb@1.9.2 doesn't existe (anymore?)
https://www.npmjs.com/package/usb/v/1.8.8
Using 1.8.8 solved the issue

For Fix The Problem Please Migrate To USB Version 2
@see https://github.com/node-usb/node-usb#migrating-to-v200
@see #388

i am change node_module/escpos-usb like
let { usb } = require('usb');

Like my pull request. please merger @song940

please close this issue

This actually seem to be related with a major release of usb [1]

From what I checked, this project didn't pin the dependency [2], and it was working fine with version 1.9.2

Using yarn set resolution usb@npm:* 1.9.2 --save for now fixed this for me

[1] https://www.npmjs.com/package/usb [2] https://github.com/song940/node-escpos/blob/v3/packages/usb/package.json#L10

Thank a lot, it's working for me now