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.
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.
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}
/>
);
}
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>
</>
);
}
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" />
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.
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>
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.
You have a PHP/Node.js SSR site and need to show barcodes on product pages after JS loads.
jsbarcode<!-- In your SSR template -->
<svg id="product-barcode"></svg>
<script>
JsBarcode("#product-barcode", "{{ product.sku }}");
</script>
Users can edit product SKUs in a form, and the barcode preview updates live.
react-barcodefunction LabelEditor() {
const [sku, setSku] = useState("");
return (
<div>
<input value={sku} onChange={e => setSku(e.target.value)} />
{sku && <Barcode value={sku} format="CODE128" />}
</div>
);
}
You need to generate thousands of barcodes in a worker or offscreen canvas for batch printing.
jsbarcode// In a web worker or headless context
const canvas = new OffscreenCanvas(200, 100);
JsBarcode(canvas, "BATCH123", { format: "CODE128" });
| Feature | jsbarcode | react-barcode |
|---|---|---|
| Framework | Vanilla JS | React only |
| Rendering Style | Imperative | Declarative |
| Re-renders | Manual | Automatic (via React) |
| Output Formats | SVG, Canvas, <img> | SVG only |
| DOM Control | Full (you manage element) | Abstracted (component manages) |
| Use Outside React | ✅ Yes | ❌ No |
jsbarcode.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.
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.
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.
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.
<svg id="barcode"></svg>
<!-- or -->
<canvas id="barcode"></canvas>
<!-- or -->
<img id="barcode"/>
JsBarcode("#barcode", "Hi!");
// or with jQuery
$("#barcode").JsBarcode("Hi!");
JsBarcode("#barcode", "1234", {
format: "pharmacode",
lineColor: "#0aa",
width:4,
height:40,
displayValue: false
});
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();
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();
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.
Download or get the CDN link to the script:
| Name | Supported barcodes | Size (gzip) | CDN / Download |
|---|---|---|---|
| All | All the barcodes! | 11 kB | JsBarcode.all.min.js |
| CODE128 | CODE128 (auto and force mode) | 6.2 kB | JsBarcode.code128.min.js |
| CODE39 | CODE39 | 5.1 kB | JsBarcode.code39.min.js |
| EAN / UPC | EAN-13, EAN-8, EAN-5, EAN-2, UPC (A) | 6.6 kB | JsBarcode.ean-upc.min.js |
| ITF | ITF, ITF-14 | 5 kB | JsBarcode.itf.min.js |
| MSI | MSI, MSI10, MSI11, MSI1010, MSI1110 | 5 kB | JsBarcode.msi.min.js |
| Pharmacode | Pharmacode | 4.7 kB | JsBarcode.pharmacode.min.js |
| Codabar | Codabar | 4.9 kB | JsBarcode.codabar.min.js |
| CODE93 | CODE93 | JsBarcode.code93.min.js |
Include the script in your code:
<script src="JsBarcode.all.min.js"></script>
You are done! Go generate some barcodes :smile:
You can also use Bower or npm to install and manage the library.
bower install jsbarcode --save
npm install jsbarcode --save
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
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);
For information about how to use the options, see the wiki page.
| Option | Default value | Type |
|---|---|---|
format | "auto" (CODE128) | String |
width | 2 | Number |
height | 100 | Number |
displayValue | true | Boolean |
text | undefined | String |
fontOptions | "" | String |
font | "monospace" | String |
textAlign | "center" | String |
textPosition | "bottom" | String |
textMargin | 2 | Number |
fontSize | 20 | Number |
background | "#ffffff" | String (CSS color) |
lineColor | "#000000" | String (CSS color) |
margin | 10 | Number |
marginTop | undefined | Number |
marginBottom | undefined | Number |
marginLeft | undefined | Number |
marginRight | undefined | Number |
valid | function(valid){} | Function |
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.
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.