~6 min read • Updated Dec 26, 2025
1. Writing BigUInt64
buf.writeBigUInt64BE(value, offset): Writes an unsigned 64-bit bigint in big-endian format.buf.writeBigUInt64LE(value, offset): Writes an unsigned 64-bit bigint in little-endian format.
const buf = Buffer.allocUnsafe(8); buf.writeBigUInt64BE(0xdecafafecacefaden, 0); console.log(buf); //
2. Writing Double and Float
buf.writeDoubleBE(value, offset): Writes a 64-bit floating-point number in big-endian format.buf.writeDoubleLE(value, offset): Writes a 64-bit floating-point number in little-endian format.buf.writeFloatBE(value, offset): Writes a 32-bit floating-point number in big-endian format.buf.writeFloatLE(value, offset): Writes a 32-bit floating-point number in little-endian format.
const buf = Buffer.allocUnsafe(8); buf.writeDoubleBE(123.456, 0); console.log(buf);
3. Writing Small Integers
buf.writeInt8(value, offset): Writes a signed 8-bit integer.buf.writeUInt8(value, offset): Writes an unsigned 8-bit integer.
const buf = Buffer.allocUnsafe(2); buf.writeInt8(2, 0); buf.writeInt8(-2, 1); console.log(buf); //
4. Writing Int16 and UInt16
buf.writeInt16BE(value, offset): Writes a signed 16-bit integer in big-endian format.buf.writeInt16LE(value, offset): Writes a signed 16-bit integer in little-endian format.buf.writeUInt16BE(value, offset): Writes an unsigned 16-bit integer in big-endian format.buf.writeUInt16LE(value, offset): Writes an unsigned 16-bit integer in little-endian format.
5. Writing Int32 and UInt32
buf.writeInt32BE(value, offset): Writes a signed 32-bit integer in big-endian format.buf.writeInt32LE(value, offset): Writes a signed 32-bit integer in little-endian format.buf.writeUInt32BE(value, offset): Writes an unsigned 32-bit integer in big-endian format.buf.writeUInt32LE(value, offset): Writes an unsigned 32-bit integer in little-endian format.
6. Writing IntBE / IntLE and UIntBE / UIntLE
Writes integers of variable length (1–6 bytes) in big-endian or little-endian format.
const buf = Buffer.allocUnsafe(6); buf.writeIntBE(0x1234567890ab, 0, 6); console.log(buf); //
7. Writing Strings
buf.write(string[, offset[, length]][, encoding]): Writes a string into the Buffer using the specified encoding (default: UTF-8).
const buf = Buffer.alloc(256);
const len = buf.write('½ + ¼ = ¾', 0);
console.log(`${len} bytes: ${buf.toString('utf8',0,len)}`);
Conclusion
The Buffer write methods provide precise control over storing binary data in different formats. With support for signed and unsigned integers, floating-point numbers, big integers, and strings, these methods are essential for low-level data processing in Node.js applications.
1. Handling Function Arguments
Addons often expose functions to JavaScript. These functions must validate the number and type of arguments before performing operations.
// addon.cc void Add(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); if (args.Length() < 2) { isolate->ThrowException(Exception::TypeError( String::NewFromUtf8(isolate,"Wrong number of arguments").ToLocalChecked())); return; } if (!args[0]->IsNumber() || !args[1]->IsNumber()) { isolate->ThrowException(Exception::TypeError( String::NewFromUtf8(isolate,"Wrong arguments").ToLocalChecked())); return; } double value = args[0].As ()->Value() + args[1].As ()->Value(); Local num = Number::New(isolate, value); args.GetReturnValue().Set(num); }
This function adds two numbers and returns the result to JavaScript.
2. Executing Callbacks
Addons can accept JavaScript functions as arguments and invoke them from C++.
// addon.cc void RunCallback(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); Local context = isolate->GetCurrentContext(); Local cb = Local ::Cast(args[0]); const unsigned argc = 1; Local argv[argc] = { String::NewFromUtf8(isolate,"hello world").ToLocalChecked() }; cb->Call(context, Null(isolate), argc, argv).ToLocalChecked(); }
In this example, the callback is invoked synchronously and prints "hello world".
3. Object Factory
Addons can create and return new objects from C++ functions. These objects can contain properties set from input arguments.
// addon.cc void CreateObject(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); Local context = isolate->GetCurrentContext(); Local
This function creates an object with a msg property equal to the input string.
Conclusion
Node.js Addons provide powerful tools for extending JavaScript with native C/C++ functionality. Proper handling of function arguments, executing callbacks, and creating objects are key techniques for building efficient and reliable Addons.
1. Function Factory
This pattern creates a JavaScript function that wraps a C++ function and returns it back to JavaScript.
// addon.cc void MyFunction(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); args.GetReturnValue().Set(String::NewFromUtf8(isolate, "hello world").ToLocalChecked()); } void CreateFunction(const FunctionCallbackInfo & args) { Isolate* isolate = args.GetIsolate(); Local context = isolate->GetCurrentContext(); Local tpl = FunctionTemplate::New(isolate, MyFunction); Local fn = tpl->GetFunction(context).ToLocalChecked(); fn->SetName(String::NewFromUtf8(isolate, "theFunction").ToLocalChecked()); args.GetReturnValue().Set(fn); }
This code creates a JavaScript function that, when invoked, returns the string "hello world".
2. Wrapping C++ Objects
Using node::ObjectWrap, C++ classes can be wrapped so that new instances can be created with the JavaScript new operator.
// myobject.h
class MyObject : public node::ObjectWrap {
public:
static void Init(v8::Local exports);
private:
explicit MyObject(double value = 0);
~MyObject();
static void New(const v8::FunctionCallbackInfo& args);
static void PlusOne(const v8::FunctionCallbackInfo& args);
double value_;
};
In myobject.cc, methods are implemented and added to the prototype:
NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);
The plusOne method increments the internal value of the object.
3. Factory for Wrapped Objects
Instead of explicitly using new in JavaScript, a factory method can be implemented in C++ to create new instances:
// addon.cc void CreateObject(const FunctionCallbackInfo& args) { MyObject::NewInstance(args); }
This approach allows objects to be created without directly calling new in JavaScript.
Conclusion
The Function Factory and ObjectWrap patterns are powerful techniques for Node.js Addon development. They enable developers to expose C++ functions and objects directly to JavaScript, bridging native performance with the flexibility of JavaScript applications.
1. Creating and Passing Objects
A createObject function is used to instantiate new C++ objects. The add function then accepts two MyObject instances, unwraps them, and sums their internal values.
// addon.cc void Add(const FunctionCallbackInfo& args) { Isolate* isolate = args.GetIsolate(); Local context = isolate->GetCurrentContext(); MyObject* obj1 = node::ObjectWrap::Unwrap ( args[0]->ToObject(context).ToLocalChecked()); MyObject* obj2 = node::ObjectWrap::Unwrap ( args[1]->ToObject(context).ToLocalChecked()); double sum = obj1->value() + obj2->value(); args.GetReturnValue().Set(Number::New(isolate, sum)); }
2. Accessing Internal Values
To expose the private value_ field of MyObject, a public accessor method is added:
// myobject.h
inline double value() const { return value_; }
3. Implementing MyObject
The MyObject class is implemented similarly to previous versions, but now supports unwrapping so its values can be used across different functions.
MyObject::MyObject(double value) : value_(value) {}
void MyObject::NewInstance(const FunctionCallbackInfo& args) {
// Create a new instance and return it
}
4. Testing in JavaScript
After compiling the Addon, objects can be created and passed around between functions:
// test.js
const addon = require('./build/Release/addon');
const obj1 = addon.createObject(10);
const obj2 = addon.createObject(20);
const result = addon.add(obj1, obj2);
console.log(result); // 30
Conclusion
Using ObjectWrap::Unwrap, wrapped C++ objects can be passed around in JavaScript and reused across multiple functions. This technique enables advanced interaction between native code and JavaScript, making it essential for building complex Node.js Addons.
Written & researched by Dr. Shahin Siami