Printing with ESC/POS Printers using JavaScript: A 2026 Guide

Learn to print with ESC/POS printers using JavaScript via a Node.js server. Overcome client-side limitations for seamless network printing.

Printing with ESC/POS Printers using JavaScript: A 2026 Guide

ESC/POS printers are widely used in retail and hospitality environments for their efficiency and reliability. However, printing directly from JavaScript can be a challenge due to browser restrictions. In this tutorial, you will learn how to overcome these limitations by using a combination of JavaScript and a backend service to print to ESC/POS printers seamlessly.

Key Takeaways

  • Understand the limitations of printing directly from JavaScript.
  • Learn to configure a Node.js server to handle print jobs.
  • Discover how to send print commands using ESC/POS to network printers.
  • Handle multiple printers and manage their IP addresses effectively.
  • Troubleshoot common issues when printing with ESC/POS printers.

Printing directly from a web page using JavaScript can be frustrating due to security restrictions that prevent direct access to system resources. Therefore, developers often use server-side applications to facilitate printing tasks. This guide will show you how to set up a Node.js server to send ESC/POS commands to a networked printer, providing a seamless printing experience from your web application.

Prerequisites

  • Basic knowledge of JavaScript and Node.js
  • A working ESC/POS printer connected to the network
  • Node.js installed on your system (version 18.0.0 or later recommended)
  • Access to a Windows machine with the necessary printer drivers installed
  • Familiarity with installing Node.js packages

Step 1: Set Up Your Node.js Environment

First, ensure that Node.js is installed on your system. You can download the latest version from the official Node.js website. Once installed, verify the installation by running:

node -v

This should return the version number if Node.js is correctly installed.

Step 2: Install Necessary Packages

Create a new directory for your project and initialize a new Node.js project:

mkdir escpos-printing && cd escpos-printing
npm init -y

Next, you need to install the escpos package, which will allow you to send commands to the printer:

npm install escpos

The escpos package provides a convenient API to interact with ESC/POS printers, supporting various connection methods such as USB, Bluetooth, and network.

Step 3: Configure Your Node.js Server

Create a new file named server.js in your project directory. This file will contain the code to set up a simple HTTP server and handle print requests:

const escpos = require('escpos');
const http = require('http');
const network = require('escpos-network');

// Set up the printer connection
const device = new network.Network('192.168.1.100'); // Replace with your printer's IP
const printer = new escpos.Printer(device);

// Create an HTTP server
const server = http.createServer((req, res) => {
  if (req.method === 'POST' && req.url === '/print') {
    let body = '';
    req.on('data', chunk => {
      body += chunk.toString();
    });
    req.on('end', () => {
      const data = JSON.parse(body);
      device.open(() => {
        printer
          .text(data.message)
          .cut()
          .close();
      });
      res.writeHead(200, {'Content-Type': 'application/json'});
      res.end(JSON.stringify({status: 'success'}));
    });
  } else {
    res.writeHead(404, {'Content-Type': 'text/plain'});
    res.end('Not Found');
  }
});

server.listen(3000, () => {
  console.log('Server running at http://localhost:3000/');
});

This server listens for POST requests at /print and sends the received message to the printer. Replace '192.168.1.100' with your printer's IP address.

Step 4: Sending Print Requests from JavaScript

To send print requests from your web application, you can use the Fetch API to make a POST request to your Node.js server. Here’s an example of how you can implement this:

fetch('http://localhost:3000/print', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    message: 'Hello, ESC/POS Printer!'
  })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

Ensure that your Node.js server is running before sending requests.

Step 5: Manage Multiple Printers

If you have multiple printers, you can manage them by maintaining a configuration file or database that maps printer names to IP addresses. Here is a simple example using a JSON file:

{
  "printers": {
    "FrontDesk": "192.168.1.101",
    "Kitchen": "192.168.1.102"
  }
}

You can then modify your server to read from this file and dynamically choose the printer based on the request data.

Common Errors/Troubleshooting

  • Connection Issues: Ensure that the printer's IP address is correctly configured and that it is accessible from your network.
  • Printer Not Responding: Check if the printer is powered on and ready to receive commands. Verify driver installation on the Windows machine.
  • Certificate Problems: Unlike some solutions like QZ Tray, this setup does not require dealing with certificates, thus avoiding related issues.

Conclusion

By setting up a Node.js server to handle print requests, you can bypass the limitations of client-side JavaScript printing. This method is flexible, allowing for easy management of multiple printers and network configurations. With the guidance provided, you should be able to implement a robust solution for printing with ESC/POS printers using JavaScript in 2026 and beyond.

Frequently Asked Questions

Can I print directly from the browser using JavaScript?

No, due to security restrictions, browsers do not allow direct access to system resources such as printers. A server-side solution is required.

Why use Node.js for printing?

Node.js allows you to create a backend service that can handle print requests and communicate with printers over a network, bypassing browser limitations.

What is ESC/POS?

ESC/POS is a command protocol for sending instructions to receipt printers. It is widely used for retail and point-of-sale applications.

How do I find my printer’s IP address?

You can find the IP address in your printer's network settings or through your network router’s admin panel.