jsbarcode vs react-barcode
Generating Barcodes in Web Applications
jsbarcodereact-barcodeSimilar Packages:
Generating Barcodes in Web Applications

jsbarcode and react-barcode are both JavaScript libraries for generating barcode graphics in web applications. jsbarcode is a lightweight, framework-agnostic library that renders barcodes directly to HTML canvas, SVG, or image elements using vanilla JavaScript. react-barcode is a React-specific wrapper built on top of jsbarcode, exposing its functionality through a declarative React component API. Both support common barcode formats like CODE128, EAN, UPC, and more, but differ significantly in integration style, rendering control, and use-case suitability.

Npm Package Weekly Downloads Trend
3 Years
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
jsbarcode626,5105,804617 kB1366 months agoMIT
react-barcode173,51844011.7 kB199 months agoISC

jsbarcode vs react-barcode: Choosing the Right Barcode Library for Your Web App

Both jsbarcode and react-barcode let you generate standard-compliant barcodes (like CODE128, EAN-13, UPC-A) directly in the browser. But they serve different architectural needs. Let’s compare how they work under real-world conditions.

🧱 Core Architecture: Vanilla JS vs React Component

jsbarcode is a pure JavaScript utility. You call it imperatively with a DOM element (or selector) and configuration options. It directly manipulates that element to render the barcode as SVG, canvas, or <img>.

// jsbarcode: imperative DOM mutation
import JsBarcode from "jsbarcode";

// Assumes <svg id="barcode"></svg> exists in DOM
JsBarcode("#barcode", "1234567890", {
  format: "CODE128",
  displayValue: true
});

react-barcode wraps jsbarcode into a React component. You declare it in JSX with props, and it manages the underlying DOM element and re-renders automatically when props change.

// react-barcode: declarative component
import Barcode from "react-barcode";

function ProductLabel({ sku }) {
  return (
    <Barcode
      value={sku}
      format="CODE128"
      displayValue={true}
    />
  );
}

🔁 Re-rendering Behavior: Manual vs Automatic

With jsbarcode, you must manually trigger re-renders when data changes. This gives you full control but requires extra logic.

// jsbarcode: manual update on state change
function updateBarcode(newSku) {
  JsBarcode("#barcode", newSku, { format: "CODE128" });
}

// In a vanilla app or non-React framework
someButton.addEventListener("click", () => updateBarcode("NEW123"));

With react-barcode, React’s reconciliation handles updates automatically. If value prop changes, the barcode re-renders without extra code.

// react-barcode: automatic re-render
function DynamicLabel() {
  const [sku, setSku] = useState("INITIAL");

  return (
    <>
      <Barcode value={sku} format="CODE128" />
      <button onClick={() => setSku("UPDATED")}>
        Change SKU
      </button>
    </>
  );
}

🖼️ Rendering Targets: Flexible vs Constrained

jsbarcode supports three output formats: SVG, Canvas, and <img> (data URL). You choose by passing the appropriate DOM element type.

// Render to canvas
const canvas = document.getElementById("myCanvas");
JsBarcode(canvas, "12345");

// Render to img tag (as data URL)
const img = document.getElementById("myImg");
JsBarcode(img, "12345");

react-barcode only renders to SVG by default. While you can technically pass a renderer prop, the component is designed around SVG output and doesn’t expose easy canvas or image rendering.

// react-barcode: SVG-only (no canvas/img support out of the box)
<Barcode value="12345" format="CODE128" />

⚙️ Configuration and Options

Both libraries share the same core configuration options because react-barcode delegates to jsbarcode. However, the way you pass them differs.

jsbarcode uses a flat options object:

JsBarcode("#barcode", "12345", {
  format: "EAN13",
  lineColor: "#000",
  width: 2,
  height: 100,
  displayValue: true,
  fontOptions: "bold"
});

react-barcode maps these options to component props:

<Barcode
  value="12345"
  format="EAN13"
  lineColor="#000"
  width={2}
  height={100}
  displayValue={true}
  fontOptions="bold"
/>

Note: All jsbarcode options are supported as props in react-barcode, per its documentation.

🧪 Error Handling and Validation

Both libraries throw errors for invalid inputs (e.g., unsupported characters for a given format). However, error handling differs in practice.

With jsbarcode, you wrap calls in try/catch:

