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

usb.on is not a function #428

Open
JuanDa237 opened this issue Nov 15, 2023 · 5 comments
Open

usb.on is not a function #428

JuanDa237 opened this issue Nov 15, 2023 · 5 comments

Comments

@JuanDa237
Copy link

Node v18.15.0

"dependencies": {
"escpos": "^3.0.0-alpha.6",
"escpos-usb": "^3.0.0-alpha.4"
}

Code:

const escpos = require('escpos');
escpos.USB = require('escpos-usb');

const device = new escpos.USB(0x2BDF, 0x0280);
const printer = new escpos.Printer(device);

device.open(function (error) {
if (error) {
console.error('Error opening USB device:', error);
return;
}

printer
    .font('a')
    .align('ct')
    .style('bu')
    .size(1, 1)
    .text('Your Store Name')
    .text('----------------')
    .align('lt')
    .text('Item 1       $10.00')
    .text('Item 2        $7.50')
    .text('----------------')
    .align('rt')
    .text('Total:      $17.50')
    .cut()
    .close();

});

Error:
C:\Users\juan\Desktop\test-printer\node_modules\escpos-usb\index.js:52
usb.on('detach', function(device){
^

TypeError: usb.on is not a function
at new USB (C:\Users\x\Desktop\test-printer\node_modules\escpos-usb\index.js:52:7)
at Object. (C:\Users\x\Desktop\test-printer\index.js:5:16)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47

@SZZH
Copy link

SZZH commented Nov 15, 2023

maybe you can try, usb module version to 1

@SZZH
Copy link

SZZH commented Nov 15, 2023

maybe you can try, usb module version to 1

pnpm i [email protected]

@giegiey
Copy link

giegiey commented Jan 6, 2024

npm install -S [email protected]

@joshuayoes
Copy link

You can also run apply this patch-package patch

Add this to patch/escpos-usb+3.0.0-alpha.4.patch

diff --git a/node_modules/escpos-usb/index.js b/node_modules/escpos-usb/index.js
index 8c21546..7b3aa42 100644
--- a/node_modules/escpos-usb/index.js
+++ b/node_modules/escpos-usb/index.js
@@ -25,7 +25,7 @@ const IFACE_CLASS = {
 function USB(vid, pid){
 
   if (!usb) {
-    usb = require('usb');
+    usb = require('usb').usb;
   }
 
   EventEmitter.call(this);
@@ -67,7 +67,7 @@ function USB(vid, pid){
  */
 USB.findPrinter = function(){
   if (!usb) {
-    usb = require('usb');
+    usb = require('usb').usb
   }
   return usb.getDeviceList().filter(function(device){
     try{

Then run npx patch-package in your project.

@codewithshinde
Copy link

This worked for me !!

  • go to node_modules/escpos-usb/index.js
  • comment down the following lines
  // usb.on('detach', function(device){
  //   if(device == self.device) {
  //     self.emit('detach'    , device);
  //     self.emit('disconnect', device);
  //     self.device = null;
  //   }
  // });

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

5 participants