64-bit Integer Representation for JavaScript
The `node-int64` library provides a class, `Int64`, to precisely represent 64-bit integers in JavaScript, addressing the inherent limitation of JavaScript's IEEE 754 double-precision floats which lose integer precision beyond +/- 2^53. It is currently at version 0.4.0, which was published 11 years ago, and the project is not actively maintained, with its author noting that native `BigInt`s will likely obsolete it. The library aims to provide a Number-like object primarily for carrying 64-bit integer values, such as those encountered in binary data formats like Apache Thrift, rather than for performing 64-bit integer arithmetic. While instances can be coerced to JavaScript numbers for basic operations, values exceeding the safe integer range will resolve to `Infinity`. Its primary utility lies in preserving the exact 64-bit binary representation, which can be extracted as an octet string or Node.js `Buffer`. This makes it suitable for interoperability with systems requiring precise 64-bit data types, distinguishing it from libraries focused on arbitrary-precision arithmetic.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` in an ES module context or a browser environment without a bundler that polyfills `require`.fixThis package is CommonJS-only. Ensure your Node.js project is configured for CommonJS (e.g., without `"type": "module"` in `package.json` or by using a bundler). For browser environments, a bundler is necessary. -
TypeError: Int64 is not a constructor
cause The `Int64` symbol was not correctly imported or assigned, or the package failed to load.fixVerify that `const Int64 = require('node-int64');` is correctly placed and executed before `new Int64(...)` calls. Check `node_modules` for `node-int64`. -
ReferenceError: Buffer is not defined
cause Attempting to use `Buffer` in a browser environment without a polyfill, or in an older Node.js context where `Buffer` might not be globally available if not explicitly required/imported (less common in modern Node.js).fixFor browser usage, a polyfill for Node.js `Buffer` is required. In Node.js, `Buffer` is typically global, but if not, ensure `const Buffer = require('buffer').Buffer;` or similar is used.
Warnings
- breaking The `new Buffer()` constructor used in older examples and package internals is deprecated and can pose security risks. It has been replaced by `Buffer.from()`, `Buffer.alloc()`, and `Buffer.allocUnsafe()`.
- gotcha This library is designed for *representing* 64-bit integers, not for performing 64-bit *arithmetic*. Operations like addition or subtraction on `Int64` instances will implicitly convert them to standard JavaScript numbers, leading to precision loss if the value exceeds 2^53.
- deprecated The `node-int64` package is not actively maintained, and its author suggests it will be obsoleted by the native `BigInt` feature in JavaScript. Users are encouraged to migrate to `BigInt` for 64-bit integer handling when feasible.
- gotcha When an `Int64` instance represents a value outside the safe integer range of JavaScript numbers (i.e., beyond +/- 2^53), attempting to coerce it to a number (e.g., through `valueOf()`, `+` operator, or implicit conversion) will result in `Infinity` or `NaN`, despite the internal 64-bit representation being accurate.
Install
-
npm install node-int64 -
yarn add node-int64 -
pnpm add node-int64
Imports
- Int64
import Int64 from 'node-int64'; // This package is CommonJS-only. import { Int64 } from 'node-int64'; // This package is CommonJS-only.const Int64 = require('node-int64'); - new Int64(value)
const myInt = new Int64('123456789abcdef0'); - Int64 (TypeScript type)
import { Int64 } from 'node-int64'; // The community-maintained types use 'export = Int64'.import Int64 = require('node-int64'); // or /// <reference types="node-int64" />
Quickstart
const Int64 = require('node-int64');
// Create an Int64 from a JavaScript number (up to 2^53 precision)
const smallInt = new Int64(0x123456789);
console.log(`Small Int: ${smallInt.toString(16)} (value: ${smallInt.valueOf()})`);
// Create an Int64 from a hexadecimal string for full 64-bit precision
const bigHex = '123456789abcdef0';
const largeInt = new Int64(bigHex);
console.log(`Large Int (hex): ${largeInt.toOctetString()} (value: ${largeInt.valueOf()})`);
// Create an Int64 from a Node.js Buffer
const buffer = Buffer.from([0xDE, 0xAD, 0xBE, 0xEF, 0x01, 0x02, 0x03, 0x04]);
const intFromBuffer = new Int64(buffer);
console.log(`From Buffer: ${intFromBuffer.toOctetString()} (value: ${intFromBuffer.valueOf()})`);
// Extract the 64-bit value into a Buffer
const outputBuffer = intFromBuffer.toBuffer();
console.log(`To Buffer: ${outputBuffer.toString('hex')}`);
// Check if the internal value is within JS safe integer range
console.log(`Is smallInt finite (JS precision): ${isFinite(smallInt)}`);
console.log(`Is largeInt finite (JS precision): ${isFinite(largeInt)}`);