try {
  JsBarcode("#barcode", "INVALID!@#", { format: "EAN13" });
} catch (err) {
  console.error("Barcode generation failed:", err.message);
}

With react-barcode, errors during render will bubble up as React rendering errors. You typically handle them with an error boundary:

<ErrorBoundary fallback={<div>Invalid barcode</div>}>
  <Barcode value="INVALID!@#" format="EAN13" />
</ErrorBoundary>

📦 Bundle Impact and Dependencies

jsbarcode has zero dependencies and is extremely lightweight. It’s suitable for performance-sensitive contexts like mobile web or embedded systems.

react-barcode depends on jsbarcode and React. If you’re already in a React app, the incremental cost is small—but it adds no value outside React.

🌐 Real-World Use Cases

Case 1: Server-Rendered E-Commerce Site with Client Barcode

You have a PHP/Node.js SSR site and need to show barcodes on product pages after JS loads.

  • Best choice: jsbarcode
  • Why? No React needed; just drop in a script tag and call it on page load.
<!-- In your SSR template -->
<svg id="product-barcode"></svg>
<script>
  JsBarcode("#product-barcode", "{{ product.sku }}");
</script>

Case 2: React Admin Dashboard with Editable Labels

Users can edit product SKUs in a form, and the barcode preview updates live.

  • Best choice: react-barcode
  • Why? Declarative binding to state avoids manual DOM updates.
function LabelEditor() {
  const [sku, setSku] = useState("");
  return (
    <div>
      <input value={sku} onChange={e => setSku(e.target.value)} />
      {sku && <Barcode value={sku} format="CODE128" />}
    </div>
  );
}

Case 3: Printing High-Density Barcodes to Canvas

You need to generate thousands of barcodes in a worker or offscreen canvas for batch printing.

  • Best choice: jsbarcode
  • Why? Direct canvas access and no React overhead.
// In a web worker or headless context
const canvas = new OffscreenCanvas(200, 100);
JsBarcode(canvas, "BATCH123", { format: "CODE128" });

📌 Summary Table

Featurejsbarcodereact-barcode
FrameworkVanilla JSReact only
Rendering StyleImperativeDeclarative
Re-rendersManualAutomatic (via React)
Output FormatsSVG, Canvas, <img>SVG only
DOM ControlFull (you manage element)Abstracted (component manages)
Use Outside React✅ Yes❌ No

💡 Final Recommendation

  • If you’re not using React, or need canvas/image output, or want maximum control and minimal abstraction — go with jsbarcode.
  • If you’re in a React app and want simple, self-updating barcode components without managing DOM refs — use react-barcode.

Both are well-maintained and production-ready. The choice isn’t about quality—it’s about fitting the right tool to your app’s architecture.

How to Choose: jsbarcode vs react-barcode
  • jsbarcode:

    Choose jsbarcode if you're working outside React, need fine-grained control over DOM rendering (e.g., direct canvas manipulation), or want to minimize abstraction layers. It’s ideal for vanilla JS apps, server-rendered pages with client-side enhancement, or performance-critical scenarios where you manage element lifecycle manually.

  • react-barcode:

    Choose react-barcode if you’re building a React application and prefer a component-based, declarative approach. It handles DOM updates automatically in response to prop changes and integrates cleanly with React’s reconciliation model, making it simpler to embed barcodes in JSX without managing refs or manual re-renders.

README for jsbarcode

JsBarcode

Build Status Scrutinizer Code Quality CDN License

Introduction

JsBarcode is a barcode generator written in JavaScript. It supports multiple barcode formats and works in browsers and with Node.js. It has no dependencies when it is used for the web but works with jQuery if you are into that.

Demo

Barcode Generator

Simple CodePen Demo

Settings CodePen Demo

Supported barcodes:

  • CODE128
    • CODE128 (automatic mode switching)
    • CODE128 A/B/C (force mode)
  • EAN
    • EAN-13
    • EAN-8
    • EAN-5
    • EAN-2
    • UPC (A)
    • UPC (E)
  • CODE39
  • ITF
    • ITF
    • ITF-14
  • MSI
    • MSI10
    • MSI11
    • MSI1010
    • MSI1110
  • Pharmacode
  • Codabar
  • CODE93

Examples for browsers:

First create a canvas (or image)

<svg id="barcode"></svg>
<!-- or -->
<canvas id="barcode"></canvas>
<!-- or -->
<img id="barcode"/>

Simple example:

