Barcode Writer in Pure JavaScript (bwip-js)

4.9.2 · active · verified Tue Apr 21

bwip-js is a JavaScript library that provides a comprehensive barcode generation solution, translating the Barcode Writer in Pure PostScript (BWIPP) code into native JavaScript. It supports over 100 different barcode types and standards, including all common linear and two-dimensional formats. The library is currently at version 4.9.2, with its underlying BWIPP engine updated to 2026-03-31. Releases are frequent, typically coinciding with BWIPP updates or critical bug fixes, as seen with recent rapid patches for version 4.9.x. It differentiates itself by offering broad platform support (browsers, Node.js, React, React Native, Electron) and output formats (PNG, Canvas, SVG), and by being a pure JavaScript implementation requiring no external binaries or server-side rendering other than a JavaScript runtime.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to generate a Code 128 barcode as a PNG buffer using the `@bwip-js/node` package and save it to a local file. This showcases basic configuration and file system interaction in Node.js.

import { toBuffer } from '@bwip-js/node';
import { writeFile } from 'fs/promises';
import { resolve } from 'path';

async function generateBarcode() {
  try {
    // Generate a Code 128 barcode with specific properties
    const pngBuffer = await toBuffer({
      bcid: 'code128',     // Barcode type
      text: 'BWIPJS-SAMPLE-123', // Text to encode
      scale: 3,             // 3x scaling factor
      height: 10,           // Bar height, in millimeters
      includetext: true,    // Show human-readable text below the barcode
      textxalign: 'center', // Center the text
      backgroundcolor: 'ffffff', // White background (hex color)
      barcolor: '000000',    // Black bars (hex color)
      padding: 5             // 5mm padding around the barcode
    });

    // Define the output path for the PNG file
    const outputPath = resolve(process.cwd(), 'barcode.png');
    await writeFile(outputPath, pngBuffer);
    console.log(`Barcode generated successfully at ${outputPath}`);
  } catch (e) {
    console.error('Error generating barcode:', e instanceof Error ? e.message : e);
  }
}

generateBarcode();

view raw JSON →