{"id":11431,"library":"node-int64","title":"64-bit Integer Representation for JavaScript","description":"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.","status":"abandoned","version":"0.4.0","language":"javascript","source_language":"en","source_url":"https://github.com/broofa/node-int64","tags":["javascript","math","integer","int64"],"install":[{"cmd":"npm install node-int64","lang":"bash","label":"npm"},{"cmd":"yarn add node-int64","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-int64","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The `node-int64` package is a CommonJS module and does not support ESM imports. It exports the `Int64` class directly as `module.exports`.","wrong":"import Int64 from 'node-int64'; // This package is CommonJS-only.\nimport { Int64 } from 'node-int64'; // This package is CommonJS-only.","symbol":"Int64","correct":"const Int64 = require('node-int64');"},{"note":"The constructor accepts numbers, hex strings, two 32-bit words, or Buffer/Uint8Array instances.","symbol":"new Int64(value)","correct":"const myInt = new Int64('123456789abcdef0');"},{"note":"For TypeScript, the community-maintained `@types/node-int64` package should be installed. It uses `export = Int64`, requiring a `require` style import for types.","wrong":"import { Int64 } from 'node-int64'; // The community-maintained types use 'export = Int64'.","symbol":"Int64 (TypeScript type)","correct":"import Int64 = require('node-int64');\n// or\n/// <reference types=\"node-int64\" />"}],"quickstart":{"code":"const Int64 = require('node-int64');\n\n// Create an Int64 from a JavaScript number (up to 2^53 precision)\nconst smallInt = new Int64(0x123456789);\nconsole.log(`Small Int: ${smallInt.toString(16)} (value: ${smallInt.valueOf()})`);\n\n// Create an Int64 from a hexadecimal string for full 64-bit precision\nconst bigHex = '123456789abcdef0';\nconst largeInt = new Int64(bigHex);\nconsole.log(`Large Int (hex): ${largeInt.toOctetString()} (value: ${largeInt.valueOf()})`);\n\n// Create an Int64 from a Node.js Buffer\nconst buffer = Buffer.from([0xDE, 0xAD, 0xBE, 0xEF, 0x01, 0x02, 0x03, 0x04]);\nconst intFromBuffer = new Int64(buffer);\nconsole.log(`From Buffer: ${intFromBuffer.toOctetString()} (value: ${intFromBuffer.valueOf()})`);\n\n// Extract the 64-bit value into a Buffer\nconst outputBuffer = intFromBuffer.toBuffer();\nconsole.log(`To Buffer: ${outputBuffer.toString('hex')}`);\n\n// Check if the internal value is within JS safe integer range\nconsole.log(`Is smallInt finite (JS precision): ${isFinite(smallInt)}`);\nconsole.log(`Is largeInt finite (JS precision): ${isFinite(largeInt)}`);","lang":"javascript","description":"Demonstrates creating `Int64` instances from various inputs (number, hex string, Buffer) and basic output operations like `toOctetString` and `toBuffer`."},"warnings":[{"fix":"While `node-int64` itself uses `new Buffer()` internally, direct usage in consuming code should be updated. For creating Int64 from a buffer, use `new Int64(Buffer.from([...]))` or `new Int64(new Uint8Array([...]))`.","message":"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()`.","severity":"breaking","affected_versions":">=0.4.0 (for Node.js v6+)"},{"fix":"If 64-bit arithmetic is required, consider using native `BigInt` (available in Node.js v10.5.0+ and modern browsers) or a different arbitrary-precision arithmetic library like `js-big-decimal`.","message":"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.","severity":"gotcha","affected_versions":">=0.4.0"},{"fix":"Evaluate migrating to native JavaScript `BigInt`. For example, `new Int64('...')` can often be replaced with `BigInt('0x...')` or `BigInt('...')`.","message":"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.","severity":"deprecated","affected_versions":">=0.4.0"},{"fix":"Always use methods like `toOctetString()` or `toBuffer()` to retrieve the full 64-bit precision, or `toString(radix)` for string representation, rather than relying on numerical coercion, especially for large values.","message":"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.","severity":"gotcha","affected_versions":">=0.4.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"This 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.","cause":"Attempting to use `require()` in an ES module context or a browser environment without a bundler that polyfills `require`.","error":"ReferenceError: require is not defined"},{"fix":"Verify that `const Int64 = require('node-int64');` is correctly placed and executed before `new Int64(...)` calls. Check `node_modules` for `node-int64`.","cause":"The `Int64` symbol was not correctly imported or assigned, or the package failed to load.","error":"TypeError: Int64 is not a constructor"},{"fix":"For 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.","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).","error":"ReferenceError: Buffer is not defined"}],"ecosystem":"npm"}