1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width"> 6 <meta name="nodejs.org:node-version" content="v18.20.1"> 7 <title>Util | Node.js v18.20.1 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/util.html"> 12 <script async defer src="assets/api.js" type="text/javascript"></script> 13 <style>@media(max-width:558px){.with-42-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:718px){.with-62-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:574px){.with-44-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:566px){.with-43-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:550px){.with-41-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style> 14</head> 15<body class="alt apidoc" id="api-section-util"> 16 <div id="content" class="clearfix"> 17 <div id="column2" class="interior"> 18 <div id="intro" class="interior"> 19 <a href="/" title="Go back to the home page"> 20 Node.js 21 </a> 22 </div> 23 <ul> 24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 26</ul> 27<hr class="line"> 28<ul> 29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 33<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 38<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 39<li><a href="console.html" class="nav-console">Console</a></li> 40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 45<li><a href="dns.html" class="nav-dns">DNS</a></li> 46<li><a href="domain.html" class="nav-domain">Domain</a></li> 47<li><a href="errors.html" class="nav-errors">Errors</a></li> 48<li><a href="events.html" class="nav-events">Events</a></li> 49<li><a href="fs.html" class="nav-fs">File system</a></li> 50<li><a href="globals.html" class="nav-globals">Globals</a></li> 51<li><a href="http.html" class="nav-http">HTTP</a></li> 52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 53<li><a href="https.html" class="nav-https">HTTPS</a></li> 54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 55<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 60<li><a href="net.html" class="nav-net">Net</a></li> 61<li><a href="os.html" class="nav-os">OS</a></li> 62<li><a href="path.html" class="nav-path">Path</a></li> 63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 65<li><a href="process.html" class="nav-process">Process</a></li> 66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 68<li><a href="readline.html" class="nav-readline">Readline</a></li> 69<li><a href="repl.html" class="nav-repl">REPL</a></li> 70<li><a href="report.html" class="nav-report">Report</a></li> 71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 72<li><a href="stream.html" class="nav-stream">Stream</a></li> 73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 74<li><a href="test.html" class="nav-test">Test runner</a></li> 75<li><a href="timers.html" class="nav-timers">Timers</a></li> 76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 78<li><a href="tty.html" class="nav-tty">TTY</a></li> 79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 80<li><a href="url.html" class="nav-url">URL</a></li> 81<li><a href="util.html" class="nav-util active">Utilities</a></li> 82<li><a href="v8.html" class="nav-v8">V8</a></li> 83<li><a href="vm.html" class="nav-vm">VM</a></li> 84<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 89</ul> 90<hr class="line"> 91<ul> 92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 93</ul> 94 </div> 95 96 <div id="column1" data-id="util" class="interior"> 97 <header class="header"> 98 <div class="header-container"> 99 <h1>Node.js v18.20.1 documentation</h1> 100 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 101 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 102 <path fill="none" d="M0 0h24v24H0z" /> 103 <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> 104 <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> 105 </svg> 106 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 107 <path d="M0 0h24v24H0z" fill="none" /> 108 <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> 109 </svg> 110 </button> 111 </div> 112 <div id="gtoc"> 113 <ul> 114 <li class="pinned-header">Node.js v18.20.1</li> 115 116 <li class="picker-header"> 117 <a href="#"> 118 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 119 Table of contents 120 </a> 121 122 <div class="picker"><div class="toc"><ul> 123<li><span class="stability_2"><a href="#util">Util</a></span> 124<ul> 125<li><a href="#utilcallbackifyoriginal"><code>util.callbackify(original)</code></a></li> 126<li><a href="#utildebuglogsection-callback"><code>util.debuglog(section[, callback])</code></a> 127<ul> 128<li><a href="#debuglogenabled"><code>debuglog().enabled</code></a></li> 129</ul> 130</li> 131<li><a href="#utildebugsection"><code>util.debug(section)</code></a></li> 132<li><a href="#utildeprecatefn-msg-code"><code>util.deprecate(fn, msg[, code])</code></a></li> 133<li><a href="#utilformatformat-args"><code>util.format(format[, ...args])</code></a></li> 134<li><a href="#utilformatwithoptionsinspectoptions-format-args"><code>util.formatWithOptions(inspectOptions, format[, ...args])</code></a></li> 135<li><a href="#utilgetsystemerrornameerr"><code>util.getSystemErrorName(err)</code></a></li> 136<li><a href="#utilgetsystemerrormap"><code>util.getSystemErrorMap()</code></a></li> 137<li><span class="stability_3"><a href="#utilinheritsconstructor-superconstructor"><code>util.inherits(constructor, superConstructor)</code></a></span></li> 138<li><a href="#utilinspectobject-options"><code>util.inspect(object[, options])</code></a></li> 139<li><a href="#utilinspectobject-showhidden-depth-colors"><code>util.inspect(object[, showHidden[, depth[, colors]]])</code></a> 140<ul> 141<li><a href="#customizing-utilinspect-colors">Customizing <code>util.inspect</code> colors</a> 142<ul> 143<li><a href="#modifiers">Modifiers</a></li> 144<li><a href="#foreground-colors">Foreground colors</a></li> 145<li><a href="#background-colors">Background colors</a></li> 146</ul> 147</li> 148<li><a href="#custom-inspection-functions-on-objects">Custom inspection functions on objects</a></li> 149<li><a href="#utilinspectcustom"><code>util.inspect.custom</code></a></li> 150<li><a href="#utilinspectdefaultoptions"><code>util.inspect.defaultOptions</code></a></li> 151</ul> 152</li> 153<li><a href="#utilisdeepstrictequalval1-val2"><code>util.isDeepStrictEqual(val1, val2)</code></a></li> 154<li><span class="stability_1"><a href="#class-utilmimetype">Class: <code>util.MIMEType</code></a></span> 155<ul> 156<li><a href="#constructor-new-mimetypeinput">Constructor: <code>new MIMEType(input)</code></a></li> 157<li><a href="#mimetype"><code>mime.type</code></a></li> 158<li><a href="#mimesubtype"><code>mime.subtype</code></a></li> 159<li><a href="#mimeessence"><code>mime.essence</code></a></li> 160<li><a href="#mimeparams"><code>mime.params</code></a></li> 161<li><a href="#mimetostring"><code>mime.toString()</code></a></li> 162<li><a href="#mimetojson"><code>mime.toJSON()</code></a></li> 163</ul> 164</li> 165<li><a href="#class-utilmimeparams">Class: <code>util.MIMEParams</code></a> 166<ul> 167<li><a href="#constructor-new-mimeparams">Constructor: <code>new MIMEParams()</code></a></li> 168<li><a href="#mimeparamsdeletename"><code>mimeParams.delete(name)</code></a></li> 169<li><a href="#mimeparamsentries"><code>mimeParams.entries()</code></a></li> 170<li><a href="#mimeparamsgetname"><code>mimeParams.get(name)</code></a></li> 171<li><a href="#mimeparamshasname"><code>mimeParams.has(name)</code></a></li> 172<li><a href="#mimeparamskeys"><code>mimeParams.keys()</code></a></li> 173<li><a href="#mimeparamssetname-value"><code>mimeParams.set(name, value)</code></a></li> 174<li><a href="#mimeparamsvalues"><code>mimeParams.values()</code></a></li> 175<li><a href="#mimeparamsiterator"><code>mimeParams[@@iterator]()</code></a></li> 176</ul> 177</li> 178<li><span class="stability_1"><a href="#utilparseargsconfig"><code>util.parseArgs([config])</code></a></span> 179<ul> 180<li><a href="#parseargs-tokens"><code>parseArgs</code> <code>tokens</code></a></li> 181</ul> 182</li> 183<li><a href="#utilpromisifyoriginal"><code>util.promisify(original)</code></a> 184<ul> 185<li><a href="#custom-promisified-functions">Custom promisified functions</a></li> 186<li><a href="#utilpromisifycustom"><code>util.promisify.custom</code></a></li> 187</ul> 188</li> 189<li><a href="#utilstripvtcontrolcharactersstr"><code>util.stripVTControlCharacters(str)</code></a></li> 190<li><a href="#class-utiltextdecoder">Class: <code>util.TextDecoder</code></a> 191<ul> 192<li><a href="#whatwg-supported-encodings">WHATWG supported encodings</a> 193<ul> 194<li><a href="#encodings-supported-by-default-with-full-icu-data">Encodings supported by default (with full ICU data)</a></li> 195<li><a href="#encodings-supported-when-nodejs-is-built-with-the-small-icu-option">Encodings supported when Node.js is built with the <code>small-icu</code> option</a></li> 196<li><a href="#encodings-supported-when-icu-is-disabled">Encodings supported when ICU is disabled</a></li> 197</ul> 198</li> 199<li><a href="#new-textdecoderencoding-options"><code>new TextDecoder([encoding[, options]])</code></a></li> 200<li><a href="#textdecoderdecodeinput-options"><code>textDecoder.decode([input[, options]])</code></a></li> 201<li><a href="#textdecoderencoding"><code>textDecoder.encoding</code></a></li> 202<li><a href="#textdecoderfatal"><code>textDecoder.fatal</code></a></li> 203<li><a href="#textdecoderignorebom"><code>textDecoder.ignoreBOM</code></a></li> 204</ul> 205</li> 206<li><a href="#class-utiltextencoder">Class: <code>util.TextEncoder</code></a> 207<ul> 208<li><a href="#textencoderencodeinput"><code>textEncoder.encode([input])</code></a></li> 209<li><a href="#textencoderencodeintosrc-dest"><code>textEncoder.encodeInto(src, dest)</code></a></li> 210<li><a href="#textencoderencoding"><code>textEncoder.encoding</code></a></li> 211</ul> 212</li> 213<li><a href="#utiltousvstringstring"><code>util.toUSVString(string)</code></a></li> 214<li><span class="stability_1"><a href="#utiltransferableabortcontroller"><code>util.transferableAbortController()</code></a></span></li> 215<li><span class="stability_1"><a href="#utiltransferableabortsignalsignal"><code>util.transferableAbortSignal(signal)</code></a></span></li> 216<li><span class="stability_1"><a href="#utilabortedsignal-resource"><code>util.aborted(signal, resource)</code></a></span></li> 217<li><a href="#utiltypes"><code>util.types</code></a> 218<ul> 219<li><a href="#utiltypesisanyarraybuffervalue"><code>util.types.isAnyArrayBuffer(value)</code></a></li> 220<li><a href="#utiltypesisarraybufferviewvalue"><code>util.types.isArrayBufferView(value)</code></a></li> 221<li><a href="#utiltypesisargumentsobjectvalue"><code>util.types.isArgumentsObject(value)</code></a></li> 222<li><a href="#utiltypesisarraybuffervalue"><code>util.types.isArrayBuffer(value)</code></a></li> 223<li><a href="#utiltypesisasyncfunctionvalue"><code>util.types.isAsyncFunction(value)</code></a></li> 224<li><a href="#utiltypesisbigint64arrayvalue"><code>util.types.isBigInt64Array(value)</code></a></li> 225<li><a href="#utiltypesisbiguint64arrayvalue"><code>util.types.isBigUint64Array(value)</code></a></li> 226<li><a href="#utiltypesisbooleanobjectvalue"><code>util.types.isBooleanObject(value)</code></a></li> 227<li><a href="#utiltypesisboxedprimitivevalue"><code>util.types.isBoxedPrimitive(value)</code></a></li> 228<li><a href="#utiltypesiscryptokeyvalue"><code>util.types.isCryptoKey(value)</code></a></li> 229<li><a href="#utiltypesisdataviewvalue"><code>util.types.isDataView(value)</code></a></li> 230<li><a href="#utiltypesisdatevalue"><code>util.types.isDate(value)</code></a></li> 231<li><a href="#utiltypesisexternalvalue"><code>util.types.isExternal(value)</code></a></li> 232<li><a href="#utiltypesisfloat32arrayvalue"><code>util.types.isFloat32Array(value)</code></a></li> 233<li><a href="#utiltypesisfloat64arrayvalue"><code>util.types.isFloat64Array(value)</code></a></li> 234<li><a href="#utiltypesisgeneratorfunctionvalue"><code>util.types.isGeneratorFunction(value)</code></a></li> 235<li><a href="#utiltypesisgeneratorobjectvalue"><code>util.types.isGeneratorObject(value)</code></a></li> 236<li><a href="#utiltypesisint8arrayvalue"><code>util.types.isInt8Array(value)</code></a></li> 237<li><a href="#utiltypesisint16arrayvalue"><code>util.types.isInt16Array(value)</code></a></li> 238<li><a href="#utiltypesisint32arrayvalue"><code>util.types.isInt32Array(value)</code></a></li> 239<li><a href="#utiltypesiskeyobjectvalue"><code>util.types.isKeyObject(value)</code></a></li> 240<li><a href="#utiltypesismapvalue"><code>util.types.isMap(value)</code></a></li> 241<li><a href="#utiltypesismapiteratorvalue"><code>util.types.isMapIterator(value)</code></a></li> 242<li><a href="#utiltypesismodulenamespaceobjectvalue"><code>util.types.isModuleNamespaceObject(value)</code></a></li> 243<li><a href="#utiltypesisnativeerrorvalue"><code>util.types.isNativeError(value)</code></a></li> 244<li><a href="#utiltypesisnumberobjectvalue"><code>util.types.isNumberObject(value)</code></a></li> 245<li><a href="#utiltypesispromisevalue"><code>util.types.isPromise(value)</code></a></li> 246<li><a href="#utiltypesisproxyvalue"><code>util.types.isProxy(value)</code></a></li> 247<li><a href="#utiltypesisregexpvalue"><code>util.types.isRegExp(value)</code></a></li> 248<li><a href="#utiltypesissetvalue"><code>util.types.isSet(value)</code></a></li> 249<li><a href="#utiltypesissetiteratorvalue"><code>util.types.isSetIterator(value)</code></a></li> 250<li><a href="#utiltypesissharedarraybuffervalue"><code>util.types.isSharedArrayBuffer(value)</code></a></li> 251<li><a href="#utiltypesisstringobjectvalue"><code>util.types.isStringObject(value)</code></a></li> 252<li><a href="#utiltypesissymbolobjectvalue"><code>util.types.isSymbolObject(value)</code></a></li> 253<li><a href="#utiltypesistypedarrayvalue"><code>util.types.isTypedArray(value)</code></a></li> 254<li><a href="#utiltypesisuint8arrayvalue"><code>util.types.isUint8Array(value)</code></a></li> 255<li><a href="#utiltypesisuint8clampedarrayvalue"><code>util.types.isUint8ClampedArray(value)</code></a></li> 256<li><a href="#utiltypesisuint16arrayvalue"><code>util.types.isUint16Array(value)</code></a></li> 257<li><a href="#utiltypesisuint32arrayvalue"><code>util.types.isUint32Array(value)</code></a></li> 258<li><a href="#utiltypesisweakmapvalue"><code>util.types.isWeakMap(value)</code></a></li> 259<li><a href="#utiltypesisweaksetvalue"><code>util.types.isWeakSet(value)</code></a></li> 260<li><span class="stability_0"><a href="#utiltypesiswebassemblycompiledmodulevalue"><code>util.types.isWebAssemblyCompiledModule(value)</code></a></span></li> 261</ul> 262</li> 263<li><a href="#deprecated-apis">Deprecated APIs</a> 264<ul> 265<li><span class="stability_0"><a href="#util_extendtarget-source"><code>util._extend(target, source)</code></a></span></li> 266<li><span class="stability_0"><a href="#utilisarrayobject"><code>util.isArray(object)</code></a></span></li> 267<li><span class="stability_0"><a href="#utilisbooleanobject"><code>util.isBoolean(object)</code></a></span></li> 268<li><span class="stability_0"><a href="#utilisbufferobject"><code>util.isBuffer(object)</code></a></span></li> 269<li><span class="stability_0"><a href="#utilisdateobject"><code>util.isDate(object)</code></a></span></li> 270<li><span class="stability_0"><a href="#utiliserrorobject"><code>util.isError(object)</code></a></span></li> 271<li><span class="stability_0"><a href="#utilisfunctionobject"><code>util.isFunction(object)</code></a></span></li> 272<li><span class="stability_0"><a href="#utilisnullobject"><code>util.isNull(object)</code></a></span></li> 273<li><span class="stability_0"><a href="#utilisnullorundefinedobject"><code>util.isNullOrUndefined(object)</code></a></span></li> 274<li><span class="stability_0"><a href="#utilisnumberobject"><code>util.isNumber(object)</code></a></span></li> 275<li><span class="stability_0"><a href="#utilisobjectobject"><code>util.isObject(object)</code></a></span></li> 276<li><span class="stability_0"><a href="#utilisprimitiveobject"><code>util.isPrimitive(object)</code></a></span></li> 277<li><span class="stability_0"><a href="#utilisregexpobject"><code>util.isRegExp(object)</code></a></span></li> 278<li><span class="stability_0"><a href="#utilisstringobject"><code>util.isString(object)</code></a></span></li> 279<li><span class="stability_0"><a href="#utilissymbolobject"><code>util.isSymbol(object)</code></a></span></li> 280<li><span class="stability_0"><a href="#utilisundefinedobject"><code>util.isUndefined(object)</code></a></span></li> 281<li><span class="stability_0"><a href="#utillogstring"><code>util.log(string)</code></a></span></li> 282</ul> 283</li> 284</ul> 285</li> 286</ul></div></div> 287 </li> 288 289 290 <li class="picker-header"> 291 <a href="#"> 292 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 293 Index 294 </a> 295 296 <div class="picker"><ul> 297<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 298<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 299 300 <li> 301 <a href="index.html">Index</a> 302 </li> 303 </ul> 304 305<hr class="line"> 306<ul> 307<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 308<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 309<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 310<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 311<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 312<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 313<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 314<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 315<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 316<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 317<li><a href="console.html" class="nav-console">Console</a></li> 318<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 319<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 320<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 321<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 322<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 323<li><a href="dns.html" class="nav-dns">DNS</a></li> 324<li><a href="domain.html" class="nav-domain">Domain</a></li> 325<li><a href="errors.html" class="nav-errors">Errors</a></li> 326<li><a href="events.html" class="nav-events">Events</a></li> 327<li><a href="fs.html" class="nav-fs">File system</a></li> 328<li><a href="globals.html" class="nav-globals">Globals</a></li> 329<li><a href="http.html" class="nav-http">HTTP</a></li> 330<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 331<li><a href="https.html" class="nav-https">HTTPS</a></li> 332<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 333<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 334<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 335<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 336<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 337<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 338<li><a href="net.html" class="nav-net">Net</a></li> 339<li><a href="os.html" class="nav-os">OS</a></li> 340<li><a href="path.html" class="nav-path">Path</a></li> 341<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 342<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 343<li><a href="process.html" class="nav-process">Process</a></li> 344<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 345<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 346<li><a href="readline.html" class="nav-readline">Readline</a></li> 347<li><a href="repl.html" class="nav-repl">REPL</a></li> 348<li><a href="report.html" class="nav-report">Report</a></li> 349<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 350<li><a href="stream.html" class="nav-stream">Stream</a></li> 351<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 352<li><a href="test.html" class="nav-test">Test runner</a></li> 353<li><a href="timers.html" class="nav-timers">Timers</a></li> 354<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 355<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 356<li><a href="tty.html" class="nav-tty">TTY</a></li> 357<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 358<li><a href="url.html" class="nav-url">URL</a></li> 359<li><a href="util.html" class="nav-util active">Utilities</a></li> 360<li><a href="v8.html" class="nav-v8">V8</a></li> 361<li><a href="vm.html" class="nav-vm">VM</a></li> 362<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 363<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 364<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 365<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 366<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 367</ul> 368<hr class="line"> 369<ul> 370<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 371</ul></div> 372 </li> 373 374 375 <li class="picker-header"> 376 <a href="#"> 377 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 378 Other versions 379 </a> 380 <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/util.html">21.x</a></li> 381<li><a href="https://nodejs.org/docs/latest-v20.x/api/util.html">20.x <b>LTS</b></a></li> 382<li><a href="https://nodejs.org/docs/latest-v19.x/api/util.html">19.x</a></li> 383<li><a href="https://nodejs.org/docs/latest-v18.x/api/util.html">18.x <b>LTS</b></a></li> 384<li><a href="https://nodejs.org/docs/latest-v17.x/api/util.html">17.x</a></li> 385<li><a href="https://nodejs.org/docs/latest-v16.x/api/util.html">16.x</a></li> 386<li><a href="https://nodejs.org/docs/latest-v15.x/api/util.html">15.x</a></li> 387<li><a href="https://nodejs.org/docs/latest-v14.x/api/util.html">14.x</a></li> 388<li><a href="https://nodejs.org/docs/latest-v13.x/api/util.html">13.x</a></li> 389<li><a href="https://nodejs.org/docs/latest-v12.x/api/util.html">12.x</a></li> 390<li><a href="https://nodejs.org/docs/latest-v11.x/api/util.html">11.x</a></li> 391<li><a href="https://nodejs.org/docs/latest-v10.x/api/util.html">10.x</a></li> 392<li><a href="https://nodejs.org/docs/latest-v9.x/api/util.html">9.x</a></li> 393<li><a href="https://nodejs.org/docs/latest-v8.x/api/util.html">8.x</a></li> 394<li><a href="https://nodejs.org/docs/latest-v7.x/api/util.html">7.x</a></li> 395<li><a href="https://nodejs.org/docs/latest-v6.x/api/util.html">6.x</a></li> 396<li><a href="https://nodejs.org/docs/latest-v5.x/api/util.html">5.x</a></li> 397<li><a href="https://nodejs.org/docs/latest-v4.x/api/util.html">4.x</a></li> 398<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/util.html">0.12.x</a></li> 399<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/util.html">0.10.x</a></li></ol></div> 400 </li> 401 402 <li class="picker-header"> 403 <a href="#"> 404 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 405 Options 406 </a> 407 408 <div class="picker"> 409 <ul> 410 <li> 411 <a href="all.html">View on single page</a> 412 </li> 413 <li> 414 <a href="util.json">View as JSON</a> 415 </li> 416 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/util.md">Edit on GitHub</a></li> 417 </ul> 418 </div> 419 </li> 420 </ul> 421 </div> 422 <hr> 423 </header> 424 425 <details id="toc" open><summary>Table of contents</summary><ul> 426<li><span class="stability_2"><a href="#util">Util</a></span> 427<ul> 428<li><a href="#utilcallbackifyoriginal"><code>util.callbackify(original)</code></a></li> 429<li><a href="#utildebuglogsection-callback"><code>util.debuglog(section[, callback])</code></a> 430<ul> 431<li><a href="#debuglogenabled"><code>debuglog().enabled</code></a></li> 432</ul> 433</li> 434<li><a href="#utildebugsection"><code>util.debug(section)</code></a></li> 435<li><a href="#utildeprecatefn-msg-code"><code>util.deprecate(fn, msg[, code])</code></a></li> 436<li><a href="#utilformatformat-args"><code>util.format(format[, ...args])</code></a></li> 437<li><a href="#utilformatwithoptionsinspectoptions-format-args"><code>util.formatWithOptions(inspectOptions, format[, ...args])</code></a></li> 438<li><a href="#utilgetsystemerrornameerr"><code>util.getSystemErrorName(err)</code></a></li> 439<li><a href="#utilgetsystemerrormap"><code>util.getSystemErrorMap()</code></a></li> 440<li><span class="stability_3"><a href="#utilinheritsconstructor-superconstructor"><code>util.inherits(constructor, superConstructor)</code></a></span></li> 441<li><a href="#utilinspectobject-options"><code>util.inspect(object[, options])</code></a></li> 442<li><a href="#utilinspectobject-showhidden-depth-colors"><code>util.inspect(object[, showHidden[, depth[, colors]]])</code></a> 443<ul> 444<li><a href="#customizing-utilinspect-colors">Customizing <code>util.inspect</code> colors</a> 445<ul> 446<li><a href="#modifiers">Modifiers</a></li> 447<li><a href="#foreground-colors">Foreground colors</a></li> 448<li><a href="#background-colors">Background colors</a></li> 449</ul> 450</li> 451<li><a href="#custom-inspection-functions-on-objects">Custom inspection functions on objects</a></li> 452<li><a href="#utilinspectcustom"><code>util.inspect.custom</code></a></li> 453<li><a href="#utilinspectdefaultoptions"><code>util.inspect.defaultOptions</code></a></li> 454</ul> 455</li> 456<li><a href="#utilisdeepstrictequalval1-val2"><code>util.isDeepStrictEqual(val1, val2)</code></a></li> 457<li><span class="stability_1"><a href="#class-utilmimetype">Class: <code>util.MIMEType</code></a></span> 458<ul> 459<li><a href="#constructor-new-mimetypeinput">Constructor: <code>new MIMEType(input)</code></a></li> 460<li><a href="#mimetype"><code>mime.type</code></a></li> 461<li><a href="#mimesubtype"><code>mime.subtype</code></a></li> 462<li><a href="#mimeessence"><code>mime.essence</code></a></li> 463<li><a href="#mimeparams"><code>mime.params</code></a></li> 464<li><a href="#mimetostring"><code>mime.toString()</code></a></li> 465<li><a href="#mimetojson"><code>mime.toJSON()</code></a></li> 466</ul> 467</li> 468<li><a href="#class-utilmimeparams">Class: <code>util.MIMEParams</code></a> 469<ul> 470<li><a href="#constructor-new-mimeparams">Constructor: <code>new MIMEParams()</code></a></li> 471<li><a href="#mimeparamsdeletename"><code>mimeParams.delete(name)</code></a></li> 472<li><a href="#mimeparamsentries"><code>mimeParams.entries()</code></a></li> 473<li><a href="#mimeparamsgetname"><code>mimeParams.get(name)</code></a></li> 474<li><a href="#mimeparamshasname"><code>mimeParams.has(name)</code></a></li> 475<li><a href="#mimeparamskeys"><code>mimeParams.keys()</code></a></li> 476<li><a href="#mimeparamssetname-value"><code>mimeParams.set(name, value)</code></a></li> 477<li><a href="#mimeparamsvalues"><code>mimeParams.values()</code></a></li> 478<li><a href="#mimeparamsiterator"><code>mimeParams[@@iterator]()</code></a></li> 479</ul> 480</li> 481<li><span class="stability_1"><a href="#utilparseargsconfig"><code>util.parseArgs([config])</code></a></span> 482<ul> 483<li><a href="#parseargs-tokens"><code>parseArgs</code> <code>tokens</code></a></li> 484</ul> 485</li> 486<li><a href="#utilpromisifyoriginal"><code>util.promisify(original)</code></a> 487<ul> 488<li><a href="#custom-promisified-functions">Custom promisified functions</a></li> 489<li><a href="#utilpromisifycustom"><code>util.promisify.custom</code></a></li> 490</ul> 491</li> 492<li><a href="#utilstripvtcontrolcharactersstr"><code>util.stripVTControlCharacters(str)</code></a></li> 493<li><a href="#class-utiltextdecoder">Class: <code>util.TextDecoder</code></a> 494<ul> 495<li><a href="#whatwg-supported-encodings">WHATWG supported encodings</a> 496<ul> 497<li><a href="#encodings-supported-by-default-with-full-icu-data">Encodings supported by default (with full ICU data)</a></li> 498<li><a href="#encodings-supported-when-nodejs-is-built-with-the-small-icu-option">Encodings supported when Node.js is built with the <code>small-icu</code> option</a></li> 499<li><a href="#encodings-supported-when-icu-is-disabled">Encodings supported when ICU is disabled</a></li> 500</ul> 501</li> 502<li><a href="#new-textdecoderencoding-options"><code>new TextDecoder([encoding[, options]])</code></a></li> 503<li><a href="#textdecoderdecodeinput-options"><code>textDecoder.decode([input[, options]])</code></a></li> 504<li><a href="#textdecoderencoding"><code>textDecoder.encoding</code></a></li> 505<li><a href="#textdecoderfatal"><code>textDecoder.fatal</code></a></li> 506<li><a href="#textdecoderignorebom"><code>textDecoder.ignoreBOM</code></a></li> 507</ul> 508</li> 509<li><a href="#class-utiltextencoder">Class: <code>util.TextEncoder</code></a> 510<ul> 511<li><a href="#textencoderencodeinput"><code>textEncoder.encode([input])</code></a></li> 512<li><a href="#textencoderencodeintosrc-dest"><code>textEncoder.encodeInto(src, dest)</code></a></li> 513<li><a href="#textencoderencoding"><code>textEncoder.encoding</code></a></li> 514</ul> 515</li> 516<li><a href="#utiltousvstringstring"><code>util.toUSVString(string)</code></a></li> 517<li><span class="stability_1"><a href="#utiltransferableabortcontroller"><code>util.transferableAbortController()</code></a></span></li> 518<li><span class="stability_1"><a href="#utiltransferableabortsignalsignal"><code>util.transferableAbortSignal(signal)</code></a></span></li> 519<li><span class="stability_1"><a href="#utilabortedsignal-resource"><code>util.aborted(signal, resource)</code></a></span></li> 520<li><a href="#utiltypes"><code>util.types</code></a> 521<ul> 522<li><a href="#utiltypesisanyarraybuffervalue"><code>util.types.isAnyArrayBuffer(value)</code></a></li> 523<li><a href="#utiltypesisarraybufferviewvalue"><code>util.types.isArrayBufferView(value)</code></a></li> 524<li><a href="#utiltypesisargumentsobjectvalue"><code>util.types.isArgumentsObject(value)</code></a></li> 525<li><a href="#utiltypesisarraybuffervalue"><code>util.types.isArrayBuffer(value)</code></a></li> 526<li><a href="#utiltypesisasyncfunctionvalue"><code>util.types.isAsyncFunction(value)</code></a></li> 527<li><a href="#utiltypesisbigint64arrayvalue"><code>util.types.isBigInt64Array(value)</code></a></li> 528<li><a href="#utiltypesisbiguint64arrayvalue"><code>util.types.isBigUint64Array(value)</code></a></li> 529<li><a href="#utiltypesisbooleanobjectvalue"><code>util.types.isBooleanObject(value)</code></a></li> 530<li><a href="#utiltypesisboxedprimitivevalue"><code>util.types.isBoxedPrimitive(value)</code></a></li> 531<li><a href="#utiltypesiscryptokeyvalue"><code>util.types.isCryptoKey(value)</code></a></li> 532<li><a href="#utiltypesisdataviewvalue"><code>util.types.isDataView(value)</code></a></li> 533<li><a href="#utiltypesisdatevalue"><code>util.types.isDate(value)</code></a></li> 534<li><a href="#utiltypesisexternalvalue"><code>util.types.isExternal(value)</code></a></li> 535<li><a href="#utiltypesisfloat32arrayvalue"><code>util.types.isFloat32Array(value)</code></a></li> 536<li><a href="#utiltypesisfloat64arrayvalue"><code>util.types.isFloat64Array(value)</code></a></li> 537<li><a href="#utiltypesisgeneratorfunctionvalue"><code>util.types.isGeneratorFunction(value)</code></a></li> 538<li><a href="#utiltypesisgeneratorobjectvalue"><code>util.types.isGeneratorObject(value)</code></a></li> 539<li><a href="#utiltypesisint8arrayvalue"><code>util.types.isInt8Array(value)</code></a></li> 540<li><a href="#utiltypesisint16arrayvalue"><code>util.types.isInt16Array(value)</code></a></li> 541<li><a href="#utiltypesisint32arrayvalue"><code>util.types.isInt32Array(value)</code></a></li> 542<li><a href="#utiltypesiskeyobjectvalue"><code>util.types.isKeyObject(value)</code></a></li> 543<li><a href="#utiltypesismapvalue"><code>util.types.isMap(value)</code></a></li> 544<li><a href="#utiltypesismapiteratorvalue"><code>util.types.isMapIterator(value)</code></a></li> 545<li><a href="#utiltypesismodulenamespaceobjectvalue"><code>util.types.isModuleNamespaceObject(value)</code></a></li> 546<li><a href="#utiltypesisnativeerrorvalue"><code>util.types.isNativeError(value)</code></a></li> 547<li><a href="#utiltypesisnumberobjectvalue"><code>util.types.isNumberObject(value)</code></a></li> 548<li><a href="#utiltypesispromisevalue"><code>util.types.isPromise(value)</code></a></li> 549<li><a href="#utiltypesisproxyvalue"><code>util.types.isProxy(value)</code></a></li> 550<li><a href="#utiltypesisregexpvalue"><code>util.types.isRegExp(value)</code></a></li> 551<li><a href="#utiltypesissetvalue"><code>util.types.isSet(value)</code></a></li> 552<li><a href="#utiltypesissetiteratorvalue"><code>util.types.isSetIterator(value)</code></a></li> 553<li><a href="#utiltypesissharedarraybuffervalue"><code>util.types.isSharedArrayBuffer(value)</code></a></li> 554<li><a href="#utiltypesisstringobjectvalue"><code>util.types.isStringObject(value)</code></a></li> 555<li><a href="#utiltypesissymbolobjectvalue"><code>util.types.isSymbolObject(value)</code></a></li> 556<li><a href="#utiltypesistypedarrayvalue"><code>util.types.isTypedArray(value)</code></a></li> 557<li><a href="#utiltypesisuint8arrayvalue"><code>util.types.isUint8Array(value)</code></a></li> 558<li><a href="#utiltypesisuint8clampedarrayvalue"><code>util.types.isUint8ClampedArray(value)</code></a></li> 559<li><a href="#utiltypesisuint16arrayvalue"><code>util.types.isUint16Array(value)</code></a></li> 560<li><a href="#utiltypesisuint32arrayvalue"><code>util.types.isUint32Array(value)</code></a></li> 561<li><a href="#utiltypesisweakmapvalue"><code>util.types.isWeakMap(value)</code></a></li> 562<li><a href="#utiltypesisweaksetvalue"><code>util.types.isWeakSet(value)</code></a></li> 563<li><span class="stability_0"><a href="#utiltypesiswebassemblycompiledmodulevalue"><code>util.types.isWebAssemblyCompiledModule(value)</code></a></span></li> 564</ul> 565</li> 566<li><a href="#deprecated-apis">Deprecated APIs</a> 567<ul> 568<li><span class="stability_0"><a href="#util_extendtarget-source"><code>util._extend(target, source)</code></a></span></li> 569<li><span class="stability_0"><a href="#utilisarrayobject"><code>util.isArray(object)</code></a></span></li> 570<li><span class="stability_0"><a href="#utilisbooleanobject"><code>util.isBoolean(object)</code></a></span></li> 571<li><span class="stability_0"><a href="#utilisbufferobject"><code>util.isBuffer(object)</code></a></span></li> 572<li><span class="stability_0"><a href="#utilisdateobject"><code>util.isDate(object)</code></a></span></li> 573<li><span class="stability_0"><a href="#utiliserrorobject"><code>util.isError(object)</code></a></span></li> 574<li><span class="stability_0"><a href="#utilisfunctionobject"><code>util.isFunction(object)</code></a></span></li> 575<li><span class="stability_0"><a href="#utilisnullobject"><code>util.isNull(object)</code></a></span></li> 576<li><span class="stability_0"><a href="#utilisnullorundefinedobject"><code>util.isNullOrUndefined(object)</code></a></span></li> 577<li><span class="stability_0"><a href="#utilisnumberobject"><code>util.isNumber(object)</code></a></span></li> 578<li><span class="stability_0"><a href="#utilisobjectobject"><code>util.isObject(object)</code></a></span></li> 579<li><span class="stability_0"><a href="#utilisprimitiveobject"><code>util.isPrimitive(object)</code></a></span></li> 580<li><span class="stability_0"><a href="#utilisregexpobject"><code>util.isRegExp(object)</code></a></span></li> 581<li><span class="stability_0"><a href="#utilisstringobject"><code>util.isString(object)</code></a></span></li> 582<li><span class="stability_0"><a href="#utilissymbolobject"><code>util.isSymbol(object)</code></a></span></li> 583<li><span class="stability_0"><a href="#utilisundefinedobject"><code>util.isUndefined(object)</code></a></span></li> 584<li><span class="stability_0"><a href="#utillogstring"><code>util.log(string)</code></a></span></li> 585</ul> 586</li> 587</ul> 588</li> 589</ul></details> 590 591 <div id="apicontent"> 592 <h2>Util<span><a class="mark" href="#util" id="util">#</a></span><a aria-hidden="true" class="legacy" id="util_util"></a></h2> 593 594<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 595<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/util.js">lib/util.js</a></p> 596<p>The <code>node:util</code> module supports the needs of Node.js internal APIs. Many of the 597utilities are useful for application and module developers as well. To access 598it:</p> 599<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>);</code> <button class="copy-button">copy</button></pre> 600<section><h3><code>util.callbackify(original)</code><span><a class="mark" href="#utilcallbackifyoriginal" id="utilcallbackifyoriginal">#</a></span><a aria-hidden="true" class="legacy" id="util_util_callbackify_original"></a></h3> 601<div class="api_metadata"> 602<span>Added in: v8.2.0</span> 603</div> 604<ul> 605<li><code>original</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> An <code>async</code> function</li> 606<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> a callback style function</li> 607</ul> 608<p>Takes an <code>async</code> function (or a function that returns a <code>Promise</code>) and returns a 609function following the error-first callback style, i.e. taking 610an <code>(err, value) => ...</code> callback as the last argument. In the callback, the 611first argument will be the rejection reason (or <code>null</code> if the <code>Promise</code> 612resolved), and the second argument will be the resolved value.</p> 613<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 614 615<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">fn</span>(<span class="hljs-params"></span>) { 616 <span class="hljs-keyword">return</span> <span class="hljs-string">'hello world'</span>; 617} 618<span class="hljs-keyword">const</span> callbackFunction = util.<span class="hljs-title function_">callbackify</span>(fn); 619 620<span class="hljs-title function_">callbackFunction</span>(<span class="hljs-function">(<span class="hljs-params">err, ret</span>) =></span> { 621 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 622 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(ret); 623});</code> <button class="copy-button">copy</button></pre> 624<p>Will print:</p> 625<pre><code class="language-text">hello world</code> <button class="copy-button">copy</button></pre> 626<p>The callback is executed asynchronously, and will have a limited stack trace. 627If the callback throws, the process will emit an <a href="process.html#event-uncaughtexception"><code>'uncaughtException'</code></a> 628event, and if not handled will exit.</p> 629<p>Since <code>null</code> has a special meaning as the first argument to a callback, if a 630wrapped function rejects a <code>Promise</code> with a falsy value as a reason, the value 631is wrapped in an <code>Error</code> with the original value stored in a field named 632<code>reason</code>.</p> 633<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">fn</span>(<span class="hljs-params"></span>) { 634 <span class="hljs-keyword">return</span> <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">reject</span>(<span class="hljs-literal">null</span>); 635} 636<span class="hljs-keyword">const</span> callbackFunction = util.<span class="hljs-title function_">callbackify</span>(fn); 637 638<span class="hljs-title function_">callbackFunction</span>(<span class="hljs-function">(<span class="hljs-params">err, ret</span>) =></span> { 639 <span class="hljs-comment">// When the Promise was rejected with `null` it is wrapped with an Error and</span> 640 <span class="hljs-comment">// the original value is stored in `reason`.</span> 641 err && <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">hasOwn</span>(err, <span class="hljs-string">'reason'</span>) && err.<span class="hljs-property">reason</span> === <span class="hljs-literal">null</span>; <span class="hljs-comment">// true</span> 642});</code> <button class="copy-button">copy</button></pre> 643</section><section><h3><code>util.debuglog(section[, callback])</code><span><a class="mark" href="#utildebuglogsection-callback" id="utildebuglogsection-callback">#</a></span><a aria-hidden="true" class="legacy" id="util_util_debuglog_section_callback"></a></h3> 644<div class="api_metadata"> 645<span>Added in: v0.11.3</span> 646</div> 647<ul> 648<li><code>section</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A string identifying the portion of the application for 649which the <code>debuglog</code> function is being created.</li> 650<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A callback invoked the first time the logging function 651is called with a function argument that is a more optimized logging function.</li> 652<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The logging function</li> 653</ul> 654<p>The <code>util.debuglog()</code> method is used to create a function that conditionally 655writes debug messages to <code>stderr</code> based on the existence of the <code>NODE_DEBUG</code> 656environment variable. If the <code>section</code> name appears within the value of that 657environment variable, then the returned function operates similar to 658<a href="console.html#consoleerrordata-args"><code>console.error()</code></a>. If not, then the returned function is a no-op.</p> 659<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 660<span class="hljs-keyword">const</span> debuglog = util.<span class="hljs-title function_">debuglog</span>(<span class="hljs-string">'foo'</span>); 661 662<span class="hljs-title function_">debuglog</span>(<span class="hljs-string">'hello from foo [%d]'</span>, <span class="hljs-number">123</span>);</code> <button class="copy-button">copy</button></pre> 663<p>If this program is run with <code>NODE_DEBUG=foo</code> in the environment, then 664it will output something like:</p> 665<pre><code class="language-console">FOO 3245: hello from foo [123]</code> <button class="copy-button">copy</button></pre> 666<p>where <code>3245</code> is the process id. If it is not run with that 667environment variable set, then it will not print anything.</p> 668<p>The <code>section</code> supports wildcard also:</p> 669<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 670<span class="hljs-keyword">const</span> debuglog = util.<span class="hljs-title function_">debuglog</span>(<span class="hljs-string">'foo-bar'</span>); 671 672<span class="hljs-title function_">debuglog</span>(<span class="hljs-string">'hi there, it\'s foo-bar [%d]'</span>, <span class="hljs-number">2333</span>);</code> <button class="copy-button">copy</button></pre> 673<p>if it is run with <code>NODE_DEBUG=foo*</code> in the environment, then it will output 674something like:</p> 675<pre><code class="language-console">FOO-BAR 3257: hi there, it's foo-bar [2333]</code> <button class="copy-button">copy</button></pre> 676<p>Multiple comma-separated <code>section</code> names may be specified in the <code>NODE_DEBUG</code> 677environment variable: <code>NODE_DEBUG=fs,net,tls</code>.</p> 678<p>The optional <code>callback</code> argument can be used to replace the logging function 679with a different function that doesn't have any initialization or 680unnecessary wrapping.</p> 681<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 682<span class="hljs-keyword">let</span> debuglog = util.<span class="hljs-title function_">debuglog</span>(<span class="hljs-string">'internals'</span>, <span class="hljs-function">(<span class="hljs-params">debug</span>) =></span> { 683 <span class="hljs-comment">// Replace with a logging function that optimizes out</span> 684 <span class="hljs-comment">// testing if the section is enabled</span> 685 debuglog = debug; 686});</code> <button class="copy-button">copy</button></pre> 687<h4><code>debuglog().enabled</code><span><a class="mark" href="#debuglogenabled" id="debuglogenabled">#</a></span><a aria-hidden="true" class="legacy" id="util_debuglog_enabled"></a></h4> 688<div class="api_metadata"> 689<span>Added in: v14.9.0</span> 690</div> 691<ul> 692<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 693</ul> 694<p>The <code>util.debuglog().enabled</code> getter is used to create a test that can be used 695in conditionals based on the existence of the <code>NODE_DEBUG</code> environment variable. 696If the <code>section</code> name appears within the value of that environment variable, 697then the returned value will be <code>true</code>. If not, then the returned value will be 698<code>false</code>.</p> 699<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 700<span class="hljs-keyword">const</span> enabled = util.<span class="hljs-title function_">debuglog</span>(<span class="hljs-string">'foo'</span>).<span class="hljs-property">enabled</span>; 701<span class="hljs-keyword">if</span> (enabled) { 702 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'hello from foo [%d]'</span>, <span class="hljs-number">123</span>); 703}</code> <button class="copy-button">copy</button></pre> 704<p>If this program is run with <code>NODE_DEBUG=foo</code> in the environment, then it will 705output something like:</p> 706<pre><code class="language-console">hello from foo [123]</code> <button class="copy-button">copy</button></pre> 707</section><section><h3><code>util.debug(section)</code><span><a class="mark" href="#utildebugsection" id="utildebugsection">#</a></span><a aria-hidden="true" class="legacy" id="util_util_debug_section"></a></h3> 708<div class="api_metadata"> 709<span>Added in: v14.9.0</span> 710</div> 711<p>Alias for <code>util.debuglog</code>. Usage allows for readability of that doesn't imply 712logging when only using <code>util.debuglog().enabled</code>.</p> 713</section><section><h3><code>util.deprecate(fn, msg[, code])</code><span><a class="mark" href="#utildeprecatefn-msg-code" id="utildeprecatefn-msg-code">#</a></span><a aria-hidden="true" class="legacy" id="util_util_deprecate_fn_msg_code"></a></h3> 714<div class="api_metadata"> 715<details class="changelog"><summary>History</summary> 716<table> 717<tbody><tr><th>Version</th><th>Changes</th></tr> 718<tr><td>v10.0.0</td> 719<td><p>Deprecation warnings are only emitted once for each code.</p></td></tr> 720<tr><td>v0.8.0</td> 721<td><p><span>Added in: v0.8.0</span></p></td></tr> 722</tbody></table> 723</details> 724</div> 725<ul> 726<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The function that is being deprecated.</li> 727<li><code>msg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A warning message to display when the deprecated function is 728invoked.</li> 729<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A deprecation code. See the <a href="deprecations.html#list-of-deprecated-apis">list of deprecated APIs</a> for a 730list of codes.</li> 731<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The deprecated function wrapped to emit a warning.</li> 732</ul> 733<p>The <code>util.deprecate()</code> method wraps <code>fn</code> (which may be a function or class) in 734such a way that it is marked as deprecated.</p> 735<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 736 737<span class="hljs-built_in">exports</span>.<span class="hljs-property">obsoleteFunction</span> = util.<span class="hljs-title function_">deprecate</span>(<span class="hljs-function">() =></span> { 738 <span class="hljs-comment">// Do something here.</span> 739}, <span class="hljs-string">'obsoleteFunction() is deprecated. Use newShinyFunction() instead.'</span>);</code> <button class="copy-button">copy</button></pre> 740<p>When called, <code>util.deprecate()</code> will return a function that will emit a 741<code>DeprecationWarning</code> using the <a href="process.html#event-warning"><code>'warning'</code></a> event. The warning will 742be emitted and printed to <code>stderr</code> the first time the returned function is 743called. After the warning is emitted, the wrapped function is called without 744emitting a warning.</p> 745<p>If the same optional <code>code</code> is supplied in multiple calls to <code>util.deprecate()</code>, 746the warning will be emitted only once for that <code>code</code>.</p> 747<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 748 749<span class="hljs-keyword">const</span> fn1 = util.<span class="hljs-title function_">deprecate</span>(someFunction, someMessage, <span class="hljs-string">'DEP0001'</span>); 750<span class="hljs-keyword">const</span> fn2 = util.<span class="hljs-title function_">deprecate</span>(someOtherFunction, someOtherMessage, <span class="hljs-string">'DEP0001'</span>); 751<span class="hljs-title function_">fn1</span>(); <span class="hljs-comment">// Emits a deprecation warning with code DEP0001</span> 752<span class="hljs-title function_">fn2</span>(); <span class="hljs-comment">// Does not emit a deprecation warning because it has the same code</span></code> <button class="copy-button">copy</button></pre> 753<p>If either the <code>--no-deprecation</code> or <code>--no-warnings</code> command-line flags are 754used, or if the <code>process.noDeprecation</code> property is set to <code>true</code> <em>prior</em> to 755the first deprecation warning, the <code>util.deprecate()</code> method does nothing.</p> 756<p>If the <code>--trace-deprecation</code> or <code>--trace-warnings</code> command-line flags are set, 757or the <code>process.traceDeprecation</code> property is set to <code>true</code>, a warning and a 758stack trace are printed to <code>stderr</code> the first time the deprecated function is 759called.</p> 760<p>If the <code>--throw-deprecation</code> command-line flag is set, or the 761<code>process.throwDeprecation</code> property is set to <code>true</code>, then an exception will be 762thrown when the deprecated function is called.</p> 763<p>The <code>--throw-deprecation</code> command-line flag and <code>process.throwDeprecation</code> 764property take precedence over <code>--trace-deprecation</code> and 765<code>process.traceDeprecation</code>.</p> 766</section><section><h3><code>util.format(format[, ...args])</code><span><a class="mark" href="#utilformatformat-args" id="utilformatformat-args">#</a></span><a aria-hidden="true" class="legacy" id="util_util_format_format_args"></a></h3> 767<div class="api_metadata"> 768<details class="changelog"><summary>History</summary> 769<table> 770<tbody><tr><th>Version</th><th>Changes</th></tr> 771<tr><td>v12.11.0</td> 772<td><p>The <code>%c</code> specifier is ignored now.</p></td></tr> 773<tr><td>v12.0.0</td> 774<td><p>The <code>format</code> argument is now only taken as such if it actually contains format specifiers.</p></td></tr> 775<tr><td>v12.0.0</td> 776<td><p>If the <code>format</code> argument is not a format string, the output string's formatting is no longer dependent on the type of the first argument. This change removes previously present quotes from strings that were being output when the first argument was not a string.</p></td></tr> 777<tr><td>v11.4.0</td> 778<td><p>The <code>%d</code>, <code>%f</code>, and <code>%i</code> specifiers now support Symbols properly.</p></td></tr> 779<tr><td>v11.4.0</td> 780<td><p>The <code>%o</code> specifier's <code>depth</code> has default depth of 4 again.</p></td></tr> 781<tr><td>v11.0.0</td> 782<td><p>The <code>%o</code> specifier's <code>depth</code> option will now fall back to the default depth.</p></td></tr> 783<tr><td>v10.12.0</td> 784<td><p>The <code>%d</code> and <code>%i</code> specifiers now support BigInt.</p></td></tr> 785<tr><td>v8.4.0</td> 786<td><p>The <code>%o</code> and <code>%O</code> specifiers are supported now.</p></td></tr> 787<tr><td>v0.5.3</td> 788<td><p><span>Added in: v0.5.3</span></p></td></tr> 789</tbody></table> 790</details> 791</div> 792<ul> 793<li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A <code>printf</code>-like format string.</li> 794</ul> 795<p>The <code>util.format()</code> method returns a formatted string using the first argument 796as a <code>printf</code>-like format string which can contain zero or more format 797specifiers. Each specifier is replaced with the converted value from the 798corresponding argument. Supported specifiers are:</p> 799<ul> 800<li><code>%s</code>: <code>String</code> will be used to convert all values except <code>BigInt</code>, <code>Object</code> 801and <code>-0</code>. <code>BigInt</code> values will be represented with an <code>n</code> and Objects that 802have no user defined <code>toString</code> function are inspected using <code>util.inspect()</code> 803with options <code>{ depth: 0, colors: false, compact: 3 }</code>.</li> 804<li><code>%d</code>: <code>Number</code> will be used to convert all values except <code>BigInt</code> and 805<code>Symbol</code>.</li> 806<li><code>%i</code>: <code>parseInt(value, 10)</code> is used for all values except <code>BigInt</code> and 807<code>Symbol</code>.</li> 808<li><code>%f</code>: <code>parseFloat(value)</code> is used for all values expect <code>Symbol</code>.</li> 809<li><code>%j</code>: JSON. Replaced with the string <code>'[Circular]'</code> if the argument contains 810circular references.</li> 811<li><code>%o</code>: <code>Object</code>. A string representation of an object with generic JavaScript 812object formatting. Similar to <code>util.inspect()</code> with options 813<code>{ showHidden: true, showProxy: true }</code>. This will show the full object 814including non-enumerable properties and proxies.</li> 815<li><code>%O</code>: <code>Object</code>. A string representation of an object with generic JavaScript 816object formatting. Similar to <code>util.inspect()</code> without options. This will show 817the full object not including non-enumerable properties and proxies.</li> 818<li><code>%c</code>: <code>CSS</code>. This specifier is ignored and will skip any CSS passed in.</li> 819<li><code>%%</code>: single percent sign (<code>'%'</code>). This does not consume an argument.</li> 820<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The formatted string</li> 821</ul> 822<p>If a specifier does not have a corresponding argument, it is not replaced:</p> 823<pre><code class="language-js">util.<span class="hljs-title function_">format</span>(<span class="hljs-string">'%s:%s'</span>, <span class="hljs-string">'foo'</span>); 824<span class="hljs-comment">// Returns: 'foo:%s'</span></code> <button class="copy-button">copy</button></pre> 825<p>Values that are not part of the format string are formatted using 826<code>util.inspect()</code> if their type is not <code>string</code>.</p> 827<p>If there are more arguments passed to the <code>util.format()</code> method than the 828number of specifiers, the extra arguments are concatenated to the returned 829string, separated by spaces:</p> 830<pre><code class="language-js">util.<span class="hljs-title function_">format</span>(<span class="hljs-string">'%s:%s'</span>, <span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>, <span class="hljs-string">'baz'</span>); 831<span class="hljs-comment">// Returns: 'foo:bar baz'</span></code> <button class="copy-button">copy</button></pre> 832<p>If the first argument does not contain a valid format specifier, <code>util.format()</code> 833returns a string that is the concatenation of all arguments separated by spaces:</p> 834<pre><code class="language-js">util.<span class="hljs-title function_">format</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>); 835<span class="hljs-comment">// Returns: '1 2 3'</span></code> <button class="copy-button">copy</button></pre> 836<p>If only one argument is passed to <code>util.format()</code>, it is returned as it is 837without any formatting:</p> 838<pre><code class="language-js">util.<span class="hljs-title function_">format</span>(<span class="hljs-string">'%% %s'</span>); 839<span class="hljs-comment">// Returns: '%% %s'</span></code> <button class="copy-button">copy</button></pre> 840<p><code>util.format()</code> is a synchronous method that is intended as a debugging tool. 841Some input values can have a significant performance overhead that can block the 842event loop. Use this function with care and never in a hot code path.</p> 843</section><section><h3><code>util.formatWithOptions(inspectOptions, format[, ...args])</code><span><a class="mark" href="#utilformatwithoptionsinspectoptions-format-args" id="utilformatwithoptionsinspectoptions-format-args">#</a></span><a aria-hidden="true" class="legacy" id="util_util_formatwithoptions_inspectoptions_format_args"></a></h3> 844<div class="api_metadata"> 845<span>Added in: v10.0.0</span> 846</div> 847<ul> 848<li><code>inspectOptions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 849<li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 850</ul> 851<p>This function is identical to <a href="#utilformatformat-args"><code>util.format()</code></a>, except in that it takes 852an <code>inspectOptions</code> argument which specifies options that are passed along to 853<a href="#utilinspectobject-options"><code>util.inspect()</code></a>.</p> 854<pre><code class="language-js">util.<span class="hljs-title function_">formatWithOptions</span>({ <span class="hljs-attr">colors</span>: <span class="hljs-literal">true</span> }, <span class="hljs-string">'See object %O'</span>, { <span class="hljs-attr">foo</span>: <span class="hljs-number">42</span> }); 855<span class="hljs-comment">// Returns 'See object { foo: 42 }', where `42` is colored as a number</span> 856<span class="hljs-comment">// when printed to a terminal.</span></code> <button class="copy-button">copy</button></pre> 857</section><section><h3><code>util.getSystemErrorName(err)</code><span><a class="mark" href="#utilgetsystemerrornameerr" id="utilgetsystemerrornameerr">#</a></span><a aria-hidden="true" class="legacy" id="util_util_getsystemerrorname_err"></a></h3> 858<div class="api_metadata"> 859<span>Added in: v9.7.0</span> 860</div> 861<ul> 862<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 863<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 864</ul> 865<p>Returns the string name for a numeric error code that comes from a Node.js API. 866The mapping between error codes and error names is platform-dependent. 867See <a href="errors.html#common-system-errors">Common System Errors</a> for the names of common errors.</p> 868<pre><code class="language-js">fs.<span class="hljs-title function_">access</span>(<span class="hljs-string">'file/that/does/not/exist'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 869 <span class="hljs-keyword">const</span> name = util.<span class="hljs-title function_">getSystemErrorName</span>(err.<span class="hljs-property">errno</span>); 870 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(name); <span class="hljs-comment">// ENOENT</span> 871});</code> <button class="copy-button">copy</button></pre> 872</section><section><h3><code>util.getSystemErrorMap()</code><span><a class="mark" href="#utilgetsystemerrormap" id="utilgetsystemerrormap">#</a></span><a aria-hidden="true" class="legacy" id="util_util_getsystemerrormap"></a></h3> 873<div class="api_metadata"> 874<span>Added in: v16.0.0, v14.17.0</span> 875</div> 876<ul> 877<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" class="type"><Map></a></li> 878</ul> 879<p>Returns a Map of all system error codes available from the Node.js API. 880The mapping between error codes and error names is platform-dependent. 881See <a href="errors.html#common-system-errors">Common System Errors</a> for the names of common errors.</p> 882<pre><code class="language-js">fs.<span class="hljs-title function_">access</span>(<span class="hljs-string">'file/that/does/not/exist'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 883 <span class="hljs-keyword">const</span> errorMap = util.<span class="hljs-title function_">getSystemErrorMap</span>(); 884 <span class="hljs-keyword">const</span> name = errorMap.<span class="hljs-title function_">get</span>(err.<span class="hljs-property">errno</span>); 885 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(name); <span class="hljs-comment">// ENOENT</span> 886});</code> <button class="copy-button">copy</button></pre> 887</section><section><h3><code>util.inherits(constructor, superConstructor)</code><span><a class="mark" href="#utilinheritsconstructor-superconstructor" id="utilinheritsconstructor-superconstructor">#</a></span><a aria-hidden="true" class="legacy" id="util_util_inherits_constructor_superconstructor"></a></h3> 888<div class="api_metadata"> 889<details class="changelog"><summary>History</summary> 890<table> 891<tbody><tr><th>Version</th><th>Changes</th></tr> 892<tr><td>v5.0.0</td> 893<td><p>The <code>constructor</code> parameter can refer to an ES6 class now.</p></td></tr> 894<tr><td>v0.3.0</td> 895<td><p><span>Added in: v0.3.0</span></p></td></tr> 896</tbody></table> 897</details> 898</div> 899<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use ES2015 class syntax and <code>extends</code> keyword instead.</div><p></p> 900<ul> 901<li><code>constructor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 902<li><code>superConstructor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 903</ul> 904<p>Usage of <code>util.inherits()</code> is discouraged. Please use the ES6 <code>class</code> and 905<code>extends</code> keywords to get language level inheritance support. Also note 906that the two styles are <a href="https://github.com/nodejs/node/issues/4179">semantically incompatible</a>.</p> 907<p>Inherit the prototype methods from one <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor">constructor</a> into another. The 908prototype of <code>constructor</code> will be set to a new object created from 909<code>superConstructor</code>.</p> 910<p>This mainly adds some input validation on top of 911<code>Object.setPrototypeOf(constructor.prototype, superConstructor.prototype)</code>. 912As an additional convenience, <code>superConstructor</code> will be accessible 913through the <code>constructor.super_</code> property.</p> 914<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 915<span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 916 917<span class="hljs-keyword">function</span> <span class="hljs-title function_">MyStream</span>(<span class="hljs-params"></span>) { 918 <span class="hljs-title class_">EventEmitter</span>.<span class="hljs-title function_">call</span>(<span class="hljs-variable language_">this</span>); 919} 920 921util.<span class="hljs-title function_">inherits</span>(<span class="hljs-title class_">MyStream</span>, <span class="hljs-title class_">EventEmitter</span>); 922 923<span class="hljs-title class_">MyStream</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">write</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">data</span>) { 924 <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'data'</span>, data); 925}; 926 927<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyStream</span>(); 928 929<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stream <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">EventEmitter</span>); <span class="hljs-comment">// true</span> 930<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">MyStream</span>.<span class="hljs-property">super_</span> === <span class="hljs-title class_">EventEmitter</span>); <span class="hljs-comment">// true</span> 931 932stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 933 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Received data: "<span class="hljs-subst">${data}</span>"`</span>); 934}); 935stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'It works!'</span>); <span class="hljs-comment">// Received data: "It works!"</span></code> <button class="copy-button">copy</button></pre> 936<p>ES6 example using <code>class</code> and <code>extends</code>:</p> 937<pre><code class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 938 939<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyStream</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_ inherited__">EventEmitter</span> { 940 <span class="hljs-title function_">write</span>(<span class="hljs-params">data</span>) { 941 <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'data'</span>, data); 942 } 943} 944 945<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyStream</span>(); 946 947stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 948 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Received data: "<span class="hljs-subst">${data}</span>"`</span>); 949}); 950stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'With ES6'</span>);</code> <button class="copy-button">copy</button></pre> 951</section><section><h3><code>util.inspect(object[, options])</code><span><a class="mark" href="#utilinspectobject-options" id="utilinspectobject-options">#</a></span><a aria-hidden="true" class="legacy" id="util_util_inspect_object_options"></a></h3> 952</section><section><h3><code>util.inspect(object[, showHidden[, depth[, colors]]])</code><span><a class="mark" href="#utilinspectobject-showhidden-depth-colors" id="utilinspectobject-showhidden-depth-colors">#</a></span><a aria-hidden="true" class="legacy" id="util_util_inspect_object_showhidden_depth_colors"></a></h3> 953<div class="api_metadata"> 954<details class="changelog"><summary>History</summary> 955<table> 956<tbody><tr><th>Version</th><th>Changes</th></tr> 957<tr><td>v17.3.0, v16.14.0</td> 958<td><p>The <code>numericSeparator</code> option is supported now.</p></td></tr> 959<tr><td>v13.0.0</td> 960<td><p>Circular references now include a marker to the reference.</p></td></tr> 961<tr><td>v14.6.0, v12.19.0</td> 962<td><p>If <code>object</code> is from a different <code>vm.Context</code> now, a custom inspection function on it will not receive context-specific arguments anymore.</p></td></tr> 963<tr><td>v13.13.0, v12.17.0</td> 964<td><p>The <code>maxStringLength</code> option is supported now.</p></td></tr> 965<tr><td>v13.5.0, v12.16.0</td> 966<td><p>User defined prototype properties are inspected in case <code>showHidden</code> is <code>true</code>.</p></td></tr> 967<tr><td>v12.0.0</td> 968<td><p>The <code>compact</code> options default is changed to <code>3</code> and the <code>breakLength</code> options default is changed to <code>80</code>.</p></td></tr> 969<tr><td>v12.0.0</td> 970<td><p>Internal properties no longer appear in the context argument of a custom inspection function.</p></td></tr> 971<tr><td>v11.11.0</td> 972<td><p>The <code>compact</code> option accepts numbers for a new output mode.</p></td></tr> 973<tr><td>v11.7.0</td> 974<td><p>ArrayBuffers now also show their binary contents.</p></td></tr> 975<tr><td>v11.5.0</td> 976<td><p>The <code>getters</code> option is supported now.</p></td></tr> 977<tr><td>v11.4.0</td> 978<td><p>The <code>depth</code> default changed back to <code>2</code>.</p></td></tr> 979<tr><td>v11.0.0</td> 980<td><p>The <code>depth</code> default changed to <code>20</code>.</p></td></tr> 981<tr><td>v11.0.0</td> 982<td><p>The inspection output is now limited to about 128 MiB. Data above that size will not be fully inspected.</p></td></tr> 983<tr><td>v10.12.0</td> 984<td><p>The <code>sorted</code> option is supported now.</p></td></tr> 985<tr><td>v10.6.0</td> 986<td><p>Inspecting linked lists and similar objects is now possible up to the maximum call stack size.</p></td></tr> 987<tr><td>v10.0.0</td> 988<td><p>The <code>WeakMap</code> and <code>WeakSet</code> entries can now be inspected as well.</p></td></tr> 989<tr><td>v9.9.0</td> 990<td><p>The <code>compact</code> option is supported now.</p></td></tr> 991<tr><td>v6.6.0</td> 992<td><p>Custom inspection functions can now return <code>this</code>.</p></td></tr> 993<tr><td>v6.3.0</td> 994<td><p>The <code>breakLength</code> option is supported now.</p></td></tr> 995<tr><td>v6.1.0</td> 996<td><p>The <code>maxArrayLength</code> option is supported now; in particular, long arrays are truncated by default.</p></td></tr> 997<tr><td>v6.1.0</td> 998<td><p>The <code>showProxy</code> option is supported now.</p></td></tr> 999<tr><td>v0.3.0</td> 1000<td><p><span>Added in: v0.3.0</span></p></td></tr> 1001</tbody></table> 1002</details> 1003</div> 1004<ul> 1005<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Any JavaScript primitive or <code>Object</code>.</li> 1006<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1007<ul> 1008<li><code>showHidden</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, <code>object</code>'s non-enumerable symbols and 1009properties are included in the formatted result. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and 1010<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet"><code>WeakSet</code></a> entries are also included as well as user defined prototype 1011properties (excluding method properties). <strong>Default:</strong> <code>false</code>.</li> 1012<li><code>depth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Specifies the number of times to recurse while formatting 1013<code>object</code>. This is useful for inspecting large objects. To recurse up to 1014the maximum call stack size pass <code>Infinity</code> or <code>null</code>. 1015<strong>Default:</strong> <code>2</code>.</li> 1016<li><code>colors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the output is styled with ANSI color 1017codes. Colors are customizable. See <a href="#customizing-utilinspect-colors">Customizing <code>util.inspect</code> colors</a>. 1018<strong>Default:</strong> <code>false</code>.</li> 1019<li><code>customInspect</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>false</code>, 1020<code>[util.inspect.custom](depth, opts, inspect)</code> functions are not invoked. 1021<strong>Default:</strong> <code>true</code>.</li> 1022<li><code>showProxy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, <code>Proxy</code> inspection includes 1023the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy#Terminology"><code>target</code> and <code>handler</code></a> objects. <strong>Default:</strong> <code>false</code>.</li> 1024<li><code>maxArrayLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Specifies the maximum number of <code>Array</code>, 1025<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a>, and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet"><code>WeakSet</code></a> elements to include when 1026formatting. Set to <code>null</code> or <code>Infinity</code> to show all elements. Set to <code>0</code> or 1027negative to show no elements. <strong>Default:</strong> <code>100</code>.</li> 1028<li><code>maxStringLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Specifies the maximum number of characters to 1029include when formatting. Set to <code>null</code> or <code>Infinity</code> to show all elements. 1030Set to <code>0</code> or negative to show no characters. <strong>Default:</strong> <code>10000</code>.</li> 1031<li><code>breakLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The length at which input values are split across 1032multiple lines. Set to <code>Infinity</code> to format the input as a single line 1033(in combination with <code>compact</code> set to <code>true</code> or any number >= <code>1</code>). 1034<strong>Default:</strong> <code>80</code>.</li> 1035<li><code>compact</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Setting this to <code>false</code> causes each object key 1036to be displayed on a new line. It will break on new lines in text that is 1037longer than <code>breakLength</code>. If set to a number, the most <code>n</code> inner elements 1038are united on a single line as long as all properties fit into 1039<code>breakLength</code>. Short array elements are also grouped together. For more 1040information, see the example below. <strong>Default:</strong> <code>3</code>.</li> 1041<li><code>sorted</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> If set to <code>true</code> or a function, all properties 1042of an object, and <code>Set</code> and <code>Map</code> entries are sorted in the resulting 1043string. If set to <code>true</code> the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort">default sort</a> is used. If set to a function, 1044it is used as a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Parameters">compare function</a>.</li> 1045<li><code>getters</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> If set to <code>true</code>, getters are inspected. If set 1046to <code>'get'</code>, only getters without a corresponding setter are inspected. If 1047set to <code>'set'</code>, only getters with a corresponding setter are inspected. 1048This might cause side effects depending on the getter function. 1049<strong>Default:</strong> <code>false</code>.</li> 1050<li><code>numericSeparator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If set to <code>true</code>, an underscore is used to 1051separate every three digits in all bigints and numbers. 1052<strong>Default:</strong> <code>false</code>.</li> 1053</ul> 1054</li> 1055<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The representation of <code>object</code>.</li> 1056</ul> 1057<p>The <code>util.inspect()</code> method returns a string representation of <code>object</code> that is 1058intended for debugging. The output of <code>util.inspect</code> may change at any time 1059and should not be depended upon programmatically. Additional <code>options</code> may be 1060passed that alter the result. 1061<code>util.inspect()</code> will use the constructor's name and/or <code>@@toStringTag</code> to make 1062an identifiable tag for an inspected value.</p> 1063<pre><code class="language-js"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> { 1064 get [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toStringTag</span>]() { 1065 <span class="hljs-keyword">return</span> <span class="hljs-string">'bar'</span>; 1066 } 1067} 1068 1069<span class="hljs-keyword">class</span> <span class="hljs-title class_">Bar</span> {} 1070 1071<span class="hljs-keyword">const</span> baz = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">create</span>(<span class="hljs-literal">null</span>, { [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toStringTag</span>]: { <span class="hljs-attr">value</span>: <span class="hljs-string">'foo'</span> } }); 1072 1073util.<span class="hljs-title function_">inspect</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Foo</span>()); <span class="hljs-comment">// 'Foo [bar] {}'</span> 1074util.<span class="hljs-title function_">inspect</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Bar</span>()); <span class="hljs-comment">// 'Bar {}'</span> 1075util.<span class="hljs-title function_">inspect</span>(baz); <span class="hljs-comment">// '[foo] {}'</span></code> <button class="copy-button">copy</button></pre> 1076<p>Circular references point to their anchor by using a reference index:</p> 1077<pre><code class="language-js"><span class="hljs-keyword">const</span> { inspect } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1078 1079<span class="hljs-keyword">const</span> obj = {}; 1080obj.<span class="hljs-property">a</span> = [obj]; 1081obj.<span class="hljs-property">b</span> = {}; 1082obj.<span class="hljs-property">b</span>.<span class="hljs-property">inner</span> = obj.<span class="hljs-property">b</span>; 1083obj.<span class="hljs-property">b</span>.<span class="hljs-property">obj</span> = obj; 1084 1085<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">inspect</span>(obj)); 1086<span class="hljs-comment">// <ref *1> {</span> 1087<span class="hljs-comment">// a: [ [Circular *1] ],</span> 1088<span class="hljs-comment">// b: <ref *2> { inner: [Circular *2], obj: [Circular *1] }</span> 1089<span class="hljs-comment">// }</span></code> <button class="copy-button">copy</button></pre> 1090<p>The following example inspects all properties of the <code>util</code> object:</p> 1091<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1092 1093<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class="hljs-title function_">inspect</span>(util, { <span class="hljs-attr">showHidden</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">depth</span>: <span class="hljs-literal">null</span> }));</code> <button class="copy-button">copy</button></pre> 1094<p>The following example highlights the effect of the <code>compact</code> option:</p> 1095<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1096 1097<span class="hljs-keyword">const</span> o = { 1098 <span class="hljs-attr">a</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, [[ 1099 <span class="hljs-string">'Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit, sed do '</span> + 1100 <span class="hljs-string">'eiusmod \ntempor incididunt ut labore et dolore magna aliqua.'</span>, 1101 <span class="hljs-string">'test'</span>, 1102 <span class="hljs-string">'foo'</span>]], <span class="hljs-number">4</span>], 1103 <span class="hljs-attr">b</span>: <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>([[<span class="hljs-string">'za'</span>, <span class="hljs-number">1</span>], [<span class="hljs-string">'zb'</span>, <span class="hljs-string">'test'</span>]]), 1104}; 1105<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class="hljs-title function_">inspect</span>(o, { <span class="hljs-attr">compact</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">depth</span>: <span class="hljs-number">5</span>, <span class="hljs-attr">breakLength</span>: <span class="hljs-number">80</span> })); 1106 1107<span class="hljs-comment">// { a:</span> 1108<span class="hljs-comment">// [ 1,</span> 1109<span class="hljs-comment">// 2,</span> 1110<span class="hljs-comment">// [ [ 'Lorem ipsum dolor sit amet,\nconsectetur [...]', // A long line</span> 1111<span class="hljs-comment">// 'test',</span> 1112<span class="hljs-comment">// 'foo' ] ],</span> 1113<span class="hljs-comment">// 4 ],</span> 1114<span class="hljs-comment">// b: Map(2) { 'za' => 1, 'zb' => 'test' } }</span> 1115 1116<span class="hljs-comment">// Setting `compact` to false or an integer creates more reader friendly output.</span> 1117<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class="hljs-title function_">inspect</span>(o, { <span class="hljs-attr">compact</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">depth</span>: <span class="hljs-number">5</span>, <span class="hljs-attr">breakLength</span>: <span class="hljs-number">80</span> })); 1118 1119<span class="hljs-comment">// {</span> 1120<span class="hljs-comment">// a: [</span> 1121<span class="hljs-comment">// 1,</span> 1122<span class="hljs-comment">// 2,</span> 1123<span class="hljs-comment">// [</span> 1124<span class="hljs-comment">// [</span> 1125<span class="hljs-comment">// 'Lorem ipsum dolor sit amet,\n' +</span> 1126<span class="hljs-comment">// 'consectetur adipiscing elit, sed do eiusmod \n' +</span> 1127<span class="hljs-comment">// 'tempor incididunt ut labore et dolore magna aliqua.',</span> 1128<span class="hljs-comment">// 'test',</span> 1129<span class="hljs-comment">// 'foo'</span> 1130<span class="hljs-comment">// ]</span> 1131<span class="hljs-comment">// ],</span> 1132<span class="hljs-comment">// 4</span> 1133<span class="hljs-comment">// ],</span> 1134<span class="hljs-comment">// b: Map(2) {</span> 1135<span class="hljs-comment">// 'za' => 1,</span> 1136<span class="hljs-comment">// 'zb' => 'test'</span> 1137<span class="hljs-comment">// }</span> 1138<span class="hljs-comment">// }</span> 1139 1140<span class="hljs-comment">// Setting `breakLength` to e.g. 150 will print the "Lorem ipsum" text in a</span> 1141<span class="hljs-comment">// single line.</span></code> <button class="copy-button">copy</button></pre> 1142<p>The <code>showHidden</code> option allows <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet"><code>WeakSet</code></a> entries to be 1143inspected. If there are more entries than <code>maxArrayLength</code>, there is no 1144guarantee which entries are displayed. That means retrieving the same 1145<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet"><code>WeakSet</code></a> entries twice may result in different output. Furthermore, entries 1146with no remaining strong references may be garbage collected at any time.</p> 1147<pre><code class="language-js"><span class="hljs-keyword">const</span> { inspect } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1148 1149<span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">a</span>: <span class="hljs-number">1</span> }; 1150<span class="hljs-keyword">const</span> obj2 = { <span class="hljs-attr">b</span>: <span class="hljs-number">2</span> }; 1151<span class="hljs-keyword">const</span> weakSet = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakSet</span>([obj, obj2]); 1152 1153<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">inspect</span>(weakSet, { <span class="hljs-attr">showHidden</span>: <span class="hljs-literal">true</span> })); 1154<span class="hljs-comment">// WeakSet { { a: 1 }, { b: 2 } }</span></code> <button class="copy-button">copy</button></pre> 1155<p>The <code>sorted</code> option ensures that an object's property insertion order does not 1156impact the result of <code>util.inspect()</code>.</p> 1157<pre><code class="language-js"><span class="hljs-keyword">const</span> { inspect } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1158<span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 1159 1160<span class="hljs-keyword">const</span> o1 = { 1161 <span class="hljs-attr">b</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>], 1162 <span class="hljs-attr">a</span>: <span class="hljs-string">'`a` comes before `b`'</span>, 1163 <span class="hljs-attr">c</span>: <span class="hljs-keyword">new</span> <span class="hljs-title class_">Set</span>([<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>]), 1164}; 1165<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">inspect</span>(o1, { <span class="hljs-attr">sorted</span>: <span class="hljs-literal">true</span> })); 1166<span class="hljs-comment">// { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set(3) { 1, 2, 3 } }</span> 1167<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">inspect</span>(o1, { <span class="hljs-attr">sorted</span>: <span class="hljs-function">(<span class="hljs-params">a, b</span>) =></span> b.<span class="hljs-title function_">localeCompare</span>(a) })); 1168<span class="hljs-comment">// { c: Set(3) { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' }</span> 1169 1170<span class="hljs-keyword">const</span> o2 = { 1171 <span class="hljs-attr">c</span>: <span class="hljs-keyword">new</span> <span class="hljs-title class_">Set</span>([<span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>]), 1172 <span class="hljs-attr">a</span>: <span class="hljs-string">'`a` comes before `b`'</span>, 1173 <span class="hljs-attr">b</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>], 1174}; 1175assert.<span class="hljs-property">strict</span>.<span class="hljs-title function_">equal</span>( 1176 <span class="hljs-title function_">inspect</span>(o1, { <span class="hljs-attr">sorted</span>: <span class="hljs-literal">true</span> }), 1177 <span class="hljs-title function_">inspect</span>(o2, { <span class="hljs-attr">sorted</span>: <span class="hljs-literal">true</span> }), 1178);</code> <button class="copy-button">copy</button></pre> 1179<p>The <code>numericSeparator</code> option adds an underscore every three digits to all 1180numbers.</p> 1181<pre><code class="language-js"><span class="hljs-keyword">const</span> { inspect } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1182 1183<span class="hljs-keyword">const</span> thousand = <span class="hljs-number">1_000</span>; 1184<span class="hljs-keyword">const</span> million = <span class="hljs-number">1_000_000</span>; 1185<span class="hljs-keyword">const</span> bigNumber = <span class="hljs-number">123_456_789n</span>; 1186<span class="hljs-keyword">const</span> bigDecimal = <span class="hljs-number">1_234.123_45</span>; 1187 1188<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(thousand, million, bigNumber, bigDecimal); 1189<span class="hljs-comment">// 1_000 1_000_000 123_456_789n 1_234.123_45</span></code> <button class="copy-button">copy</button></pre> 1190<p><code>util.inspect()</code> is a synchronous method intended for debugging. Its maximum 1191output length is approximately 128 MiB. Inputs that result in longer output will 1192be truncated.</p> 1193<h4>Customizing <code>util.inspect</code> colors<span><a class="mark" href="#customizing-utilinspect-colors" id="customizing-utilinspect-colors">#</a></span><a aria-hidden="true" class="legacy" id="util_customizing_util_inspect_colors"></a></h4> 1194 1195<p>Color output (if enabled) of <code>util.inspect</code> is customizable globally 1196via the <code>util.inspect.styles</code> and <code>util.inspect.colors</code> properties.</p> 1197<p><code>util.inspect.styles</code> is a map associating a style name to a color from 1198<code>util.inspect.colors</code>.</p> 1199<p>The default styles and associated colors are:</p> 1200<ul> 1201<li><code>bigint</code>: <code>yellow</code></li> 1202<li><code>boolean</code>: <code>yellow</code></li> 1203<li><code>date</code>: <code>magenta</code></li> 1204<li><code>module</code>: <code>underline</code></li> 1205<li><code>name</code>: (no styling)</li> 1206<li><code>null</code>: <code>bold</code></li> 1207<li><code>number</code>: <code>yellow</code></li> 1208<li><code>regexp</code>: <code>red</code></li> 1209<li><code>special</code>: <code>cyan</code> (e.g., <code>Proxies</code>)</li> 1210<li><code>string</code>: <code>green</code></li> 1211<li><code>symbol</code>: <code>green</code></li> 1212<li><code>undefined</code>: <code>grey</code></li> 1213</ul> 1214<p>Color styling uses ANSI control codes that may not be supported on all 1215terminals. To verify color support use <a href="tty.html#writestreamhascolorscount-env"><code>tty.hasColors()</code></a>.</p> 1216<p>Predefined control codes are listed below (grouped as "Modifiers", "Foreground 1217colors", and "Background colors").</p> 1218<h5>Modifiers<span><a class="mark" href="#modifiers" id="modifiers">#</a></span><a aria-hidden="true" class="legacy" id="util_modifiers"></a></h5> 1219<p>Modifier support varies throughout different terminals. They will mostly be 1220ignored, if not supported.</p> 1221<ul> 1222<li><code>reset</code> - Resets all (color) modifiers to their defaults</li> 1223<li><strong>bold</strong> - Make text bold</li> 1224<li><em>italic</em> - Make text italic</li> 1225<li><span style="border-bottom: 1px;">underline</span> - Make text underlined</li> 1226<li><del>strikethrough</del> - Puts a horizontal line through the center of the text 1227(Alias: <code>strikeThrough</code>, <code>crossedout</code>, <code>crossedOut</code>)</li> 1228<li><code>hidden</code> - Prints the text, but makes it invisible (Alias: conceal)</li> 1229<li><span style="opacity: 0.5;">dim</span> - Decreased color intensity (Alias: 1230<code>faint</code>)</li> 1231<li><span style="border-top: 1px">overlined</span> - Make text overlined</li> 1232<li>blink - Hides and shows the text in an interval</li> 1233<li><span style="filter: invert(100%)">inverse</span> - Swap foreground and 1234background colors (Alias: <code>swapcolors</code>, <code>swapColors</code>)</li> 1235<li><span style="border-bottom: 1px double;">doubleunderline</span> - Make text 1236double underlined (Alias: <code>doubleUnderline</code>)</li> 1237<li><span style="border: 1px">framed</span> - Draw a frame around the text</li> 1238</ul> 1239<h5>Foreground colors<span><a class="mark" href="#foreground-colors" id="foreground-colors">#</a></span><a aria-hidden="true" class="legacy" id="util_foreground_colors"></a></h5> 1240<ul> 1241<li><code>black</code></li> 1242<li><code>red</code></li> 1243<li><code>green</code></li> 1244<li><code>yellow</code></li> 1245<li><code>blue</code></li> 1246<li><code>magenta</code></li> 1247<li><code>cyan</code></li> 1248<li><code>white</code></li> 1249<li><code>gray</code> (alias: <code>grey</code>, <code>blackBright</code>)</li> 1250<li><code>redBright</code></li> 1251<li><code>greenBright</code></li> 1252<li><code>yellowBright</code></li> 1253<li><code>blueBright</code></li> 1254<li><code>magentaBright</code></li> 1255<li><code>cyanBright</code></li> 1256<li><code>whiteBright</code></li> 1257</ul> 1258<h5>Background colors<span><a class="mark" href="#background-colors" id="background-colors">#</a></span><a aria-hidden="true" class="legacy" id="util_background_colors"></a></h5> 1259<ul> 1260<li><code>bgBlack</code></li> 1261<li><code>bgRed</code></li> 1262<li><code>bgGreen</code></li> 1263<li><code>bgYellow</code></li> 1264<li><code>bgBlue</code></li> 1265<li><code>bgMagenta</code></li> 1266<li><code>bgCyan</code></li> 1267<li><code>bgWhite</code></li> 1268<li><code>bgGray</code> (alias: <code>bgGrey</code>, <code>bgBlackBright</code>)</li> 1269<li><code>bgRedBright</code></li> 1270<li><code>bgGreenBright</code></li> 1271<li><code>bgYellowBright</code></li> 1272<li><code>bgBlueBright</code></li> 1273<li><code>bgMagentaBright</code></li> 1274<li><code>bgCyanBright</code></li> 1275<li><code>bgWhiteBright</code></li> 1276</ul> 1277<h4>Custom inspection functions on objects<span><a class="mark" href="#custom-inspection-functions-on-objects" id="custom-inspection-functions-on-objects">#</a></span><a aria-hidden="true" class="legacy" id="util_custom_inspection_functions_on_objects"></a></h4> 1278 1279<div class="api_metadata"> 1280<details class="changelog"><summary>History</summary> 1281<table> 1282<tbody><tr><th>Version</th><th>Changes</th></tr> 1283<tr><td>v17.3.0, v16.14.0</td> 1284<td><p>The inspect argument is added for more interoperability.</p></td></tr> 1285<tr><td>v0.1.97</td> 1286<td><p><span>Added in: v0.1.97</span></p></td></tr> 1287</tbody></table> 1288</details> 1289</div> 1290<p>Objects may also define their own 1291<a href="#utilinspectcustom"><code>[util.inspect.custom](depth, opts, inspect)</code></a> function, 1292which <code>util.inspect()</code> will invoke and use the result of when inspecting 1293the object.</p> 1294<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1295 1296<span class="hljs-keyword">class</span> <span class="hljs-title class_">Box</span> { 1297 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">value</span>) { 1298 <span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span> = value; 1299 } 1300 1301 [util.<span class="hljs-property">inspect</span>.<span class="hljs-property">custom</span>](depth, options, inspect) { 1302 <span class="hljs-keyword">if</span> (depth < <span class="hljs-number">0</span>) { 1303 <span class="hljs-keyword">return</span> options.<span class="hljs-title function_">stylize</span>(<span class="hljs-string">'[Box]'</span>, <span class="hljs-string">'special'</span>); 1304 } 1305 1306 <span class="hljs-keyword">const</span> newOptions = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">assign</span>({}, options, { 1307 <span class="hljs-attr">depth</span>: options.<span class="hljs-property">depth</span> === <span class="hljs-literal">null</span> ? <span class="hljs-literal">null</span> : options.<span class="hljs-property">depth</span> - <span class="hljs-number">1</span>, 1308 }); 1309 1310 <span class="hljs-comment">// Five space padding because that's the size of "Box< ".</span> 1311 <span class="hljs-keyword">const</span> padding = <span class="hljs-string">' '</span>.<span class="hljs-title function_">repeat</span>(<span class="hljs-number">5</span>); 1312 <span class="hljs-keyword">const</span> inner = <span class="hljs-title function_">inspect</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span>, newOptions) 1313 .<span class="hljs-title function_">replace</span>(<span class="hljs-regexp">/\n/g</span>, <span class="hljs-string">`\n<span class="hljs-subst">${padding}</span>`</span>); 1314 <span class="hljs-keyword">return</span> <span class="hljs-string">`<span class="hljs-subst">${options.stylize(<span class="hljs-string">'Box'</span>, <span class="hljs-string">'special'</span>)}</span>< <span class="hljs-subst">${inner}</span> >`</span>; 1315 } 1316} 1317 1318<span class="hljs-keyword">const</span> box = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Box</span>(<span class="hljs-literal">true</span>); 1319 1320util.<span class="hljs-title function_">inspect</span>(box); 1321<span class="hljs-comment">// Returns: "Box< true >"</span></code> <button class="copy-button">copy</button></pre> 1322<p>Custom <code>[util.inspect.custom](depth, opts, inspect)</code> functions typically return 1323a string but may return a value of any type that will be formatted accordingly 1324by <code>util.inspect()</code>.</p> 1325<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1326 1327<span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">foo</span>: <span class="hljs-string">'this will not show up in the inspect() output'</span> }; 1328obj[util.<span class="hljs-property">inspect</span>.<span class="hljs-property">custom</span>] = <span class="hljs-function">(<span class="hljs-params">depth</span>) =></span> { 1329 <span class="hljs-keyword">return</span> { <span class="hljs-attr">bar</span>: <span class="hljs-string">'baz'</span> }; 1330}; 1331 1332util.<span class="hljs-title function_">inspect</span>(obj); 1333<span class="hljs-comment">// Returns: "{ bar: 'baz' }"</span></code> <button class="copy-button">copy</button></pre> 1334<h4><code>util.inspect.custom</code><span><a class="mark" href="#utilinspectcustom" id="utilinspectcustom">#</a></span><a aria-hidden="true" class="legacy" id="util_util_inspect_custom"></a></h4> 1335<div class="api_metadata"> 1336<details class="changelog"><summary>History</summary> 1337<table> 1338<tbody><tr><th>Version</th><th>Changes</th></tr> 1339<tr><td>v10.12.0</td> 1340<td><p>This is now defined as a shared symbol.</p></td></tr> 1341<tr><td>v6.6.0</td> 1342<td><p><span>Added in: v6.6.0</span></p></td></tr> 1343</tbody></table> 1344</details> 1345</div> 1346<ul> 1347<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> that can be used to declare custom inspect functions.</li> 1348</ul> 1349<p>In addition to being accessible through <code>util.inspect.custom</code>, this 1350symbol is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for">registered globally</a> and can be 1351accessed in any environment as <code>Symbol.for('nodejs.util.inspect.custom')</code>.</p> 1352<p>Using this allows code to be written in a portable fashion, so that the custom 1353inspect function is used in an Node.js environment and ignored in the browser. 1354The <code>util.inspect()</code> function itself is passed as third argument to the custom 1355inspect function to allow further portability.</p> 1356<pre><code class="language-js"><span class="hljs-keyword">const</span> customInspectSymbol = <span class="hljs-title class_">Symbol</span>.<span class="hljs-title function_">for</span>(<span class="hljs-string">'nodejs.util.inspect.custom'</span>); 1357 1358<span class="hljs-keyword">class</span> <span class="hljs-title class_">Password</span> { 1359 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">value</span>) { 1360 <span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span> = value; 1361 } 1362 1363 <span class="hljs-title function_">toString</span>(<span class="hljs-params"></span>) { 1364 <span class="hljs-keyword">return</span> <span class="hljs-string">'xxxxxxxx'</span>; 1365 } 1366 1367 [customInspectSymbol](depth, inspectOptions, inspect) { 1368 <span class="hljs-keyword">return</span> <span class="hljs-string">`Password <<span class="hljs-subst">${<span class="hljs-variable language_">this</span>.toString()}</span>>`</span>; 1369 } 1370} 1371 1372<span class="hljs-keyword">const</span> password = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Password</span>(<span class="hljs-string">'r0sebud'</span>); 1373<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(password); 1374<span class="hljs-comment">// Prints Password <xxxxxxxx></span></code> <button class="copy-button">copy</button></pre> 1375<p>See <a href="#custom-inspection-functions-on-objects">Custom inspection functions on Objects</a> for more details.</p> 1376<h4><code>util.inspect.defaultOptions</code><span><a class="mark" href="#utilinspectdefaultoptions" id="utilinspectdefaultoptions">#</a></span><a aria-hidden="true" class="legacy" id="util_util_inspect_defaultoptions"></a></h4> 1377<div class="api_metadata"> 1378<span>Added in: v6.4.0</span> 1379</div> 1380<p>The <code>defaultOptions</code> value allows customization of the default options used by 1381<code>util.inspect</code>. This is useful for functions like <code>console.log</code> or 1382<code>util.format</code> which implicitly call into <code>util.inspect</code>. It shall be set to an 1383object containing one or more valid <a href="#utilinspectobject-options"><code>util.inspect()</code></a> options. Setting 1384option properties directly is also supported.</p> 1385<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1386<span class="hljs-keyword">const</span> arr = <span class="hljs-title class_">Array</span>(<span class="hljs-number">101</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-number">0</span>); 1387 1388<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(arr); <span class="hljs-comment">// Logs the truncated array</span> 1389util.<span class="hljs-property">inspect</span>.<span class="hljs-property">defaultOptions</span>.<span class="hljs-property">maxArrayLength</span> = <span class="hljs-literal">null</span>; 1390<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(arr); <span class="hljs-comment">// logs the full array</span></code> <button class="copy-button">copy</button></pre> 1391</section><section><h3><code>util.isDeepStrictEqual(val1, val2)</code><span><a class="mark" href="#utilisdeepstrictequalval1-val2" id="utilisdeepstrictequalval1-val2">#</a></span><a aria-hidden="true" class="legacy" id="util_util_isdeepstrictequal_val1_val2"></a></h3> 1392<div class="api_metadata"> 1393<span>Added in: v9.0.0</span> 1394</div> 1395<ul> 1396<li><code>val1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1397<li><code>val2</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1398<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1399</ul> 1400<p>Returns <code>true</code> if there is deep strict equality between <code>val1</code> and <code>val2</code>. 1401Otherwise, returns <code>false</code>.</p> 1402<p>See <a href="assert.html#assertdeepstrictequalactual-expected-message"><code>assert.deepStrictEqual()</code></a> for more information about deep strict 1403equality.</p> 1404</section><section><h3>Class: <code>util.MIMEType</code><span><a class="mark" href="#class-utilmimetype" id="class-utilmimetype">#</a></span><a aria-hidden="true" class="legacy" id="util_class_util_mimetype"></a></h3> 1405<div class="api_metadata"> 1406<span>Added in: v18.13.0</span> 1407</div> 1408<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 1409<p>An implementation of <a href="https://bmeck.github.io/node-proposal-mime-api/">the MIMEType class</a>.</p> 1410<p>In accordance with browser conventions, all properties of <code>MIMEType</code> objects 1411are implemented as getters and setters on the class prototype, rather than as 1412data properties on the object itself.</p> 1413<p>A MIME string is a structured string containing multiple meaningful 1414components. When parsed, a <code>MIMEType</code> object is returned containing 1415properties for each of these components.</p> 1416<h4>Constructor: <code>new MIMEType(input)</code><span><a class="mark" href="#constructor-new-mimetypeinput" id="constructor-new-mimetypeinput">#</a></span><a aria-hidden="true" class="legacy" id="util_constructor_new_mimetype_input"></a></h4> 1417<ul> 1418<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The input MIME to parse</li> 1419</ul> 1420<p>Creates a new <code>MIMEType</code> object by parsing the <code>input</code>.</p> 1421 1422<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">MIMEType</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:util'</span>; 1423 1424<span class="hljs-keyword">const</span> myMIME = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'text/plain'</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MIMEType</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1425 1426<span class="hljs-keyword">const</span> myMIME = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'text/plain'</span>);</code><button class="copy-button">copy</button></pre> 1427<p>A <code>TypeError</code> will be thrown if the <code>input</code> is not a valid MIME. Note 1428that an effort will be made to coerce the given values into strings. For 1429instance:</p> 1430 1431<pre class="with-62-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">MIMEType</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:util'</span>; 1432<span class="hljs-keyword">const</span> myMIME = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>({ <span class="hljs-attr">toString</span>: <span class="hljs-function">() =></span> <span class="hljs-string">'text/plain'</span> }); 1433<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">String</span>(myMIME)); 1434<span class="hljs-comment">// Prints: text/plain</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MIMEType</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1435<span class="hljs-keyword">const</span> myMIME = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>({ <span class="hljs-attr">toString</span>: <span class="hljs-function">() =></span> <span class="hljs-string">'text/plain'</span> }); 1436<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">String</span>(myMIME)); 1437<span class="hljs-comment">// Prints: text/plain</span></code><button class="copy-button">copy</button></pre> 1438<h4><code>mime.type</code><span><a class="mark" href="#mimetype" id="mimetype">#</a></span><a aria-hidden="true" class="legacy" id="util_mime_type"></a></h4> 1439<ul> 1440<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1441</ul> 1442<p>Gets and sets the type portion of the MIME.</p> 1443 1444<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">MIMEType</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:util'</span>; 1445 1446<span class="hljs-keyword">const</span> myMIME = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'text/javascript'</span>); 1447<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">type</span>); 1448<span class="hljs-comment">// Prints: text</span> 1449myMIME.<span class="hljs-property">type</span> = <span class="hljs-string">'application'</span>; 1450<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">type</span>); 1451<span class="hljs-comment">// Prints: application</span> 1452<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">String</span>(myMIME)); 1453<span class="hljs-comment">// Prints: application/javascript</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MIMEType</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1454 1455<span class="hljs-keyword">const</span> myMIME = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'text/javascript'</span>); 1456<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">type</span>); 1457<span class="hljs-comment">// Prints: text</span> 1458myMIME.<span class="hljs-property">type</span> = <span class="hljs-string">'application'</span>; 1459<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">type</span>); 1460<span class="hljs-comment">// Prints: application</span> 1461<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">String</span>(myMIME)); 1462<span class="hljs-comment">// Prints: application/javascript</span></code><button class="copy-button">copy</button></pre> 1463<h4><code>mime.subtype</code><span><a class="mark" href="#mimesubtype" id="mimesubtype">#</a></span><a aria-hidden="true" class="legacy" id="util_mime_subtype"></a></h4> 1464<ul> 1465<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1466</ul> 1467<p>Gets and sets the subtype portion of the MIME.</p> 1468 1469<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">MIMEType</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:util'</span>; 1470 1471<span class="hljs-keyword">const</span> myMIME = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'text/ecmascript'</span>); 1472<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">subtype</span>); 1473<span class="hljs-comment">// Prints: ecmascript</span> 1474myMIME.<span class="hljs-property">subtype</span> = <span class="hljs-string">'javascript'</span>; 1475<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">subtype</span>); 1476<span class="hljs-comment">// Prints: javascript</span> 1477<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">String</span>(myMIME)); 1478<span class="hljs-comment">// Prints: text/javascript</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MIMEType</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1479 1480<span class="hljs-keyword">const</span> myMIME = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'text/ecmascript'</span>); 1481<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">subtype</span>); 1482<span class="hljs-comment">// Prints: ecmascript</span> 1483myMIME.<span class="hljs-property">subtype</span> = <span class="hljs-string">'javascript'</span>; 1484<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">subtype</span>); 1485<span class="hljs-comment">// Prints: javascript</span> 1486<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">String</span>(myMIME)); 1487<span class="hljs-comment">// Prints: text/javascript</span></code><button class="copy-button">copy</button></pre> 1488<h4><code>mime.essence</code><span><a class="mark" href="#mimeessence" id="mimeessence">#</a></span><a aria-hidden="true" class="legacy" id="util_mime_essence"></a></h4> 1489<ul> 1490<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1491</ul> 1492<p>Gets the essence of the MIME. This property is read only. 1493Use <code>mime.type</code> or <code>mime.subtype</code> to alter the MIME.</p> 1494 1495<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">MIMEType</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:util'</span>; 1496 1497<span class="hljs-keyword">const</span> myMIME = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'text/javascript;key=value'</span>); 1498<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">essence</span>); 1499<span class="hljs-comment">// Prints: text/javascript</span> 1500myMIME.<span class="hljs-property">type</span> = <span class="hljs-string">'application'</span>; 1501<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">essence</span>); 1502<span class="hljs-comment">// Prints: application/javascript</span> 1503<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">String</span>(myMIME)); 1504<span class="hljs-comment">// Prints: application/javascript;key=value</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MIMEType</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1505 1506<span class="hljs-keyword">const</span> myMIME = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'text/javascript;key=value'</span>); 1507<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">essence</span>); 1508<span class="hljs-comment">// Prints: text/javascript</span> 1509myMIME.<span class="hljs-property">type</span> = <span class="hljs-string">'application'</span>; 1510<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">essence</span>); 1511<span class="hljs-comment">// Prints: application/javascript</span> 1512<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">String</span>(myMIME)); 1513<span class="hljs-comment">// Prints: application/javascript;key=value</span></code><button class="copy-button">copy</button></pre> 1514<h4><code>mime.params</code><span><a class="mark" href="#mimeparams" id="mimeparams">#</a></span><a aria-hidden="true" class="legacy" id="util_mime_params"></a></h4> 1515<ul> 1516<li><a href="util.html#class-utilmimeparams" class="type"><MIMEParams></a></li> 1517</ul> 1518<p>Gets the <a href="#class-utilmimeparams"><code>MIMEParams</code></a> object representing the 1519parameters of the MIME. This property is read-only. See 1520<a href="#class-utilmimeparams"><code>MIMEParams</code></a> documentation for details.</p> 1521<h4><code>mime.toString()</code><span><a class="mark" href="#mimetostring" id="mimetostring">#</a></span><a aria-hidden="true" class="legacy" id="util_mime_tostring"></a></h4> 1522<ul> 1523<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1524</ul> 1525<p>The <code>toString()</code> method on the <code>MIMEType</code> object returns the serialized MIME.</p> 1526<p>Because of the need for standard compliance, this method does not allow users 1527to customize the serialization process of the MIME.</p> 1528<h4><code>mime.toJSON()</code><span><a class="mark" href="#mimetojson" id="mimetojson">#</a></span><a aria-hidden="true" class="legacy" id="util_mime_tojson"></a></h4> 1529<ul> 1530<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1531</ul> 1532<p>Alias for <a href="#mimetostring"><code>mime.toString()</code></a>.</p> 1533<p>This method is automatically called when an <code>MIMEType</code> object is serialized 1534with <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify"><code>JSON.stringify()</code></a>.</p> 1535 1536<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">MIMEType</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:util'</span>; 1537 1538<span class="hljs-keyword">const</span> myMIMES = [ 1539 <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'image/png'</span>), 1540 <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'image/gif'</span>), 1541]; 1542<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(myMIMES)); 1543<span class="hljs-comment">// Prints: ["image/png", "image/gif"]</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MIMEType</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1544 1545<span class="hljs-keyword">const</span> myMIMES = [ 1546 <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'image/png'</span>), 1547 <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'image/gif'</span>), 1548]; 1549<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(myMIMES)); 1550<span class="hljs-comment">// Prints: ["image/png", "image/gif"]</span></code><button class="copy-button">copy</button></pre> 1551</section><section><h3>Class: <code>util.MIMEParams</code><span><a class="mark" href="#class-utilmimeparams" id="class-utilmimeparams">#</a></span><a aria-hidden="true" class="legacy" id="util_class_util_mimeparams"></a></h3> 1552<div class="api_metadata"> 1553<span>Added in: v18.13.0</span> 1554</div> 1555<p>The <code>MIMEParams</code> API provides read and write access to the parameters of a 1556<code>MIMEType</code>.</p> 1557<h4>Constructor: <code>new MIMEParams()</code><span><a class="mark" href="#constructor-new-mimeparams" id="constructor-new-mimeparams">#</a></span><a aria-hidden="true" class="legacy" id="util_constructor_new_mimeparams"></a></h4> 1558<p>Creates a new <code>MIMEParams</code> object by with empty parameters</p> 1559 1560<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">MIMEParams</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:util'</span>; 1561 1562<span class="hljs-keyword">const</span> myParams = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEParams</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MIMEParams</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1563 1564<span class="hljs-keyword">const</span> myParams = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEParams</span>();</code><button class="copy-button">copy</button></pre> 1565<h4><code>mimeParams.delete(name)</code><span><a class="mark" href="#mimeparamsdeletename" id="mimeparamsdeletename">#</a></span><a aria-hidden="true" class="legacy" id="util_mimeparams_delete_name"></a></h4> 1566<ul> 1567<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1568</ul> 1569<p>Remove all name-value pairs whose name is <code>name</code>.</p> 1570<h4><code>mimeParams.entries()</code><span><a class="mark" href="#mimeparamsentries" id="mimeparamsentries">#</a></span><a aria-hidden="true" class="legacy" id="util_mimeparams_entries"></a></h4> 1571<ul> 1572<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 1573</ul> 1574<p>Returns an iterator over each of the name-value pairs in the parameters. 1575Each item of the iterator is a JavaScript <code>Array</code>. The first item of the array 1576is the <code>name</code>, the second item of the array is the <code>value</code>.</p> 1577<h4><code>mimeParams.get(name)</code><span><a class="mark" href="#mimeparamsgetname" id="mimeparamsgetname">#</a></span><a aria-hidden="true" class="legacy" id="util_mimeparams_get_name"></a></h4> 1578<ul> 1579<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1580<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> or <code>null</code> if there is no name-value pair with the given 1581<code>name</code>.</li> 1582</ul> 1583<p>Returns the value of the first name-value pair whose name is <code>name</code>. If there 1584are no such pairs, <code>null</code> is returned.</p> 1585<h4><code>mimeParams.has(name)</code><span><a class="mark" href="#mimeparamshasname" id="mimeparamshasname">#</a></span><a aria-hidden="true" class="legacy" id="util_mimeparams_has_name"></a></h4> 1586<ul> 1587<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1588<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1589</ul> 1590<p>Returns <code>true</code> if there is at least one name-value pair whose name is <code>name</code>.</p> 1591<h4><code>mimeParams.keys()</code><span><a class="mark" href="#mimeparamskeys" id="mimeparamskeys">#</a></span><a aria-hidden="true" class="legacy" id="util_mimeparams_keys"></a></h4> 1592<ul> 1593<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 1594</ul> 1595<p>Returns an iterator over the names of each name-value pair.</p> 1596 1597<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">MIMEType</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:util'</span>; 1598 1599<span class="hljs-keyword">const</span> { params } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'text/plain;foo=0;bar=1'</span>); 1600<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="hljs-keyword">of</span> params.<span class="hljs-title function_">keys</span>()) { 1601 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name); 1602} 1603<span class="hljs-comment">// Prints:</span> 1604<span class="hljs-comment">// foo</span> 1605<span class="hljs-comment">// bar</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MIMEType</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1606 1607<span class="hljs-keyword">const</span> { params } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'text/plain;foo=0;bar=1'</span>); 1608<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="hljs-keyword">of</span> params.<span class="hljs-title function_">keys</span>()) { 1609 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name); 1610} 1611<span class="hljs-comment">// Prints:</span> 1612<span class="hljs-comment">// foo</span> 1613<span class="hljs-comment">// bar</span></code><button class="copy-button">copy</button></pre> 1614<h4><code>mimeParams.set(name, value)</code><span><a class="mark" href="#mimeparamssetname-value" id="mimeparamssetname-value">#</a></span><a aria-hidden="true" class="legacy" id="util_mimeparams_set_name_value"></a></h4> 1615<ul> 1616<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1617<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1618</ul> 1619<p>Sets the value in the <code>MIMEParams</code> object associated with <code>name</code> to 1620<code>value</code>. If there are any pre-existing name-value pairs whose names are <code>name</code>, 1621set the first such pair's value to <code>value</code>.</p> 1622 1623<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">MIMEType</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:util'</span>; 1624 1625<span class="hljs-keyword">const</span> { params } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'text/plain;foo=0;bar=1'</span>); 1626params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'def'</span>); 1627params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'baz'</span>, <span class="hljs-string">'xyz'</span>); 1628<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>()); 1629<span class="hljs-comment">// Prints: foo=def&bar=1&baz=xyz</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MIMEType</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1630 1631<span class="hljs-keyword">const</span> { params } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'text/plain;foo=0;bar=1'</span>); 1632params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'def'</span>); 1633params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'baz'</span>, <span class="hljs-string">'xyz'</span>); 1634<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>()); 1635<span class="hljs-comment">// Prints: foo=def&bar=1&baz=xyz</span></code><button class="copy-button">copy</button></pre> 1636<h4><code>mimeParams.values()</code><span><a class="mark" href="#mimeparamsvalues" id="mimeparamsvalues">#</a></span><a aria-hidden="true" class="legacy" id="util_mimeparams_values"></a></h4> 1637<ul> 1638<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 1639</ul> 1640<p>Returns an iterator over the values of each name-value pair.</p> 1641<h4><code>mimeParams[@@iterator]()</code><span><a class="mark" href="#mimeparamsiterator" id="mimeparamsiterator">#</a></span><a aria-hidden="true" class="legacy" id="util_mimeparams_iterator"></a></h4> 1642<ul> 1643<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 1644</ul> 1645<p>Alias for <a href="#mimeparamsentries"><code>mimeParams.entries()</code></a>.</p> 1646 1647<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">MIMEType</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:util'</span>; 1648 1649<span class="hljs-keyword">const</span> { params } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'text/plain;foo=bar;xyz=baz'</span>); 1650<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> [name, value] <span class="hljs-keyword">of</span> params) { 1651 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name, value); 1652} 1653<span class="hljs-comment">// Prints:</span> 1654<span class="hljs-comment">// foo bar</span> 1655<span class="hljs-comment">// xyz baz</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MIMEType</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1656 1657<span class="hljs-keyword">const</span> { params } = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'text/plain;foo=bar;xyz=baz'</span>); 1658<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> [name, value] <span class="hljs-keyword">of</span> params) { 1659 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name, value); 1660} 1661<span class="hljs-comment">// Prints:</span> 1662<span class="hljs-comment">// foo bar</span> 1663<span class="hljs-comment">// xyz baz</span></code><button class="copy-button">copy</button></pre> 1664</section><section><h3><code>util.parseArgs([config])</code><span><a class="mark" href="#utilparseargsconfig" id="utilparseargsconfig">#</a></span><a aria-hidden="true" class="legacy" id="util_util_parseargs_config"></a></h3> 1665<div class="api_metadata"> 1666<details class="changelog"><summary>History</summary> 1667<table> 1668<tbody><tr><th>Version</th><th>Changes</th></tr> 1669<tr><td>v18.11.0</td> 1670<td><p>Add support for default values in input <code>config</code>.</p></td></tr> 1671<tr><td>v18.7.0, v16.17.0</td> 1672<td><p>add support for returning detailed parse information using <code>tokens</code> in input <code>config</code> and returned properties.</p></td></tr> 1673<tr><td>v18.3.0</td> 1674<td><p><span>Added in: v18.3.0</span></p></td></tr> 1675</tbody></table> 1676</details> 1677</div> 1678<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 1679<ul> 1680<li> 1681<p><code>config</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Used to provide arguments for parsing and to configure 1682the parser. <code>config</code> supports the following properties:</p> 1683<ul> 1684<li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> array of argument strings. <strong>Default:</strong> <code>process.argv</code> 1685with <code>execPath</code> and <code>filename</code> removed.</li> 1686<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Used to describe arguments known to the parser. 1687Keys of <code>options</code> are the long names of options and values are an 1688<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> accepting the following properties: 1689<ul> 1690<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Type of argument, which must be either <code>boolean</code> or <code>string</code>.</li> 1691<li><code>multiple</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether this option can be provided multiple 1692times. If <code>true</code>, all values will be collected in an array. If 1693<code>false</code>, values for the option are last-wins. <strong>Default:</strong> <code>false</code>.</li> 1694<li><code>short</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A single character alias for the option.</li> 1695<li><code>default</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean[]></a> The default option 1696value when it is not set by args. It must be of the same type as the 1697<code>type</code> property. When <code>multiple</code> is <code>true</code>, it must be an array.</li> 1698</ul> 1699</li> 1700<li><code>strict</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Should an error be thrown when unknown arguments 1701are encountered, or when arguments are passed that do not match the 1702<code>type</code> configured in <code>options</code>. 1703<strong>Default:</strong> <code>true</code>.</li> 1704<li><code>allowPositionals</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether this command accepts positional 1705arguments. 1706<strong>Default:</strong> <code>false</code> if <code>strict</code> is <code>true</code>, otherwise <code>true</code>.</li> 1707<li><code>tokens</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Return the parsed tokens. This is useful for extending 1708the built-in behavior, from adding additional checks through to reprocessing 1709the tokens in different ways. 1710<strong>Default:</strong> <code>false</code>.</li> 1711</ul> 1712</li> 1713<li> 1714<p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> The parsed command line arguments:</p> 1715<ul> 1716<li><code>values</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> A mapping of parsed option names with their <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> 1717or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> values.</li> 1718<li><code>positionals</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> Positional arguments.</li> 1719<li><code>tokens</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> See <a href="#parseargs-tokens">parseArgs tokens</a> 1720section. Only returned if <code>config</code> includes <code>tokens: true</code>.</li> 1721</ul> 1722</li> 1723</ul> 1724<p>Provides a higher level API for command-line argument parsing than interacting 1725with <code>process.argv</code> directly. Takes a specification for the expected arguments 1726and returns a structured object with the parsed options and positionals.</p> 1727 1728<pre class="with-43-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { parseArgs } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:util'</span>; 1729<span class="hljs-keyword">const</span> args = [<span class="hljs-string">'-f'</span>, <span class="hljs-string">'--bar'</span>, <span class="hljs-string">'b'</span>]; 1730<span class="hljs-keyword">const</span> options = { 1731 <span class="hljs-attr">foo</span>: { 1732 <span class="hljs-attr">type</span>: <span class="hljs-string">'boolean'</span>, 1733 <span class="hljs-attr">short</span>: <span class="hljs-string">'f'</span>, 1734 }, 1735 <span class="hljs-attr">bar</span>: { 1736 <span class="hljs-attr">type</span>: <span class="hljs-string">'string'</span>, 1737 }, 1738}; 1739<span class="hljs-keyword">const</span> { 1740 values, 1741 positionals, 1742} = <span class="hljs-title function_">parseArgs</span>({ args, options }); 1743<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(values, positionals); 1744<span class="hljs-comment">// Prints: [Object: null prototype] { foo: true, bar: 'b' } []</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { parseArgs } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1745<span class="hljs-keyword">const</span> args = [<span class="hljs-string">'-f'</span>, <span class="hljs-string">'--bar'</span>, <span class="hljs-string">'b'</span>]; 1746<span class="hljs-keyword">const</span> options = { 1747 <span class="hljs-attr">foo</span>: { 1748 <span class="hljs-attr">type</span>: <span class="hljs-string">'boolean'</span>, 1749 <span class="hljs-attr">short</span>: <span class="hljs-string">'f'</span>, 1750 }, 1751 <span class="hljs-attr">bar</span>: { 1752 <span class="hljs-attr">type</span>: <span class="hljs-string">'string'</span>, 1753 }, 1754}; 1755<span class="hljs-keyword">const</span> { 1756 values, 1757 positionals, 1758} = <span class="hljs-title function_">parseArgs</span>({ args, options }); 1759<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(values, positionals); 1760<span class="hljs-comment">// Prints: [Object: null prototype] { foo: true, bar: 'b' } []</span></code><button class="copy-button">copy</button></pre> 1761<p><code>util.parseArgs</code> is experimental and behavior may change. Join the 1762conversation in <a href="https://github.com/pkgjs/parseargs">pkgjs/parseargs</a> to contribute to the design.</p> 1763<h4><code>parseArgs</code> <code>tokens</code><span><a class="mark" href="#parseargs-tokens" id="parseargs-tokens">#</a></span><a aria-hidden="true" class="legacy" id="util_parseargs_tokens"></a></h4> 1764<p>Detailed parse information is available for adding custom behaviors by 1765specifying <code>tokens: true</code> in the configuration. 1766The returned tokens have properties describing:</p> 1767<ul> 1768<li>all tokens 1769<ul> 1770<li><code>kind</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> One of 'option', 'positional', or 'option-terminator'.</li> 1771<li><code>index</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Index of element in <code>args</code> containing token. So the 1772source argument for a token is <code>args[token.index]</code>.</li> 1773</ul> 1774</li> 1775<li>option tokens 1776<ul> 1777<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Long name of option.</li> 1778<li><code>rawName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> How option used in args, like <code>-f</code> of <code>--foo</code>.</li> 1779<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> Option value specified in args. 1780Undefined for boolean options.</li> 1781<li><code>inlineValue</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> Whether option value specified inline, 1782like <code>--foo=bar</code>.</li> 1783</ul> 1784</li> 1785<li>positional tokens 1786<ul> 1787<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The value of the positional argument in args (i.e. <code>args[index]</code>).</li> 1788</ul> 1789</li> 1790<li>option-terminator token</li> 1791</ul> 1792<p>The returned tokens are in the order encountered in the input args. Options 1793that appear more than once in args produce a token for each use. Short option 1794groups like <code>-xy</code> expand to a token for each option. So <code>-xxx</code> produces 1795three tokens.</p> 1796<p>For example to use the returned tokens to add support for a negated option 1797like <code>--no-color</code>, the tokens can be reprocessed to change the value stored 1798for the negated option.</p> 1799 1800<pre class="with-43-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { parseArgs } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:util'</span>; 1801 1802<span class="hljs-keyword">const</span> options = { 1803 <span class="hljs-string">'color'</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">'boolean'</span> }, 1804 <span class="hljs-string">'no-color'</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">'boolean'</span> }, 1805 <span class="hljs-string">'logfile'</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">'string'</span> }, 1806 <span class="hljs-string">'no-logfile'</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">'boolean'</span> }, 1807}; 1808<span class="hljs-keyword">const</span> { values, tokens } = <span class="hljs-title function_">parseArgs</span>({ options, <span class="hljs-attr">tokens</span>: <span class="hljs-literal">true</span> }); 1809 1810<span class="hljs-comment">// Reprocess the option tokens and overwrite the returned values.</span> 1811tokens 1812 .<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">token</span>) =></span> token.<span class="hljs-property">kind</span> === <span class="hljs-string">'option'</span>) 1813 .<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">token</span>) =></span> { 1814 <span class="hljs-keyword">if</span> (token.<span class="hljs-property">name</span>.<span class="hljs-title function_">startsWith</span>(<span class="hljs-string">'no-'</span>)) { 1815 <span class="hljs-comment">// Store foo:false for --no-foo</span> 1816 <span class="hljs-keyword">const</span> positiveName = token.<span class="hljs-property">name</span>.<span class="hljs-title function_">slice</span>(<span class="hljs-number">3</span>); 1817 values[positiveName] = <span class="hljs-literal">false</span>; 1818 <span class="hljs-keyword">delete</span> values[token.<span class="hljs-property">name</span>]; 1819 } <span class="hljs-keyword">else</span> { 1820 <span class="hljs-comment">// Resave value so last one wins if both --foo and --no-foo.</span> 1821 values[token.<span class="hljs-property">name</span>] = token.<span class="hljs-property">value</span> ?? <span class="hljs-literal">true</span>; 1822 } 1823 }); 1824 1825<span class="hljs-keyword">const</span> color = values.<span class="hljs-property">color</span>; 1826<span class="hljs-keyword">const</span> logfile = values.<span class="hljs-property">logfile</span> ?? <span class="hljs-string">'default.log'</span>; 1827 1828<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>({ logfile, color });</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { parseArgs } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1829 1830<span class="hljs-keyword">const</span> options = { 1831 <span class="hljs-string">'color'</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">'boolean'</span> }, 1832 <span class="hljs-string">'no-color'</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">'boolean'</span> }, 1833 <span class="hljs-string">'logfile'</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">'string'</span> }, 1834 <span class="hljs-string">'no-logfile'</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">'boolean'</span> }, 1835}; 1836<span class="hljs-keyword">const</span> { values, tokens } = <span class="hljs-title function_">parseArgs</span>({ options, <span class="hljs-attr">tokens</span>: <span class="hljs-literal">true</span> }); 1837 1838<span class="hljs-comment">// Reprocess the option tokens and overwrite the returned values.</span> 1839tokens 1840 .<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">token</span>) =></span> token.<span class="hljs-property">kind</span> === <span class="hljs-string">'option'</span>) 1841 .<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">token</span>) =></span> { 1842 <span class="hljs-keyword">if</span> (token.<span class="hljs-property">name</span>.<span class="hljs-title function_">startsWith</span>(<span class="hljs-string">'no-'</span>)) { 1843 <span class="hljs-comment">// Store foo:false for --no-foo</span> 1844 <span class="hljs-keyword">const</span> positiveName = token.<span class="hljs-property">name</span>.<span class="hljs-title function_">slice</span>(<span class="hljs-number">3</span>); 1845 values[positiveName] = <span class="hljs-literal">false</span>; 1846 <span class="hljs-keyword">delete</span> values[token.<span class="hljs-property">name</span>]; 1847 } <span class="hljs-keyword">else</span> { 1848 <span class="hljs-comment">// Resave value so last one wins if both --foo and --no-foo.</span> 1849 values[token.<span class="hljs-property">name</span>] = token.<span class="hljs-property">value</span> ?? <span class="hljs-literal">true</span>; 1850 } 1851 }); 1852 1853<span class="hljs-keyword">const</span> color = values.<span class="hljs-property">color</span>; 1854<span class="hljs-keyword">const</span> logfile = values.<span class="hljs-property">logfile</span> ?? <span class="hljs-string">'default.log'</span>; 1855 1856<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>({ logfile, color });</code><button class="copy-button">copy</button></pre> 1857<p>Example usage showing negated options, and when an option is used 1858multiple ways then last one wins.</p> 1859<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node negate.js</span> 1860{ logfile: 'default.log', color: undefined } 1861<span class="hljs-meta prompt_">$ </span><span class="language-bash">node negate.js --no-logfile --no-color</span> 1862{ logfile: false, color: false } 1863<span class="hljs-meta prompt_">$ </span><span class="language-bash">node negate.js --logfile=test.log --color</span> 1864{ logfile: 'test.log', color: true } 1865<span class="hljs-meta prompt_">$ </span><span class="language-bash">node negate.js --no-logfile --logfile=test.log --color --no-color</span> 1866{ logfile: 'test.log', color: false }</code> <button class="copy-button">copy</button></pre> 1867</section><section><h3><code>util.promisify(original)</code><span><a class="mark" href="#utilpromisifyoriginal" id="utilpromisifyoriginal">#</a></span><a aria-hidden="true" class="legacy" id="util_util_promisify_original"></a></h3> 1868<div class="api_metadata"> 1869<span>Added in: v8.0.0</span> 1870</div> 1871<ul> 1872<li><code>original</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1873<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1874</ul> 1875<p>Takes a function following the common error-first callback style, i.e. taking 1876an <code>(err, value) => ...</code> callback as the last argument, and returns a version 1877that returns promises.</p> 1878<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1879<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 1880 1881<span class="hljs-keyword">const</span> stat = util.<span class="hljs-title function_">promisify</span>(fs.<span class="hljs-property">stat</span>); 1882<span class="hljs-title function_">stat</span>(<span class="hljs-string">'.'</span>).<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">stats</span>) =></span> { 1883 <span class="hljs-comment">// Do something with `stats`</span> 1884}).<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-params">error</span>) =></span> { 1885 <span class="hljs-comment">// Handle the error.</span> 1886});</code> <button class="copy-button">copy</button></pre> 1887<p>Or, equivalently using <code>async function</code>s:</p> 1888<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1889<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 1890 1891<span class="hljs-keyword">const</span> stat = util.<span class="hljs-title function_">promisify</span>(fs.<span class="hljs-property">stat</span>); 1892 1893<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">callStat</span>(<span class="hljs-params"></span>) { 1894 <span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span class="hljs-title function_">stat</span>(<span class="hljs-string">'.'</span>); 1895 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`This directory is owned by <span class="hljs-subst">${stats.uid}</span>`</span>); 1896}</code> <button class="copy-button">copy</button></pre> 1897<p>If there is an <code>original[util.promisify.custom]</code> property present, <code>promisify</code> 1898will return its value, see <a href="#custom-promisified-functions">Custom promisified functions</a>.</p> 1899<p><code>promisify()</code> assumes that <code>original</code> is a function taking a callback as its 1900final argument in all cases. If <code>original</code> is not a function, <code>promisify()</code> 1901will throw an error. If <code>original</code> is a function but its last argument is not 1902an error-first callback, it will still be passed an error-first 1903callback as its last argument.</p> 1904<p>Using <code>promisify()</code> on class methods or other methods that use <code>this</code> may not 1905work as expected unless handled specially:</p> 1906<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1907 1908<span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> { 1909 <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) { 1910 <span class="hljs-variable language_">this</span>.<span class="hljs-property">a</span> = <span class="hljs-number">42</span>; 1911 } 1912 1913 <span class="hljs-title function_">bar</span>(<span class="hljs-params">callback</span>) { 1914 <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">a</span>); 1915 } 1916} 1917 1918<span class="hljs-keyword">const</span> foo = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Foo</span>(); 1919 1920<span class="hljs-keyword">const</span> naiveBar = util.<span class="hljs-title function_">promisify</span>(foo.<span class="hljs-property">bar</span>); 1921<span class="hljs-comment">// TypeError: Cannot read property 'a' of undefined</span> 1922<span class="hljs-comment">// naiveBar().then(a => console.log(a));</span> 1923 1924naiveBar.<span class="hljs-title function_">call</span>(foo).<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">a</span>) =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a)); <span class="hljs-comment">// '42'</span> 1925 1926<span class="hljs-keyword">const</span> bindBar = naiveBar.<span class="hljs-title function_">bind</span>(foo); 1927<span class="hljs-title function_">bindBar</span>().<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">a</span>) =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a)); <span class="hljs-comment">// '42'</span></code> <button class="copy-button">copy</button></pre> 1928<h4>Custom promisified functions<span><a class="mark" href="#custom-promisified-functions" id="custom-promisified-functions">#</a></span><a aria-hidden="true" class="legacy" id="util_custom_promisified_functions"></a></h4> 1929<p>Using the <code>util.promisify.custom</code> symbol one can override the return value of 1930<a href="#utilpromisifyoriginal"><code>util.promisify()</code></a>:</p> 1931<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 1932 1933<span class="hljs-keyword">function</span> <span class="hljs-title function_">doSomething</span>(<span class="hljs-params">foo, callback</span>) { 1934 <span class="hljs-comment">// ...</span> 1935} 1936 1937doSomething[util.<span class="hljs-property">promisify</span>.<span class="hljs-property">custom</span>] = <span class="hljs-function">(<span class="hljs-params">foo</span>) =></span> { 1938 <span class="hljs-keyword">return</span> <span class="hljs-title function_">getPromiseSomehow</span>(); 1939}; 1940 1941<span class="hljs-keyword">const</span> promisified = util.<span class="hljs-title function_">promisify</span>(doSomething); 1942<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(promisified === doSomething[util.<span class="hljs-property">promisify</span>.<span class="hljs-property">custom</span>]); 1943<span class="hljs-comment">// prints 'true'</span></code> <button class="copy-button">copy</button></pre> 1944<p>This can be useful for cases where the original function does not follow the 1945standard format of taking an error-first callback as the last argument.</p> 1946<p>For example, with a function that takes in 1947<code>(foo, onSuccessCallback, onErrorCallback)</code>:</p> 1948<pre><code class="language-js">doSomething[util.<span class="hljs-property">promisify</span>.<span class="hljs-property">custom</span>] = <span class="hljs-function">(<span class="hljs-params">foo</span>) =></span> { 1949 <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =></span> { 1950 <span class="hljs-title function_">doSomething</span>(foo, resolve, reject); 1951 }); 1952};</code> <button class="copy-button">copy</button></pre> 1953<p>If <code>promisify.custom</code> is defined but is not a function, <code>promisify()</code> will 1954throw an error.</p> 1955<h4><code>util.promisify.custom</code><span><a class="mark" href="#utilpromisifycustom" id="utilpromisifycustom">#</a></span><a aria-hidden="true" class="legacy" id="util_util_promisify_custom"></a></h4> 1956<div class="api_metadata"> 1957<details class="changelog"><summary>History</summary> 1958<table> 1959<tbody><tr><th>Version</th><th>Changes</th></tr> 1960<tr><td>v13.12.0, v12.16.2</td> 1961<td><p>This is now defined as a shared symbol.</p></td></tr> 1962<tr><td>v8.0.0</td> 1963<td><p><span>Added in: v8.0.0</span></p></td></tr> 1964</tbody></table> 1965</details> 1966</div> 1967<ul> 1968<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> that can be used to declare custom promisified variants of functions, 1969see <a href="#custom-promisified-functions">Custom promisified functions</a>.</li> 1970</ul> 1971<p>In addition to being accessible through <code>util.promisify.custom</code>, this 1972symbol is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for">registered globally</a> and can be 1973accessed in any environment as <code>Symbol.for('nodejs.util.promisify.custom')</code>.</p> 1974<p>For example, with a function that takes in 1975<code>(foo, onSuccessCallback, onErrorCallback)</code>:</p> 1976<pre><code class="language-js"><span class="hljs-keyword">const</span> kCustomPromisifiedSymbol = <span class="hljs-title class_">Symbol</span>.<span class="hljs-title function_">for</span>(<span class="hljs-string">'nodejs.util.promisify.custom'</span>); 1977 1978doSomething[kCustomPromisifiedSymbol] = <span class="hljs-function">(<span class="hljs-params">foo</span>) =></span> { 1979 <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =></span> { 1980 <span class="hljs-title function_">doSomething</span>(foo, resolve, reject); 1981 }); 1982};</code> <button class="copy-button">copy</button></pre> 1983</section><section><h3><code>util.stripVTControlCharacters(str)</code><span><a class="mark" href="#utilstripvtcontrolcharactersstr" id="utilstripvtcontrolcharactersstr">#</a></span><a aria-hidden="true" class="legacy" id="util_util_stripvtcontrolcharacters_str"></a></h3> 1984<div class="api_metadata"> 1985<span>Added in: v16.11.0</span> 1986</div> 1987<ul> 1988<li><code>str</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1989<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1990</ul> 1991<p>Returns <code>str</code> with any ANSI escape codes removed.</p> 1992<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class="hljs-title function_">stripVTControlCharacters</span>(<span class="hljs-string">'\u001B[4mvalue\u001B[0m'</span>)); 1993<span class="hljs-comment">// Prints "value"</span></code> <button class="copy-button">copy</button></pre> 1994</section><section><h3>Class: <code>util.TextDecoder</code><span><a class="mark" href="#class-utiltextdecoder" id="class-utiltextdecoder">#</a></span><a aria-hidden="true" class="legacy" id="util_class_util_textdecoder"></a></h3> 1995<div class="api_metadata"> 1996<span>Added in: v8.3.0</span> 1997</div> 1998<p>An implementation of the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a> <code>TextDecoder</code> API.</p> 1999<pre><code class="language-js"><span class="hljs-keyword">const</span> decoder = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TextDecoder</span>(); 2000<span class="hljs-keyword">const</span> u8arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>([<span class="hljs-number">72</span>, <span class="hljs-number">101</span>, <span class="hljs-number">108</span>, <span class="hljs-number">108</span>, <span class="hljs-number">111</span>]); 2001<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(decoder.<span class="hljs-title function_">decode</span>(u8arr)); <span class="hljs-comment">// Hello</span></code> <button class="copy-button">copy</button></pre> 2002<h4>WHATWG supported encodings<span><a class="mark" href="#whatwg-supported-encodings" id="whatwg-supported-encodings">#</a></span><a aria-hidden="true" class="legacy" id="util_whatwg_supported_encodings"></a></h4> 2003<p>Per the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a>, the encodings supported by the 2004<code>TextDecoder</code> API are outlined in the tables below. For each encoding, 2005one or more aliases may be used.</p> 2006<p>Different Node.js build configurations support different sets of encodings. 2007(see <a href="intl.html">Internationalization</a>)</p> 2008<h5>Encodings supported by default (with full ICU data)<span><a class="mark" href="#encodings-supported-by-default-with-full-icu-data" id="encodings-supported-by-default-with-full-icu-data">#</a></span><a aria-hidden="true" class="legacy" id="util_encodings_supported_by_default_with_full_icu_data"></a></h5> 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154<table><thead><tr><th>Encoding</th><th>Aliases</th></tr></thead><tbody><tr><td><code>'ibm866'</code></td><td><code>'866'</code>, <code>'cp866'</code>, <code>'csibm866'</code></td></tr><tr><td><code>'iso-8859-2'</code></td><td><code>'csisolatin2'</code>, <code>'iso-ir-101'</code>, <code>'iso8859-2'</code>, <code>'iso88592'</code>, <code>'iso_8859-2'</code>, <code>'iso_8859-2:1987'</code>, <code>'l2'</code>, <code>'latin2'</code></td></tr><tr><td><code>'iso-8859-3'</code></td><td><code>'csisolatin3'</code>, <code>'iso-ir-109'</code>, <code>'iso8859-3'</code>, <code>'iso88593'</code>, <code>'iso_8859-3'</code>, <code>'iso_8859-3:1988'</code>, <code>'l3'</code>, <code>'latin3'</code></td></tr><tr><td><code>'iso-8859-4'</code></td><td><code>'csisolatin4'</code>, <code>'iso-ir-110'</code>, <code>'iso8859-4'</code>, <code>'iso88594'</code>, <code>'iso_8859-4'</code>, <code>'iso_8859-4:1988'</code>, <code>'l4'</code>, <code>'latin4'</code></td></tr><tr><td><code>'iso-8859-5'</code></td><td><code>'csisolatincyrillic'</code>, <code>'cyrillic'</code>, <code>'iso-ir-144'</code>, <code>'iso8859-5'</code>, <code>'iso88595'</code>, <code>'iso_8859-5'</code>, <code>'iso_8859-5:1988'</code></td></tr><tr><td><code>'iso-8859-6'</code></td><td><code>'arabic'</code>, <code>'asmo-708'</code>, <code>'csiso88596e'</code>, <code>'csiso88596i'</code>, <code>'csisolatinarabic'</code>, <code>'ecma-114'</code>, <code>'iso-8859-6-e'</code>, <code>'iso-8859-6-i'</code>, <code>'iso-ir-127'</code>, <code>'iso8859-6'</code>, <code>'iso88596'</code>, <code>'iso_8859-6'</code>, <code>'iso_8859-6:1987'</code></td></tr><tr><td><code>'iso-8859-7'</code></td><td><code>'csisolatingreek'</code>, <code>'ecma-118'</code>, <code>'elot_928'</code>, <code>'greek'</code>, <code>'greek8'</code>, <code>'iso-ir-126'</code>, <code>'iso8859-7'</code>, <code>'iso88597'</code>, <code>'iso_8859-7'</code>, <code>'iso_8859-7:1987'</code>, <code>'sun_eu_greek'</code></td></tr><tr><td><code>'iso-8859-8'</code></td><td><code>'csiso88598e'</code>, <code>'csisolatinhebrew'</code>, <code>'hebrew'</code>, <code>'iso-8859-8-e'</code>, <code>'iso-ir-138'</code>, <code>'iso8859-8'</code>, <code>'iso88598'</code>, <code>'iso_8859-8'</code>, <code>'iso_8859-8:1988'</code>, <code>'visual'</code></td></tr><tr><td><code>'iso-8859-8-i'</code></td><td><code>'csiso88598i'</code>, <code>'logical'</code></td></tr><tr><td><code>'iso-8859-10'</code></td><td><code>'csisolatin6'</code>, <code>'iso-ir-157'</code>, <code>'iso8859-10'</code>, <code>'iso885910'</code>, <code>'l6'</code>, <code>'latin6'</code></td></tr><tr><td><code>'iso-8859-13'</code></td><td><code>'iso8859-13'</code>, <code>'iso885913'</code></td></tr><tr><td><code>'iso-8859-14'</code></td><td><code>'iso8859-14'</code>, <code>'iso885914'</code></td></tr><tr><td><code>'iso-8859-15'</code></td><td><code>'csisolatin9'</code>, <code>'iso8859-15'</code>, <code>'iso885915'</code>, <code>'iso_8859-15'</code>, <code>'l9'</code></td></tr><tr><td><code>'koi8-r'</code></td><td><code>'cskoi8r'</code>, <code>'koi'</code>, <code>'koi8'</code>, <code>'koi8_r'</code></td></tr><tr><td><code>'koi8-u'</code></td><td><code>'koi8-ru'</code></td></tr><tr><td><code>'macintosh'</code></td><td><code>'csmacintosh'</code>, <code>'mac'</code>, <code>'x-mac-roman'</code></td></tr><tr><td><code>'windows-874'</code></td><td><code>'dos-874'</code>, <code>'iso-8859-11'</code>, <code>'iso8859-11'</code>, <code>'iso885911'</code>, <code>'tis-620'</code></td></tr><tr><td><code>'windows-1250'</code></td><td><code>'cp1250'</code>, <code>'x-cp1250'</code></td></tr><tr><td><code>'windows-1251'</code></td><td><code>'cp1251'</code>, <code>'x-cp1251'</code></td></tr><tr><td><code>'windows-1252'</code></td><td><code>'ansi_x3.4-1968'</code>, <code>'ascii'</code>, <code>'cp1252'</code>, <code>'cp819'</code>, <code>'csisolatin1'</code>, <code>'ibm819'</code>, <code>'iso-8859-1'</code>, <code>'iso-ir-100'</code>, <code>'iso8859-1'</code>, <code>'iso88591'</code>, <code>'iso_8859-1'</code>, <code>'iso_8859-1:1987'</code>, <code>'l1'</code>, <code>'latin1'</code>, <code>'us-ascii'</code>, <code>'x-cp1252'</code></td></tr><tr><td><code>'windows-1253'</code></td><td><code>'cp1253'</code>, <code>'x-cp1253'</code></td></tr><tr><td><code>'windows-1254'</code></td><td><code>'cp1254'</code>, <code>'csisolatin5'</code>, <code>'iso-8859-9'</code>, <code>'iso-ir-148'</code>, <code>'iso8859-9'</code>, <code>'iso88599'</code>, <code>'iso_8859-9'</code>, <code>'iso_8859-9:1989'</code>, <code>'l5'</code>, <code>'latin5'</code>, <code>'x-cp1254'</code></td></tr><tr><td><code>'windows-1255'</code></td><td><code>'cp1255'</code>, <code>'x-cp1255'</code></td></tr><tr><td><code>'windows-1256'</code></td><td><code>'cp1256'</code>, <code>'x-cp1256'</code></td></tr><tr><td><code>'windows-1257'</code></td><td><code>'cp1257'</code>, <code>'x-cp1257'</code></td></tr><tr><td><code>'windows-1258'</code></td><td><code>'cp1258'</code>, <code>'x-cp1258'</code></td></tr><tr><td><code>'x-mac-cyrillic'</code></td><td><code>'x-mac-ukrainian'</code></td></tr><tr><td><code>'gbk'</code></td><td><code>'chinese'</code>, <code>'csgb2312'</code>, <code>'csiso58gb231280'</code>, <code>'gb2312'</code>, <code>'gb_2312'</code>, <code>'gb_2312-80'</code>, <code>'iso-ir-58'</code>, <code>'x-gbk'</code></td></tr><tr><td><code>'gb18030'</code></td><td></td></tr><tr><td><code>'big5'</code></td><td><code>'big5-hkscs'</code>, <code>'cn-big5'</code>, <code>'csbig5'</code>, <code>'x-x-big5'</code></td></tr><tr><td><code>'euc-jp'</code></td><td><code>'cseucpkdfmtjapanese'</code>, <code>'x-euc-jp'</code></td></tr><tr><td><code>'iso-2022-jp'</code></td><td><code>'csiso2022jp'</code></td></tr><tr><td><code>'shift_jis'</code></td><td><code>'csshiftjis'</code>, <code>'ms932'</code>, <code>'ms_kanji'</code>, <code>'shift-jis'</code>, <code>'sjis'</code>, <code>'windows-31j'</code>, <code>'x-sjis'</code></td></tr><tr><td><code>'euc-kr'</code></td><td><code>'cseuckr'</code>, <code>'csksc56011987'</code>, <code>'iso-ir-149'</code>, <code>'korean'</code>, <code>'ks_c_5601-1987'</code>, <code>'ks_c_5601-1989'</code>, <code>'ksc5601'</code>, <code>'ksc_5601'</code>, <code>'windows-949'</code></td></tr></tbody></table> 2155<h5>Encodings supported when Node.js is built with the <code>small-icu</code> option<span><a class="mark" href="#encodings-supported-when-nodejs-is-built-with-the-small-icu-option" id="encodings-supported-when-nodejs-is-built-with-the-small-icu-option">#</a></span><a aria-hidden="true" class="legacy" id="util_encodings_supported_when_node_js_is_built_with_the_small_icu_option"></a></h5> 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177<table><thead><tr><th>Encoding</th><th>Aliases</th></tr></thead><tbody><tr><td><code>'utf-8'</code></td><td><code>'unicode-1-1-utf-8'</code>, <code>'utf8'</code></td></tr><tr><td><code>'utf-16le'</code></td><td><code>'utf-16'</code></td></tr><tr><td><code>'utf-16be'</code></td><td></td></tr></tbody></table> 2178<h5>Encodings supported when ICU is disabled<span><a class="mark" href="#encodings-supported-when-icu-is-disabled" id="encodings-supported-when-icu-is-disabled">#</a></span><a aria-hidden="true" class="legacy" id="util_encodings_supported_when_icu_is_disabled"></a></h5> 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196<table><thead><tr><th>Encoding</th><th>Aliases</th></tr></thead><tbody><tr><td><code>'utf-8'</code></td><td><code>'unicode-1-1-utf-8'</code>, <code>'utf8'</code></td></tr><tr><td><code>'utf-16le'</code></td><td><code>'utf-16'</code></td></tr></tbody></table> 2197<p>The <code>'iso-8859-16'</code> encoding listed in the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a> 2198is not supported.</p> 2199<h4><code>new TextDecoder([encoding[, options]])</code><span><a class="mark" href="#new-textdecoderencoding-options" id="new-textdecoderencoding-options">#</a></span><a aria-hidden="true" class="legacy" id="util_new_textdecoder_encoding_options"></a></h4> 2200<div class="api_metadata"> 2201<details class="changelog"><summary>History</summary> 2202<table> 2203<tbody><tr><th>Version</th><th>Changes</th></tr> 2204<tr><td>v11.0.0</td> 2205<td><p>The class is now available on the global object.</p></td></tr> 2206<tr><td>v8.3.0</td> 2207<td><p><span>Added in: v8.3.0</span></p></td></tr> 2208</tbody></table> 2209</details> 2210</div> 2211<ul> 2212<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Identifies the <code>encoding</code> that this <code>TextDecoder</code> instance 2213supports. <strong>Default:</strong> <code>'utf-8'</code>.</li> 2214<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2215<ul> 2216<li><code>fatal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if decoding failures are fatal. 2217This option is not supported when ICU is disabled 2218(see <a href="intl.html">Internationalization</a>). <strong>Default:</strong> <code>false</code>.</li> 2219<li><code>ignoreBOM</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, the <code>TextDecoder</code> will include the byte 2220order mark in the decoded result. When <code>false</code>, the byte order mark will 2221be removed from the output. This option is only used when <code>encoding</code> is 2222<code>'utf-8'</code>, <code>'utf-16be'</code>, or <code>'utf-16le'</code>. <strong>Default:</strong> <code>false</code>.</li> 2223</ul> 2224</li> 2225</ul> 2226<p>Creates a new <code>TextDecoder</code> instance. The <code>encoding</code> may specify one of the 2227supported encodings or an alias.</p> 2228<p>The <code>TextDecoder</code> class is also available on the global object.</p> 2229<h4><code>textDecoder.decode([input[, options]])</code><span><a class="mark" href="#textdecoderdecodeinput-options" id="textdecoderdecodeinput-options">#</a></span><a aria-hidden="true" class="legacy" id="util_textdecoder_decode_input_options"></a></h4> 2230<ul> 2231<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type"><ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> An <code>ArrayBuffer</code>, <code>DataView</code>, or 2232<code>TypedArray</code> instance containing the encoded data.</li> 2233<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2234<ul> 2235<li><code>stream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if additional chunks of data are expected. 2236<strong>Default:</strong> <code>false</code>.</li> 2237</ul> 2238</li> 2239<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2240</ul> 2241<p>Decodes the <code>input</code> and returns a string. If <code>options.stream</code> is <code>true</code>, any 2242incomplete byte sequences occurring at the end of the <code>input</code> are buffered 2243internally and emitted after the next call to <code>textDecoder.decode()</code>.</p> 2244<p>If <code>textDecoder.fatal</code> is <code>true</code>, decoding errors that occur will result in a 2245<code>TypeError</code> being thrown.</p> 2246<h4><code>textDecoder.encoding</code><span><a class="mark" href="#textdecoderencoding" id="textdecoderencoding">#</a></span><a aria-hidden="true" class="legacy" id="util_textdecoder_encoding"></a></h4> 2247<ul> 2248<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2249</ul> 2250<p>The encoding supported by the <code>TextDecoder</code> instance.</p> 2251<h4><code>textDecoder.fatal</code><span><a class="mark" href="#textdecoderfatal" id="textdecoderfatal">#</a></span><a aria-hidden="true" class="legacy" id="util_textdecoder_fatal"></a></h4> 2252<ul> 2253<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2254</ul> 2255<p>The value will be <code>true</code> if decoding errors result in a <code>TypeError</code> being 2256thrown.</p> 2257<h4><code>textDecoder.ignoreBOM</code><span><a class="mark" href="#textdecoderignorebom" id="textdecoderignorebom">#</a></span><a aria-hidden="true" class="legacy" id="util_textdecoder_ignorebom"></a></h4> 2258<ul> 2259<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2260</ul> 2261<p>The value will be <code>true</code> if the decoding result will include the byte order 2262mark.</p> 2263</section><section><h3>Class: <code>util.TextEncoder</code><span><a class="mark" href="#class-utiltextencoder" id="class-utiltextencoder">#</a></span><a aria-hidden="true" class="legacy" id="util_class_util_textencoder"></a></h3> 2264<div class="api_metadata"> 2265<details class="changelog"><summary>History</summary> 2266<table> 2267<tbody><tr><th>Version</th><th>Changes</th></tr> 2268<tr><td>v11.0.0</td> 2269<td><p>The class is now available on the global object.</p></td></tr> 2270<tr><td>v8.3.0</td> 2271<td><p><span>Added in: v8.3.0</span></p></td></tr> 2272</tbody></table> 2273</details> 2274</div> 2275<p>An implementation of the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a> <code>TextEncoder</code> API. All 2276instances of <code>TextEncoder</code> only support UTF-8 encoding.</p> 2277<pre><code class="language-js"><span class="hljs-keyword">const</span> encoder = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TextEncoder</span>(); 2278<span class="hljs-keyword">const</span> uint8array = encoder.<span class="hljs-title function_">encode</span>(<span class="hljs-string">'this is some data'</span>);</code> <button class="copy-button">copy</button></pre> 2279<p>The <code>TextEncoder</code> class is also available on the global object.</p> 2280<h4><code>textEncoder.encode([input])</code><span><a class="mark" href="#textencoderencodeinput" id="textencoderencodeinput">#</a></span><a aria-hidden="true" class="legacy" id="util_textencoder_encode_input"></a></h4> 2281<ul> 2282<li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The text to encode. <strong>Default:</strong> an empty string.</li> 2283<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a></li> 2284</ul> 2285<p>UTF-8 encodes the <code>input</code> string and returns a <code>Uint8Array</code> containing the 2286encoded bytes.</p> 2287<h4><code>textEncoder.encodeInto(src, dest)</code><span><a class="mark" href="#textencoderencodeintosrc-dest" id="textencoderencodeintosrc-dest">#</a></span><a aria-hidden="true" class="legacy" id="util_textencoder_encodeinto_src_dest"></a></h4> 2288<ul> 2289<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The text to encode.</li> 2290<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a> The array to hold the encode result.</li> 2291<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2292<ul> 2293<li><code>read</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The read Unicode code units of src.</li> 2294<li><code>written</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The written UTF-8 bytes of dest.</li> 2295</ul> 2296</li> 2297</ul> 2298<p>UTF-8 encodes the <code>src</code> string to the <code>dest</code> Uint8Array and returns an object 2299containing the read Unicode code units and written UTF-8 bytes.</p> 2300<pre><code class="language-js"><span class="hljs-keyword">const</span> encoder = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TextEncoder</span>(); 2301<span class="hljs-keyword">const</span> src = <span class="hljs-string">'this is some data'</span>; 2302<span class="hljs-keyword">const</span> dest = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(<span class="hljs-number">10</span>); 2303<span class="hljs-keyword">const</span> { read, written } = encoder.<span class="hljs-title function_">encodeInto</span>(src, dest);</code> <button class="copy-button">copy</button></pre> 2304<h4><code>textEncoder.encoding</code><span><a class="mark" href="#textencoderencoding" id="textencoderencoding">#</a></span><a aria-hidden="true" class="legacy" id="util_textencoder_encoding"></a></h4> 2305<ul> 2306<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2307</ul> 2308<p>The encoding supported by the <code>TextEncoder</code> instance. Always set to <code>'utf-8'</code>.</p> 2309</section><section><h3><code>util.toUSVString(string)</code><span><a class="mark" href="#utiltousvstringstring" id="utiltousvstringstring">#</a></span><a aria-hidden="true" class="legacy" id="util_util_tousvstring_string"></a></h3> 2310<div class="api_metadata"> 2311<span>Added in: v16.8.0, v14.18.0</span> 2312</div> 2313<ul> 2314<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2315</ul> 2316<p>Returns the <code>string</code> after replacing any surrogate code points 2317(or equivalently, any unpaired surrogate code units) with the 2318Unicode "replacement character" U+FFFD.</p> 2319</section><section><h3><code>util.transferableAbortController()</code><span><a class="mark" href="#utiltransferableabortcontroller" id="utiltransferableabortcontroller">#</a></span><a aria-hidden="true" class="legacy" id="util_util_transferableabortcontroller"></a></h3> 2320<div class="api_metadata"> 2321<span>Added in: v18.11.0</span> 2322</div> 2323<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 2324<p>Creates and returns an <a href="globals.html#class-abortcontroller" class="type"><AbortController></a> instance whose <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> is marked 2325as transferable and can be used with <code>structuredClone()</code> or <code>postMessage()</code>.</p> 2326</section><section><h3><code>util.transferableAbortSignal(signal)</code><span><a class="mark" href="#utiltransferableabortsignalsignal" id="utiltransferableabortsignalsignal">#</a></span><a aria-hidden="true" class="legacy" id="util_util_transferableabortsignal_signal"></a></h3> 2327<div class="api_metadata"> 2328<span>Added in: v18.11.0</span> 2329</div> 2330<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 2331<ul> 2332<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a></li> 2333<li>Returns: <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a></li> 2334</ul> 2335<p>Marks the given <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> as transferable so that it can be used with 2336<code>structuredClone()</code> and <code>postMessage()</code>.</p> 2337<pre><code class="language-js"><span class="hljs-keyword">const</span> signal = <span class="hljs-title function_">transferableAbortSignal</span>(<span class="hljs-title class_">AbortSignal</span>.<span class="hljs-title function_">timeout</span>(<span class="hljs-number">100</span>)); 2338<span class="hljs-keyword">const</span> channel = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 2339channel.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(signal, [signal]);</code> <button class="copy-button">copy</button></pre> 2340</section><section><h3><code>util.aborted(signal, resource)</code><span><a class="mark" href="#utilabortedsignal-resource" id="utilabortedsignal-resource">#</a></span><a aria-hidden="true" class="legacy" id="util_util_aborted_signal_resource"></a></h3> 2341<div class="api_metadata"> 2342<span>Added in: v18.16.0</span> 2343</div> 2344<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 2345<ul> 2346<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a></li> 2347<li><code>resource</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Any non-null entity, reference to which is held weakly.</li> 2348<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 2349</ul> 2350<p>Listens to abort event on the provided <code>signal</code> and 2351returns a promise that is fulfilled when the <code>signal</code> is 2352aborted. If the passed <code>resource</code> is garbage collected before the <code>signal</code> is 2353aborted, the returned promise shall remain pending indefinitely.</p> 2354 2355<pre class="with-41-chars"><input class="js-flavor-selector" type="checkbox" aria-label="Show modern ES modules syntax"><code class="language-js cjs"><span class="hljs-keyword">const</span> { aborted } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 2356 2357<span class="hljs-keyword">const</span> dependent = <span class="hljs-title function_">obtainSomethingAbortable</span>(); 2358 2359<span class="hljs-title function_">aborted</span>(dependent.<span class="hljs-property">signal</span>, dependent).<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> { 2360 <span class="hljs-comment">// Do something when dependent is aborted.</span> 2361}); 2362 2363dependent.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 2364 dependent.<span class="hljs-title function_">abort</span>(); 2365});</code><code class="language-js mjs"><span class="hljs-keyword">import</span> { aborted } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:util'</span>; 2366 2367<span class="hljs-keyword">const</span> dependent = <span class="hljs-title function_">obtainSomethingAbortable</span>(); 2368 2369<span class="hljs-title function_">aborted</span>(dependent.<span class="hljs-property">signal</span>, dependent).<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> { 2370 <span class="hljs-comment">// Do something when dependent is aborted.</span> 2371}); 2372 2373dependent.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 2374 dependent.<span class="hljs-title function_">abort</span>(); 2375});</code><button class="copy-button">copy</button></pre> 2376</section><section><h3><code>util.types</code><span><a class="mark" href="#utiltypes" id="utiltypes">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types"></a></h3> 2377<div class="api_metadata"> 2378<details class="changelog"><summary>History</summary> 2379<table> 2380<tbody><tr><th>Version</th><th>Changes</th></tr> 2381<tr><td>v15.3.0</td> 2382<td><p>Exposed as <code>require('util/types')</code>.</p></td></tr> 2383<tr><td>v10.0.0</td> 2384<td><p><span>Added in: v10.0.0</span></p></td></tr> 2385</tbody></table> 2386</details> 2387</div> 2388<p><code>util.types</code> provides type checks for different kinds of built-in objects. 2389Unlike <code>instanceof</code> or <code>Object.prototype.toString.call(value)</code>, these checks do 2390not inspect properties of the object that are accessible from JavaScript (like 2391their prototype), and usually have the overhead of calling into C++.</p> 2392<p>The result generally does not make any guarantees about what kinds of 2393properties or behavior a value exposes in JavaScript. They are primarily 2394useful for addon developers who prefer to do type checking in JavaScript.</p> 2395<p>The API is accessible via <code>require('node:util').types</code> or <code>require('node:util/types')</code>.</p> 2396<h4><code>util.types.isAnyArrayBuffer(value)</code><span><a class="mark" href="#utiltypesisanyarraybuffervalue" id="utiltypesisanyarraybuffervalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isanyarraybuffer_value"></a></h4> 2397<div class="api_metadata"> 2398<span>Added in: v10.0.0</span> 2399</div> 2400<ul> 2401<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2402<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2403</ul> 2404<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> or 2405<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a> instance.</p> 2406<p>See also <a href="#utiltypesisarraybuffervalue"><code>util.types.isArrayBuffer()</code></a> and 2407<a href="#utiltypesissharedarraybuffervalue"><code>util.types.isSharedArrayBuffer()</code></a>.</p> 2408<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isAnyArrayBuffer</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>()); <span class="hljs-comment">// Returns true</span> 2409util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isAnyArrayBuffer</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">SharedArrayBuffer</span>()); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2410<h4><code>util.types.isArrayBufferView(value)</code><span><a class="mark" href="#utiltypesisarraybufferviewvalue" id="utiltypesisarraybufferviewvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isarraybufferview_value"></a></h4> 2411<div class="api_metadata"> 2412<span>Added in: v10.0.0</span> 2413</div> 2414<ul> 2415<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2416<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2417</ul> 2418<p>Returns <code>true</code> if the value is an instance of one of the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> 2419views, such as typed array objects or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView"><code>DataView</code></a>. Equivalent to 2420<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView"><code>ArrayBuffer.isView()</code></a>.</p> 2421<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isArrayBufferView</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Int8Array</span>()); <span class="hljs-comment">// true</span> 2422util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isArrayBufferView</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'hello world'</span>)); <span class="hljs-comment">// true</span> 2423util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isArrayBufferView</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">DataView</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>(<span class="hljs-number">16</span>))); <span class="hljs-comment">// true</span> 2424util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isArrayBufferView</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>()); <span class="hljs-comment">// false</span></code> <button class="copy-button">copy</button></pre> 2425<h4><code>util.types.isArgumentsObject(value)</code><span><a class="mark" href="#utiltypesisargumentsobjectvalue" id="utiltypesisargumentsobjectvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isargumentsobject_value"></a></h4> 2426<div class="api_metadata"> 2427<span>Added in: v10.0.0</span> 2428</div> 2429<ul> 2430<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2431<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2432</ul> 2433<p>Returns <code>true</code> if the value is an <code>arguments</code> object.</p> 2434<!-- eslint-disable prefer-rest-params --> 2435<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) { 2436 util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isArgumentsObject</span>(<span class="hljs-variable language_">arguments</span>); <span class="hljs-comment">// Returns true</span> 2437}</code> <button class="copy-button">copy</button></pre> 2438<h4><code>util.types.isArrayBuffer(value)</code><span><a class="mark" href="#utiltypesisarraybuffervalue" id="utiltypesisarraybuffervalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isarraybuffer_value"></a></h4> 2439<div class="api_metadata"> 2440<span>Added in: v10.0.0</span> 2441</div> 2442<ul> 2443<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2444<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2445</ul> 2446<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> instance. 2447This does <em>not</em> include <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a> instances. Usually, it is 2448desirable to test for both; See <a href="#utiltypesisanyarraybuffervalue"><code>util.types.isAnyArrayBuffer()</code></a> for that.</p> 2449<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isArrayBuffer</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>()); <span class="hljs-comment">// Returns true</span> 2450util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isArrayBuffer</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">SharedArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2451<h4><code>util.types.isAsyncFunction(value)</code><span><a class="mark" href="#utiltypesisasyncfunctionvalue" id="utiltypesisasyncfunctionvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isasyncfunction_value"></a></h4> 2452<div class="api_metadata"> 2453<span>Added in: v10.0.0</span> 2454</div> 2455<ul> 2456<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2457<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2458</ul> 2459<p>Returns <code>true</code> if the value is an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function">async function</a>. 2460This only reports back what the JavaScript engine is seeing; 2461in particular, the return value may not match the original source code if 2462a transpilation tool was used.</p> 2463<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isAsyncFunction</span>(<span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) {}); <span class="hljs-comment">// Returns false</span> 2464util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isAsyncFunction</span>(<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) {}); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2465<h4><code>util.types.isBigInt64Array(value)</code><span><a class="mark" href="#utiltypesisbigint64arrayvalue" id="utiltypesisbigint64arrayvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isbigint64array_value"></a></h4> 2466<div class="api_metadata"> 2467<span>Added in: v10.0.0</span> 2468</div> 2469<ul> 2470<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2471<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2472</ul> 2473<p>Returns <code>true</code> if the value is a <code>BigInt64Array</code> instance.</p> 2474<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBigInt64Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">BigInt64Array</span>()); <span class="hljs-comment">// Returns true</span> 2475util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBigInt64Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">BigUint64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2476<h4><code>util.types.isBigUint64Array(value)</code><span><a class="mark" href="#utiltypesisbiguint64arrayvalue" id="utiltypesisbiguint64arrayvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isbiguint64array_value"></a></h4> 2477<div class="api_metadata"> 2478<span>Added in: v10.0.0</span> 2479</div> 2480<ul> 2481<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2482<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2483</ul> 2484<p>Returns <code>true</code> if the value is a <code>BigUint64Array</code> instance.</p> 2485<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBigUint64Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">BigInt64Array</span>()); <span class="hljs-comment">// Returns false</span> 2486util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBigUint64Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">BigUint64Array</span>()); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2487<h4><code>util.types.isBooleanObject(value)</code><span><a class="mark" href="#utiltypesisbooleanobjectvalue" id="utiltypesisbooleanobjectvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isbooleanobject_value"></a></h4> 2488<div class="api_metadata"> 2489<span>Added in: v10.0.0</span> 2490</div> 2491<ul> 2492<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2493<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2494</ul> 2495<p>Returns <code>true</code> if the value is a boolean object, e.g. created 2496by <code>new Boolean()</code>.</p> 2497<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class="hljs-literal">false</span>); <span class="hljs-comment">// Returns false</span> 2498util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class="hljs-literal">true</span>); <span class="hljs-comment">// Returns false</span> 2499util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Boolean</span>(<span class="hljs-literal">false</span>)); <span class="hljs-comment">// Returns true</span> 2500util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Boolean</span>(<span class="hljs-literal">true</span>)); <span class="hljs-comment">// Returns true</span> 2501util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class="hljs-title class_">Boolean</span>(<span class="hljs-literal">false</span>)); <span class="hljs-comment">// Returns false</span> 2502util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class="hljs-title class_">Boolean</span>(<span class="hljs-literal">true</span>)); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2503<h4><code>util.types.isBoxedPrimitive(value)</code><span><a class="mark" href="#utiltypesisboxedprimitivevalue" id="utiltypesisboxedprimitivevalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isboxedprimitive_value"></a></h4> 2504<div class="api_metadata"> 2505<span>Added in: v10.11.0</span> 2506</div> 2507<ul> 2508<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2509<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2510</ul> 2511<p>Returns <code>true</code> if the value is any boxed primitive object, e.g. created 2512by <code>new Boolean()</code>, <code>new String()</code> or <code>Object(Symbol())</code>.</p> 2513<p>For example:</p> 2514<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBoxedPrimitive</span>(<span class="hljs-literal">false</span>); <span class="hljs-comment">// Returns false</span> 2515util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBoxedPrimitive</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Boolean</span>(<span class="hljs-literal">false</span>)); <span class="hljs-comment">// Returns true</span> 2516util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBoxedPrimitive</span>(<span class="hljs-title class_">Symbol</span>(<span class="hljs-string">'foo'</span>)); <span class="hljs-comment">// Returns false</span> 2517util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBoxedPrimitive</span>(<span class="hljs-title class_">Object</span>(<span class="hljs-title class_">Symbol</span>(<span class="hljs-string">'foo'</span>))); <span class="hljs-comment">// Returns true</span> 2518util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBoxedPrimitive</span>(<span class="hljs-title class_">Object</span>(<span class="hljs-title class_">BigInt</span>(<span class="hljs-number">5</span>))); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2519<h4><code>util.types.isCryptoKey(value)</code><span><a class="mark" href="#utiltypesiscryptokeyvalue" id="utiltypesiscryptokeyvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_iscryptokey_value"></a></h4> 2520<div class="api_metadata"> 2521<span>Added in: v16.2.0</span> 2522</div> 2523<ul> 2524<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2525<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2526</ul> 2527<p>Returns <code>true</code> if <code>value</code> is a <a href="webcrypto.html#class-cryptokey" class="type"><CryptoKey></a>, <code>false</code> otherwise.</p> 2528<h4><code>util.types.isDataView(value)</code><span><a class="mark" href="#utiltypesisdataviewvalue" id="utiltypesisdataviewvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isdataview_value"></a></h4> 2529<div class="api_metadata"> 2530<span>Added in: v10.0.0</span> 2531</div> 2532<ul> 2533<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2534<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2535</ul> 2536<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView"><code>DataView</code></a> instance.</p> 2537<pre><code class="language-js"><span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>(<span class="hljs-number">20</span>); 2538util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isDataView</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">DataView</span>(ab)); <span class="hljs-comment">// Returns true</span> 2539util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isDataView</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2540<p>See also <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView"><code>ArrayBuffer.isView()</code></a>.</p> 2541<h4><code>util.types.isDate(value)</code><span><a class="mark" href="#utiltypesisdatevalue" id="utiltypesisdatevalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isdate_value"></a></h4> 2542<div class="api_metadata"> 2543<span>Added in: v10.0.0</span> 2544</div> 2545<ul> 2546<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2547<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2548</ul> 2549<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"><code>Date</code></a> instance.</p> 2550<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isDate</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>()); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2551<h4><code>util.types.isExternal(value)</code><span><a class="mark" href="#utiltypesisexternalvalue" id="utiltypesisexternalvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isexternal_value"></a></h4> 2552<div class="api_metadata"> 2553<span>Added in: v10.0.0</span> 2554</div> 2555<ul> 2556<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2557<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2558</ul> 2559<p>Returns <code>true</code> if the value is a native <code>External</code> value.</p> 2560<p>A native <code>External</code> value is a special type of object that contains a 2561raw C++ pointer (<code>void*</code>) for access from native code, and has no other 2562properties. Such objects are created either by Node.js internals or native 2563addons. In JavaScript, they are <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze">frozen</a> objects with a 2564<code>null</code> prototype.</p> 2565<pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><js_native_api.h></span></span> 2566<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><stdlib.h></span></span> 2567napi_value result; 2568<span class="hljs-type">static</span> napi_value <span class="hljs-title function_">MyNapi</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> { 2569 <span class="hljs-type">int</span>* raw = (<span class="hljs-type">int</span>*) <span class="hljs-built_in">malloc</span>(<span class="hljs-number">1024</span>); 2570 napi_status status = napi_create_external(env, (<span class="hljs-type">void</span>*) raw, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, &result); 2571 <span class="hljs-keyword">if</span> (status != napi_ok) { 2572 napi_throw_error(env, <span class="hljs-literal">NULL</span>, <span class="hljs-string">"napi_create_external failed"</span>); 2573 <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 2574 } 2575 <span class="hljs-keyword">return</span> result; 2576} 2577... 2578DECLARE_NAPI_PROPERTY(<span class="hljs-string">"myNapi"</span>, MyNapi) 2579...</code> <button class="copy-button">copy</button></pre> 2580<pre><code class="language-js"><span class="hljs-keyword">const</span> native = <span class="hljs-built_in">require</span>(<span class="hljs-string">'napi_addon.node'</span>); 2581<span class="hljs-keyword">const</span> data = native.<span class="hljs-title function_">myNapi</span>(); 2582util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isExternal</span>(data); <span class="hljs-comment">// returns true</span> 2583util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isExternal</span>(<span class="hljs-number">0</span>); <span class="hljs-comment">// returns false</span> 2584util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isExternal</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(<span class="hljs-string">'foo'</span>)); <span class="hljs-comment">// returns false</span></code> <button class="copy-button">copy</button></pre> 2585<p>For further information on <code>napi_create_external</code>, refer to 2586<a href="n-api.html#napi_create_external"><code>napi_create_external()</code></a>.</p> 2587<h4><code>util.types.isFloat32Array(value)</code><span><a class="mark" href="#utiltypesisfloat32arrayvalue" id="utiltypesisfloat32arrayvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isfloat32array_value"></a></h4> 2588<div class="api_metadata"> 2589<span>Added in: v10.0.0</span> 2590</div> 2591<ul> 2592<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2593<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2594</ul> 2595<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array"><code>Float32Array</code></a> instance.</p> 2596<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isFloat32Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span> 2597util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isFloat32Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Float32Array</span>()); <span class="hljs-comment">// Returns true</span> 2598util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isFloat32Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2599<h4><code>util.types.isFloat64Array(value)</code><span><a class="mark" href="#utiltypesisfloat64arrayvalue" id="utiltypesisfloat64arrayvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isfloat64array_value"></a></h4> 2600<div class="api_metadata"> 2601<span>Added in: v10.0.0</span> 2602</div> 2603<ul> 2604<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2605<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2606</ul> 2607<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array"><code>Float64Array</code></a> instance.</p> 2608<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isFloat64Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span> 2609util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isFloat64Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>()); <span class="hljs-comment">// Returns false</span> 2610util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isFloat64Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Float64Array</span>()); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2611<h4><code>util.types.isGeneratorFunction(value)</code><span><a class="mark" href="#utiltypesisgeneratorfunctionvalue" id="utiltypesisgeneratorfunctionvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isgeneratorfunction_value"></a></h4> 2612<div class="api_metadata"> 2613<span>Added in: v10.0.0</span> 2614</div> 2615<ul> 2616<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2617<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2618</ul> 2619<p>Returns <code>true</code> if the value is a generator function. 2620This only reports back what the JavaScript engine is seeing; 2621in particular, the return value may not match the original source code if 2622a transpilation tool was used.</p> 2623<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isGeneratorFunction</span>(<span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) {}); <span class="hljs-comment">// Returns false</span> 2624util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isGeneratorFunction</span>(<span class="hljs-keyword">function</span>* <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) {}); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2625<h4><code>util.types.isGeneratorObject(value)</code><span><a class="mark" href="#utiltypesisgeneratorobjectvalue" id="utiltypesisgeneratorobjectvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isgeneratorobject_value"></a></h4> 2626<div class="api_metadata"> 2627<span>Added in: v10.0.0</span> 2628</div> 2629<ul> 2630<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2631<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2632</ul> 2633<p>Returns <code>true</code> if the value is a generator object as returned from a 2634built-in generator function. 2635This only reports back what the JavaScript engine is seeing; 2636in particular, the return value may not match the original source code if 2637a transpilation tool was used.</p> 2638<pre><code class="language-js"><span class="hljs-keyword">function</span>* <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) {} 2639<span class="hljs-keyword">const</span> generator = <span class="hljs-title function_">foo</span>(); 2640util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isGeneratorObject</span>(generator); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2641<h4><code>util.types.isInt8Array(value)</code><span><a class="mark" href="#utiltypesisint8arrayvalue" id="utiltypesisint8arrayvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isint8array_value"></a></h4> 2642<div class="api_metadata"> 2643<span>Added in: v10.0.0</span> 2644</div> 2645<ul> 2646<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2647<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2648</ul> 2649<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8Array"><code>Int8Array</code></a> instance.</p> 2650<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isInt8Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span> 2651util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isInt8Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Int8Array</span>()); <span class="hljs-comment">// Returns true</span> 2652util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isInt8Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2653<h4><code>util.types.isInt16Array(value)</code><span><a class="mark" href="#utiltypesisint16arrayvalue" id="utiltypesisint16arrayvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isint16array_value"></a></h4> 2654<div class="api_metadata"> 2655<span>Added in: v10.0.0</span> 2656</div> 2657<ul> 2658<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2659<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2660</ul> 2661<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int16Array"><code>Int16Array</code></a> instance.</p> 2662<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isInt16Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span> 2663util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isInt16Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Int16Array</span>()); <span class="hljs-comment">// Returns true</span> 2664util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isInt16Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2665<h4><code>util.types.isInt32Array(value)</code><span><a class="mark" href="#utiltypesisint32arrayvalue" id="utiltypesisint32arrayvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isint32array_value"></a></h4> 2666<div class="api_metadata"> 2667<span>Added in: v10.0.0</span> 2668</div> 2669<ul> 2670<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2671<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2672</ul> 2673<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int32Array"><code>Int32Array</code></a> instance.</p> 2674<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isInt32Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span> 2675util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isInt32Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Int32Array</span>()); <span class="hljs-comment">// Returns true</span> 2676util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isInt32Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2677<h4><code>util.types.isKeyObject(value)</code><span><a class="mark" href="#utiltypesiskeyobjectvalue" id="utiltypesiskeyobjectvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_iskeyobject_value"></a></h4> 2678<div class="api_metadata"> 2679<span>Added in: v16.2.0</span> 2680</div> 2681<ul> 2682<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2683<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2684</ul> 2685<p>Returns <code>true</code> if <code>value</code> is a <a href="crypto.html#class-keyobject" class="type"><KeyObject></a>, <code>false</code> otherwise.</p> 2686<h4><code>util.types.isMap(value)</code><span><a class="mark" href="#utiltypesismapvalue" id="utiltypesismapvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_ismap_value"></a></h4> 2687<div class="api_metadata"> 2688<span>Added in: v10.0.0</span> 2689</div> 2690<ul> 2691<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2692<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2693</ul> 2694<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><code>Map</code></a> instance.</p> 2695<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isMap</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>()); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2696<h4><code>util.types.isMapIterator(value)</code><span><a class="mark" href="#utiltypesismapiteratorvalue" id="utiltypesismapiteratorvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_ismapiterator_value"></a></h4> 2697<div class="api_metadata"> 2698<span>Added in: v10.0.0</span> 2699</div> 2700<ul> 2701<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2702<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2703</ul> 2704<p>Returns <code>true</code> if the value is an iterator returned for a built-in 2705<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><code>Map</code></a> instance.</p> 2706<pre><code class="language-js"><span class="hljs-keyword">const</span> map = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>(); 2707util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isMapIterator</span>(map.<span class="hljs-title function_">keys</span>()); <span class="hljs-comment">// Returns true</span> 2708util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isMapIterator</span>(map.<span class="hljs-title function_">values</span>()); <span class="hljs-comment">// Returns true</span> 2709util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isMapIterator</span>(map.<span class="hljs-title function_">entries</span>()); <span class="hljs-comment">// Returns true</span> 2710util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isMapIterator</span>(map[<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">iterator</span>]()); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2711<h4><code>util.types.isModuleNamespaceObject(value)</code><span><a class="mark" href="#utiltypesismodulenamespaceobjectvalue" id="utiltypesismodulenamespaceobjectvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_ismodulenamespaceobject_value"></a></h4> 2712<div class="api_metadata"> 2713<span>Added in: v10.0.0</span> 2714</div> 2715<ul> 2716<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2717<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2718</ul> 2719<p>Returns <code>true</code> if the value is an instance of a <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects">Module Namespace Object</a>.</p> 2720<!-- eslint-skip --> 2721<pre><code class="language-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> ns <span class="hljs-keyword">from</span> <span class="hljs-string">'./a.js'</span>; 2722 2723util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isModuleNamespaceObject</span>(ns); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2724<h4><code>util.types.isNativeError(value)</code><span><a class="mark" href="#utiltypesisnativeerrorvalue" id="utiltypesisnativeerrorvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isnativeerror_value"></a></h4> 2725<div class="api_metadata"> 2726<span>Added in: v10.0.0</span> 2727</div> 2728<ul> 2729<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2730<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2731</ul> 2732<p>Returns <code>true</code> if the value was returned by the constructor of a 2733<a href="https://tc39.es/ecma262/#sec-error-objects">built-in <code>Error</code> type</a>.</p> 2734<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isNativeError</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>())); <span class="hljs-comment">// true</span> 2735<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isNativeError</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>())); <span class="hljs-comment">// true</span> 2736<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isNativeError</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">RangeError</span>())); <span class="hljs-comment">// true</span></code> <button class="copy-button">copy</button></pre> 2737<p>Subclasses of the native error types are also native errors:</p> 2738<pre><code class="language-js"><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyError</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_ inherited__">Error</span> {} 2739<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isNativeError</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">MyError</span>())); <span class="hljs-comment">// true</span></code> <button class="copy-button">copy</button></pre> 2740<p>A value being <code>instanceof</code> a native error class is not equivalent to <code>isNativeError()</code> 2741returning <code>true</code> for that value. <code>isNativeError()</code> returns <code>true</code> for errors 2742which come from a different <a href="https://tc39.es/ecma262/#realm">realm</a> while <code>instanceof Error</code> returns <code>false</code> 2743for these errors:</p> 2744<pre><code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:vm'</span>); 2745<span class="hljs-keyword">const</span> context = vm.<span class="hljs-title function_">createContext</span>({}); 2746<span class="hljs-keyword">const</span> myError = vm.<span class="hljs-title function_">runInContext</span>(<span class="hljs-string">'new Error()'</span>, context); 2747<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isNativeError</span>(myError)); <span class="hljs-comment">// true</span> 2748<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myError <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">Error</span>); <span class="hljs-comment">// false</span></code> <button class="copy-button">copy</button></pre> 2749<p>Conversely, <code>isNativeError()</code> returns <code>false</code> for all objects which were not 2750returned by the constructor of a native error. That includes values 2751which are <code>instanceof</code> native errors:</p> 2752<pre><code class="language-js"><span class="hljs-keyword">const</span> myError = { <span class="hljs-attr">__proto__</span>: <span class="hljs-title class_">Error</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span> }; 2753<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isNativeError</span>(myError)); <span class="hljs-comment">// false</span> 2754<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myError <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">Error</span>); <span class="hljs-comment">// true</span></code> <button class="copy-button">copy</button></pre> 2755<h4><code>util.types.isNumberObject(value)</code><span><a class="mark" href="#utiltypesisnumberobjectvalue" id="utiltypesisnumberobjectvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isnumberobject_value"></a></h4> 2756<div class="api_metadata"> 2757<span>Added in: v10.0.0</span> 2758</div> 2759<ul> 2760<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2761<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2762</ul> 2763<p>Returns <code>true</code> if the value is a number object, e.g. created 2764by <code>new Number()</code>.</p> 2765<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isNumberObject</span>(<span class="hljs-number">0</span>); <span class="hljs-comment">// Returns false</span> 2766util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isNumberObject</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Number</span>(<span class="hljs-number">0</span>)); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2767<h4><code>util.types.isPromise(value)</code><span><a class="mark" href="#utiltypesispromisevalue" id="utiltypesispromisevalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_ispromise_value"></a></h4> 2768<div class="api_metadata"> 2769<span>Added in: v10.0.0</span> 2770</div> 2771<ul> 2772<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2773<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2774</ul> 2775<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise"><code>Promise</code></a>.</p> 2776<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isPromise</span>(<span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>(<span class="hljs-number">42</span>)); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2777<h4><code>util.types.isProxy(value)</code><span><a class="mark" href="#utiltypesisproxyvalue" id="utiltypesisproxyvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isproxy_value"></a></h4> 2778<div class="api_metadata"> 2779<span>Added in: v10.0.0</span> 2780</div> 2781<ul> 2782<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2783<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2784</ul> 2785<p>Returns <code>true</code> if the value is a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy"><code>Proxy</code></a> instance.</p> 2786<pre><code class="language-js"><span class="hljs-keyword">const</span> target = {}; 2787<span class="hljs-keyword">const</span> proxy = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Proxy</span>(target, {}); 2788util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isProxy</span>(target); <span class="hljs-comment">// Returns false</span> 2789util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isProxy</span>(proxy); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2790<h4><code>util.types.isRegExp(value)</code><span><a class="mark" href="#utiltypesisregexpvalue" id="utiltypesisregexpvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isregexp_value"></a></h4> 2791<div class="api_metadata"> 2792<span>Added in: v10.0.0</span> 2793</div> 2794<ul> 2795<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2796<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2797</ul> 2798<p>Returns <code>true</code> if the value is a regular expression object.</p> 2799<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isRegExp</span>(<span class="hljs-regexp">/abc/</span>); <span class="hljs-comment">// Returns true</span> 2800util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isRegExp</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">RegExp</span>(<span class="hljs-string">'abc'</span>)); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2801<h4><code>util.types.isSet(value)</code><span><a class="mark" href="#utiltypesissetvalue" id="utiltypesissetvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isset_value"></a></h4> 2802<div class="api_metadata"> 2803<span>Added in: v10.0.0</span> 2804</div> 2805<ul> 2806<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2807<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2808</ul> 2809<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set"><code>Set</code></a> instance.</p> 2810<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSet</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Set</span>()); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2811<h4><code>util.types.isSetIterator(value)</code><span><a class="mark" href="#utiltypesissetiteratorvalue" id="utiltypesissetiteratorvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_issetiterator_value"></a></h4> 2812<div class="api_metadata"> 2813<span>Added in: v10.0.0</span> 2814</div> 2815<ul> 2816<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2817<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2818</ul> 2819<p>Returns <code>true</code> if the value is an iterator returned for a built-in 2820<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set"><code>Set</code></a> instance.</p> 2821<pre><code class="language-js"><span class="hljs-keyword">const</span> set = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Set</span>(); 2822util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSetIterator</span>(set.<span class="hljs-title function_">keys</span>()); <span class="hljs-comment">// Returns true</span> 2823util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSetIterator</span>(set.<span class="hljs-title function_">values</span>()); <span class="hljs-comment">// Returns true</span> 2824util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSetIterator</span>(set.<span class="hljs-title function_">entries</span>()); <span class="hljs-comment">// Returns true</span> 2825util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSetIterator</span>(set[<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">iterator</span>]()); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2826<h4><code>util.types.isSharedArrayBuffer(value)</code><span><a class="mark" href="#utiltypesissharedarraybuffervalue" id="utiltypesissharedarraybuffervalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_issharedarraybuffer_value"></a></h4> 2827<div class="api_metadata"> 2828<span>Added in: v10.0.0</span> 2829</div> 2830<ul> 2831<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2832<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2833</ul> 2834<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a> instance. 2835This does <em>not</em> include <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> instances. Usually, it is 2836desirable to test for both; See <a href="#utiltypesisanyarraybuffervalue"><code>util.types.isAnyArrayBuffer()</code></a> for that.</p> 2837<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSharedArrayBuffer</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span> 2838util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSharedArrayBuffer</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">SharedArrayBuffer</span>()); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2839<h4><code>util.types.isStringObject(value)</code><span><a class="mark" href="#utiltypesisstringobjectvalue" id="utiltypesisstringobjectvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isstringobject_value"></a></h4> 2840<div class="api_metadata"> 2841<span>Added in: v10.0.0</span> 2842</div> 2843<ul> 2844<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2845<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2846</ul> 2847<p>Returns <code>true</code> if the value is a string object, e.g. created 2848by <code>new String()</code>.</p> 2849<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isStringObject</span>(<span class="hljs-string">'foo'</span>); <span class="hljs-comment">// Returns false</span> 2850util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isStringObject</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(<span class="hljs-string">'foo'</span>)); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2851<h4><code>util.types.isSymbolObject(value)</code><span><a class="mark" href="#utiltypesissymbolobjectvalue" id="utiltypesissymbolobjectvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_issymbolobject_value"></a></h4> 2852<div class="api_metadata"> 2853<span>Added in: v10.0.0</span> 2854</div> 2855<ul> 2856<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2857<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2858</ul> 2859<p>Returns <code>true</code> if the value is a symbol object, created 2860by calling <code>Object()</code> on a <code>Symbol</code> primitive.</p> 2861<pre><code class="language-js"><span class="hljs-keyword">const</span> symbol = <span class="hljs-title class_">Symbol</span>(<span class="hljs-string">'foo'</span>); 2862util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSymbolObject</span>(symbol); <span class="hljs-comment">// Returns false</span> 2863util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSymbolObject</span>(<span class="hljs-title class_">Object</span>(symbol)); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2864<h4><code>util.types.isTypedArray(value)</code><span><a class="mark" href="#utiltypesistypedarrayvalue" id="utiltypesistypedarrayvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_istypedarray_value"></a></h4> 2865<div class="api_metadata"> 2866<span>Added in: v10.0.0</span> 2867</div> 2868<ul> 2869<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2870<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2871</ul> 2872<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> instance.</p> 2873<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isTypedArray</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span> 2874util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isTypedArray</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>()); <span class="hljs-comment">// Returns true</span> 2875util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isTypedArray</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Float64Array</span>()); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2876<p>See also <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView"><code>ArrayBuffer.isView()</code></a>.</p> 2877<h4><code>util.types.isUint8Array(value)</code><span><a class="mark" href="#utiltypesisuint8arrayvalue" id="utiltypesisuint8arrayvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isuint8array_value"></a></h4> 2878<div class="api_metadata"> 2879<span>Added in: v10.0.0</span> 2880</div> 2881<ul> 2882<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2883<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2884</ul> 2885<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> instance.</p> 2886<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint8Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span> 2887util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint8Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>()); <span class="hljs-comment">// Returns true</span> 2888util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint8Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2889<h4><code>util.types.isUint8ClampedArray(value)</code><span><a class="mark" href="#utiltypesisuint8clampedarrayvalue" id="utiltypesisuint8clampedarrayvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isuint8clampedarray_value"></a></h4> 2890<div class="api_metadata"> 2891<span>Added in: v10.0.0</span> 2892</div> 2893<ul> 2894<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2895<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2896</ul> 2897<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray"><code>Uint8ClampedArray</code></a> instance.</p> 2898<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint8ClampedArray</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span> 2899util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint8ClampedArray</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8ClampedArray</span>()); <span class="hljs-comment">// Returns true</span> 2900util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint8ClampedArray</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2901<h4><code>util.types.isUint16Array(value)</code><span><a class="mark" href="#utiltypesisuint16arrayvalue" id="utiltypesisuint16arrayvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isuint16array_value"></a></h4> 2902<div class="api_metadata"> 2903<span>Added in: v10.0.0</span> 2904</div> 2905<ul> 2906<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2907<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2908</ul> 2909<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array"><code>Uint16Array</code></a> instance.</p> 2910<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint16Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span> 2911util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint16Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>()); <span class="hljs-comment">// Returns true</span> 2912util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint16Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2913<h4><code>util.types.isUint32Array(value)</code><span><a class="mark" href="#utiltypesisuint32arrayvalue" id="utiltypesisuint32arrayvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isuint32array_value"></a></h4> 2914<div class="api_metadata"> 2915<span>Added in: v10.0.0</span> 2916</div> 2917<ul> 2918<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2919<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2920</ul> 2921<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array"><code>Uint32Array</code></a> instance.</p> 2922<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint32Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>()); <span class="hljs-comment">// Returns false</span> 2923util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint32Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint32Array</span>()); <span class="hljs-comment">// Returns true</span> 2924util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint32Array</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2925<h4><code>util.types.isWeakMap(value)</code><span><a class="mark" href="#utiltypesisweakmapvalue" id="utiltypesisweakmapvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isweakmap_value"></a></h4> 2926<div class="api_metadata"> 2927<span>Added in: v10.0.0</span> 2928</div> 2929<ul> 2930<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2931<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2932</ul> 2933<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> instance.</p> 2934<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isWeakMap</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakMap</span>()); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2935<h4><code>util.types.isWeakSet(value)</code><span><a class="mark" href="#utiltypesisweaksetvalue" id="utiltypesisweaksetvalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_isweakset_value"></a></h4> 2936<div class="api_metadata"> 2937<span>Added in: v10.0.0</span> 2938</div> 2939<ul> 2940<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2941<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2942</ul> 2943<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet"><code>WeakSet</code></a> instance.</p> 2944<pre><code class="language-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isWeakSet</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakSet</span>()); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2945<h4><code>util.types.isWebAssemblyCompiledModule(value)</code><span><a class="mark" href="#utiltypesiswebassemblycompiledmodulevalue" id="utiltypesiswebassemblycompiledmodulevalue">#</a></span><a aria-hidden="true" class="legacy" id="util_util_types_iswebassemblycompiledmodule_value"></a></h4> 2946<div class="api_metadata"> 2947<span>Added in: v10.0.0</span><span>Deprecated since: v14.0.0</span> 2948</div> 2949<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <code>value instanceof WebAssembly.Module</code> instead.</div><p></p> 2950<ul> 2951<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2952<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2953</ul> 2954<p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module"><code>WebAssembly.Module</code></a> instance.</p> 2955<pre><code class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WebAssembly</span>.<span class="hljs-title class_">Module</span>(wasmBuffer); 2956util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isWebAssemblyCompiledModule</span>(<span class="hljs-variable language_">module</span>); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2957</section><section><h3>Deprecated APIs<span><a class="mark" href="#deprecated-apis" id="deprecated-apis">#</a></span><a aria-hidden="true" class="legacy" id="util_deprecated_apis"></a></h3> 2958<p>The following APIs are deprecated and should no longer be used. Existing 2959applications and modules should be updated to find alternative approaches.</p> 2960<h4><code>util._extend(target, source)</code><span><a class="mark" href="#util_extendtarget-source" id="util_extendtarget-source">#</a></span><a aria-hidden="true" class="legacy" id="util_util_extend_target_source"></a></h4> 2961<div class="api_metadata"> 2962<span>Added in: v0.7.5</span><span>Deprecated since: v6.0.0</span> 2963</div> 2964<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign"><code>Object.assign()</code></a> instead.</div><p></p> 2965<ul> 2966<li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2967<li><code>source</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2968</ul> 2969<p>The <code>util._extend()</code> method was never intended to be used outside of internal 2970Node.js modules. The community found and used it anyway.</p> 2971<p>It is deprecated and should not be used in new code. JavaScript comes with very 2972similar built-in functionality through <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign"><code>Object.assign()</code></a>.</p> 2973<h4><code>util.isArray(object)</code><span><a class="mark" href="#utilisarrayobject" id="utilisarrayobject">#</a></span><a aria-hidden="true" class="legacy" id="util_util_isarray_object"></a></h4> 2974<div class="api_metadata"> 2975<span>Added in: v0.6.0</span><span>Deprecated since: v4.0.0</span> 2976</div> 2977<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray"><code>Array.isArray()</code></a> instead.</div><p></p> 2978<ul> 2979<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2980<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2981</ul> 2982<p>Alias for <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray"><code>Array.isArray()</code></a>.</p> 2983<p>Returns <code>true</code> if the given <code>object</code> is an <code>Array</code>. Otherwise, returns <code>false</code>.</p> 2984<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 2985 2986util.<span class="hljs-title function_">isArray</span>([]); 2987<span class="hljs-comment">// Returns: true</span> 2988util.<span class="hljs-title function_">isArray</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Array</span>()); 2989<span class="hljs-comment">// Returns: true</span> 2990util.<span class="hljs-title function_">isArray</span>({}); 2991<span class="hljs-comment">// Returns: false</span></code> <button class="copy-button">copy</button></pre> 2992<h4><code>util.isBoolean(object)</code><span><a class="mark" href="#utilisbooleanobject" id="utilisbooleanobject">#</a></span><a aria-hidden="true" class="legacy" id="util_util_isboolean_object"></a></h4> 2993<div class="api_metadata"> 2994<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 2995</div> 2996<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <code>typeof value === 'boolean'</code> instead.</div><p></p> 2997<ul> 2998<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2999<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3000</ul> 3001<p>Returns <code>true</code> if the given <code>object</code> is a <code>Boolean</code>. Otherwise, returns <code>false</code>.</p> 3002<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 3003 3004util.<span class="hljs-title function_">isBoolean</span>(<span class="hljs-number">1</span>); 3005<span class="hljs-comment">// Returns: false</span> 3006util.<span class="hljs-title function_">isBoolean</span>(<span class="hljs-number">0</span>); 3007<span class="hljs-comment">// Returns: false</span> 3008util.<span class="hljs-title function_">isBoolean</span>(<span class="hljs-literal">false</span>); 3009<span class="hljs-comment">// Returns: true</span></code> <button class="copy-button">copy</button></pre> 3010<h4><code>util.isBuffer(object)</code><span><a class="mark" href="#utilisbufferobject" id="utilisbufferobject">#</a></span><a aria-hidden="true" class="legacy" id="util_util_isbuffer_object"></a></h4> 3011<div class="api_metadata"> 3012<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3013</div> 3014<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="buffer.html#static-method-bufferisbufferobj"><code>Buffer.isBuffer()</code></a> instead.</div><p></p> 3015<ul> 3016<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3017<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3018</ul> 3019<p>Returns <code>true</code> if the given <code>object</code> is a <code>Buffer</code>. Otherwise, returns <code>false</code>.</p> 3020<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 3021 3022util.<span class="hljs-title function_">isBuffer</span>({ <span class="hljs-attr">length</span>: <span class="hljs-number">0</span> }); 3023<span class="hljs-comment">// Returns: false</span> 3024util.<span class="hljs-title function_">isBuffer</span>([]); 3025<span class="hljs-comment">// Returns: false</span> 3026util.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'hello world'</span>)); 3027<span class="hljs-comment">// Returns: true</span></code> <button class="copy-button">copy</button></pre> 3028<h4><code>util.isDate(object)</code><span><a class="mark" href="#utilisdateobject" id="utilisdateobject">#</a></span><a aria-hidden="true" class="legacy" id="util_util_isdate_object"></a></h4> 3029<div class="api_metadata"> 3030<span>Added in: v0.6.0</span><span>Deprecated since: v4.0.0</span> 3031</div> 3032<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#utiltypesisdatevalue"><code>util.types.isDate()</code></a> instead.</div><p></p> 3033<ul> 3034<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3035<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3036</ul> 3037<p>Returns <code>true</code> if the given <code>object</code> is a <code>Date</code>. Otherwise, returns <code>false</code>.</p> 3038<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 3039 3040util.<span class="hljs-title function_">isDate</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>()); 3041<span class="hljs-comment">// Returns: true</span> 3042util.<span class="hljs-title function_">isDate</span>(<span class="hljs-title class_">Date</span>()); 3043<span class="hljs-comment">// false (without 'new' returns a String)</span> 3044util.<span class="hljs-title function_">isDate</span>({}); 3045<span class="hljs-comment">// Returns: false</span></code> <button class="copy-button">copy</button></pre> 3046<h4><code>util.isError(object)</code><span><a class="mark" href="#utiliserrorobject" id="utiliserrorobject">#</a></span><a aria-hidden="true" class="legacy" id="util_util_iserror_object"></a></h4> 3047<div class="api_metadata"> 3048<span>Added in: v0.6.0</span><span>Deprecated since: v4.0.0</span> 3049</div> 3050<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#utiltypesisnativeerrorvalue"><code>util.types.isNativeError()</code></a> instead.</div><p></p> 3051<ul> 3052<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3053<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3054</ul> 3055<p>Returns <code>true</code> if the given <code>object</code> is an <a href="errors.html#class-error"><code>Error</code></a>. Otherwise, returns 3056<code>false</code>.</p> 3057<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 3058 3059util.<span class="hljs-title function_">isError</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>()); 3060<span class="hljs-comment">// Returns: true</span> 3061util.<span class="hljs-title function_">isError</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>()); 3062<span class="hljs-comment">// Returns: true</span> 3063util.<span class="hljs-title function_">isError</span>({ <span class="hljs-attr">name</span>: <span class="hljs-string">'Error'</span>, <span class="hljs-attr">message</span>: <span class="hljs-string">'an error occurred'</span> }); 3064<span class="hljs-comment">// Returns: false</span></code> <button class="copy-button">copy</button></pre> 3065<p>This method relies on <code>Object.prototype.toString()</code> behavior. It is 3066possible to obtain an incorrect result when the <code>object</code> argument manipulates 3067<code>@@toStringTag</code>.</p> 3068<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 3069<span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">name</span>: <span class="hljs-string">'Error'</span>, <span class="hljs-attr">message</span>: <span class="hljs-string">'an error occurred'</span> }; 3070 3071util.<span class="hljs-title function_">isError</span>(obj); 3072<span class="hljs-comment">// Returns: false</span> 3073obj[<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toStringTag</span>] = <span class="hljs-string">'Error'</span>; 3074util.<span class="hljs-title function_">isError</span>(obj); 3075<span class="hljs-comment">// Returns: true</span></code> <button class="copy-button">copy</button></pre> 3076<h4><code>util.isFunction(object)</code><span><a class="mark" href="#utilisfunctionobject" id="utilisfunctionobject">#</a></span><a aria-hidden="true" class="legacy" id="util_util_isfunction_object"></a></h4> 3077<div class="api_metadata"> 3078<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3079</div> 3080<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <code>typeof value === 'function'</code> instead.</div><p></p> 3081<ul> 3082<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3083<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3084</ul> 3085<p>Returns <code>true</code> if the given <code>object</code> is a <code>Function</code>. Otherwise, returns 3086<code>false</code>.</p> 3087<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 3088 3089<span class="hljs-keyword">function</span> <span class="hljs-title function_">Foo</span>(<span class="hljs-params"></span>) {} 3090<span class="hljs-keyword">const</span> <span class="hljs-title function_">Bar</span> = (<span class="hljs-params"></span>) => {}; 3091 3092util.<span class="hljs-title function_">isFunction</span>({}); 3093<span class="hljs-comment">// Returns: false</span> 3094util.<span class="hljs-title function_">isFunction</span>(<span class="hljs-title class_">Foo</span>); 3095<span class="hljs-comment">// Returns: true</span> 3096util.<span class="hljs-title function_">isFunction</span>(<span class="hljs-title class_">Bar</span>); 3097<span class="hljs-comment">// Returns: true</span></code> <button class="copy-button">copy</button></pre> 3098<h4><code>util.isNull(object)</code><span><a class="mark" href="#utilisnullobject" id="utilisnullobject">#</a></span><a aria-hidden="true" class="legacy" id="util_util_isnull_object"></a></h4> 3099<div class="api_metadata"> 3100<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3101</div> 3102<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <code>value === null</code> instead.</div><p></p> 3103<ul> 3104<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3105<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3106</ul> 3107<p>Returns <code>true</code> if the given <code>object</code> is strictly <code>null</code>. Otherwise, returns 3108<code>false</code>.</p> 3109<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 3110 3111util.<span class="hljs-title function_">isNull</span>(<span class="hljs-number">0</span>); 3112<span class="hljs-comment">// Returns: false</span> 3113util.<span class="hljs-title function_">isNull</span>(<span class="hljs-literal">undefined</span>); 3114<span class="hljs-comment">// Returns: false</span> 3115util.<span class="hljs-title function_">isNull</span>(<span class="hljs-literal">null</span>); 3116<span class="hljs-comment">// Returns: true</span></code> <button class="copy-button">copy</button></pre> 3117<h4><code>util.isNullOrUndefined(object)</code><span><a class="mark" href="#utilisnullorundefinedobject" id="utilisnullorundefinedobject">#</a></span><a aria-hidden="true" class="legacy" id="util_util_isnullorundefined_object"></a></h4> 3118<div class="api_metadata"> 3119<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3120</div> 3121<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use 3122<code>value === undefined || value === null</code> instead.</div><p></p> 3123<ul> 3124<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3125<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3126</ul> 3127<p>Returns <code>true</code> if the given <code>object</code> is <code>null</code> or <code>undefined</code>. Otherwise, 3128returns <code>false</code>.</p> 3129<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 3130 3131util.<span class="hljs-title function_">isNullOrUndefined</span>(<span class="hljs-number">0</span>); 3132<span class="hljs-comment">// Returns: false</span> 3133util.<span class="hljs-title function_">isNullOrUndefined</span>(<span class="hljs-literal">undefined</span>); 3134<span class="hljs-comment">// Returns: true</span> 3135util.<span class="hljs-title function_">isNullOrUndefined</span>(<span class="hljs-literal">null</span>); 3136<span class="hljs-comment">// Returns: true</span></code> <button class="copy-button">copy</button></pre> 3137<h4><code>util.isNumber(object)</code><span><a class="mark" href="#utilisnumberobject" id="utilisnumberobject">#</a></span><a aria-hidden="true" class="legacy" id="util_util_isnumber_object"></a></h4> 3138<div class="api_metadata"> 3139<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3140</div> 3141<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <code>typeof value === 'number'</code> instead.</div><p></p> 3142<ul> 3143<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3144<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3145</ul> 3146<p>Returns <code>true</code> if the given <code>object</code> is a <code>Number</code>. Otherwise, returns <code>false</code>.</p> 3147<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 3148 3149util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-literal">false</span>); 3150<span class="hljs-comment">// Returns: false</span> 3151util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-title class_">Infinity</span>); 3152<span class="hljs-comment">// Returns: true</span> 3153util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-number">0</span>); 3154<span class="hljs-comment">// Returns: true</span> 3155util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-title class_">NaN</span>); 3156<span class="hljs-comment">// Returns: true</span></code> <button class="copy-button">copy</button></pre> 3157<h4><code>util.isObject(object)</code><span><a class="mark" href="#utilisobjectobject" id="utilisobjectobject">#</a></span><a aria-hidden="true" class="legacy" id="util_util_isobject_object"></a></h4> 3158<div class="api_metadata"> 3159<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3160</div> 3161<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: 3162Use <code>value !== null && typeof value === 'object'</code> instead.</div><p></p> 3163<ul> 3164<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3165<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3166</ul> 3167<p>Returns <code>true</code> if the given <code>object</code> is strictly an <code>Object</code> <strong>and</strong> not a 3168<code>Function</code> (even though functions are objects in JavaScript). 3169Otherwise, returns <code>false</code>.</p> 3170<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 3171 3172util.<span class="hljs-title function_">isObject</span>(<span class="hljs-number">5</span>); 3173<span class="hljs-comment">// Returns: false</span> 3174util.<span class="hljs-title function_">isObject</span>(<span class="hljs-literal">null</span>); 3175<span class="hljs-comment">// Returns: false</span> 3176util.<span class="hljs-title function_">isObject</span>({}); 3177<span class="hljs-comment">// Returns: true</span> 3178util.<span class="hljs-title function_">isObject</span>(<span class="hljs-function">() =></span> {}); 3179<span class="hljs-comment">// Returns: false</span></code> <button class="copy-button">copy</button></pre> 3180<h4><code>util.isPrimitive(object)</code><span><a class="mark" href="#utilisprimitiveobject" id="utilisprimitiveobject">#</a></span><a aria-hidden="true" class="legacy" id="util_util_isprimitive_object"></a></h4> 3181<div class="api_metadata"> 3182<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3183</div> 3184<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use 3185<code>(typeof value !== 'object' && typeof value !== 'function') || value === null</code> 3186instead.</div><p></p> 3187<ul> 3188<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3189<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3190</ul> 3191<p>Returns <code>true</code> if the given <code>object</code> is a primitive type. Otherwise, returns 3192<code>false</code>.</p> 3193<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 3194 3195util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-number">5</span>); 3196<span class="hljs-comment">// Returns: true</span> 3197util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-string">'foo'</span>); 3198<span class="hljs-comment">// Returns: true</span> 3199util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-literal">false</span>); 3200<span class="hljs-comment">// Returns: true</span> 3201util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-literal">null</span>); 3202<span class="hljs-comment">// Returns: true</span> 3203util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-literal">undefined</span>); 3204<span class="hljs-comment">// Returns: true</span> 3205util.<span class="hljs-title function_">isPrimitive</span>({}); 3206<span class="hljs-comment">// Returns: false</span> 3207util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-function">() =></span> {}); 3208<span class="hljs-comment">// Returns: false</span> 3209util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-regexp">/^$/</span>); 3210<span class="hljs-comment">// Returns: false</span> 3211util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>()); 3212<span class="hljs-comment">// Returns: false</span></code> <button class="copy-button">copy</button></pre> 3213<h4><code>util.isRegExp(object)</code><span><a class="mark" href="#utilisregexpobject" id="utilisregexpobject">#</a></span><a aria-hidden="true" class="legacy" id="util_util_isregexp_object"></a></h4> 3214<div class="api_metadata"> 3215<span>Added in: v0.6.0</span><span>Deprecated since: v4.0.0</span> 3216</div> 3217<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated</div><p></p> 3218<ul> 3219<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3220<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3221</ul> 3222<p>Returns <code>true</code> if the given <code>object</code> is a <code>RegExp</code>. Otherwise, returns <code>false</code>.</p> 3223<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 3224 3225util.<span class="hljs-title function_">isRegExp</span>(<span class="hljs-regexp">/some regexp/</span>); 3226<span class="hljs-comment">// Returns: true</span> 3227util.<span class="hljs-title function_">isRegExp</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">RegExp</span>(<span class="hljs-string">'another regexp'</span>)); 3228<span class="hljs-comment">// Returns: true</span> 3229util.<span class="hljs-title function_">isRegExp</span>({}); 3230<span class="hljs-comment">// Returns: false</span></code> <button class="copy-button">copy</button></pre> 3231<h4><code>util.isString(object)</code><span><a class="mark" href="#utilisstringobject" id="utilisstringobject">#</a></span><a aria-hidden="true" class="legacy" id="util_util_isstring_object"></a></h4> 3232<div class="api_metadata"> 3233<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3234</div> 3235<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <code>typeof value === 'string'</code> instead.</div><p></p> 3236<ul> 3237<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3238<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3239</ul> 3240<p>Returns <code>true</code> if the given <code>object</code> is a <code>string</code>. Otherwise, returns <code>false</code>.</p> 3241<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 3242 3243util.<span class="hljs-title function_">isString</span>(<span class="hljs-string">''</span>); 3244<span class="hljs-comment">// Returns: true</span> 3245util.<span class="hljs-title function_">isString</span>(<span class="hljs-string">'foo'</span>); 3246<span class="hljs-comment">// Returns: true</span> 3247util.<span class="hljs-title function_">isString</span>(<span class="hljs-title class_">String</span>(<span class="hljs-string">'foo'</span>)); 3248<span class="hljs-comment">// Returns: true</span> 3249util.<span class="hljs-title function_">isString</span>(<span class="hljs-number">5</span>); 3250<span class="hljs-comment">// Returns: false</span></code> <button class="copy-button">copy</button></pre> 3251<h4><code>util.isSymbol(object)</code><span><a class="mark" href="#utilissymbolobject" id="utilissymbolobject">#</a></span><a aria-hidden="true" class="legacy" id="util_util_issymbol_object"></a></h4> 3252<div class="api_metadata"> 3253<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3254</div> 3255<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <code>typeof value === 'symbol'</code> instead.</div><p></p> 3256<ul> 3257<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3258<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3259</ul> 3260<p>Returns <code>true</code> if the given <code>object</code> is a <code>Symbol</code>. Otherwise, returns <code>false</code>.</p> 3261<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 3262 3263util.<span class="hljs-title function_">isSymbol</span>(<span class="hljs-number">5</span>); 3264<span class="hljs-comment">// Returns: false</span> 3265util.<span class="hljs-title function_">isSymbol</span>(<span class="hljs-string">'foo'</span>); 3266<span class="hljs-comment">// Returns: false</span> 3267util.<span class="hljs-title function_">isSymbol</span>(<span class="hljs-title class_">Symbol</span>(<span class="hljs-string">'foo'</span>)); 3268<span class="hljs-comment">// Returns: true</span></code> <button class="copy-button">copy</button></pre> 3269<h4><code>util.isUndefined(object)</code><span><a class="mark" href="#utilisundefinedobject" id="utilisundefinedobject">#</a></span><a aria-hidden="true" class="legacy" id="util_util_isundefined_object"></a></h4> 3270<div class="api_metadata"> 3271<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3272</div> 3273<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <code>value === undefined</code> instead.</div><p></p> 3274<ul> 3275<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3276<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3277</ul> 3278<p>Returns <code>true</code> if the given <code>object</code> is <code>undefined</code>. Otherwise, returns <code>false</code>.</p> 3279<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 3280 3281<span class="hljs-keyword">const</span> foo = <span class="hljs-literal">undefined</span>; 3282util.<span class="hljs-title function_">isUndefined</span>(<span class="hljs-number">5</span>); 3283<span class="hljs-comment">// Returns: false</span> 3284util.<span class="hljs-title function_">isUndefined</span>(foo); 3285<span class="hljs-comment">// Returns: true</span> 3286util.<span class="hljs-title function_">isUndefined</span>(<span class="hljs-literal">null</span>); 3287<span class="hljs-comment">// Returns: false</span></code> <button class="copy-button">copy</button></pre> 3288<h4><code>util.log(string)</code><span><a class="mark" href="#utillogstring" id="utillogstring">#</a></span><a aria-hidden="true" class="legacy" id="util_util_log_string"></a></h4> 3289<div class="api_metadata"> 3290<span>Added in: v0.3.0</span><span>Deprecated since: v6.0.0</span> 3291</div> 3292<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use a third party module instead.</div><p></p> 3293<ul> 3294<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3295</ul> 3296<p>The <code>util.log()</code> method prints the given <code>string</code> to <code>stdout</code> with an included 3297timestamp.</p> 3298<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:util'</span>); 3299 3300util.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Timestamped message.'</span>);</code> <button class="copy-button">copy</button></pre></section> 3301 <!-- API END --> 3302 </div> 3303 </div> 3304 </div> 3305</body> 3306</html> 3307