Making Dynamsoft Barcode SDK an Addon for Node.js

Do you know which programming language is currently ranking first on GitHub? It is JavaScript! JavaScript is not only used for HTML5 Web application development, but also for server-side programming with runtime environment Node.js. With npm, the package manager for Node.js, it is so easy to install any Web frameworks or utilities. Although Node.js has provided many powerful JavaScript APIs, sometimes we have to create add-ons to empower Node.js with extra functionalities. In this tutorial, I’d like to share how to implement a JavaScript Barcode application with the custom addon, wrapped with C/C++ APIs of Dynamsoft Barcode SDK, for Node.js.

Dynamsoft Barcode Reader SDK Ads Powered by Dynamsoft

Setting Up Environment for Node.js Addon Development

To build add-ons for Node.js, we need to install the tool node-gyp:

npm install -g node-gyp

Create an empty C/C++ file and a configuration file binding.gyp. Specify add-on name and C/C++ source files:

  "targets": [
      "target_name": "dbr",
      "sources": [ "" ]

Build dbr.node with the following command:

node-gyp configure install

Under folder build, you will see Visual Studio files and dbr.node:

	/ binding.sln
	/ dbr.vcxproj
	/ dbr.vcxproj.filters
	/ config.gypi
	/ Release
		/ dbr.node
		/ dbr.pdb
		/ obj

For more information, please read the basic tutorial of Node.js addons.

Wrapping Dynamsoft Barcode SDK as the Addon for Node.js

Download and install Dynamsoft Barcode Reader.

 DBR download button



Let’s double-click binding.sln to import the project into Visual Studio. Since node-gyp has added Node.js-relevant configurations, you just need to insert the directories of Dynamsoft Barcode SDK header files and libraries. Besides, add the post-build event:

copy "{installation directory}\Dynamsoft\Barcode Reader 2.0 Trial\Redist\C_C++*.dll" "$(OutDir)"

Register a native method, and export the initialization function:

void Init(Handle<Object> exports) {
	NODE_SET_METHOD(exports, "decodeFile", DecodeFile);

NODE_MODULE(dbr, Init)

Convert barcode results to data for V8 JavaScript engine:

void DecodeFile(const FunctionCallbackInfo<Value>& args) {

	Isolate* isolate = Isolate::GetCurrent();
	HandleScope scope(isolate);

	// convert v8 string to char *
	String::Utf8Value utfStr(args[0]->ToString());
	char *pFileName = *utfStr;

	int option_iMaxBarcodesNumPerPage = -1;
	int option_llBarcodeFormat = -1;

	pBarcodeResultArray pResults = NULL;
	ReaderOptions option;
	SetOptions(&option, option_iMaxBarcodesNumPerPage, option_llBarcodeFormat);

	// decode barcode image file
	int ret = DBR_DecodeFile(

	if (ret == DBR_OK){
		int count = pResults->iBarcodeCount;
		pBarcodeResult* ppBarcodes = pResults->ppBarcodes;
		pBarcodeResult tmp = NULL;

		// javascript callback function
		Local<Function> cb = Local<Function>::Cast(args[1]);
		const unsigned argc = 1;

		// array for storing barcode results
		Local<Array> barcodeResults = Array::New(isolate);

		for (int i = 0; i < count; i++)
			tmp = ppBarcodes[i];

			Local<Object> result = Object::New(isolate);
			result->Set(String::NewFromUtf8(isolate, "format"), Number::New(isolate, tmp->llFormat));
			result->Set(String::NewFromUtf8(isolate, "value"), String::NewFromUtf8(isolate, tmp->pBarcodeData));

			barcodeResults->Set(Number::New(isolate, i), result);

		// release memory

		Local<Value> argv[argc] = { barcodeResults };
		cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);

For more information, please refer to Chrome V8.

Create a JavaScript file for test:

var dbr = require('./build/Release/dbr');
var readline = require('readline');

var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout

rl.question("Please input a barcode image path: ", function(answer) {
  // e.g. F:\git\Dynamsoft-Barcode-Reader\Images\AllSupportedBarcodeTypes.tif
      var result = null;
      for (index in msg) {
        result = msg[index]


Finally, run your simple JavaScript Barcode application with command line tool:

Barcode addon for Node.js

Source Code

Search Blog Posts