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.18.2"> 7 <title>Util | Node.js v18.18.2 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.18.2 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.18.2</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-v20.x/api/util.html">20.x</a></li> 381<li><a href="https://nodejs.org/docs/latest-v19.x/api/util.html">19.x</a></li> 382<li><a href="https://nodejs.org/docs/latest-v18.x/api/util.html">18.x <b>LTS</b></a></li> 383<li><a href="https://nodejs.org/docs/latest-v17.x/api/util.html">17.x</a></li> 384<li><a href="https://nodejs.org/docs/latest-v16.x/api/util.html">16.x</a></li> 385<li><a href="https://nodejs.org/docs/latest-v15.x/api/util.html">15.x</a></li> 386<li><a href="https://nodejs.org/docs/latest-v14.x/api/util.html">14.x</a></li> 387<li><a href="https://nodejs.org/docs/latest-v13.x/api/util.html">13.x</a></li> 388<li><a href="https://nodejs.org/docs/latest-v12.x/api/util.html">12.x</a></li> 389<li><a href="https://nodejs.org/docs/latest-v11.x/api/util.html">11.x</a></li> 390<li><a href="https://nodejs.org/docs/latest-v10.x/api/util.html">10.x</a></li> 391<li><a href="https://nodejs.org/docs/latest-v9.x/api/util.html">9.x</a></li> 392<li><a href="https://nodejs.org/docs/latest-v8.x/api/util.html">8.x</a></li> 393<li><a href="https://nodejs.org/docs/latest-v7.x/api/util.html">7.x</a></li> 394<li><a href="https://nodejs.org/docs/latest-v6.x/api/util.html">6.x</a></li> 395<li><a href="https://nodejs.org/docs/latest-v5.x/api/util.html">5.x</a></li> 396<li><a href="https://nodejs.org/docs/latest-v4.x/api/util.html">4.x</a></li> 397<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/util.html">0.12.x</a></li> 398<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/util.html">0.10.x</a></li></ol></div> 399 </li> 400 401 <li class="picker-header"> 402 <a href="#"> 403 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 404 Options 405 </a> 406 407 <div class="picker"> 408 <ul> 409 <li> 410 <a href="all.html">View on single page</a> 411 </li> 412 <li> 413 <a href="util.json">View as JSON</a> 414 </li> 415 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/util.md">Edit on GitHub</a></li> 416 </ul> 417 </div> 418 </li> 419 </ul> 420 </div> 421 <hr> 422 </header> 423 424 <details id="toc" open><summary>Table of contents</summary><ul> 425<li><span class="stability_2"><a href="#util">Util</a></span> 426<ul> 427<li><a href="#utilcallbackifyoriginal"><code>util.callbackify(original)</code></a></li> 428<li><a href="#utildebuglogsection-callback"><code>util.debuglog(section[, callback])</code></a> 429<ul> 430<li><a href="#debuglogenabled"><code>debuglog().enabled</code></a></li> 431</ul> 432</li> 433<li><a href="#utildebugsection"><code>util.debug(section)</code></a></li> 434<li><a href="#utildeprecatefn-msg-code"><code>util.deprecate(fn, msg[, code])</code></a></li> 435<li><a href="#utilformatformat-args"><code>util.format(format[, ...args])</code></a></li> 436<li><a href="#utilformatwithoptionsinspectoptions-format-args"><code>util.formatWithOptions(inspectOptions, format[, ...args])</code></a></li> 437<li><a href="#utilgetsystemerrornameerr"><code>util.getSystemErrorName(err)</code></a></li> 438<li><a href="#utilgetsystemerrormap"><code>util.getSystemErrorMap()</code></a></li> 439<li><span class="stability_3"><a href="#utilinheritsconstructor-superconstructor"><code>util.inherits(constructor, superConstructor)</code></a></span></li> 440<li><a href="#utilinspectobject-options"><code>util.inspect(object[, options])</code></a></li> 441<li><a href="#utilinspectobject-showhidden-depth-colors"><code>util.inspect(object[, showHidden[, depth[, colors]]])</code></a> 442<ul> 443<li><a href="#customizing-utilinspect-colors">Customizing <code>util.inspect</code> colors</a> 444<ul> 445<li><a href="#modifiers">Modifiers</a></li> 446<li><a href="#foreground-colors">Foreground colors</a></li> 447<li><a href="#background-colors">Background colors</a></li> 448</ul> 449</li> 450<li><a href="#custom-inspection-functions-on-objects">Custom inspection functions on objects</a></li> 451<li><a href="#utilinspectcustom"><code>util.inspect.custom</code></a></li> 452<li><a href="#utilinspectdefaultoptions"><code>util.inspect.defaultOptions</code></a></li> 453</ul> 454</li> 455<li><a href="#utilisdeepstrictequalval1-val2"><code>util.isDeepStrictEqual(val1, val2)</code></a></li> 456<li><span class="stability_1"><a href="#class-utilmimetype">Class: <code>util.MIMEType</code></a></span> 457<ul> 458<li><a href="#constructor-new-mimetypeinput">Constructor: <code>new MIMEType(input)</code></a></li> 459<li><a href="#mimetype"><code>mime.type</code></a></li> 460<li><a href="#mimesubtype"><code>mime.subtype</code></a></li> 461<li><a href="#mimeessence"><code>mime.essence</code></a></li> 462<li><a href="#mimeparams"><code>mime.params</code></a></li> 463<li><a href="#mimetostring"><code>mime.toString()</code></a></li> 464<li><a href="#mimetojson"><code>mime.toJSON()</code></a></li> 465</ul> 466</li> 467<li><a href="#class-utilmimeparams">Class: <code>util.MIMEParams</code></a> 468<ul> 469<li><a href="#constructor-new-mimeparams">Constructor: <code>new MIMEParams()</code></a></li> 470<li><a href="#mimeparamsdeletename"><code>mimeParams.delete(name)</code></a></li> 471<li><a href="#mimeparamsentries"><code>mimeParams.entries()</code></a></li> 472<li><a href="#mimeparamsgetname"><code>mimeParams.get(name)</code></a></li> 473<li><a href="#mimeparamshasname"><code>mimeParams.has(name)</code></a></li> 474<li><a href="#mimeparamskeys"><code>mimeParams.keys()</code></a></li> 475<li><a href="#mimeparamssetname-value"><code>mimeParams.set(name, value)</code></a></li> 476<li><a href="#mimeparamsvalues"><code>mimeParams.values()</code></a></li> 477<li><a href="#mimeparamsiterator"><code>mimeParams[@@iterator]()</code></a></li> 478</ul> 479</li> 480<li><span class="stability_1"><a href="#utilparseargsconfig"><code>util.parseArgs([config])</code></a></span> 481<ul> 482<li><a href="#parseargs-tokens"><code>parseArgs</code> <code>tokens</code></a></li> 483</ul> 484</li> 485<li><a href="#utilpromisifyoriginal"><code>util.promisify(original)</code></a> 486<ul> 487<li><a href="#custom-promisified-functions">Custom promisified functions</a></li> 488<li><a href="#utilpromisifycustom"><code>util.promisify.custom</code></a></li> 489</ul> 490</li> 491<li><a href="#utilstripvtcontrolcharactersstr"><code>util.stripVTControlCharacters(str)</code></a></li> 492<li><a href="#class-utiltextdecoder">Class: <code>util.TextDecoder</code></a> 493<ul> 494<li><a href="#whatwg-supported-encodings">WHATWG supported encodings</a> 495<ul> 496<li><a href="#encodings-supported-by-default-with-full-icu-data">Encodings supported by default (with full ICU data)</a></li> 497<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> 498<li><a href="#encodings-supported-when-icu-is-disabled">Encodings supported when ICU is disabled</a></li> 499</ul> 500</li> 501<li><a href="#new-textdecoderencoding-options"><code>new TextDecoder([encoding[, options]])</code></a></li> 502<li><a href="#textdecoderdecodeinput-options"><code>textDecoder.decode([input[, options]])</code></a></li> 503<li><a href="#textdecoderencoding"><code>textDecoder.encoding</code></a></li> 504<li><a href="#textdecoderfatal"><code>textDecoder.fatal</code></a></li> 505<li><a href="#textdecoderignorebom"><code>textDecoder.ignoreBOM</code></a></li> 506</ul> 507</li> 508<li><a href="#class-utiltextencoder">Class: <code>util.TextEncoder</code></a> 509<ul> 510<li><a href="#textencoderencodeinput"><code>textEncoder.encode([input])</code></a></li> 511<li><a href="#textencoderencodeintosrc-dest"><code>textEncoder.encodeInto(src, dest)</code></a></li> 512<li><a href="#textencoderencoding"><code>textEncoder.encoding</code></a></li> 513</ul> 514</li> 515<li><a href="#utiltousvstringstring"><code>util.toUSVString(string)</code></a></li> 516<li><span class="stability_1"><a href="#utiltransferableabortcontroller"><code>util.transferableAbortController()</code></a></span></li> 517<li><span class="stability_1"><a href="#utiltransferableabortsignalsignal"><code>util.transferableAbortSignal(signal)</code></a></span></li> 518<li><span class="stability_1"><a href="#utilabortedsignal-resource"><code>util.aborted(signal, resource)</code></a></span></li> 519<li><a href="#utiltypes"><code>util.types</code></a> 520<ul> 521<li><a href="#utiltypesisanyarraybuffervalue"><code>util.types.isAnyArrayBuffer(value)</code></a></li> 522<li><a href="#utiltypesisarraybufferviewvalue"><code>util.types.isArrayBufferView(value)</code></a></li> 523<li><a href="#utiltypesisargumentsobjectvalue"><code>util.types.isArgumentsObject(value)</code></a></li> 524<li><a href="#utiltypesisarraybuffervalue"><code>util.types.isArrayBuffer(value)</code></a></li> 525<li><a href="#utiltypesisasyncfunctionvalue"><code>util.types.isAsyncFunction(value)</code></a></li> 526<li><a href="#utiltypesisbigint64arrayvalue"><code>util.types.isBigInt64Array(value)</code></a></li> 527<li><a href="#utiltypesisbiguint64arrayvalue"><code>util.types.isBigUint64Array(value)</code></a></li> 528<li><a href="#utiltypesisbooleanobjectvalue"><code>util.types.isBooleanObject(value)</code></a></li> 529<li><a href="#utiltypesisboxedprimitivevalue"><code>util.types.isBoxedPrimitive(value)</code></a></li> 530<li><a href="#utiltypesiscryptokeyvalue"><code>util.types.isCryptoKey(value)</code></a></li> 531<li><a href="#utiltypesisdataviewvalue"><code>util.types.isDataView(value)</code></a></li> 532<li><a href="#utiltypesisdatevalue"><code>util.types.isDate(value)</code></a></li> 533<li><a href="#utiltypesisexternalvalue"><code>util.types.isExternal(value)</code></a></li> 534<li><a href="#utiltypesisfloat32arrayvalue"><code>util.types.isFloat32Array(value)</code></a></li> 535<li><a href="#utiltypesisfloat64arrayvalue"><code>util.types.isFloat64Array(value)</code></a></li> 536<li><a href="#utiltypesisgeneratorfunctionvalue"><code>util.types.isGeneratorFunction(value)</code></a></li> 537<li><a href="#utiltypesisgeneratorobjectvalue"><code>util.types.isGeneratorObject(value)</code></a></li> 538<li><a href="#utiltypesisint8arrayvalue"><code>util.types.isInt8Array(value)</code></a></li> 539<li><a href="#utiltypesisint16arrayvalue"><code>util.types.isInt16Array(value)</code></a></li> 540<li><a href="#utiltypesisint32arrayvalue"><code>util.types.isInt32Array(value)</code></a></li> 541<li><a href="#utiltypesiskeyobjectvalue"><code>util.types.isKeyObject(value)</code></a></li> 542<li><a href="#utiltypesismapvalue"><code>util.types.isMap(value)</code></a></li> 543<li><a href="#utiltypesismapiteratorvalue"><code>util.types.isMapIterator(value)</code></a></li> 544<li><a href="#utiltypesismodulenamespaceobjectvalue"><code>util.types.isModuleNamespaceObject(value)</code></a></li> 545<li><a href="#utiltypesisnativeerrorvalue"><code>util.types.isNativeError(value)</code></a></li> 546<li><a href="#utiltypesisnumberobjectvalue"><code>util.types.isNumberObject(value)</code></a></li> 547<li><a href="#utiltypesispromisevalue"><code>util.types.isPromise(value)</code></a></li> 548<li><a href="#utiltypesisproxyvalue"><code>util.types.isProxy(value)</code></a></li> 549<li><a href="#utiltypesisregexpvalue"><code>util.types.isRegExp(value)</code></a></li> 550<li><a href="#utiltypesissetvalue"><code>util.types.isSet(value)</code></a></li> 551<li><a href="#utiltypesissetiteratorvalue"><code>util.types.isSetIterator(value)</code></a></li> 552<li><a href="#utiltypesissharedarraybuffervalue"><code>util.types.isSharedArrayBuffer(value)</code></a></li> 553<li><a href="#utiltypesisstringobjectvalue"><code>util.types.isStringObject(value)</code></a></li> 554<li><a href="#utiltypesissymbolobjectvalue"><code>util.types.isSymbolObject(value)</code></a></li> 555<li><a href="#utiltypesistypedarrayvalue"><code>util.types.isTypedArray(value)</code></a></li> 556<li><a href="#utiltypesisuint8arrayvalue"><code>util.types.isUint8Array(value)</code></a></li> 557<li><a href="#utiltypesisuint8clampedarrayvalue"><code>util.types.isUint8ClampedArray(value)</code></a></li> 558<li><a href="#utiltypesisuint16arrayvalue"><code>util.types.isUint16Array(value)</code></a></li> 559<li><a href="#utiltypesisuint32arrayvalue"><code>util.types.isUint32Array(value)</code></a></li> 560<li><a href="#utiltypesisweakmapvalue"><code>util.types.isWeakMap(value)</code></a></li> 561<li><a href="#utiltypesisweaksetvalue"><code>util.types.isWeakSet(value)</code></a></li> 562<li><span class="stability_0"><a href="#utiltypesiswebassemblycompiledmodulevalue"><code>util.types.isWebAssemblyCompiledModule(value)</code></a></span></li> 563</ul> 564</li> 565<li><a href="#deprecated-apis">Deprecated APIs</a> 566<ul> 567<li><span class="stability_0"><a href="#util_extendtarget-source"><code>util._extend(target, source)</code></a></span></li> 568<li><span class="stability_0"><a href="#utilisarrayobject"><code>util.isArray(object)</code></a></span></li> 569<li><span class="stability_0"><a href="#utilisbooleanobject"><code>util.isBoolean(object)</code></a></span></li> 570<li><span class="stability_0"><a href="#utilisbufferobject"><code>util.isBuffer(object)</code></a></span></li> 571<li><span class="stability_0"><a href="#utilisdateobject"><code>util.isDate(object)</code></a></span></li> 572<li><span class="stability_0"><a href="#utiliserrorobject"><code>util.isError(object)</code></a></span></li> 573<li><span class="stability_0"><a href="#utilisfunctionobject"><code>util.isFunction(object)</code></a></span></li> 574<li><span class="stability_0"><a href="#utilisnullobject"><code>util.isNull(object)</code></a></span></li> 575<li><span class="stability_0"><a href="#utilisnullorundefinedobject"><code>util.isNullOrUndefined(object)</code></a></span></li> 576<li><span class="stability_0"><a href="#utilisnumberobject"><code>util.isNumber(object)</code></a></span></li> 577<li><span class="stability_0"><a href="#utilisobjectobject"><code>util.isObject(object)</code></a></span></li> 578<li><span class="stability_0"><a href="#utilisprimitiveobject"><code>util.isPrimitive(object)</code></a></span></li> 579<li><span class="stability_0"><a href="#utilisregexpobject"><code>util.isRegExp(object)</code></a></span></li> 580<li><span class="stability_0"><a href="#utilisstringobject"><code>util.isString(object)</code></a></span></li> 581<li><span class="stability_0"><a href="#utilissymbolobject"><code>util.isSymbol(object)</code></a></span></li> 582<li><span class="stability_0"><a href="#utilisundefinedobject"><code>util.isUndefined(object)</code></a></span></li> 583<li><span class="stability_0"><a href="#utillogstring"><code>util.log(string)</code></a></span></li> 584</ul> 585</li> 586</ul> 587</li> 588</ul></details> 589 590 <div id="apicontent"> 591 <h2>Util<span><a class="mark" href="#util" id="util">#</a></span><a aria-hidden="true" class="legacy" id="util_util"></a></h2> 592 593<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 594<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.18.2/lib/util.js">lib/util.js</a></p> 595<p>The <code>node:util</code> module supports the needs of Node.js internal APIs. Many of the 596utilities are useful for application and module developers as well. To access 597it:</p> 598<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> 599<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> 600<div class="api_metadata"> 601<span>Added in: v8.2.0</span> 602</div> 603<ul> 604<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> 605<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> 606</ul> 607<p>Takes an <code>async</code> function (or a function that returns a <code>Promise</code>) and returns a 608function following the error-first callback style, i.e. taking 609an <code>(err, value) => ...</code> callback as the last argument. In the callback, the 610first argument will be the rejection reason (or <code>null</code> if the <code>Promise</code> 611resolved), and the second argument will be the resolved value.</p> 612<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>); 613 614<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">fn</span>(<span class="hljs-params"></span>) { 615 <span class="hljs-keyword">return</span> <span class="hljs-string">'hello world'</span>; 616} 617<span class="hljs-keyword">const</span> callbackFunction = util.<span class="hljs-title function_">callbackify</span>(fn); 618 619<span class="hljs-title function_">callbackFunction</span>(<span class="hljs-function">(<span class="hljs-params">err, ret</span>) =></span> { 620 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err; 621 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(ret); 622});</code> <button class="copy-button">copy</button></pre> 623<p>Will print:</p> 624<pre><code class="language-text">hello world</code> <button class="copy-button">copy</button></pre> 625<p>The callback is executed asynchronously, and will have a limited stack trace. 626If the callback throws, the process will emit an <a href="process.html#event-uncaughtexception"><code>'uncaughtException'</code></a> 627event, and if not handled will exit.</p> 628<p>Since <code>null</code> has a special meaning as the first argument to a callback, if a 629wrapped function rejects a <code>Promise</code> with a falsy value as a reason, the value 630is wrapped in an <code>Error</code> with the original value stored in a field named 631<code>reason</code>.</p> 632<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">fn</span>(<span class="hljs-params"></span>) { 633 <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>); 634} 635<span class="hljs-keyword">const</span> callbackFunction = util.<span class="hljs-title function_">callbackify</span>(fn); 636 637<span class="hljs-title function_">callbackFunction</span>(<span class="hljs-function">(<span class="hljs-params">err, ret</span>) =></span> { 638 <span class="hljs-comment">// When the Promise was rejected with `null` it is wrapped with an Error and</span> 639 <span class="hljs-comment">// the original value is stored in `reason`.</span> 640 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> 641});</code> <button class="copy-button">copy</button></pre> 642</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> 643<div class="api_metadata"> 644<span>Added in: v0.11.3</span> 645</div> 646<ul> 647<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 648which the <code>debuglog</code> function is being created.</li> 649<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 650is called with a function argument that is a more optimized logging function.</li> 651<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> 652</ul> 653<p>The <code>util.debuglog()</code> method is used to create a function that conditionally 654writes debug messages to <code>stderr</code> based on the existence of the <code>NODE_DEBUG</code> 655environment variable. If the <code>section</code> name appears within the value of that 656environment variable, then the returned function operates similar to 657<a href="console.html#consoleerrordata-args"><code>console.error()</code></a>. If not, then the returned function is a no-op.</p> 658<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>); 659<span class="hljs-keyword">const</span> debuglog = util.<span class="hljs-title function_">debuglog</span>(<span class="hljs-string">'foo'</span>); 660 661<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> 662<p>If this program is run with <code>NODE_DEBUG=foo</code> in the environment, then 663it will output something like:</p> 664<pre><code class="language-console">FOO 3245: hello from foo [123]</code> <button class="copy-button">copy</button></pre> 665<p>where <code>3245</code> is the process id. If it is not run with that 666environment variable set, then it will not print anything.</p> 667<p>The <code>section</code> supports wildcard also:</p> 668<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>); 669<span class="hljs-keyword">const</span> debuglog = util.<span class="hljs-title function_">debuglog</span>(<span class="hljs-string">'foo-bar'</span>); 670 671<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> 672<p>if it is run with <code>NODE_DEBUG=foo*</code> in the environment, then it will output 673something like:</p> 674<pre><code class="language-console">FOO-BAR 3257: hi there, it's foo-bar [2333]</code> <button class="copy-button">copy</button></pre> 675<p>Multiple comma-separated <code>section</code> names may be specified in the <code>NODE_DEBUG</code> 676environment variable: <code>NODE_DEBUG=fs,net,tls</code>.</p> 677<p>The optional <code>callback</code> argument can be used to replace the logging function 678with a different function that doesn't have any initialization or 679unnecessary wrapping.</p> 680<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>); 681<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> { 682 <span class="hljs-comment">// Replace with a logging function that optimizes out</span> 683 <span class="hljs-comment">// testing if the section is enabled</span> 684 debuglog = debug; 685});</code> <button class="copy-button">copy</button></pre> 686<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> 687<div class="api_metadata"> 688<span>Added in: v14.9.0</span> 689</div> 690<ul> 691<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 692</ul> 693<p>The <code>util.debuglog().enabled</code> getter is used to create a test that can be used 694in conditionals based on the existence of the <code>NODE_DEBUG</code> environment variable. 695If the <code>section</code> name appears within the value of that environment variable, 696then the returned value will be <code>true</code>. If not, then the returned value will be 697<code>false</code>.</p> 698<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>); 699<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>; 700<span class="hljs-keyword">if</span> (enabled) { 701 <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>); 702}</code> <button class="copy-button">copy</button></pre> 703<p>If this program is run with <code>NODE_DEBUG=foo</code> in the environment, then it will 704output something like:</p> 705<pre><code class="language-console">hello from foo [123]</code> <button class="copy-button">copy</button></pre> 706</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> 707<div class="api_metadata"> 708<span>Added in: v14.9.0</span> 709</div> 710<p>Alias for <code>util.debuglog</code>. Usage allows for readability of that doesn't imply 711logging when only using <code>util.debuglog().enabled</code>.</p> 712</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> 713<div class="api_metadata"> 714<details class="changelog"><summary>History</summary> 715<table> 716<tbody><tr><th>Version</th><th>Changes</th></tr> 717<tr><td>v10.0.0</td> 718<td><p>Deprecation warnings are only emitted once for each code.</p></td></tr> 719<tr><td>v0.8.0</td> 720<td><p><span>Added in: v0.8.0</span></p></td></tr> 721</tbody></table> 722</details> 723</div> 724<ul> 725<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> 726<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 727invoked.</li> 728<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 729list of codes.</li> 730<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> 731</ul> 732<p>The <code>util.deprecate()</code> method wraps <code>fn</code> (which may be a function or class) in 733such a way that it is marked as deprecated.</p> 734<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>); 735 736<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> { 737 <span class="hljs-comment">// Do something here.</span> 738}, <span class="hljs-string">'obsoleteFunction() is deprecated. Use newShinyFunction() instead.'</span>);</code> <button class="copy-button">copy</button></pre> 739<p>When called, <code>util.deprecate()</code> will return a function that will emit a 740<code>DeprecationWarning</code> using the <a href="process.html#event-warning"><code>'warning'</code></a> event. The warning will 741be emitted and printed to <code>stderr</code> the first time the returned function is 742called. After the warning is emitted, the wrapped function is called without 743emitting a warning.</p> 744<p>If the same optional <code>code</code> is supplied in multiple calls to <code>util.deprecate()</code>, 745the warning will be emitted only once for that <code>code</code>.</p> 746<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>); 747 748<span class="hljs-keyword">const</span> fn1 = util.<span class="hljs-title function_">deprecate</span>(someFunction, someMessage, <span class="hljs-string">'DEP0001'</span>); 749<span class="hljs-keyword">const</span> fn2 = util.<span class="hljs-title function_">deprecate</span>(someOtherFunction, someOtherMessage, <span class="hljs-string">'DEP0001'</span>); 750<span class="hljs-title function_">fn1</span>(); <span class="hljs-comment">// Emits a deprecation warning with code DEP0001</span> 751<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> 752<p>If either the <code>--no-deprecation</code> or <code>--no-warnings</code> command-line flags are 753used, or if the <code>process.noDeprecation</code> property is set to <code>true</code> <em>prior</em> to 754the first deprecation warning, the <code>util.deprecate()</code> method does nothing.</p> 755<p>If the <code>--trace-deprecation</code> or <code>--trace-warnings</code> command-line flags are set, 756or the <code>process.traceDeprecation</code> property is set to <code>true</code>, a warning and a 757stack trace are printed to <code>stderr</code> the first time the deprecated function is 758called.</p> 759<p>If the <code>--throw-deprecation</code> command-line flag is set, or the 760<code>process.throwDeprecation</code> property is set to <code>true</code>, then an exception will be 761thrown when the deprecated function is called.</p> 762<p>The <code>--throw-deprecation</code> command-line flag and <code>process.throwDeprecation</code> 763property take precedence over <code>--trace-deprecation</code> and 764<code>process.traceDeprecation</code>.</p> 765</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> 766<div class="api_metadata"> 767<details class="changelog"><summary>History</summary> 768<table> 769<tbody><tr><th>Version</th><th>Changes</th></tr> 770<tr><td>v12.11.0</td> 771<td><p>The <code>%c</code> specifier is ignored now.</p></td></tr> 772<tr><td>v12.0.0</td> 773<td><p>The <code>format</code> argument is now only taken as such if it actually contains format specifiers.</p></td></tr> 774<tr><td>v12.0.0</td> 775<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> 776<tr><td>v11.4.0</td> 777<td><p>The <code>%d</code>, <code>%f</code>, and <code>%i</code> specifiers now support Symbols properly.</p></td></tr> 778<tr><td>v11.4.0</td> 779<td><p>The <code>%o</code> specifier's <code>depth</code> has default depth of 4 again.</p></td></tr> 780<tr><td>v11.0.0</td> 781<td><p>The <code>%o</code> specifier's <code>depth</code> option will now fall back to the default depth.</p></td></tr> 782<tr><td>v10.12.0</td> 783<td><p>The <code>%d</code> and <code>%i</code> specifiers now support BigInt.</p></td></tr> 784<tr><td>v8.4.0</td> 785<td><p>The <code>%o</code> and <code>%O</code> specifiers are supported now.</p></td></tr> 786<tr><td>v0.5.3</td> 787<td><p><span>Added in: v0.5.3</span></p></td></tr> 788</tbody></table> 789</details> 790</div> 791<ul> 792<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> 793</ul> 794<p>The <code>util.format()</code> method returns a formatted string using the first argument 795as a <code>printf</code>-like format string which can contain zero or more format 796specifiers. Each specifier is replaced with the converted value from the 797corresponding argument. Supported specifiers are:</p> 798<ul> 799<li><code>%s</code>: <code>String</code> will be used to convert all values except <code>BigInt</code>, <code>Object</code> 800and <code>-0</code>. <code>BigInt</code> values will be represented with an <code>n</code> and Objects that 801have no user defined <code>toString</code> function are inspected using <code>util.inspect()</code> 802with options <code>{ depth: 0, colors: false, compact: 3 }</code>.</li> 803<li><code>%d</code>: <code>Number</code> will be used to convert all values except <code>BigInt</code> and 804<code>Symbol</code>.</li> 805<li><code>%i</code>: <code>parseInt(value, 10)</code> is used for all values except <code>BigInt</code> and 806<code>Symbol</code>.</li> 807<li><code>%f</code>: <code>parseFloat(value)</code> is used for all values expect <code>Symbol</code>.</li> 808<li><code>%j</code>: JSON. Replaced with the string <code>'[Circular]'</code> if the argument contains 809circular references.</li> 810<li><code>%o</code>: <code>Object</code>. A string representation of an object with generic JavaScript 811object formatting. Similar to <code>util.inspect()</code> with options 812<code>{ showHidden: true, showProxy: true }</code>. This will show the full object 813including non-enumerable properties and proxies.</li> 814<li><code>%O</code>: <code>Object</code>. A string representation of an object with generic JavaScript 815object formatting. Similar to <code>util.inspect()</code> without options. This will show 816the full object not including non-enumerable properties and proxies.</li> 817<li><code>%c</code>: <code>CSS</code>. This specifier is ignored and will skip any CSS passed in.</li> 818<li><code>%%</code>: single percent sign (<code>'%'</code>). This does not consume an argument.</li> 819<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> 820</ul> 821<p>If a specifier does not have a corresponding argument, it is not replaced:</p> 822<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>); 823<span class="hljs-comment">// Returns: 'foo:%s'</span></code> <button class="copy-button">copy</button></pre> 824<p>Values that are not part of the format string are formatted using 825<code>util.inspect()</code> if their type is not <code>string</code>.</p> 826<p>If there are more arguments passed to the <code>util.format()</code> method than the 827number of specifiers, the extra arguments are concatenated to the returned 828string, separated by spaces:</p> 829<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>); 830<span class="hljs-comment">// Returns: 'foo:bar baz'</span></code> <button class="copy-button">copy</button></pre> 831<p>If the first argument does not contain a valid format specifier, <code>util.format()</code> 832returns a string that is the concatenation of all arguments separated by spaces:</p> 833<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>); 834<span class="hljs-comment">// Returns: '1 2 3'</span></code> <button class="copy-button">copy</button></pre> 835<p>If only one argument is passed to <code>util.format()</code>, it is returned as it is 836without any formatting:</p> 837<pre><code class="language-js">util.<span class="hljs-title function_">format</span>(<span class="hljs-string">'%% %s'</span>); 838<span class="hljs-comment">// Returns: '%% %s'</span></code> <button class="copy-button">copy</button></pre> 839<p><code>util.format()</code> is a synchronous method that is intended as a debugging tool. 840Some input values can have a significant performance overhead that can block the 841event loop. Use this function with care and never in a hot code path.</p> 842</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> 843<div class="api_metadata"> 844<span>Added in: v10.0.0</span> 845</div> 846<ul> 847<li><code>inspectOptions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 848<li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 849</ul> 850<p>This function is identical to <a href="#utilformatformat-args"><code>util.format()</code></a>, except in that it takes 851an <code>inspectOptions</code> argument which specifies options that are passed along to 852<a href="#utilinspectobject-options"><code>util.inspect()</code></a>.</p> 853<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> }); 854<span class="hljs-comment">// Returns 'See object { foo: 42 }', where `42` is colored as a number</span> 855<span class="hljs-comment">// when printed to a terminal.</span></code> <button class="copy-button">copy</button></pre> 856</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> 857<div class="api_metadata"> 858<span>Added in: v9.7.0</span> 859</div> 860<ul> 861<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 862<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 863</ul> 864<p>Returns the string name for a numeric error code that comes from a Node.js API. 865The mapping between error codes and error names is platform-dependent. 866See <a href="errors.html#common-system-errors">Common System Errors</a> for the names of common errors.</p> 867<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> { 868 <span class="hljs-keyword">const</span> name = util.<span class="hljs-title function_">getSystemErrorName</span>(err.<span class="hljs-property">errno</span>); 869 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(name); <span class="hljs-comment">// ENOENT</span> 870});</code> <button class="copy-button">copy</button></pre> 871</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> 872<div class="api_metadata"> 873<span>Added in: v16.0.0, v14.17.0</span> 874</div> 875<ul> 876<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" class="type"><Map></a></li> 877</ul> 878<p>Returns a Map of all system error codes available from the Node.js API. 879The mapping between error codes and error names is platform-dependent. 880See <a href="errors.html#common-system-errors">Common System Errors</a> for the names of common errors.</p> 881<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> { 882 <span class="hljs-keyword">const</span> errorMap = util.<span class="hljs-title function_">getSystemErrorMap</span>(); 883 <span class="hljs-keyword">const</span> name = errorMap.<span class="hljs-title function_">get</span>(err.<span class="hljs-property">errno</span>); 884 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(name); <span class="hljs-comment">// ENOENT</span> 885});</code> <button class="copy-button">copy</button></pre> 886</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> 887<div class="api_metadata"> 888<details class="changelog"><summary>History</summary> 889<table> 890<tbody><tr><th>Version</th><th>Changes</th></tr> 891<tr><td>v5.0.0</td> 892<td><p>The <code>constructor</code> parameter can refer to an ES6 class now.</p></td></tr> 893<tr><td>v0.3.0</td> 894<td><p><span>Added in: v0.3.0</span></p></td></tr> 895</tbody></table> 896</details> 897</div> 898<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> 899<ul> 900<li><code>constructor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 901<li><code>superConstructor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 902</ul> 903<p>Usage of <code>util.inherits()</code> is discouraged. Please use the ES6 <code>class</code> and 904<code>extends</code> keywords to get language level inheritance support. Also note 905that the two styles are <a href="https://github.com/nodejs/node/issues/4179">semantically incompatible</a>.</p> 906<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 907prototype of <code>constructor</code> will be set to a new object created from 908<code>superConstructor</code>.</p> 909<p>This mainly adds some input validation on top of 910<code>Object.setPrototypeOf(constructor.prototype, superConstructor.prototype)</code>. 911As an additional convenience, <code>superConstructor</code> will be accessible 912through the <code>constructor.super_</code> property.</p> 913<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>); 914<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>); 915 916<span class="hljs-keyword">function</span> <span class="hljs-title function_">MyStream</span>(<span class="hljs-params"></span>) { 917 <span class="hljs-title class_">EventEmitter</span>.<span class="hljs-title function_">call</span>(<span class="hljs-variable language_">this</span>); 918} 919 920util.<span class="hljs-title function_">inherits</span>(<span class="hljs-title class_">MyStream</span>, <span class="hljs-title class_">EventEmitter</span>); 921 922<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>) { 923 <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'data'</span>, data); 924}; 925 926<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyStream</span>(); 927 928<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> 929<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> 930 931stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 932 <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>); 933}); 934stream.<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> 935<p>ES6 example using <code>class</code> and <code>extends</code>:</p> 936<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>); 937 938<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> { 939 <span class="hljs-title function_">write</span>(<span class="hljs-params">data</span>) { 940 <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'data'</span>, data); 941 } 942} 943 944<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyStream</span>(); 945 946stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">data</span>) =></span> { 947 <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>); 948}); 949stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'With ES6'</span>);</code> <button class="copy-button">copy</button></pre> 950</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> 951</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> 952<div class="api_metadata"> 953<details class="changelog"><summary>History</summary> 954<table> 955<tbody><tr><th>Version</th><th>Changes</th></tr> 956<tr><td>v17.3.0, v16.14.0</td> 957<td><p>The <code>numericSeparator</code> option is supported now.</p></td></tr> 958<tr><td>v13.0.0</td> 959<td><p>Circular references now include a marker to the reference.</p></td></tr> 960<tr><td>v14.6.0, v12.19.0</td> 961<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> 962<tr><td>v13.13.0, v12.17.0</td> 963<td><p>The <code>maxStringLength</code> option is supported now.</p></td></tr> 964<tr><td>v13.5.0, v12.16.0</td> 965<td><p>User defined prototype properties are inspected in case <code>showHidden</code> is <code>true</code>.</p></td></tr> 966<tr><td>v12.0.0</td> 967<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> 968<tr><td>v12.0.0</td> 969<td><p>Internal properties no longer appear in the context argument of a custom inspection function.</p></td></tr> 970<tr><td>v11.11.0</td> 971<td><p>The <code>compact</code> option accepts numbers for a new output mode.</p></td></tr> 972<tr><td>v11.7.0</td> 973<td><p>ArrayBuffers now also show their binary contents.</p></td></tr> 974<tr><td>v11.5.0</td> 975<td><p>The <code>getters</code> option is supported now.</p></td></tr> 976<tr><td>v11.4.0</td> 977<td><p>The <code>depth</code> default changed back to <code>2</code>.</p></td></tr> 978<tr><td>v11.0.0</td> 979<td><p>The <code>depth</code> default changed to <code>20</code>.</p></td></tr> 980<tr><td>v11.0.0</td> 981<td><p>The inspection output is now limited to about 128 MiB. Data above that size will not be fully inspected.</p></td></tr> 982<tr><td>v10.12.0</td> 983<td><p>The <code>sorted</code> option is supported now.</p></td></tr> 984<tr><td>v10.6.0</td> 985<td><p>Inspecting linked lists and similar objects is now possible up to the maximum call stack size.</p></td></tr> 986<tr><td>v10.0.0</td> 987<td><p>The <code>WeakMap</code> and <code>WeakSet</code> entries can now be inspected as well.</p></td></tr> 988<tr><td>v9.9.0</td> 989<td><p>The <code>compact</code> option is supported now.</p></td></tr> 990<tr><td>v6.6.0</td> 991<td><p>Custom inspection functions can now return <code>this</code>.</p></td></tr> 992<tr><td>v6.3.0</td> 993<td><p>The <code>breakLength</code> option is supported now.</p></td></tr> 994<tr><td>v6.1.0</td> 995<td><p>The <code>maxArrayLength</code> option is supported now; in particular, long arrays are truncated by default.</p></td></tr> 996<tr><td>v6.1.0</td> 997<td><p>The <code>showProxy</code> option is supported now.</p></td></tr> 998<tr><td>v0.3.0</td> 999<td><p><span>Added in: v0.3.0</span></p></td></tr> 1000</tbody></table> 1001</details> 1002</div> 1003<ul> 1004<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> 1005<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1006<ul> 1007<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 1008properties 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 1009<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 1010properties (excluding method properties). <strong>Default:</strong> <code>false</code>.</li> 1011<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 1012<code>object</code>. This is useful for inspecting large objects. To recurse up to 1013the maximum call stack size pass <code>Infinity</code> or <code>null</code>. 1014<strong>Default:</strong> <code>2</code>.</li> 1015<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 1016codes. Colors are customizable. See <a href="#customizing-utilinspect-colors">Customizing <code>util.inspect</code> colors</a>. 1017<strong>Default:</strong> <code>false</code>.</li> 1018<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>, 1019<code>[util.inspect.custom](depth, opts, inspect)</code> functions are not invoked. 1020<strong>Default:</strong> <code>true</code>.</li> 1021<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 1022the <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> 1023<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>, 1024<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 1025formatting. Set to <code>null</code> or <code>Infinity</code> to show all elements. Set to <code>0</code> or 1026negative to show no elements. <strong>Default:</strong> <code>100</code>.</li> 1027<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 1028include when formatting. Set to <code>null</code> or <code>Infinity</code> to show all elements. 1029Set to <code>0</code> or negative to show no characters. <strong>Default:</strong> <code>10000</code>.</li> 1030<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 1031multiple lines. Set to <code>Infinity</code> to format the input as a single line 1032(in combination with <code>compact</code> set to <code>true</code> or any number >= <code>1</code>). 1033<strong>Default:</strong> <code>80</code>.</li> 1034<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 1035to be displayed on a new line. It will break on new lines in text that is 1036longer than <code>breakLength</code>. If set to a number, the most <code>n</code> inner elements 1037are united on a single line as long as all properties fit into 1038<code>breakLength</code>. Short array elements are also grouped together. For more 1039information, see the example below. <strong>Default:</strong> <code>3</code>.</li> 1040<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 1041of an object, and <code>Set</code> and <code>Map</code> entries are sorted in the resulting 1042string. 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, 1043it 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> 1044<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 1045to <code>'get'</code>, only getters without a corresponding setter are inspected. If 1046set to <code>'set'</code>, only getters with a corresponding setter are inspected. 1047This might cause side effects depending on the getter function. 1048<strong>Default:</strong> <code>false</code>.</li> 1049<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 1050separate every three digits in all bigints and numbers. 1051<strong>Default:</strong> <code>false</code>.</li> 1052</ul> 1053</li> 1054<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> 1055</ul> 1056<p>The <code>util.inspect()</code> method returns a string representation of <code>object</code> that is 1057intended for debugging. The output of <code>util.inspect</code> may change at any time 1058and should not be depended upon programmatically. Additional <code>options</code> may be 1059passed that alter the result. 1060<code>util.inspect()</code> will use the constructor's name and/or <code>@@toStringTag</code> to make 1061an identifiable tag for an inspected value.</p> 1062<pre><code class="language-js"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> { 1063 get [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toStringTag</span>]() { 1064 <span class="hljs-keyword">return</span> <span class="hljs-string">'bar'</span>; 1065 } 1066} 1067 1068<span class="hljs-keyword">class</span> <span class="hljs-title class_">Bar</span> {} 1069 1070<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> } }); 1071 1072util.<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> 1073util.<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> 1074util.<span class="hljs-title function_">inspect</span>(baz); <span class="hljs-comment">// '[foo] {}'</span></code> <button class="copy-button">copy</button></pre> 1075<p>Circular references point to their anchor by using a reference index:</p> 1076<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>); 1077 1078<span class="hljs-keyword">const</span> obj = {}; 1079obj.<span class="hljs-property">a</span> = [obj]; 1080obj.<span class="hljs-property">b</span> = {}; 1081obj.<span class="hljs-property">b</span>.<span class="hljs-property">inner</span> = obj.<span class="hljs-property">b</span>; 1082obj.<span class="hljs-property">b</span>.<span class="hljs-property">obj</span> = obj; 1083 1084<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">inspect</span>(obj)); 1085<span class="hljs-comment">// <ref *1> {</span> 1086<span class="hljs-comment">// a: [ [Circular *1] ],</span> 1087<span class="hljs-comment">// b: <ref *2> { inner: [Circular *2], obj: [Circular *1] }</span> 1088<span class="hljs-comment">// }</span></code> <button class="copy-button">copy</button></pre> 1089<p>The following example inspects all properties of the <code>util</code> object:</p> 1090<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>); 1091 1092<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> 1093<p>The following example highlights the effect of the <code>compact</code> option:</p> 1094<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>); 1095 1096<span class="hljs-keyword">const</span> o = { 1097 <span class="hljs-attr">a</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, [[ 1098 <span class="hljs-string">'Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit, sed do '</span> + 1099 <span class="hljs-string">'eiusmod \ntempor incididunt ut labore et dolore magna aliqua.'</span>, 1100 <span class="hljs-string">'test'</span>, 1101 <span class="hljs-string">'foo'</span>]], <span class="hljs-number">4</span>], 1102 <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>]]), 1103}; 1104<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> })); 1105 1106<span class="hljs-comment">// { a:</span> 1107<span class="hljs-comment">// [ 1,</span> 1108<span class="hljs-comment">// 2,</span> 1109<span class="hljs-comment">// [ [ 'Lorem ipsum dolor sit amet,\nconsectetur [...]', // A long line</span> 1110<span class="hljs-comment">// 'test',</span> 1111<span class="hljs-comment">// 'foo' ] ],</span> 1112<span class="hljs-comment">// 4 ],</span> 1113<span class="hljs-comment">// b: Map(2) { 'za' => 1, 'zb' => 'test' } }</span> 1114 1115<span class="hljs-comment">// Setting `compact` to false or an integer creates more reader friendly output.</span> 1116<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> })); 1117 1118<span class="hljs-comment">// {</span> 1119<span class="hljs-comment">// a: [</span> 1120<span class="hljs-comment">// 1,</span> 1121<span class="hljs-comment">// 2,</span> 1122<span class="hljs-comment">// [</span> 1123<span class="hljs-comment">// [</span> 1124<span class="hljs-comment">// 'Lorem ipsum dolor sit amet,\n' +</span> 1125<span class="hljs-comment">// 'consectetur adipiscing elit, sed do eiusmod \n' +</span> 1126<span class="hljs-comment">// 'tempor incididunt ut labore et dolore magna aliqua.',</span> 1127<span class="hljs-comment">// 'test',</span> 1128<span class="hljs-comment">// 'foo'</span> 1129<span class="hljs-comment">// ]</span> 1130<span class="hljs-comment">// ],</span> 1131<span class="hljs-comment">// 4</span> 1132<span class="hljs-comment">// ],</span> 1133<span class="hljs-comment">// b: Map(2) {</span> 1134<span class="hljs-comment">// 'za' => 1,</span> 1135<span class="hljs-comment">// 'zb' => 'test'</span> 1136<span class="hljs-comment">// }</span> 1137<span class="hljs-comment">// }</span> 1138 1139<span class="hljs-comment">// Setting `breakLength` to e.g. 150 will print the "Lorem ipsum" text in a</span> 1140<span class="hljs-comment">// single line.</span></code> <button class="copy-button">copy</button></pre> 1141<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 1142inspected. If there are more entries than <code>maxArrayLength</code>, there is no 1143guarantee which entries are displayed. That means retrieving the same 1144<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 1145with no remaining strong references may be garbage collected at any time.</p> 1146<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>); 1147 1148<span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">a</span>: <span class="hljs-number">1</span> }; 1149<span class="hljs-keyword">const</span> obj2 = { <span class="hljs-attr">b</span>: <span class="hljs-number">2</span> }; 1150<span class="hljs-keyword">const</span> weakSet = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WeakSet</span>([obj, obj2]); 1151 1152<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> })); 1153<span class="hljs-comment">// WeakSet { { a: 1 }, { b: 2 } }</span></code> <button class="copy-button">copy</button></pre> 1154<p>The <code>sorted</code> option ensures that an object's property insertion order does not 1155impact the result of <code>util.inspect()</code>.</p> 1156<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>); 1157<span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 1158 1159<span class="hljs-keyword">const</span> o1 = { 1160 <span class="hljs-attr">b</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>], 1161 <span class="hljs-attr">a</span>: <span class="hljs-string">'`a` comes before `b`'</span>, 1162 <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>]), 1163}; 1164<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> })); 1165<span class="hljs-comment">// { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set(3) { 1, 2, 3 } }</span> 1166<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) })); 1167<span class="hljs-comment">// { c: Set(3) { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' }</span> 1168 1169<span class="hljs-keyword">const</span> o2 = { 1170 <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>]), 1171 <span class="hljs-attr">a</span>: <span class="hljs-string">'`a` comes before `b`'</span>, 1172 <span class="hljs-attr">b</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>], 1173}; 1174assert.<span class="hljs-property">strict</span>.<span class="hljs-title function_">equal</span>( 1175 <span class="hljs-title function_">inspect</span>(o1, { <span class="hljs-attr">sorted</span>: <span class="hljs-literal">true</span> }), 1176 <span class="hljs-title function_">inspect</span>(o2, { <span class="hljs-attr">sorted</span>: <span class="hljs-literal">true</span> }), 1177);</code> <button class="copy-button">copy</button></pre> 1178<p>The <code>numericSeparator</code> option adds an underscore every three digits to all 1179numbers.</p> 1180<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>); 1181 1182<span class="hljs-keyword">const</span> thousand = <span class="hljs-number">1_000</span>; 1183<span class="hljs-keyword">const</span> million = <span class="hljs-number">1_000_000</span>; 1184<span class="hljs-keyword">const</span> bigNumber = <span class="hljs-number">123_456_789n</span>; 1185<span class="hljs-keyword">const</span> bigDecimal = <span class="hljs-number">1_234.123_45</span>; 1186 1187<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(thousand, million, bigNumber, bigDecimal); 1188<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> 1189<p><code>util.inspect()</code> is a synchronous method intended for debugging. Its maximum 1190output length is approximately 128 MiB. Inputs that result in longer output will 1191be truncated.</p> 1192<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> 1193 1194<p>Color output (if enabled) of <code>util.inspect</code> is customizable globally 1195via the <code>util.inspect.styles</code> and <code>util.inspect.colors</code> properties.</p> 1196<p><code>util.inspect.styles</code> is a map associating a style name to a color from 1197<code>util.inspect.colors</code>.</p> 1198<p>The default styles and associated colors are:</p> 1199<ul> 1200<li><code>bigint</code>: <code>yellow</code></li> 1201<li><code>boolean</code>: <code>yellow</code></li> 1202<li><code>date</code>: <code>magenta</code></li> 1203<li><code>module</code>: <code>underline</code></li> 1204<li><code>name</code>: (no styling)</li> 1205<li><code>null</code>: <code>bold</code></li> 1206<li><code>number</code>: <code>yellow</code></li> 1207<li><code>regexp</code>: <code>red</code></li> 1208<li><code>special</code>: <code>cyan</code> (e.g., <code>Proxies</code>)</li> 1209<li><code>string</code>: <code>green</code></li> 1210<li><code>symbol</code>: <code>green</code></li> 1211<li><code>undefined</code>: <code>grey</code></li> 1212</ul> 1213<p>Color styling uses ANSI control codes that may not be supported on all 1214terminals. To verify color support use <a href="tty.html#writestreamhascolorscount-env"><code>tty.hasColors()</code></a>.</p> 1215<p>Predefined control codes are listed below (grouped as "Modifiers", "Foreground 1216colors", and "Background colors").</p> 1217<h5>Modifiers<span><a class="mark" href="#modifiers" id="modifiers">#</a></span><a aria-hidden="true" class="legacy" id="util_modifiers"></a></h5> 1218<p>Modifier support varies throughout different terminals. They will mostly be 1219ignored, if not supported.</p> 1220<ul> 1221<li><code>reset</code> - Resets all (color) modifiers to their defaults</li> 1222<li><strong>bold</strong> - Make text bold</li> 1223<li><em>italic</em> - Make text italic</li> 1224<li><span style="border-bottom: 1px;">underline</span> - Make text underlined</li> 1225<li><del>strikethrough</del> - Puts a horizontal line through the center of the text 1226(Alias: <code>strikeThrough</code>, <code>crossedout</code>, <code>crossedOut</code>)</li> 1227<li><code>hidden</code> - Prints the text, but makes it invisible (Alias: conceal)</li> 1228<li><span style="opacity: 0.5;">dim</span> - Decreased color intensity (Alias: 1229<code>faint</code>)</li> 1230<li><span style="border-top: 1px">overlined</span> - Make text overlined</li> 1231<li>blink - Hides and shows the text in an interval</li> 1232<li><span style="filter: invert(100%)">inverse</span> - Swap foreground and 1233background colors (Alias: <code>swapcolors</code>, <code>swapColors</code>)</li> 1234<li><span style="border-bottom: 1px double;">doubleunderline</span> - Make text 1235double underlined (Alias: <code>doubleUnderline</code>)</li> 1236<li><span style="border: 1px">framed</span> - Draw a frame around the text</li> 1237</ul> 1238<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> 1239<ul> 1240<li><code>black</code></li> 1241<li><code>red</code></li> 1242<li><code>green</code></li> 1243<li><code>yellow</code></li> 1244<li><code>blue</code></li> 1245<li><code>magenta</code></li> 1246<li><code>cyan</code></li> 1247<li><code>white</code></li> 1248<li><code>gray</code> (alias: <code>grey</code>, <code>blackBright</code>)</li> 1249<li><code>redBright</code></li> 1250<li><code>greenBright</code></li> 1251<li><code>yellowBright</code></li> 1252<li><code>blueBright</code></li> 1253<li><code>magentaBright</code></li> 1254<li><code>cyanBright</code></li> 1255<li><code>whiteBright</code></li> 1256</ul> 1257<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> 1258<ul> 1259<li><code>bgBlack</code></li> 1260<li><code>bgRed</code></li> 1261<li><code>bgGreen</code></li> 1262<li><code>bgYellow</code></li> 1263<li><code>bgBlue</code></li> 1264<li><code>bgMagenta</code></li> 1265<li><code>bgCyan</code></li> 1266<li><code>bgWhite</code></li> 1267<li><code>bgGray</code> (alias: <code>bgGrey</code>, <code>bgBlackBright</code>)</li> 1268<li><code>bgRedBright</code></li> 1269<li><code>bgGreenBright</code></li> 1270<li><code>bgYellowBright</code></li> 1271<li><code>bgBlueBright</code></li> 1272<li><code>bgMagentaBright</code></li> 1273<li><code>bgCyanBright</code></li> 1274<li><code>bgWhiteBright</code></li> 1275</ul> 1276<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> 1277 1278<div class="api_metadata"> 1279<details class="changelog"><summary>History</summary> 1280<table> 1281<tbody><tr><th>Version</th><th>Changes</th></tr> 1282<tr><td>v17.3.0, v16.14.0</td> 1283<td><p>The inspect argument is added for more interoperability.</p></td></tr> 1284<tr><td>v0.1.97</td> 1285<td><p><span>Added in: v0.1.97</span></p></td></tr> 1286</tbody></table> 1287</details> 1288</div> 1289<p>Objects may also define their own 1290<a href="#utilinspectcustom"><code>[util.inspect.custom](depth, opts, inspect)</code></a> function, 1291which <code>util.inspect()</code> will invoke and use the result of when inspecting 1292the object.</p> 1293<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>); 1294 1295<span class="hljs-keyword">class</span> <span class="hljs-title class_">Box</span> { 1296 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">value</span>) { 1297 <span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span> = value; 1298 } 1299 1300 [util.<span class="hljs-property">inspect</span>.<span class="hljs-property">custom</span>](depth, options, inspect) { 1301 <span class="hljs-keyword">if</span> (depth < <span class="hljs-number">0</span>) { 1302 <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>); 1303 } 1304 1305 <span class="hljs-keyword">const</span> newOptions = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">assign</span>({}, options, { 1306 <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>, 1307 }); 1308 1309 <span class="hljs-comment">// Five space padding because that's the size of "Box< ".</span> 1310 <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>); 1311 <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) 1312 .<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>); 1313 <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>; 1314 } 1315} 1316 1317<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>); 1318 1319util.<span class="hljs-title function_">inspect</span>(box); 1320<span class="hljs-comment">// Returns: "Box< true >"</span></code> <button class="copy-button">copy</button></pre> 1321<p>Custom <code>[util.inspect.custom](depth, opts, inspect)</code> functions typically return 1322a string but may return a value of any type that will be formatted accordingly 1323by <code>util.inspect()</code>.</p> 1324<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>); 1325 1326<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> }; 1327obj[util.<span class="hljs-property">inspect</span>.<span class="hljs-property">custom</span>] = <span class="hljs-function">(<span class="hljs-params">depth</span>) =></span> { 1328 <span class="hljs-keyword">return</span> { <span class="hljs-attr">bar</span>: <span class="hljs-string">'baz'</span> }; 1329}; 1330 1331util.<span class="hljs-title function_">inspect</span>(obj); 1332<span class="hljs-comment">// Returns: "{ bar: 'baz' }"</span></code> <button class="copy-button">copy</button></pre> 1333<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> 1334<div class="api_metadata"> 1335<details class="changelog"><summary>History</summary> 1336<table> 1337<tbody><tr><th>Version</th><th>Changes</th></tr> 1338<tr><td>v10.12.0</td> 1339<td><p>This is now defined as a shared symbol.</p></td></tr> 1340<tr><td>v6.6.0</td> 1341<td><p><span>Added in: v6.6.0</span></p></td></tr> 1342</tbody></table> 1343</details> 1344</div> 1345<ul> 1346<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> 1347</ul> 1348<p>In addition to being accessible through <code>util.inspect.custom</code>, this 1349symbol is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for">registered globally</a> and can be 1350accessed in any environment as <code>Symbol.for('nodejs.util.inspect.custom')</code>.</p> 1351<p>Using this allows code to be written in a portable fashion, so that the custom 1352inspect function is used in an Node.js environment and ignored in the browser. 1353The <code>util.inspect()</code> function itself is passed as third argument to the custom 1354inspect function to allow further portability.</p> 1355<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>); 1356 1357<span class="hljs-keyword">class</span> <span class="hljs-title class_">Password</span> { 1358 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">value</span>) { 1359 <span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span> = value; 1360 } 1361 1362 <span class="hljs-title function_">toString</span>(<span class="hljs-params"></span>) { 1363 <span class="hljs-keyword">return</span> <span class="hljs-string">'xxxxxxxx'</span>; 1364 } 1365 1366 [customInspectSymbol](depth, inspectOptions, inspect) { 1367 <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>; 1368 } 1369} 1370 1371<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>); 1372<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(password); 1373<span class="hljs-comment">// Prints Password <xxxxxxxx></span></code> <button class="copy-button">copy</button></pre> 1374<p>See <a href="#custom-inspection-functions-on-objects">Custom inspection functions on Objects</a> for more details.</p> 1375<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> 1376<div class="api_metadata"> 1377<span>Added in: v6.4.0</span> 1378</div> 1379<p>The <code>defaultOptions</code> value allows customization of the default options used by 1380<code>util.inspect</code>. This is useful for functions like <code>console.log</code> or 1381<code>util.format</code> which implicitly call into <code>util.inspect</code>. It shall be set to an 1382object containing one or more valid <a href="#utilinspectobject-options"><code>util.inspect()</code></a> options. Setting 1383option properties directly is also supported.</p> 1384<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>); 1385<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>); 1386 1387<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(arr); <span class="hljs-comment">// Logs the truncated array</span> 1388util.<span class="hljs-property">inspect</span>.<span class="hljs-property">defaultOptions</span>.<span class="hljs-property">maxArrayLength</span> = <span class="hljs-literal">null</span>; 1389<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> 1390</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> 1391<div class="api_metadata"> 1392<span>Added in: v9.0.0</span> 1393</div> 1394<ul> 1395<li><code>val1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1396<li><code>val2</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1397<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1398</ul> 1399<p>Returns <code>true</code> if there is deep strict equality between <code>val1</code> and <code>val2</code>. 1400Otherwise, returns <code>false</code>.</p> 1401<p>See <a href="assert.html#assertdeepstrictequalactual-expected-message"><code>assert.deepStrictEqual()</code></a> for more information about deep strict 1402equality.</p> 1403</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> 1404<div class="api_metadata"> 1405<span>Added in: v18.13.0</span> 1406</div> 1407<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 1408<p>An implementation of <a href="https://bmeck.github.io/node-proposal-mime-api/">the MIMEType class</a>.</p> 1409<p>In accordance with browser conventions, all properties of <code>MIMEType</code> objects 1410are implemented as getters and setters on the class prototype, rather than as 1411data properties on the object itself.</p> 1412<p>A MIME string is a structured string containing multiple meaningful 1413components. When parsed, a <code>MIMEType</code> object is returned containing 1414properties for each of these components.</p> 1415<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> 1416<ul> 1417<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> 1418</ul> 1419<p>Creates a new <code>MIMEType</code> object by parsing the <code>input</code>.</p> 1420 1421<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>; 1422 1423<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>); 1424 1425<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> 1426<p>A <code>TypeError</code> will be thrown if the <code>input</code> is not a valid MIME. Note 1427that an effort will be made to coerce the given values into strings. For 1428instance:</p> 1429 1430<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>; 1431<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> }); 1432<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">String</span>(myMIME)); 1433<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>); 1434<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> }); 1435<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">String</span>(myMIME)); 1436<span class="hljs-comment">// Prints: text/plain</span></code><button class="copy-button">copy</button></pre> 1437<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> 1438<ul> 1439<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1440</ul> 1441<p>Gets and sets the type portion of the MIME.</p> 1442 1443<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>; 1444 1445<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>); 1446<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">type</span>); 1447<span class="hljs-comment">// Prints: text</span> 1448myMIME.<span class="hljs-property">type</span> = <span class="hljs-string">'application'</span>; 1449<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">type</span>); 1450<span class="hljs-comment">// Prints: application</span> 1451<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">String</span>(myMIME)); 1452<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>); 1453 1454<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>); 1455<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">type</span>); 1456<span class="hljs-comment">// Prints: text</span> 1457myMIME.<span class="hljs-property">type</span> = <span class="hljs-string">'application'</span>; 1458<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">type</span>); 1459<span class="hljs-comment">// Prints: application</span> 1460<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">String</span>(myMIME)); 1461<span class="hljs-comment">// Prints: application/javascript</span></code><button class="copy-button">copy</button></pre> 1462<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> 1463<ul> 1464<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1465</ul> 1466<p>Gets and sets the subtype portion of the MIME.</p> 1467 1468<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>; 1469 1470<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>); 1471<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">subtype</span>); 1472<span class="hljs-comment">// Prints: ecmascript</span> 1473myMIME.<span class="hljs-property">subtype</span> = <span class="hljs-string">'javascript'</span>; 1474<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">subtype</span>); 1475<span class="hljs-comment">// Prints: javascript</span> 1476<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">String</span>(myMIME)); 1477<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>); 1478 1479<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>); 1480<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">subtype</span>); 1481<span class="hljs-comment">// Prints: ecmascript</span> 1482myMIME.<span class="hljs-property">subtype</span> = <span class="hljs-string">'javascript'</span>; 1483<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">subtype</span>); 1484<span class="hljs-comment">// Prints: javascript</span> 1485<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">String</span>(myMIME)); 1486<span class="hljs-comment">// Prints: text/javascript</span></code><button class="copy-button">copy</button></pre> 1487<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> 1488<ul> 1489<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1490</ul> 1491<p>Gets the essence of the MIME. This property is read only. 1492Use <code>mime.type</code> or <code>mime.subtype</code> to alter the MIME.</p> 1493 1494<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>; 1495 1496<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>); 1497<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">essence</span>); 1498<span class="hljs-comment">// Prints: text/javascript</span> 1499myMIME.<span class="hljs-property">type</span> = <span class="hljs-string">'application'</span>; 1500<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">essence</span>); 1501<span class="hljs-comment">// Prints: application/javascript</span> 1502<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">String</span>(myMIME)); 1503<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>); 1504 1505<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>); 1506<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">essence</span>); 1507<span class="hljs-comment">// Prints: text/javascript</span> 1508myMIME.<span class="hljs-property">type</span> = <span class="hljs-string">'application'</span>; 1509<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myMIME.<span class="hljs-property">essence</span>); 1510<span class="hljs-comment">// Prints: application/javascript</span> 1511<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">String</span>(myMIME)); 1512<span class="hljs-comment">// Prints: application/javascript;key=value</span></code><button class="copy-button">copy</button></pre> 1513<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> 1514<ul> 1515<li><a href="util.html#class-utilmimeparams" class="type"><MIMEParams></a></li> 1516</ul> 1517<p>Gets the <a href="#class-utilmimeparams"><code>MIMEParams</code></a> object representing the 1518parameters of the MIME. This property is read-only. See 1519<a href="#class-utilmimeparams"><code>MIMEParams</code></a> documentation for details.</p> 1520<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> 1521<ul> 1522<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1523</ul> 1524<p>The <code>toString()</code> method on the <code>MIMEType</code> object returns the serialized MIME.</p> 1525<p>Because of the need for standard compliance, this method does not allow users 1526to customize the serialization process of the MIME.</p> 1527<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> 1528<ul> 1529<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1530</ul> 1531<p>Alias for <a href="#mimetostring"><code>mime.toString()</code></a>.</p> 1532<p>This method is automatically called when an <code>MIMEType</code> object is serialized 1533with <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify"><code>JSON.stringify()</code></a>.</p> 1534 1535<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>; 1536 1537<span class="hljs-keyword">const</span> myMIMES = [ 1538 <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'image/png'</span>), 1539 <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'image/gif'</span>), 1540]; 1541<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)); 1542<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>); 1543 1544<span class="hljs-keyword">const</span> myMIMES = [ 1545 <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'image/png'</span>), 1546 <span class="hljs-keyword">new</span> <span class="hljs-title class_">MIMEType</span>(<span class="hljs-string">'image/gif'</span>), 1547]; 1548<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)); 1549<span class="hljs-comment">// Prints: ["image/png", "image/gif"]</span></code><button class="copy-button">copy</button></pre> 1550</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> 1551<div class="api_metadata"> 1552<span>Added in: v18.13.0</span> 1553</div> 1554<p>The <code>MIMEParams</code> API provides read and write access to the parameters of a 1555<code>MIMEType</code>.</p> 1556<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> 1557<p>Creates a new <code>MIMEParams</code> object by with empty parameters</p> 1558 1559<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>; 1560 1561<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>); 1562 1563<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> 1564<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> 1565<ul> 1566<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1567</ul> 1568<p>Remove all name-value pairs whose name is <code>name</code>.</p> 1569<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> 1570<ul> 1571<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 1572</ul> 1573<p>Returns an iterator over each of the name-value pairs in the parameters. 1574Each item of the iterator is a JavaScript <code>Array</code>. The first item of the array 1575is the <code>name</code>, the second item of the array is the <code>value</code>.</p> 1576<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> 1577<ul> 1578<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1579<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 1580<code>name</code>.</li> 1581</ul> 1582<p>Returns the value of the first name-value pair whose name is <code>name</code>. If there 1583are no such pairs, <code>null</code> is returned.</p> 1584<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> 1585<ul> 1586<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1587<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 1588</ul> 1589<p>Returns <code>true</code> if there is at least one name-value pair whose name is <code>name</code>.</p> 1590<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> 1591<ul> 1592<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 1593</ul> 1594<p>Returns an iterator over the names of each name-value pair.</p> 1595 1596<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>; 1597 1598<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>); 1599<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>()) { 1600 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name); 1601} 1602<span class="hljs-comment">// Prints:</span> 1603<span class="hljs-comment">// foo</span> 1604<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>); 1605 1606<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>); 1607<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>()) { 1608 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name); 1609} 1610<span class="hljs-comment">// Prints:</span> 1611<span class="hljs-comment">// foo</span> 1612<span class="hljs-comment">// bar</span></code><button class="copy-button">copy</button></pre> 1613<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> 1614<ul> 1615<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1616<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1617</ul> 1618<p>Sets the value in the <code>MIMEParams</code> object associated with <code>name</code> to 1619<code>value</code>. If there are any pre-existing name-value pairs whose names are <code>name</code>, 1620set the first such pair's value to <code>value</code>.</p> 1621 1622<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>; 1623 1624<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>); 1625params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'def'</span>); 1626params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'baz'</span>, <span class="hljs-string">'xyz'</span>); 1627<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>()); 1628<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>); 1629 1630<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>); 1631params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'def'</span>); 1632params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'baz'</span>, <span class="hljs-string">'xyz'</span>); 1633<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-title function_">toString</span>()); 1634<span class="hljs-comment">// Prints: foo=def&bar=1&baz=xyz</span></code><button class="copy-button">copy</button></pre> 1635<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> 1636<ul> 1637<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 1638</ul> 1639<p>Returns an iterator over the values of each name-value pair.</p> 1640<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> 1641<ul> 1642<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type"><Iterator></a></li> 1643</ul> 1644<p>Alias for <a href="#mimeparamsentries"><code>mimeParams.entries()</code></a>.</p> 1645 1646<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>; 1647 1648<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>); 1649<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> [name, value] <span class="hljs-keyword">of</span> params) { 1650 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name, value); 1651} 1652<span class="hljs-comment">// Prints:</span> 1653<span class="hljs-comment">// foo bar</span> 1654<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>); 1655 1656<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>); 1657<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> [name, value] <span class="hljs-keyword">of</span> params) { 1658 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(name, value); 1659} 1660<span class="hljs-comment">// Prints:</span> 1661<span class="hljs-comment">// foo bar</span> 1662<span class="hljs-comment">// xyz baz</span></code><button class="copy-button">copy</button></pre> 1663</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> 1664<div class="api_metadata"> 1665<details class="changelog"><summary>History</summary> 1666<table> 1667<tbody><tr><th>Version</th><th>Changes</th></tr> 1668<tr><td>v18.11.0</td> 1669<td><p>Add support for default values in input <code>config</code>.</p></td></tr> 1670<tr><td>v18.7.0, v16.17.0</td> 1671<td><p>add support for returning detailed parse information using <code>tokens</code> in input <code>config</code> and returned properties.</p></td></tr> 1672<tr><td>v18.3.0</td> 1673<td><p><span>Added in: v18.3.0</span></p></td></tr> 1674</tbody></table> 1675</details> 1676</div> 1677<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 1678<ul> 1679<li> 1680<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 1681the parser. <code>config</code> supports the following properties:</p> 1682<ul> 1683<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> 1684with <code>execPath</code> and <code>filename</code> removed.</li> 1685<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. 1686Keys of <code>options</code> are the long names of options and values are an 1687<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> accepting the following properties: 1688<ul> 1689<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> 1690<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 1691times. If <code>true</code>, all values will be collected in an array. If 1692<code>false</code>, values for the option are last-wins. <strong>Default:</strong> <code>false</code>.</li> 1693<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> 1694<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 1695value when it is not set by args. It must be of the same type as the 1696<code>type</code> property. When <code>multiple</code> is <code>true</code>, it must be an array.</li> 1697</ul> 1698</li> 1699<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 1700are encountered, or when arguments are passed that do not match the 1701<code>type</code> configured in <code>options</code>. 1702<strong>Default:</strong> <code>true</code>.</li> 1703<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 1704arguments. 1705<strong>Default:</strong> <code>false</code> if <code>strict</code> is <code>true</code>, otherwise <code>true</code>.</li> 1706<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 1707the built-in behavior, from adding additional checks through to reprocessing 1708the tokens in different ways. 1709<strong>Default:</strong> <code>false</code>.</li> 1710</ul> 1711</li> 1712<li> 1713<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> 1714<ul> 1715<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> 1716or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> values.</li> 1717<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> 1718<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> 1719section. Only returned if <code>config</code> includes <code>tokens: true</code>.</li> 1720</ul> 1721</li> 1722</ul> 1723<p>Provides a higher level API for command-line argument parsing than interacting 1724with <code>process.argv</code> directly. Takes a specification for the expected arguments 1725and returns a structured object with the parsed options and positionals.</p> 1726 1727<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>; 1728<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>]; 1729<span class="hljs-keyword">const</span> options = { 1730 <span class="hljs-attr">foo</span>: { 1731 <span class="hljs-attr">type</span>: <span class="hljs-string">'boolean'</span>, 1732 <span class="hljs-attr">short</span>: <span class="hljs-string">'f'</span>, 1733 }, 1734 <span class="hljs-attr">bar</span>: { 1735 <span class="hljs-attr">type</span>: <span class="hljs-string">'string'</span>, 1736 }, 1737}; 1738<span class="hljs-keyword">const</span> { 1739 values, 1740 positionals, 1741} = <span class="hljs-title function_">parseArgs</span>({ args, options }); 1742<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(values, positionals); 1743<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>); 1744<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>]; 1745<span class="hljs-keyword">const</span> options = { 1746 <span class="hljs-attr">foo</span>: { 1747 <span class="hljs-attr">type</span>: <span class="hljs-string">'boolean'</span>, 1748 <span class="hljs-attr">short</span>: <span class="hljs-string">'f'</span>, 1749 }, 1750 <span class="hljs-attr">bar</span>: { 1751 <span class="hljs-attr">type</span>: <span class="hljs-string">'string'</span>, 1752 }, 1753}; 1754<span class="hljs-keyword">const</span> { 1755 values, 1756 positionals, 1757} = <span class="hljs-title function_">parseArgs</span>({ args, options }); 1758<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(values, positionals); 1759<span class="hljs-comment">// Prints: [Object: null prototype] { foo: true, bar: 'b' } []</span></code><button class="copy-button">copy</button></pre> 1760<p><code>util.parseArgs</code> is experimental and behavior may change. Join the 1761conversation in <a href="https://github.com/pkgjs/parseargs">pkgjs/parseargs</a> to contribute to the design.</p> 1762<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> 1763<p>Detailed parse information is available for adding custom behaviors by 1764specifying <code>tokens: true</code> in the configuration. 1765The returned tokens have properties describing:</p> 1766<ul> 1767<li>all tokens 1768<ul> 1769<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> 1770<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 1771source argument for a token is <code>args[token.index]</code>.</li> 1772</ul> 1773</li> 1774<li>option tokens 1775<ul> 1776<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> 1777<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> 1778<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. 1779Undefined for boolean options.</li> 1780<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, 1781like <code>--foo=bar</code>.</li> 1782</ul> 1783</li> 1784<li>positional tokens 1785<ul> 1786<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> 1787</ul> 1788</li> 1789<li>option-terminator token</li> 1790</ul> 1791<p>The returned tokens are in the order encountered in the input args. Options 1792that appear more than once in args produce a token for each use. Short option 1793groups like <code>-xy</code> expand to a token for each option. So <code>-xxx</code> produces 1794three tokens.</p> 1795<p>For example to use the returned tokens to add support for a negated option 1796like <code>--no-color</code>, the tokens can be reprocessed to change the value stored 1797for the negated option.</p> 1798 1799<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>; 1800 1801<span class="hljs-keyword">const</span> options = { 1802 <span class="hljs-string">'color'</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">'boolean'</span> }, 1803 <span class="hljs-string">'no-color'</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">'boolean'</span> }, 1804 <span class="hljs-string">'logfile'</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">'string'</span> }, 1805 <span class="hljs-string">'no-logfile'</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">'boolean'</span> }, 1806}; 1807<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> }); 1808 1809<span class="hljs-comment">// Reprocess the option tokens and overwrite the returned values.</span> 1810tokens 1811 .<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>) 1812 .<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">token</span>) =></span> { 1813 <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>)) { 1814 <span class="hljs-comment">// Store foo:false for --no-foo</span> 1815 <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>); 1816 values[positiveName] = <span class="hljs-literal">false</span>; 1817 <span class="hljs-keyword">delete</span> values[token.<span class="hljs-property">name</span>]; 1818 } <span class="hljs-keyword">else</span> { 1819 <span class="hljs-comment">// Resave value so last one wins if both --foo and --no-foo.</span> 1820 values[token.<span class="hljs-property">name</span>] = token.<span class="hljs-property">value</span> ?? <span class="hljs-literal">true</span>; 1821 } 1822 }); 1823 1824<span class="hljs-keyword">const</span> color = values.<span class="hljs-property">color</span>; 1825<span class="hljs-keyword">const</span> logfile = values.<span class="hljs-property">logfile</span> ?? <span class="hljs-string">'default.log'</span>; 1826 1827<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>); 1828 1829<span class="hljs-keyword">const</span> options = { 1830 <span class="hljs-string">'color'</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">'boolean'</span> }, 1831 <span class="hljs-string">'no-color'</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">'boolean'</span> }, 1832 <span class="hljs-string">'logfile'</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">'string'</span> }, 1833 <span class="hljs-string">'no-logfile'</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">'boolean'</span> }, 1834}; 1835<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> }); 1836 1837<span class="hljs-comment">// Reprocess the option tokens and overwrite the returned values.</span> 1838tokens 1839 .<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>) 1840 .<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">token</span>) =></span> { 1841 <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>)) { 1842 <span class="hljs-comment">// Store foo:false for --no-foo</span> 1843 <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>); 1844 values[positiveName] = <span class="hljs-literal">false</span>; 1845 <span class="hljs-keyword">delete</span> values[token.<span class="hljs-property">name</span>]; 1846 } <span class="hljs-keyword">else</span> { 1847 <span class="hljs-comment">// Resave value so last one wins if both --foo and --no-foo.</span> 1848 values[token.<span class="hljs-property">name</span>] = token.<span class="hljs-property">value</span> ?? <span class="hljs-literal">true</span>; 1849 } 1850 }); 1851 1852<span class="hljs-keyword">const</span> color = values.<span class="hljs-property">color</span>; 1853<span class="hljs-keyword">const</span> logfile = values.<span class="hljs-property">logfile</span> ?? <span class="hljs-string">'default.log'</span>; 1854 1855<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>({ logfile, color });</code><button class="copy-button">copy</button></pre> 1856<p>Example usage showing negated options, and when an option is used 1857multiple ways then last one wins.</p> 1858<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node negate.js</span> 1859{ logfile: 'default.log', color: undefined } 1860<span class="hljs-meta prompt_">$ </span><span class="language-bash">node negate.js --no-logfile --no-color</span> 1861{ logfile: false, color: false } 1862<span class="hljs-meta prompt_">$ </span><span class="language-bash">node negate.js --logfile=test.log --color</span> 1863{ logfile: 'test.log', color: true } 1864<span class="hljs-meta prompt_">$ </span><span class="language-bash">node negate.js --no-logfile --logfile=test.log --color --no-color</span> 1865{ logfile: 'test.log', color: false }</code> <button class="copy-button">copy</button></pre> 1866</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> 1867<div class="api_metadata"> 1868<span>Added in: v8.0.0</span> 1869</div> 1870<ul> 1871<li><code>original</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1872<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1873</ul> 1874<p>Takes a function following the common error-first callback style, i.e. taking 1875an <code>(err, value) => ...</code> callback as the last argument, and returns a version 1876that returns promises.</p> 1877<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>); 1878<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 1879 1880<span class="hljs-keyword">const</span> stat = util.<span class="hljs-title function_">promisify</span>(fs.<span class="hljs-property">stat</span>); 1881<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> { 1882 <span class="hljs-comment">// Do something with `stats`</span> 1883}).<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-params">error</span>) =></span> { 1884 <span class="hljs-comment">// Handle the error.</span> 1885});</code> <button class="copy-button">copy</button></pre> 1886<p>Or, equivalently using <code>async function</code>s:</p> 1887<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>); 1888<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 1889 1890<span class="hljs-keyword">const</span> stat = util.<span class="hljs-title function_">promisify</span>(fs.<span class="hljs-property">stat</span>); 1891 1892<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">callStat</span>(<span class="hljs-params"></span>) { 1893 <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>); 1894 <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>); 1895}</code> <button class="copy-button">copy</button></pre> 1896<p>If there is an <code>original[util.promisify.custom]</code> property present, <code>promisify</code> 1897will return its value, see <a href="#custom-promisified-functions">Custom promisified functions</a>.</p> 1898<p><code>promisify()</code> assumes that <code>original</code> is a function taking a callback as its 1899final argument in all cases. If <code>original</code> is not a function, <code>promisify()</code> 1900will throw an error. If <code>original</code> is a function but its last argument is not 1901an error-first callback, it will still be passed an error-first 1902callback as its last argument.</p> 1903<p>Using <code>promisify()</code> on class methods or other methods that use <code>this</code> may not 1904work as expected unless handled specially:</p> 1905<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>); 1906 1907<span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> { 1908 <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) { 1909 <span class="hljs-variable language_">this</span>.<span class="hljs-property">a</span> = <span class="hljs-number">42</span>; 1910 } 1911 1912 <span class="hljs-title function_">bar</span>(<span class="hljs-params">callback</span>) { 1913 <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>); 1914 } 1915} 1916 1917<span class="hljs-keyword">const</span> foo = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Foo</span>(); 1918 1919<span class="hljs-keyword">const</span> naiveBar = util.<span class="hljs-title function_">promisify</span>(foo.<span class="hljs-property">bar</span>); 1920<span class="hljs-comment">// TypeError: Cannot read property 'a' of undefined</span> 1921<span class="hljs-comment">// naiveBar().then(a => console.log(a));</span> 1922 1923naiveBar.<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> 1924 1925<span class="hljs-keyword">const</span> bindBar = naiveBar.<span class="hljs-title function_">bind</span>(foo); 1926<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> 1927<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> 1928<p>Using the <code>util.promisify.custom</code> symbol one can override the return value of 1929<a href="#utilpromisifyoriginal"><code>util.promisify()</code></a>:</p> 1930<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>); 1931 1932<span class="hljs-keyword">function</span> <span class="hljs-title function_">doSomething</span>(<span class="hljs-params">foo, callback</span>) { 1933 <span class="hljs-comment">// ...</span> 1934} 1935 1936doSomething[util.<span class="hljs-property">promisify</span>.<span class="hljs-property">custom</span>] = <span class="hljs-function">(<span class="hljs-params">foo</span>) =></span> { 1937 <span class="hljs-keyword">return</span> <span class="hljs-title function_">getPromiseSomehow</span>(); 1938}; 1939 1940<span class="hljs-keyword">const</span> promisified = util.<span class="hljs-title function_">promisify</span>(doSomething); 1941<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>]); 1942<span class="hljs-comment">// prints 'true'</span></code> <button class="copy-button">copy</button></pre> 1943<p>This can be useful for cases where the original function does not follow the 1944standard format of taking an error-first callback as the last argument.</p> 1945<p>For example, with a function that takes in 1946<code>(foo, onSuccessCallback, onErrorCallback)</code>:</p> 1947<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> { 1948 <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> { 1949 <span class="hljs-title function_">doSomething</span>(foo, resolve, reject); 1950 }); 1951};</code> <button class="copy-button">copy</button></pre> 1952<p>If <code>promisify.custom</code> is defined but is not a function, <code>promisify()</code> will 1953throw an error.</p> 1954<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> 1955<div class="api_metadata"> 1956<details class="changelog"><summary>History</summary> 1957<table> 1958<tbody><tr><th>Version</th><th>Changes</th></tr> 1959<tr><td>v13.12.0, v12.16.2</td> 1960<td><p>This is now defined as a shared symbol.</p></td></tr> 1961<tr><td>v8.0.0</td> 1962<td><p><span>Added in: v8.0.0</span></p></td></tr> 1963</tbody></table> 1964</details> 1965</div> 1966<ul> 1967<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, 1968see <a href="#custom-promisified-functions">Custom promisified functions</a>.</li> 1969</ul> 1970<p>In addition to being accessible through <code>util.promisify.custom</code>, this 1971symbol is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for">registered globally</a> and can be 1972accessed in any environment as <code>Symbol.for('nodejs.util.promisify.custom')</code>.</p> 1973<p>For example, with a function that takes in 1974<code>(foo, onSuccessCallback, onErrorCallback)</code>:</p> 1975<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>); 1976 1977doSomething[kCustomPromisifiedSymbol] = <span class="hljs-function">(<span class="hljs-params">foo</span>) =></span> { 1978 <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> { 1979 <span class="hljs-title function_">doSomething</span>(foo, resolve, reject); 1980 }); 1981};</code> <button class="copy-button">copy</button></pre> 1982</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> 1983<div class="api_metadata"> 1984<span>Added in: v16.11.0</span> 1985</div> 1986<ul> 1987<li><code>str</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1988<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1989</ul> 1990<p>Returns <code>str</code> with any ANSI escape codes removed.</p> 1991<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>)); 1992<span class="hljs-comment">// Prints "value"</span></code> <button class="copy-button">copy</button></pre> 1993</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> 1994<div class="api_metadata"> 1995<span>Added in: v8.3.0</span> 1996</div> 1997<p>An implementation of the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a> <code>TextDecoder</code> API.</p> 1998<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>(); 1999<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>]); 2000<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> 2001<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> 2002<p>Per the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a>, the encodings supported by the 2003<code>TextDecoder</code> API are outlined in the tables below. For each encoding, 2004one or more aliases may be used.</p> 2005<p>Different Node.js build configurations support different sets of encodings. 2006(see <a href="intl.html">Internationalization</a>)</p> 2007<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> 2008 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<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> 2154<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> 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176<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> 2177<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> 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195<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> 2196<p>The <code>'iso-8859-16'</code> encoding listed in the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a> 2197is not supported.</p> 2198<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> 2199<div class="api_metadata"> 2200<details class="changelog"><summary>History</summary> 2201<table> 2202<tbody><tr><th>Version</th><th>Changes</th></tr> 2203<tr><td>v11.0.0</td> 2204<td><p>The class is now available on the global object.</p></td></tr> 2205<tr><td>v8.3.0</td> 2206<td><p><span>Added in: v8.3.0</span></p></td></tr> 2207</tbody></table> 2208</details> 2209</div> 2210<ul> 2211<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 2212supports. <strong>Default:</strong> <code>'utf-8'</code>.</li> 2213<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2214<ul> 2215<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. 2216This option is not supported when ICU is disabled 2217(see <a href="intl.html">Internationalization</a>). <strong>Default:</strong> <code>false</code>.</li> 2218<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 2219order mark in the decoded result. When <code>false</code>, the byte order mark will 2220be removed from the output. This option is only used when <code>encoding</code> is 2221<code>'utf-8'</code>, <code>'utf-16be'</code>, or <code>'utf-16le'</code>. <strong>Default:</strong> <code>false</code>.</li> 2222</ul> 2223</li> 2224</ul> 2225<p>Creates a new <code>TextDecoder</code> instance. The <code>encoding</code> may specify one of the 2226supported encodings or an alias.</p> 2227<p>The <code>TextDecoder</code> class is also available on the global object.</p> 2228<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> 2229<ul> 2230<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 2231<code>TypedArray</code> instance containing the encoded data.</li> 2232<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2233<ul> 2234<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. 2235<strong>Default:</strong> <code>false</code>.</li> 2236</ul> 2237</li> 2238<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2239</ul> 2240<p>Decodes the <code>input</code> and returns a string. If <code>options.stream</code> is <code>true</code>, any 2241incomplete byte sequences occurring at the end of the <code>input</code> are buffered 2242internally and emitted after the next call to <code>textDecoder.decode()</code>.</p> 2243<p>If <code>textDecoder.fatal</code> is <code>true</code>, decoding errors that occur will result in a 2244<code>TypeError</code> being thrown.</p> 2245<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> 2246<ul> 2247<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2248</ul> 2249<p>The encoding supported by the <code>TextDecoder</code> instance.</p> 2250<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> 2251<ul> 2252<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2253</ul> 2254<p>The value will be <code>true</code> if decoding errors result in a <code>TypeError</code> being 2255thrown.</p> 2256<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> 2257<ul> 2258<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2259</ul> 2260<p>The value will be <code>true</code> if the decoding result will include the byte order 2261mark.</p> 2262</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> 2263<div class="api_metadata"> 2264<details class="changelog"><summary>History</summary> 2265<table> 2266<tbody><tr><th>Version</th><th>Changes</th></tr> 2267<tr><td>v11.0.0</td> 2268<td><p>The class is now available on the global object.</p></td></tr> 2269<tr><td>v8.3.0</td> 2270<td><p><span>Added in: v8.3.0</span></p></td></tr> 2271</tbody></table> 2272</details> 2273</div> 2274<p>An implementation of the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a> <code>TextEncoder</code> API. All 2275instances of <code>TextEncoder</code> only support UTF-8 encoding.</p> 2276<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>(); 2277<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> 2278<p>The <code>TextEncoder</code> class is also available on the global object.</p> 2279<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> 2280<ul> 2281<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> 2282<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type"><Uint8Array></a></li> 2283</ul> 2284<p>UTF-8 encodes the <code>input</code> string and returns a <code>Uint8Array</code> containing the 2285encoded bytes.</p> 2286<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> 2287<ul> 2288<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> 2289<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> 2290<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2291<ul> 2292<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> 2293<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> 2294</ul> 2295</li> 2296</ul> 2297<p>UTF-8 encodes the <code>src</code> string to the <code>dest</code> Uint8Array and returns an object 2298containing the read Unicode code units and written UTF-8 bytes.</p> 2299<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>(); 2300<span class="hljs-keyword">const</span> src = <span class="hljs-string">'this is some data'</span>; 2301<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>); 2302<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> 2303<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> 2304<ul> 2305<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2306</ul> 2307<p>The encoding supported by the <code>TextEncoder</code> instance. Always set to <code>'utf-8'</code>.</p> 2308</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> 2309<div class="api_metadata"> 2310<span>Added in: v16.8.0, v14.18.0</span> 2311</div> 2312<ul> 2313<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2314</ul> 2315<p>Returns the <code>string</code> after replacing any surrogate code points 2316(or equivalently, any unpaired surrogate code units) with the 2317Unicode "replacement character" U+FFFD.</p> 2318</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> 2319<div class="api_metadata"> 2320<span>Added in: v18.11.0</span> 2321</div> 2322<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 2323<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 2324as transferable and can be used with <code>structuredClone()</code> or <code>postMessage()</code>.</p> 2325</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> 2326<div class="api_metadata"> 2327<span>Added in: v18.11.0</span> 2328</div> 2329<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 2330<ul> 2331<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a></li> 2332<li>Returns: <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a></li> 2333</ul> 2334<p>Marks the given <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> as transferable so that it can be used with 2335<code>structuredClone()</code> and <code>postMessage()</code>.</p> 2336<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>)); 2337<span class="hljs-keyword">const</span> channel = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>(); 2338channel.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(signal, [signal]);</code> <button class="copy-button">copy</button></pre> 2339</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> 2340<div class="api_metadata"> 2341<span>Added in: v18.16.0</span> 2342</div> 2343<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 2344<ul> 2345<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a></li> 2346<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> 2347<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 2348</ul> 2349<p>Listens to abort event on the provided <code>signal</code> and 2350returns a promise that is fulfilled when the <code>signal</code> is 2351aborted. If the passed <code>resource</code> is garbage collected before the <code>signal</code> is 2352aborted, the returned promise shall remain pending indefinitely.</p> 2353 2354<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>); 2355 2356<span class="hljs-keyword">const</span> dependent = <span class="hljs-title function_">obtainSomethingAbortable</span>(); 2357 2358<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> { 2359 <span class="hljs-comment">// Do something when dependent is aborted.</span> 2360}); 2361 2362dependent.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 2363 dependent.<span class="hljs-title function_">abort</span>(); 2364});</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>; 2365 2366<span class="hljs-keyword">const</span> dependent = <span class="hljs-title function_">obtainSomethingAbortable</span>(); 2367 2368<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> { 2369 <span class="hljs-comment">// Do something when dependent is aborted.</span> 2370}); 2371 2372dependent.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 2373 dependent.<span class="hljs-title function_">abort</span>(); 2374});</code><button class="copy-button">copy</button></pre> 2375</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> 2376<div class="api_metadata"> 2377<details class="changelog"><summary>History</summary> 2378<table> 2379<tbody><tr><th>Version</th><th>Changes</th></tr> 2380<tr><td>v15.3.0</td> 2381<td><p>Exposed as <code>require('util/types')</code>.</p></td></tr> 2382<tr><td>v10.0.0</td> 2383<td><p><span>Added in: v10.0.0</span></p></td></tr> 2384</tbody></table> 2385</details> 2386</div> 2387<p><code>util.types</code> provides type checks for different kinds of built-in objects. 2388Unlike <code>instanceof</code> or <code>Object.prototype.toString.call(value)</code>, these checks do 2389not inspect properties of the object that are accessible from JavaScript (like 2390their prototype), and usually have the overhead of calling into C++.</p> 2391<p>The result generally does not make any guarantees about what kinds of 2392properties or behavior a value exposes in JavaScript. They are primarily 2393useful for addon developers who prefer to do type checking in JavaScript.</p> 2394<p>The API is accessible via <code>require('node:util').types</code> or <code>require('node:util/types')</code>.</p> 2395<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> 2396<div class="api_metadata"> 2397<span>Added in: v10.0.0</span> 2398</div> 2399<ul> 2400<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2401<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2402</ul> 2403<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 2404<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a> instance.</p> 2405<p>See also <a href="#utiltypesisarraybuffervalue"><code>util.types.isArrayBuffer()</code></a> and 2406<a href="#utiltypesissharedarraybuffervalue"><code>util.types.isSharedArrayBuffer()</code></a>.</p> 2407<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> 2408util.<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> 2409<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> 2410<div class="api_metadata"> 2411<span>Added in: v10.0.0</span> 2412</div> 2413<ul> 2414<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2415<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2416</ul> 2417<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> 2418views, 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 2419<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView"><code>ArrayBuffer.isView()</code></a>.</p> 2420<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> 2421util.<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> 2422util.<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> 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_">ArrayBuffer</span>()); <span class="hljs-comment">// false</span></code> <button class="copy-button">copy</button></pre> 2424<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> 2425<div class="api_metadata"> 2426<span>Added in: v10.0.0</span> 2427</div> 2428<ul> 2429<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2430<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2431</ul> 2432<p>Returns <code>true</code> if the value is an <code>arguments</code> object.</p> 2433<!-- eslint-disable prefer-rest-params --> 2434<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) { 2435 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> 2436}</code> <button class="copy-button">copy</button></pre> 2437<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> 2438<div class="api_metadata"> 2439<span>Added in: v10.0.0</span> 2440</div> 2441<ul> 2442<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2443<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2444</ul> 2445<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. 2446This 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 2447desirable to test for both; See <a href="#utiltypesisanyarraybuffervalue"><code>util.types.isAnyArrayBuffer()</code></a> for that.</p> 2448<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> 2449util.<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> 2450<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> 2451<div class="api_metadata"> 2452<span>Added in: v10.0.0</span> 2453</div> 2454<ul> 2455<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2456<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2457</ul> 2458<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>. 2459This only reports back what the JavaScript engine is seeing; 2460in particular, the return value may not match the original source code if 2461a transpilation tool was used.</p> 2462<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> 2463util.<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> 2464<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> 2465<div class="api_metadata"> 2466<span>Added in: v10.0.0</span> 2467</div> 2468<ul> 2469<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2470<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2471</ul> 2472<p>Returns <code>true</code> if the value is a <code>BigInt64Array</code> instance.</p> 2473<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> 2474util.<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> 2475<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> 2476<div class="api_metadata"> 2477<span>Added in: v10.0.0</span> 2478</div> 2479<ul> 2480<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2481<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2482</ul> 2483<p>Returns <code>true</code> if the value is a <code>BigUint64Array</code> instance.</p> 2484<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> 2485util.<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> 2486<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> 2487<div class="api_metadata"> 2488<span>Added in: v10.0.0</span> 2489</div> 2490<ul> 2491<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2492<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2493</ul> 2494<p>Returns <code>true</code> if the value is a boolean object, e.g. created 2495by <code>new Boolean()</code>.</p> 2496<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> 2497util.<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> 2498util.<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> 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">true</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-title class_">Boolean</span>(<span class="hljs-literal">false</span>)); <span class="hljs-comment">// Returns false</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">true</span>)); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2502<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> 2503<div class="api_metadata"> 2504<span>Added in: v10.11.0</span> 2505</div> 2506<ul> 2507<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2508<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2509</ul> 2510<p>Returns <code>true</code> if the value is any boxed primitive object, e.g. created 2511by <code>new Boolean()</code>, <code>new String()</code> or <code>Object(Symbol())</code>.</p> 2512<p>For example:</p> 2513<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> 2514util.<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> 2515util.<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> 2516util.<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> 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_">BigInt</span>(<span class="hljs-number">5</span>))); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2518<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> 2519<div class="api_metadata"> 2520<span>Added in: v16.2.0</span> 2521</div> 2522<ul> 2523<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2524<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2525</ul> 2526<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> 2527<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> 2528<div class="api_metadata"> 2529<span>Added in: v10.0.0</span> 2530</div> 2531<ul> 2532<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2533<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2534</ul> 2535<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> 2536<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>); 2537util.<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> 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_">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2539<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> 2540<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> 2541<div class="api_metadata"> 2542<span>Added in: v10.0.0</span> 2543</div> 2544<ul> 2545<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2546<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2547</ul> 2548<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> 2549<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> 2550<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> 2551<div class="api_metadata"> 2552<span>Added in: v10.0.0</span> 2553</div> 2554<ul> 2555<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2556<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2557</ul> 2558<p>Returns <code>true</code> if the value is a native <code>External</code> value.</p> 2559<p>A native <code>External</code> value is a special type of object that contains a 2560raw C++ pointer (<code>void*</code>) for access from native code, and has no other 2561properties. Such objects are created either by Node.js internals or native 2562addons. 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 2563<code>null</code> prototype.</p> 2564<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> 2565<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><stdlib.h></span></span> 2566napi_value result; 2567<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> { 2568 <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>); 2569 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); 2570 <span class="hljs-keyword">if</span> (status != napi_ok) { 2571 napi_throw_error(env, <span class="hljs-literal">NULL</span>, <span class="hljs-string">"napi_create_external failed"</span>); 2572 <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; 2573 } 2574 <span class="hljs-keyword">return</span> result; 2575} 2576... 2577DECLARE_NAPI_PROPERTY(<span class="hljs-string">"myNapi"</span>, MyNapi) 2578...</code> <button class="copy-button">copy</button></pre> 2579<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>); 2580<span class="hljs-keyword">const</span> data = native.<span class="hljs-title function_">myNapi</span>(); 2581util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isExternal</span>(data); <span class="hljs-comment">// returns true</span> 2582util.<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> 2583util.<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> 2584<p>For further information on <code>napi_create_external</code>, refer to 2585<a href="n-api.html#napi_create_external"><code>napi_create_external()</code></a>.</p> 2586<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> 2587<div class="api_metadata"> 2588<span>Added in: v10.0.0</span> 2589</div> 2590<ul> 2591<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2592<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2593</ul> 2594<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> 2595<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> 2596util.<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> 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_">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2598<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> 2599<div class="api_metadata"> 2600<span>Added in: v10.0.0</span> 2601</div> 2602<ul> 2603<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2604<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2605</ul> 2606<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> 2607<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> 2608util.<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> 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_">Float64Array</span>()); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2610<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> 2611<div class="api_metadata"> 2612<span>Added in: v10.0.0</span> 2613</div> 2614<ul> 2615<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2616<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2617</ul> 2618<p>Returns <code>true</code> if the value is a generator function. 2619This only reports back what the JavaScript engine is seeing; 2620in particular, the return value may not match the original source code if 2621a transpilation tool was used.</p> 2622<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> 2623util.<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> 2624<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> 2625<div class="api_metadata"> 2626<span>Added in: v10.0.0</span> 2627</div> 2628<ul> 2629<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2630<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2631</ul> 2632<p>Returns <code>true</code> if the value is a generator object as returned from a 2633built-in generator function. 2634This only reports back what the JavaScript engine is seeing; 2635in particular, the return value may not match the original source code if 2636a transpilation tool was used.</p> 2637<pre><code class="language-js"><span class="hljs-keyword">function</span>* <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) {} 2638<span class="hljs-keyword">const</span> generator = <span class="hljs-title function_">foo</span>(); 2639util.<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> 2640<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> 2641<div class="api_metadata"> 2642<span>Added in: v10.0.0</span> 2643</div> 2644<ul> 2645<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2646<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2647</ul> 2648<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> 2649<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> 2650util.<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> 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_">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2652<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> 2653<div class="api_metadata"> 2654<span>Added in: v10.0.0</span> 2655</div> 2656<ul> 2657<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2658<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2659</ul> 2660<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> 2661<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> 2662util.<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> 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_">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2664<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> 2665<div class="api_metadata"> 2666<span>Added in: v10.0.0</span> 2667</div> 2668<ul> 2669<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2670<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2671</ul> 2672<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> 2673<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> 2674util.<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> 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_">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2676<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> 2677<div class="api_metadata"> 2678<span>Added in: v16.2.0</span> 2679</div> 2680<ul> 2681<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2682<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2683</ul> 2684<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> 2685<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> 2686<div class="api_metadata"> 2687<span>Added in: v10.0.0</span> 2688</div> 2689<ul> 2690<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2691<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2692</ul> 2693<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> 2694<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> 2695<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> 2696<div class="api_metadata"> 2697<span>Added in: v10.0.0</span> 2698</div> 2699<ul> 2700<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2701<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2702</ul> 2703<p>Returns <code>true</code> if the value is an iterator returned for a built-in 2704<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><code>Map</code></a> instance.</p> 2705<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>(); 2706util.<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> 2707util.<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> 2708util.<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> 2709util.<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> 2710<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> 2711<div class="api_metadata"> 2712<span>Added in: v10.0.0</span> 2713</div> 2714<ul> 2715<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2716<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2717</ul> 2718<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> 2719<!-- eslint-skip --> 2720<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>; 2721 2722util.<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> 2723<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> 2724<div class="api_metadata"> 2725<span>Added in: v10.0.0</span> 2726</div> 2727<ul> 2728<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2729<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2730</ul> 2731<p>Returns <code>true</code> if the value was returned by the constructor of a 2732<a href="https://tc39.es/ecma262/#sec-error-objects">built-in <code>Error</code> type</a>.</p> 2733<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> 2734<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> 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_">RangeError</span>())); <span class="hljs-comment">// true</span></code> <button class="copy-button">copy</button></pre> 2736<p>Subclasses of the native error types are also native errors:</p> 2737<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> {} 2738<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> 2739<p>A value being <code>instanceof</code> a native error class is not equivalent to <code>isNativeError()</code> 2740returning <code>true</code> for that value. <code>isNativeError()</code> returns <code>true</code> for errors 2741which come from a different <a href="https://tc39.es/ecma262/#realm">realm</a> while <code>instanceof Error</code> returns <code>false</code> 2742for these errors:</p> 2743<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>); 2744<span class="hljs-keyword">const</span> context = vm.<span class="hljs-title function_">createContext</span>({}); 2745<span class="hljs-keyword">const</span> myError = vm.<span class="hljs-title function_">runInContext</span>(<span class="hljs-string">'new Error()'</span>, context); 2746<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> 2747<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> 2748<p>Conversely, <code>isNativeError()</code> returns <code>false</code> for all objects which were not 2749returned by the constructor of a native error. That includes values 2750which are <code>instanceof</code> native errors:</p> 2751<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> }; 2752<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> 2753<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> 2754<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> 2755<div class="api_metadata"> 2756<span>Added in: v10.0.0</span> 2757</div> 2758<ul> 2759<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2760<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2761</ul> 2762<p>Returns <code>true</code> if the value is a number object, e.g. created 2763by <code>new Number()</code>.</p> 2764<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> 2765util.<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> 2766<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> 2767<div class="api_metadata"> 2768<span>Added in: v10.0.0</span> 2769</div> 2770<ul> 2771<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2772<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2773</ul> 2774<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> 2775<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> 2776<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> 2777<div class="api_metadata"> 2778<span>Added in: v10.0.0</span> 2779</div> 2780<ul> 2781<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2782<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2783</ul> 2784<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> 2785<pre><code class="language-js"><span class="hljs-keyword">const</span> target = {}; 2786<span class="hljs-keyword">const</span> proxy = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Proxy</span>(target, {}); 2787util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isProxy</span>(target); <span class="hljs-comment">// Returns false</span> 2788util.<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> 2789<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> 2790<div class="api_metadata"> 2791<span>Added in: v10.0.0</span> 2792</div> 2793<ul> 2794<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2795<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2796</ul> 2797<p>Returns <code>true</code> if the value is a regular expression object.</p> 2798<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> 2799util.<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> 2800<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> 2801<div class="api_metadata"> 2802<span>Added in: v10.0.0</span> 2803</div> 2804<ul> 2805<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2806<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2807</ul> 2808<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> 2809<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> 2810<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> 2811<div class="api_metadata"> 2812<span>Added in: v10.0.0</span> 2813</div> 2814<ul> 2815<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2816<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2817</ul> 2818<p>Returns <code>true</code> if the value is an iterator returned for a built-in 2819<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set"><code>Set</code></a> instance.</p> 2820<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>(); 2821util.<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> 2822util.<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> 2823util.<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> 2824util.<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> 2825<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> 2826<div class="api_metadata"> 2827<span>Added in: v10.0.0</span> 2828</div> 2829<ul> 2830<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2831<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2832</ul> 2833<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. 2834This 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 2835desirable to test for both; See <a href="#utiltypesisanyarraybuffervalue"><code>util.types.isAnyArrayBuffer()</code></a> for that.</p> 2836<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> 2837util.<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> 2838<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> 2839<div class="api_metadata"> 2840<span>Added in: v10.0.0</span> 2841</div> 2842<ul> 2843<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2844<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2845</ul> 2846<p>Returns <code>true</code> if the value is a string object, e.g. created 2847by <code>new String()</code>.</p> 2848<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> 2849util.<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> 2850<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> 2851<div class="api_metadata"> 2852<span>Added in: v10.0.0</span> 2853</div> 2854<ul> 2855<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2856<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2857</ul> 2858<p>Returns <code>true</code> if the value is a symbol object, created 2859by calling <code>Object()</code> on a <code>Symbol</code> primitive.</p> 2860<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>); 2861util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSymbolObject</span>(symbol); <span class="hljs-comment">// Returns false</span> 2862util.<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> 2863<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> 2864<div class="api_metadata"> 2865<span>Added in: v10.0.0</span> 2866</div> 2867<ul> 2868<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2869<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2870</ul> 2871<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> 2872<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> 2873util.<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> 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_">Float64Array</span>()); <span class="hljs-comment">// Returns true</span></code> <button class="copy-button">copy</button></pre> 2875<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> 2876<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> 2877<div class="api_metadata"> 2878<span>Added in: v10.0.0</span> 2879</div> 2880<ul> 2881<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2882<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2883</ul> 2884<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> 2885<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> 2886util.<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> 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_">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2888<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> 2889<div class="api_metadata"> 2890<span>Added in: v10.0.0</span> 2891</div> 2892<ul> 2893<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2894<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2895</ul> 2896<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> 2897<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> 2898util.<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> 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_">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2900<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> 2901<div class="api_metadata"> 2902<span>Added in: v10.0.0</span> 2903</div> 2904<ul> 2905<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2906<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2907</ul> 2908<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> 2909<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> 2910util.<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> 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_">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2912<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> 2913<div class="api_metadata"> 2914<span>Added in: v10.0.0</span> 2915</div> 2916<ul> 2917<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2918<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2919</ul> 2920<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> 2921<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> 2922util.<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> 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_">Float64Array</span>()); <span class="hljs-comment">// Returns false</span></code> <button class="copy-button">copy</button></pre> 2924<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> 2925<div class="api_metadata"> 2926<span>Added in: v10.0.0</span> 2927</div> 2928<ul> 2929<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2930<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2931</ul> 2932<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> 2933<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> 2934<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> 2935<div class="api_metadata"> 2936<span>Added in: v10.0.0</span> 2937</div> 2938<ul> 2939<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2940<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2941</ul> 2942<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> 2943<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> 2944<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> 2945<div class="api_metadata"> 2946<span>Added in: v10.0.0</span><span>Deprecated since: v14.0.0</span> 2947</div> 2948<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> 2949<ul> 2950<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2951<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2952</ul> 2953<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> 2954<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); 2955util.<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> 2956</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> 2957<p>The following APIs are deprecated and should no longer be used. Existing 2958applications and modules should be updated to find alternative approaches.</p> 2959<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> 2960<div class="api_metadata"> 2961<span>Added in: v0.7.5</span><span>Deprecated since: v6.0.0</span> 2962</div> 2963<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> 2964<ul> 2965<li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2966<li><code>source</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 2967</ul> 2968<p>The <code>util._extend()</code> method was never intended to be used outside of internal 2969Node.js modules. The community found and used it anyway.</p> 2970<p>It is deprecated and should not be used in new code. JavaScript comes with very 2971similar 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> 2972<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> 2973<div class="api_metadata"> 2974<span>Added in: v0.6.0</span><span>Deprecated since: v4.0.0</span> 2975</div> 2976<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> 2977<ul> 2978<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2979<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2980</ul> 2981<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> 2982<p>Returns <code>true</code> if the given <code>object</code> is an <code>Array</code>. Otherwise, returns <code>false</code>.</p> 2983<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>); 2984 2985util.<span class="hljs-title function_">isArray</span>([]); 2986<span class="hljs-comment">// Returns: true</span> 2987util.<span class="hljs-title function_">isArray</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Array</span>()); 2988<span class="hljs-comment">// Returns: true</span> 2989util.<span class="hljs-title function_">isArray</span>({}); 2990<span class="hljs-comment">// Returns: false</span></code> <button class="copy-button">copy</button></pre> 2991<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> 2992<div class="api_metadata"> 2993<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 2994</div> 2995<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> 2996<ul> 2997<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2998<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2999</ul> 3000<p>Returns <code>true</code> if the given <code>object</code> is a <code>Boolean</code>. Otherwise, returns <code>false</code>.</p> 3001<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>); 3002 3003util.<span class="hljs-title function_">isBoolean</span>(<span class="hljs-number">1</span>); 3004<span class="hljs-comment">// Returns: false</span> 3005util.<span class="hljs-title function_">isBoolean</span>(<span class="hljs-number">0</span>); 3006<span class="hljs-comment">// Returns: false</span> 3007util.<span class="hljs-title function_">isBoolean</span>(<span class="hljs-literal">false</span>); 3008<span class="hljs-comment">// Returns: true</span></code> <button class="copy-button">copy</button></pre> 3009<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> 3010<div class="api_metadata"> 3011<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3012</div> 3013<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> 3014<ul> 3015<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3016<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3017</ul> 3018<p>Returns <code>true</code> if the given <code>object</code> is a <code>Buffer</code>. Otherwise, returns <code>false</code>.</p> 3019<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>); 3020 3021util.<span class="hljs-title function_">isBuffer</span>({ <span class="hljs-attr">length</span>: <span class="hljs-number">0</span> }); 3022<span class="hljs-comment">// Returns: false</span> 3023util.<span class="hljs-title function_">isBuffer</span>([]); 3024<span class="hljs-comment">// Returns: false</span> 3025util.<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>)); 3026<span class="hljs-comment">// Returns: true</span></code> <button class="copy-button">copy</button></pre> 3027<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> 3028<div class="api_metadata"> 3029<span>Added in: v0.6.0</span><span>Deprecated since: v4.0.0</span> 3030</div> 3031<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> 3032<ul> 3033<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3034<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3035</ul> 3036<p>Returns <code>true</code> if the given <code>object</code> is a <code>Date</code>. Otherwise, returns <code>false</code>.</p> 3037<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>); 3038 3039util.<span class="hljs-title function_">isDate</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>()); 3040<span class="hljs-comment">// Returns: true</span> 3041util.<span class="hljs-title function_">isDate</span>(<span class="hljs-title class_">Date</span>()); 3042<span class="hljs-comment">// false (without 'new' returns a String)</span> 3043util.<span class="hljs-title function_">isDate</span>({}); 3044<span class="hljs-comment">// Returns: false</span></code> <button class="copy-button">copy</button></pre> 3045<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> 3046<div class="api_metadata"> 3047<span>Added in: v0.6.0</span><span>Deprecated since: v4.0.0</span> 3048</div> 3049<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> 3050<ul> 3051<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3052<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3053</ul> 3054<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 3055<code>false</code>.</p> 3056<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>); 3057 3058util.<span class="hljs-title function_">isError</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>()); 3059<span class="hljs-comment">// Returns: true</span> 3060util.<span class="hljs-title function_">isError</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">TypeError</span>()); 3061<span class="hljs-comment">// Returns: true</span> 3062util.<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> }); 3063<span class="hljs-comment">// Returns: false</span></code> <button class="copy-button">copy</button></pre> 3064<p>This method relies on <code>Object.prototype.toString()</code> behavior. It is 3065possible to obtain an incorrect result when the <code>object</code> argument manipulates 3066<code>@@toStringTag</code>.</p> 3067<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>); 3068<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> }; 3069 3070util.<span class="hljs-title function_">isError</span>(obj); 3071<span class="hljs-comment">// Returns: false</span> 3072obj[<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toStringTag</span>] = <span class="hljs-string">'Error'</span>; 3073util.<span class="hljs-title function_">isError</span>(obj); 3074<span class="hljs-comment">// Returns: true</span></code> <button class="copy-button">copy</button></pre> 3075<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> 3076<div class="api_metadata"> 3077<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3078</div> 3079<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> 3080<ul> 3081<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3082<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3083</ul> 3084<p>Returns <code>true</code> if the given <code>object</code> is a <code>Function</code>. Otherwise, returns 3085<code>false</code>.</p> 3086<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>); 3087 3088<span class="hljs-keyword">function</span> <span class="hljs-title function_">Foo</span>(<span class="hljs-params"></span>) {} 3089<span class="hljs-keyword">const</span> <span class="hljs-title function_">Bar</span> = (<span class="hljs-params"></span>) => {}; 3090 3091util.<span class="hljs-title function_">isFunction</span>({}); 3092<span class="hljs-comment">// Returns: false</span> 3093util.<span class="hljs-title function_">isFunction</span>(<span class="hljs-title class_">Foo</span>); 3094<span class="hljs-comment">// Returns: true</span> 3095util.<span class="hljs-title function_">isFunction</span>(<span class="hljs-title class_">Bar</span>); 3096<span class="hljs-comment">// Returns: true</span></code> <button class="copy-button">copy</button></pre> 3097<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> 3098<div class="api_metadata"> 3099<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3100</div> 3101<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> 3102<ul> 3103<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3104<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3105</ul> 3106<p>Returns <code>true</code> if the given <code>object</code> is strictly <code>null</code>. Otherwise, returns 3107<code>false</code>.</p> 3108<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>); 3109 3110util.<span class="hljs-title function_">isNull</span>(<span class="hljs-number">0</span>); 3111<span class="hljs-comment">// Returns: false</span> 3112util.<span class="hljs-title function_">isNull</span>(<span class="hljs-literal">undefined</span>); 3113<span class="hljs-comment">// Returns: false</span> 3114util.<span class="hljs-title function_">isNull</span>(<span class="hljs-literal">null</span>); 3115<span class="hljs-comment">// Returns: true</span></code> <button class="copy-button">copy</button></pre> 3116<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> 3117<div class="api_metadata"> 3118<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3119</div> 3120<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use 3121<code>value === undefined || value === null</code> instead.</div><p></p> 3122<ul> 3123<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3124<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3125</ul> 3126<p>Returns <code>true</code> if the given <code>object</code> is <code>null</code> or <code>undefined</code>. Otherwise, 3127returns <code>false</code>.</p> 3128<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>); 3129 3130util.<span class="hljs-title function_">isNullOrUndefined</span>(<span class="hljs-number">0</span>); 3131<span class="hljs-comment">// Returns: false</span> 3132util.<span class="hljs-title function_">isNullOrUndefined</span>(<span class="hljs-literal">undefined</span>); 3133<span class="hljs-comment">// Returns: true</span> 3134util.<span class="hljs-title function_">isNullOrUndefined</span>(<span class="hljs-literal">null</span>); 3135<span class="hljs-comment">// Returns: true</span></code> <button class="copy-button">copy</button></pre> 3136<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> 3137<div class="api_metadata"> 3138<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3139</div> 3140<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> 3141<ul> 3142<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3143<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3144</ul> 3145<p>Returns <code>true</code> if the given <code>object</code> is a <code>Number</code>. Otherwise, returns <code>false</code>.</p> 3146<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>); 3147 3148util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-literal">false</span>); 3149<span class="hljs-comment">// Returns: false</span> 3150util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-title class_">Infinity</span>); 3151<span class="hljs-comment">// Returns: true</span> 3152util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-number">0</span>); 3153<span class="hljs-comment">// Returns: true</span> 3154util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-title class_">NaN</span>); 3155<span class="hljs-comment">// Returns: true</span></code> <button class="copy-button">copy</button></pre> 3156<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> 3157<div class="api_metadata"> 3158<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3159</div> 3160<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: 3161Use <code>value !== null && typeof value === 'object'</code> instead.</div><p></p> 3162<ul> 3163<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3164<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3165</ul> 3166<p>Returns <code>true</code> if the given <code>object</code> is strictly an <code>Object</code> <strong>and</strong> not a 3167<code>Function</code> (even though functions are objects in JavaScript). 3168Otherwise, returns <code>false</code>.</p> 3169<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>); 3170 3171util.<span class="hljs-title function_">isObject</span>(<span class="hljs-number">5</span>); 3172<span class="hljs-comment">// Returns: false</span> 3173util.<span class="hljs-title function_">isObject</span>(<span class="hljs-literal">null</span>); 3174<span class="hljs-comment">// Returns: false</span> 3175util.<span class="hljs-title function_">isObject</span>({}); 3176<span class="hljs-comment">// Returns: true</span> 3177util.<span class="hljs-title function_">isObject</span>(<span class="hljs-function">() =></span> {}); 3178<span class="hljs-comment">// Returns: false</span></code> <button class="copy-button">copy</button></pre> 3179<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> 3180<div class="api_metadata"> 3181<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3182</div> 3183<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use 3184<code>(typeof value !== 'object' && typeof value !== 'function') || value === null</code> 3185instead.</div><p></p> 3186<ul> 3187<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3188<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3189</ul> 3190<p>Returns <code>true</code> if the given <code>object</code> is a primitive type. Otherwise, returns 3191<code>false</code>.</p> 3192<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>); 3193 3194util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-number">5</span>); 3195<span class="hljs-comment">// Returns: true</span> 3196util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-string">'foo'</span>); 3197<span class="hljs-comment">// Returns: true</span> 3198util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-literal">false</span>); 3199<span class="hljs-comment">// Returns: true</span> 3200util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-literal">null</span>); 3201<span class="hljs-comment">// Returns: true</span> 3202util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-literal">undefined</span>); 3203<span class="hljs-comment">// Returns: true</span> 3204util.<span class="hljs-title function_">isPrimitive</span>({}); 3205<span class="hljs-comment">// Returns: false</span> 3206util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-function">() =></span> {}); 3207<span class="hljs-comment">// Returns: false</span> 3208util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-regexp">/^$/</span>); 3209<span class="hljs-comment">// Returns: false</span> 3210util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>()); 3211<span class="hljs-comment">// Returns: false</span></code> <button class="copy-button">copy</button></pre> 3212<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> 3213<div class="api_metadata"> 3214<span>Added in: v0.6.0</span><span>Deprecated since: v4.0.0</span> 3215</div> 3216<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated</div><p></p> 3217<ul> 3218<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3219<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3220</ul> 3221<p>Returns <code>true</code> if the given <code>object</code> is a <code>RegExp</code>. Otherwise, returns <code>false</code>.</p> 3222<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>); 3223 3224util.<span class="hljs-title function_">isRegExp</span>(<span class="hljs-regexp">/some regexp/</span>); 3225<span class="hljs-comment">// Returns: true</span> 3226util.<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>)); 3227<span class="hljs-comment">// Returns: true</span> 3228util.<span class="hljs-title function_">isRegExp</span>({}); 3229<span class="hljs-comment">// Returns: false</span></code> <button class="copy-button">copy</button></pre> 3230<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> 3231<div class="api_metadata"> 3232<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3233</div> 3234<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> 3235<ul> 3236<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3237<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3238</ul> 3239<p>Returns <code>true</code> if the given <code>object</code> is a <code>string</code>. Otherwise, returns <code>false</code>.</p> 3240<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>); 3241 3242util.<span class="hljs-title function_">isString</span>(<span class="hljs-string">''</span>); 3243<span class="hljs-comment">// Returns: true</span> 3244util.<span class="hljs-title function_">isString</span>(<span class="hljs-string">'foo'</span>); 3245<span class="hljs-comment">// Returns: true</span> 3246util.<span class="hljs-title function_">isString</span>(<span class="hljs-title class_">String</span>(<span class="hljs-string">'foo'</span>)); 3247<span class="hljs-comment">// Returns: true</span> 3248util.<span class="hljs-title function_">isString</span>(<span class="hljs-number">5</span>); 3249<span class="hljs-comment">// Returns: false</span></code> <button class="copy-button">copy</button></pre> 3250<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> 3251<div class="api_metadata"> 3252<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3253</div> 3254<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> 3255<ul> 3256<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3257<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3258</ul> 3259<p>Returns <code>true</code> if the given <code>object</code> is a <code>Symbol</code>. Otherwise, returns <code>false</code>.</p> 3260<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>); 3261 3262util.<span class="hljs-title function_">isSymbol</span>(<span class="hljs-number">5</span>); 3263<span class="hljs-comment">// Returns: false</span> 3264util.<span class="hljs-title function_">isSymbol</span>(<span class="hljs-string">'foo'</span>); 3265<span class="hljs-comment">// Returns: false</span> 3266util.<span class="hljs-title function_">isSymbol</span>(<span class="hljs-title class_">Symbol</span>(<span class="hljs-string">'foo'</span>)); 3267<span class="hljs-comment">// Returns: true</span></code> <button class="copy-button">copy</button></pre> 3268<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> 3269<div class="api_metadata"> 3270<span>Added in: v0.11.5</span><span>Deprecated since: v4.0.0</span> 3271</div> 3272<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> 3273<ul> 3274<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 3275<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 3276</ul> 3277<p>Returns <code>true</code> if the given <code>object</code> is <code>undefined</code>. Otherwise, returns <code>false</code>.</p> 3278<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>); 3279 3280<span class="hljs-keyword">const</span> foo = <span class="hljs-literal">undefined</span>; 3281util.<span class="hljs-title function_">isUndefined</span>(<span class="hljs-number">5</span>); 3282<span class="hljs-comment">// Returns: false</span> 3283util.<span class="hljs-title function_">isUndefined</span>(foo); 3284<span class="hljs-comment">// Returns: true</span> 3285util.<span class="hljs-title function_">isUndefined</span>(<span class="hljs-literal">null</span>); 3286<span class="hljs-comment">// Returns: false</span></code> <button class="copy-button">copy</button></pre> 3287<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> 3288<div class="api_metadata"> 3289<span>Added in: v0.3.0</span><span>Deprecated since: v6.0.0</span> 3290</div> 3291<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> 3292<ul> 3293<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 3294</ul> 3295<p>The <code>util.log()</code> method prints the given <code>string</code> to <code>stdout</code> with an included 3296timestamp.</p> 3297<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>); 3298 3299util.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Timestamped message.'</span>);</code> <button class="copy-button">copy</button></pre></section> 3300 <!-- API END --> 3301 </div> 3302 </div> 3303 </div> 3304</body> 3305</html> 3306