Printing receipts from JavaScript is a two-part job. First an encoder turns your receipt into ESC/POS bytes; @point-of-sale/receipt-printer-encoder (the successor to esc-pos-encoder) does this in both the browser and Node.js. Then a transport delivers the bytes: a TCP socket to port 9100 for network printers, USB or serial from Node.js, WebUSB or Web Bluetooth in some browsers, or a rawbt: link to an Android print app for Bluetooth printers that browsers can't reach.
Pick your path:
- Node.js server, network printer: encoder +
net.connect(9100, ip), or node-thermal-printer. - Browser on a desktop, USB printer: encoder + WebUSB (Chrome, Edge).
- Browser, BLE printer: encoder + Web Bluetooth.
- Web POS on Android, Bluetooth Classic or USB printer: encoder +
rawbt:base64,link to a print app.
The npm packages worth knowing
We checked these on npm and GitHub in October 2026:
| Package | Runs in | Does | Status |
|---|---|---|---|
@point-of-sale/receipt-printer-encoder | Browser, Node.js | Builds ESC/POS and StarPRNT bytes: text, styles, tables, barcodes, QR codes, images, cut, drawer | Active (v4.0.1, Sept 2026), MIT |
esc-pos-encoder | Browser, Node.js | The original ESC/POS encoder by the same author | Superseded; last release v3.0.0 (2024) |
node-thermal-printer | Node.js | Builds and sends jobs to network printers or OS printers; Epson and Star | Active (v4.6.1, Aug 2026), MIT |
escpos (node-escpos) + adapters | Node.js | Encoder plus USB, network and serial adapters | Older; latest npm release is a 2022 alpha |
@point-of-sale/webusb-receipt-printer, webbluetooth-…, webserial-… | Browser | Connect to a printer from the browser and send bytes | Maintained alongside the encoder |
For a broader list across languages, see the best ESC/POS libraries on GitHub.
Step 1: Encode a receipt
// npm install @point-of-sale/receipt-printer-encoder import ReceiptPrinterEncoder from '@point-of-sale/receipt-printer-encoder'; const encoder = new ReceiptPrinterEncoder({ language: 'esc-pos', columns: 32, // 32 for 58 mm paper, 48 for 80 mm }); const bytes = encoder .initialize() .codepage('auto') .align('center') .size(2, 2).line('CORNER CAFE').size(1, 1) .line('12 High Street') .newline() .align('left') .line('1 x Flat white 3.50') .line('2 x Croissant 5.00') .bold(true).line('TOTAL 8.50').bold(false) .newline() .align('center') .qrcode('https://example.com/r/1042') .newline(3) .cut('partial') .encode(); // Uint8Array
codepage('auto') lets the encoder pick a character table for each piece of text, which handles accents in most European languages. Scripts the printer has no table for (Arabic, Hindi, Thai and similar) must be printed as images instead. The library also has table helpers, so you don't have to pad lines by hand; we padded them here to show the 32-column layout.
Step 2a: Send to a network printer from Node.js
Most Ethernet and Wi-Fi receipt printers accept raw bytes on TCP port 9100. You don't need a library to send them:
import net from 'node:net'; function printRaw(host, bytes, port = 9100) { return new Promise((resolve, reject) => { const socket = net.connect(port, host, () => socket.end(Buffer.from(bytes))); socket.setTimeout(5000, () => socket.destroy(new Error('printer timeout'))); socket.on('close', (hadError) => hadError || resolve()); socket.on('error', reject); }); } await printRaw('192.168.1.50', bytes);
Or use node-thermal-printer, which builds and sends in one place:
import thermal from 'node-thermal-printer'; // CommonJS package const { printer: ThermalPrinter, types: PrinterTypes } = thermal; const printer = new ThermalPrinter({ type: PrinterTypes.EPSON, interface: 'tcp://192.168.1.50:9100', }); printer.alignCenter(); printer.println('CORNER CAFE'); printer.printQR('https://example.com/r/1042'); printer.cut(); printer.openCashDrawer(); await printer.execute();
Step 2b: USB and serial printers from Node.js
- Serial or USB-serial printers appear as a COM port (Windows) or
/dev/ttyUSB0(Linux). Write the bytes with theserialportpackage at the baud rate from the self-test page. - USB printer class devices on Linux often appear as
/dev/usb/lp0. Writing the bytes to that file prints them. - Through the operating system: install the printer with a raw/generic queue (CUPS raw queue on Linux and macOS, a "Generic / Text Only" printer on Windows), then send the bytes to that queue. node-thermal-printer's
printer:interface uses this route. - libusb adapters (such as escpos-usb) talk to the device directly but often need extra drivers on Windows. Try the options above first.
Step 2c: Print straight from the browser
Browsers can reach some printers directly:
- WebUSB (Chrome and Edge, desktop and Android): the user picks the printer once; you send bytes to its bulk endpoint. On Windows, the printer's own driver may claim the device first, which blocks WebUSB.
- Web Bluetooth: works only with Bluetooth Low Energy printers. Most budget receipt printers use Bluetooth Classic, which no browser can open.
- Web Serial (desktop Chrome and Edge): for serial and USB-serial printers.
The @point-of-sale/webusb-receipt-printer, webbluetooth-receipt-printer and webserial-receipt-printer packages wrap these APIs and pair well with the encoder. None of them works in Safari or Firefox, and none can reach a Bluetooth Classic printer.
Step 2d: Web POS on Android, the reliable route
On Android tablets and phones, most shops use cheap Bluetooth Classic or USB receipt printers. A web page can't open those itself, so it hands the bytes to a print app with a link. Android routes the link to the app, which prints instantly with no dialog:
function toBase64(bytes) {
let bin = '';
for (let i = 0; i < bytes.length; i += 0x8000) {
bin += String.fromCharCode(...bytes.subarray(i, i + 0x8000)); // chunked: safe for big images
}
return btoa(bin);
}
// Raw ESC/POS, sent to the printer untouched
window.location.href = 'rawbt:base64,' + toBase64(bytes);
The rawbt: scheme is understood by both RawBT and Raw Print, so the same code works with either. Raw Print also accepts plain text (rawbt: + encodeURIComponent(text)), PNG and PDF data URIs, and web links, and it can render text in any script as an image when you don't want to deal with code pages. We cover the architecture in why your web POS needs a printing engine; hybrid apps should read thermal printing from Ionic, Cordova and Capacitor.
Printing images and logos
The encoder accepts an Image, canvas or image data, and handles dithering:
const logo = new Image(); logo.src = '/logo.png'; await logo.decode(); encoder.initialize().align('center') .image(logo, { width: 320, algorithm: 'threshold' }) .encode();
Keep the width a multiple of 8 and no wider than the printer (384 dots on 58 mm, 576 on 80 mm). In Node.js, load images with a canvas package. Our guide to printing images and QR codes with ESC/POS explains thresholding vs dithering.
Common mistakes
- Sending UTF-8 strings. Printers expect bytes in their code page. Let the encoder encode text; never write
"café"to the socket as a string. - Wrong column count. Set
columnsto 32 for 58 mm or 48 (sometimes 42) for 80 mm, or tables wrap. - Building base64 with one big spread.
String.fromCharCode(...bytes)on a large image overflows the call stack; use the chunked version above. - Closing the socket too early. Use
socket.end(data), notdestroy(), so the data flushes. - No test printer. Use an ESC/POS emulator in CI and development.
Frequently asked questions
Is esc-pos-encoder still maintained?
Its author now maintains @point-of-sale/receipt-printer-encoder, which replaced EscPosEncoder and adds StarPRNT support. New projects should use the new package.
Can JavaScript print to a Bluetooth thermal printer?
In the browser, only to Bluetooth Low Energy printers via Web Bluetooth. For Bluetooth Classic printers on Android, hand the bytes to a print app with a rawbt: link. In Node.js, use a serial port or the OS's Bluetooth serial device.
How do I print to a network receipt printer from Node.js?
Open a TCP connection to the printer's IP address on port 9100 and write the ESC/POS bytes, as shown above.
Can I print receipts from a web page without a print dialog?
Yes: with WebUSB, Web Bluetooth or Web Serial on supported browsers, or on Android by sending the receipt to a print app through a rawbt: link.