JsBarcode("#barcode", "Hi!");
// or with jQuery
$("#barcode").JsBarcode("Hi!");
Result:

Result

Example with options:

JsBarcode("#barcode", "1234", {
  format: "pharmacode",
  lineColor: "#0aa",
  width:4,
  height:40,
  displayValue: false
});
Result:

Result

More advanced use case:

JsBarcode("#barcode")
  .options({font: "OCR-B"}) // Will affect all barcodes
  .EAN13("1234567890128", {fontSize: 18, textMargin: 0})
  .blank(20) // Create space between the barcodes
  .EAN5("12345", {height: 85, textPosition: "top", fontSize: 16, marginTop: 15})
  .render();
Result:

Result

Or define the value and options in the HTML element:

Use any jsbarcode-* or data-* as attributes where * is any option.

<svg class="barcode"
  jsbarcode-format="upc"
  jsbarcode-value="123456789012"
  jsbarcode-textmargin="0"
  jsbarcode-fontoptions="bold">
</svg>

And then initialize it with:

JsBarcode(".barcode").init();
Result:

Result

Retrieve the barcode values so you can render it any way you'd like

Pass in an object which will be filled with data.

const data = {};
JsBarcode(data, 'text', {...options});

data will be filled with a encodings property which has all the needed values. See wiki for an example of what data looks like.

Setup for browsers:

Step 1:

Download or get the CDN link to the script:

NameSupported barcodesSize (gzip)CDN / Download
AllAll the barcodes!11 kBJsBarcode.all.min.js
CODE128CODE128 (auto and force mode)6.2 kBJsBarcode.code128.min.js
CODE39CODE395.1 kBJsBarcode.code39.min.js
EAN / UPCEAN-13, EAN-8, EAN-5, EAN-2, UPC (A)6.6 kBJsBarcode.ean-upc.min.js
ITFITF, ITF-145 kBJsBarcode.itf.min.js
MSIMSI, MSI10, MSI11, MSI1010, MSI11105 kBJsBarcode.msi.min.js
PharmacodePharmacode4.7 kBJsBarcode.pharmacode.min.js
CodabarCodabar4.9 kBJsBarcode.codabar.min.js
CODE93CODE93JsBarcode.code93.min.js

Step 2:

Include the script in your code:

<script src="JsBarcode.all.min.js"></script>

Step 3:

You are done! Go generate some barcodes :smile:

Bower and npm:

You can also use Bower or npm to install and manage the library.

bower install jsbarcode --save
npm install jsbarcode --save

Node.js:

With canvas:

var JsBarcode = require('jsbarcode');

// Canvas v1
var Canvas = require("canvas");
// Canvas v2
var { createCanvas } = require("canvas");

// Canvas v1
var canvas = new Canvas();
// Canvas v2
var canvas = createCanvas();

JsBarcode(canvas, "Hello");

// Do what you want with the canvas
// See https://github.com/Automattic/node-canvas for more information

With svg:

const { DOMImplementation, XMLSerializer } = require('xmldom');
const xmlSerializer = new XMLSerializer();
const document = new DOMImplementation().createDocument('http://www.w3.org/1999/xhtml', 'html', null);
const svgNode = document.createElementNS('http://www.w3.org/2000/svg', 'svg');

JsBarcode(svgNode, 'test', {
    xmlDocument: document,
});

const svgText = xmlSerializer.serializeToString(svgNode);

Options:

For information about how to use the options, see the wiki page.

OptionDefault valueType
format"auto" (CODE128)String
width2Number
height100Number
displayValuetrueBoolean
textundefinedString
fontOptions""String
font"monospace"String
textAlign"center"String
textPosition"bottom"String
textMargin2Number
fontSize20Number
background"#ffffff"String (CSS color)
lineColor"#000000"String (CSS color)
margin10Number
marginTopundefinedNumber
marginBottomundefinedNumber
marginLeftundefinedNumber
marginRightundefinedNumber
validfunction(valid){}Function

Contributions and feedback:

We :heart: contributions and feedback.

If you want to contribute, please check out the CONTRIBUTING.md file.

If you have any question or suggestion create an issue or ask about it in the gitter chat.

Bug reports should always be done with a new issue.

License:

JsBarcode is shared under the MIT license. This means you can modify and use it however you want, even for comercial use. But please give this the Github repo a :star: and write a small comment of how you are using JsBarcode in the gitter chat.