Registry / serialization / utility

utility

JSON →
library1.0jsnpmunverified

The `utility` package provides a diverse collection of JavaScript and TypeScript utilities primarily designed for Node.js environments. It includes functions for common tasks such as hashing (MD5, SHA1, SHA256, HMAC), various encoding/decoding operations (Base64, URL encoding, HTML escaping), date formatting, string manipulation, and other miscellaneous helpers like `randomString` and `getParamNames`. The current stable version is 2.5.0, with frequent releases, typically monthly or bi-monthly, indicating active maintenance and feature development. Key differentiators include its focus on performance, direct TypeScript support, and a comprehensive set of functions that cover a wide range of backend needs, making it a reliable general-purpose toolkit for Node.js applications.

npm install utility
INSTALL
IMPORT
SIG · UTILITY
U
utility
serializationjavascriptv1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

All utilities (as namespace)
import * as utils from 'utility';
const utils = require('utility');
While CommonJS `require` works for older Node.js versions or CJS modules, `import * as` is the recommended way to access all utilities as a namespace in modern ESM projects and TypeScript. CommonJS support was dropped for Node.js < 16 in v2.
Specific named utility (e.g., md5)
import { md5 } from 'utility';
import md5 from 'utility';
Most utilities are exported as named exports. Attempting a default import for a named export will result in a runtime error.
Type imports
import type { Md5Algorithm } from 'utility';
When importing only types, use `import type` for better tree-shaking and clearer intent, especially in TypeScript projects.

Demonstrates common utility functions like hashing, random string generation, date formatting, and Base64 encoding using named imports.

import { md5, sha256, randomString, YYYYMMDDHHmmssSSS, base64encode } from 'utility'; async function demonstrateUtilities() { const secretMessage = 'Hello, Checklist Day!'; console.log(`Original message: ${secretMessage}`); // Hashing const md5Hash = md5(secretMessage); console.log(`MD5 Hash: ${md5Hash}`); const sha256Hash = sha256(Buffer.from(secretMessage)); console.log(`SHA256 Hash: ${sha256Hash}`); // Random string generation const sessionId = randomString(24); console.log(`Generated Session ID: ${sessionId}`); // Date formatting const formattedDate = YYYYMMDDHHmmssSSS(); console.log(`Current formatted date: ${formattedDate}`); // Base64 encoding const encodedMessage = base64encode(secretMessage); console.log(`Base64 Encoded: ${encodedMessage}`); } demonstrateUtilities().catch(console.error);
Debug
Known issues
breakingVersion 2.0.0 of `utility` dropped support for Node.js versions older than 16. Projects running on Node.js 14 or earlier will encounter compatibility issues.
fix
Upgrade your Node.js runtime to version 16.0.0 or higher. Alternatively, pin your `utility` dependency to `<2.0.0` if unable to upgrade Node.js.
affects: >=2.0.0
deprecatedThe `utils.getIP()` function has been removed. Its functionality is no longer available directly from this package.
fix
Use the dedicated `address` package (e.g., `https://github.com/node-modules/address`) to retrieve IP addresses. Install it separately with `npm install address` and import it as needed.
affects: >=0.0.12
gotchaWhen hashing objects (e.g., `md5({a:1, b:2})`), the `utility` package performs an internal JSON stringification after sorting keys. This ensures consistent hashes for objects with the same properties in different orders but might be an unexpected behavior if direct object hashing is assumed.
fix
Be aware of this implicit behavior. If you need a different serialization for object hashing, serialize the object explicitly before passing it to the hash function (e.g., `md5(JSON.stringify(myObject))`).
affects: >=1.0.0
gotchaMixing CommonJS `require` with ESM `import` in a modern Node.js project (especially `type: "module"` in `package.json`) can lead to 'require is not defined' or unexpected import resolution issues.
fix
For new projects or modules configured as ESM, consistently use `import` statements. If you must use `require` in an ESM context, consider dynamic `import()` or ensuring the module is unequivocally CommonJS.
affects: >=2.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined
Attempting to use `require()` in a JavaScript module configured as ES Module (e.g., `"type": "module"` in `package.json`).
fix
Change `const utils = require('utility');` to `import * as utils from 'utility';` or use specific named imports like `import { md5 } from 'utility';`.
TypeError: (0, utility_1.md5) is not a function
Incorrectly importing a named export as a default import, or trying to access a named export from a default-imported object that doesn't expose it that way.
fix
Ensure you are using named imports for individual utilities: `import { md5 } from 'utility';`. If you intend to import all utilities as a namespace object, use `import * as utils from 'utility';` and then call `utils.md5()`.
Assertion Error: Only function is allowed
Providing a non-function value (e.g., `null`, `undefined`, a string) to `utility.getParamNames()`, which strictly expects a function argument.
fix
Always pass a valid function reference to `utility.getParamNames()`, for example: `utility.getParamNames(function (a, b) {})` or `utility.getParamNames(() => {})`.
Upgrade
Version history
1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
39 hits · last 30 days
node
32
OpenAI (training)
1
Resources
utility — npm install utility · libregistry