node:util
The node:util module in Azion Runtime: helpers to promisify callbacks, inspect objects, format strings, and check types, with examples and the supported APIs.
The node:util module groups the Node.js helper functions for common data tasks: formatting strings, inspecting objects for debugging, converting callback-style functions to promises, and checking value types at run time. Azion Runtime supports the module through Node.js compatibility. Inside a function, use util.promisify() to adapt callback-based code to async/await, and util.inspect() to log structured data.
Examples
Each example is a complete function that imports util from node:util. The response below each example is the one a deployed function returns.
Promisify and inspect
This function converts a callback-style function to a promise, awaits it, and logs the result with util.inspect():
The function logs "Success!" and responds with:
Callback to promise conversion
This function wraps a callback-based API with util.promisify() and calls it twice with await:
The function responds with both results:
The two timestamps are equal because, in a deployed function, Date.now() does not advance during a request.
Deep object inspection
This function inspects request details and a nested object, and builds a string with util.format():
For a GET request to https://example.com/, the function responds with the request details, the formatted string, and the inspected object:
In Azion Runtime, util.inspect() prints complexData as indented JSON text with every level expanded, although the call passes depth: 2.
String formatting and deprecation
This function formats strings with placeholders, marks a function as deprecated with util.deprecate(), and calls it:
The function responds with the formatted strings and the result of each function:
The %s, %d, %j, and %% placeholders work as the example shows. The %i placeholder is not replaced: util.format() leaves %i in the output as written.
Supported APIs
The table lists the status of each node:util API in Azion Runtime:
| API | Status |
|---|---|
util.promisify() | 🟢 Supported |
util.inspect() | 🟢 Supported |
util.format() | 🟢 Supported |
util.deprecate() | 🟢 Supported |
util.isDeepStrictEqual() | 🟢 Supported |
util.inherits() | 🟡 Deprecated (use ES6 classes) |
util.callbackify() | 🟡 Partially supported |
util.formatWithOptions() | 🟡 Partially supported |
util.getSystemErrorMap() | 🟡 Partially supported |
util.getSystemErrorName() | 🟡 Partially supported |
util.log() | 🟡 Partially supported |
util.stripVTControlCharacters() | 🟡 Partially supported |
util.styleText() | 🟡 Partially supported |
util.types.isDate() | 🟢 Supported |
util.types.isRegExp() | 🟢 Supported |
util.types.isPromise() | 🟢 Supported |
util.types.isArrayBuffer() | 🟢 Supported |
util.types.isNativeError() | 🔴 Not supported |
util.types.isTypedArray() | 🟢 Supported |
util.types.isMap() | 🟢 Supported |
util.types.isSet() | 🟢 Supported |
util.types.isCryptoKey() | 🟢 Supported |
util.types.isKeyObject() | 🟢 Supported |
APIs marked 🟡 Partially supported have limited functionality compared to the full Node.js implementation.
util.types checks a value’s type at run time, as an alternative to instanceof. util.types.isDate(), util.types.isPromise(), and util.types.isRegExp() return true for a matching value. util.types.isNativeError() throws Error: [unenv] util.types.isNativeError is not implemented yet! in a deployed function and under azion dev.
Node.js deprecates util.inherits() from v5.0.0; use class syntax with extends instead. In Azion Runtime, EventEmitter from node:events is a class, so a constructor function that calls EventEmitter.call(this) to inherit from it throws TypeError: Class constructor e cannot be invoked without 'new'.