Barcode Writer in Pure JavaScript (bwip-js)
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
-
TypeError: bwipjs is not a function
cause Attempting to use `bwipjs` as a named export when it is a default export, or a CommonJS `require()` for an ESM-only package.fixIf using ES Modules, ensure `import bwipjs from 'bwip-js';` (for the main package) or `import { namedExport } from '@bwip-js/platform';`. If using CommonJS, use `const bwipjs = require('bwip-js');` for the main package. Do not use `require()` for ESM-only platform packages like `@bwip-js/browser`. -
ReferenceError: document is not defined or ReferenceError: CanvasRenderingContext2D is not defined
cause Attempting to use browser-specific rendering methods (like `toCanvas()`) or browser-dependent global objects (like `document`) within a Node.js environment without a headless browser or canvas emulation library.fixIn Node.js, use the `@bwip-js/node` package and its `toBuffer()` method for generating PNG buffers. For browser environments, use `@bwip-js/browser` and `toCanvas()` to render to an HTML canvas element. Ensure you are importing the correct platform-specific package for your target environment. -
BWIP-JS: Unknown BCID
cause The barcode identifier (`bcid`) provided to the generator function is either misspelled, not supported by the library, or uses an outdated name.fixConsult the `bwip-js` documentation or the BWIPP wiki for a complete, case-sensitive list of supported barcode types (BCIDs). Correct the `bcid` parameter to a recognized identifier.
Warnings
- breaking Version 4.9.1 and 4.9.2 addressed a critical bug (#372) that could lead to incorrect barcode generation or runtime errors in specific scenarios. Users on older 4.x versions, especially those before 4.9.1, should upgrade immediately to prevent potential data integrity or display issues.
- breaking The introduction of platform-specific packages (`@bwip-js/node`, `@bwip-js/browser`, `@bwip-js/react-native`, `@bwip-js/generic`) significantly changed import patterns. `@bwip-js/browser`, `@bwip-js/react-native`, and `@bwip-js/generic` are ES Modules (ESM) only, requiring `import` statements and modern build environments. Only the `@bwip-js/node` package and the main `bwip-js` package retain CommonJS `require()` compatibility for their primary exports.
- gotcha Version 4.8.0 introduced new text layout features from BWIPP, including a `font.rotate` property. If you are using a custom drawing interface, you may need to update your drawing object's `text()` method to correctly handle this new property, or text rendering within your barcodes might be misaligned or fail.
- breaking Versions 4.5.2 and 4.5.3 fixed critical bugs (#354) related to `azteccode` that caused corrupted encoding of binary data. Using `azteccode` with binary input in versions prior to 4.5.2 could result in unreadable or incorrectly generated barcodes.
Install
-
npm install bwip-js -
yarn add bwip-js -
pnpm add bwip-js
Imports
- bwipjs
import { bwipjs } from 'bwip-js';import bwipjs from 'bwip-js';
- bwipjs
const { bwipjs } = require('bwip-js');const bwipjs = require('bwip-js'); - toBuffer
const { toBuffer } = require('@bwip-js/node');import { toBuffer } from '@bwip-js/node'; - toCanvas
const { toCanvas } = require('@bwip-js/browser');import { toCanvas } from '@bwip-js/browser';
Quickstart
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();