• Home
  • Raw
  • Download

Lines Matching +full:persist +full:- +full:credentials

4   <meta charset="utf-8">
5 <meta name="viewport" content="width=device-width">
6 <meta name="nodejs.org:node-version" content="v14.21.2">
13 <body class="alt apidoc" id="api-section-all">
22 <li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
23 <li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
27 <li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
28 <li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
29 <li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
30 <li><a href="addons.html" class="nav-addons">C++ addons</a></li>
31 <li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
32 <li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
33 <li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
34 <li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
35 <li><a href="cli.html" class="nav-cli">Command-line options</a></li>
36 <li><a href="console.html" class="nav-console">Console</a></li>
37 <li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
38 <li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
39 <li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
40 <li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
41 <li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
42 <li><a href="dns.html" class="nav-dns">DNS</a></li>
43 <li><a href="domain.html" class="nav-domain">Domain</a></li>
44 <li><a href="errors.html" class="nav-errors">Errors</a></li>
45 <li><a href="events.html" class="nav-events">Events</a></li>
46 <li><a href="fs.html" class="nav-fs">File system</a></li>
47 <li><a href="globals.html" class="nav-globals">Globals</a></li>
48 <li><a href="http.html" class="nav-http">HTTP</a></li>
49 <li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
50 <li><a href="https.html" class="nav-https">HTTPS</a></li>
51 <li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
52 <li><a href="intl.html" class="nav-intl">Internationalization</a></li>
53 <li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
54 <li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
55 <li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li>
56 <li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
57 <li><a href="net.html" class="nav-net">Net</a></li>
58 <li><a href="os.html" class="nav-os">OS</a></li>
59 <li><a href="path.html" class="nav-path">Path</a></li>
60 <li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
61 <li><a href="policy.html" class="nav-policy">Policies</a></li>
62 <li><a href="process.html" class="nav-process">Process</a></li>
63 <li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
64 <li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
65 <li><a href="readline.html" class="nav-readline">Readline</a></li>
66 <li><a href="repl.html" class="nav-repl">REPL</a></li>
67 <li><a href="report.html" class="nav-report">Report</a></li>
68 <li><a href="stream.html" class="nav-stream">Stream</a></li>
69 <li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
70 <li><a href="timers.html" class="nav-timers">Timers</a></li>
71 <li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
72 <li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
73 <li><a href="tty.html" class="nav-tty">TTY</a></li>
74 <li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
75 <li><a href="url.html" class="nav-url">URL</a></li>
76 <li><a href="util.html" class="nav-util">Utilities</a></li>
77 <li><a href="v8.html" class="nav-v8">V8</a></li>
78 <li><a href="vm.html" class="nav-vm">VM</a></li>
79 <li><a href="wasi.html" class="nav-wasi">WASI</a></li>
80 <li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
81 <li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
85 <li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code reposito…
89 <div id="column1" data-id="all" class="interior">
91 <div class="header-container">
93 …<button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-la…
94 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
96-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.6…
97 …<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-2…
99 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
101-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.4…
117 <li class="version-picker">
119 …<ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v19.x/api/all.html">19.x</a…
120 <li><a href="https://nodejs.org/docs/latest-v18.x/api/all.html">18.x <b>LTS</b></a></li>
121 <li><a href="https://nodejs.org/docs/latest-v17.x/api/all.html">17.x</a></li>
122 <li><a href="https://nodejs.org/docs/latest-v16.x/api/all.html">16.x <b>LTS</b></a></li>
123 <li><a href="https://nodejs.org/docs/latest-v15.x/api/all.html">15.x</a></li>
124 <li><a href="https://nodejs.org/docs/latest-v14.x/api/all.html">14.x <b>LTS</b></a></li>
125 <li><a href="https://nodejs.org/docs/latest-v13.x/api/all.html">13.x</a></li>
126 <li><a href="https://nodejs.org/docs/latest-v12.x/api/all.html">12.x</a></li>
127 <li><a href="https://nodejs.org/docs/latest-v11.x/api/all.html">11.x</a></li>
128 <li><a href="https://nodejs.org/docs/latest-v10.x/api/all.html">10.x</a></li>
129 <li><a href="https://nodejs.org/docs/latest-v9.x/api/all.html">9.x</a></li>
130 <li><a href="https://nodejs.org/docs/latest-v8.x/api/all.html">8.x</a></li>
131 <li><a href="https://nodejs.org/docs/latest-v7.x/api/all.html">7.x</a></li>
132 <li><a href="https://nodejs.org/docs/latest-v6.x/api/all.html">6.x</a></li>
133 <li><a href="https://nodejs.org/docs/latest-v5.x/api/all.html">5.x</a></li>
134 <li><a href="https://nodejs.org/docs/latest-v4.x/api/all.html">4.x</a></li>
135 <li><a href="https://nodejs.org/docs/latest-v0.12.x/api/all.html">0.12.x</a></li>
136 <li><a href="https://nodejs.org/docs/latest-v0.10.x/api/all.html">0.10.x</a></li></ol>
145 <!-- TOC --><details id="toc" open><summary>Table of contents</summary>
404 …uffer_the_zero_fill_buffers_command_line_option">The <code>--zero-fill-buffers</code> command-line…
414 <li><a href="#addons_context_aware_addons">Context-aware addons</a>
425 <li><span class="stability_2"><a href="#addons_node_api">Node-API</a></span></li>
439 <li><span class="stability_2"><a href="#n_api_node_api">Node-API</a></span>
446 <li><a href="#n_api_node_gyp">node-gyp</a></li>
452 <li><a href="#n_api_node_pre_gyp">node-pre-gyp</a></li>
460 <li><a href="#n_api_node_api_version_matrix">Node-API version matrix</a></li>
467 <li><a href="#n_api_basic_node_api_data_types">Basic Node-API data types</a>
476 <li><a href="#n_api_node_api_memory_management_types">Node-API memory management types</a>
485 <li><a href="#n_api_node_api_callback_types">Node-API callback types</a>
586 …pi_functions_to_convert_from_c_types_to_node_api">Functions to convert from C types to Node-API</a>
600 <li><a href="#n_api_functions_to_convert_from_node_api_to_c_types">Functions to convert from Node-A…
750 <li><a href="#n_api_asynchronous_thread_safe_function_calls">Asynchronous thread-safe function call…
752 <li><a href="#n_api_calling_a_thread_safe_function">Calling a thread-safe function</a></li>
753 <li><a href="#n_api_reference_counting_of_thread_safe_functions">Reference counting of thread-safe …
772 <li><a href="#embedding_setting_up_per_process_state">Setting up per-process state</a></li>
773 <li><a href="#embedding_per_instance_state">Per-instance state</a></li>
883 <li><a href="#cli_command_line_options">Command-line options</a>
888 <li><a href="#cli"><code>-</code></a></li>
889 <li><a href="#cli_1"><code>--</code></a></li>
890 <li><a href="#cli_abort_on_uncaught_exception"><code>--abort-on-uncaught-exception</code></a></li>
891 <li><a href="#cli_completion_bash"><code>--completion-bash</code></a></li>
892 …lity_1"><a href="#cli_c_condition_conditions_condition"><code>-C=condition</code>, <code>--conditi…
893 <li><span class="stability_1"><a href="#cli_cpu_prof"><code>--cpu-prof</code></a></span></li>
894 <li><span class="stability_1"><a href="#cli_cpu_prof_dir"><code>--cpu-prof-dir</code></a></span></l…
895 <li><span class="stability_1"><a href="#cli_cpu_prof_interval"><code>--cpu-prof-interval</code></a>…
896 <li><span class="stability_1"><a href="#cli_cpu_prof_name"><code>--cpu-prof-name</code></a></span><…
897 <li><a href="#cli_diagnostic_dir_directory"><code>--diagnostic-dir=directory</code></a></li>
898 <li><a href="#cli_disable_proto_mode"><code>--disable-proto=mode</code></a></li>
899 <li><a href="#cli_disallow_code_generation_from_strings"><code>--disallow-code-generation-from-stri…
900 <li><a href="#cli_dns_result_order_order"><code>--dns-result-order=order</code></a></li>
901 <li><a href="#cli_enable_fips"><code>--enable-fips</code></a></li>
902 <li><a href="#cli_enable_source_maps"><code>--enable-source-maps</code></a></li>
903 <li><a href="#cli_experimental_abortcontroller"><code>--experimental-abortcontroller</code></a></li>
904 <li><a href="#cli_experimental_import_meta_resolve"><code>--experimental-import-meta-resolve</code>…
905 <li><a href="#cli_experimental_json_modules"><code>--experimental-json-modules</code></a></li>
906 <li><a href="#cli_experimental_loader_module"><code>--experimental-loader=module</code></a></li>
907 <li><a href="#cli_experimental_modules"><code>--experimental-modules</code></a></li>
908 <li><a href="#cli_experimental_policy"><code>--experimental-policy</code></a></li>
909 <li><a href="#cli_experimental_repl_await"><code>--experimental-repl-await</code></a></li>
910 <li><a href="#cli_experimental_specifier_resolution_mode"><code>--experimental-specifier-resolution…
911 <li><a href="#cli_experimental_vm_modules"><code>--experimental-vm-modules</code></a></li>
912 <li><a href="#cli_experimental_wasi_unstable_preview1"><code>--experimental-wasi-unstable-preview1<…
913 <li><a href="#cli_experimental_wasm_modules"><code>--experimental-wasm-modules</code></a></li>
914 <li><a href="#cli_force_context_aware"><code>--force-context-aware</code></a></li>
915 <li><a href="#cli_force_fips"><code>--force-fips</code></a></li>
916 <li><span class="stability_1"><a href="#cli_frozen_intrinsics"><code>--frozen-intrinsics</code></a>…
917 …_1"><a href="#cli_heapsnapshot_near_heap_limit_max_count"><code>--heapsnapshot-near-heap-limit=max…
918 <li><a href="#cli_heapsnapshot_signal_signal"><code>--heapsnapshot-signal=signal</code></a></li>
919 <li><span class="stability_1"><a href="#cli_heap_prof"><code>--heap-prof</code></a></span></li>
920 <li><span class="stability_1"><a href="#cli_heap_prof_dir"><code>--heap-prof-dir</code></a></span><…
921 <li><span class="stability_1"><a href="#cli_heap_prof_interval"><code>--heap-prof-interval</code></…
922 <li><span class="stability_1"><a href="#cli_heap_prof_name"><code>--heap-prof-name</code></a></span…
923 <li><a href="#cli_icu_data_dir_file"><code>--icu-data-dir=file</code></a></li>
924 <li><a href="#cli_input_type_type"><code>--input-type=type</code></a></li>
925 <li><a href="#cli_inspect_brk_host_port"><code>--inspect-brk[=[host:]port]</code></a></li>
926 <li><a href="#cli_inspect_port_host_port"><code>--inspect-port=[host:]port</code></a></li>
927 <li><a href="#cli_inspect_host_port"><code>--inspect[=[host:]port]</code></a>
932 <li><a href="#cli_inspect_publish_uid_stderr_http"><code>--inspect-publish-uid=stderr,http</code></…
933 <li><a href="#cli_insecure_http_parser"><code>--insecure-http-parser</code></a></li>
934 <li><a href="#cli_jitless"><code>--jitless</code></a></li>
935 <li><a href="#cli_max_http_header_size_size"><code>--max-http-header-size=size</code></a></li>
936 <li><a href="#cli_napi_modules"><code>--napi-modules</code></a></li>
937 <li><a href="#cli_no_addons"><code>--no-addons</code></a></li>
938 <li><a href="#cli_no_deprecation"><code>--no-deprecation</code></a></li>
939 <li><a href="#cli_no_force_async_hooks_checks"><code>--no-force-async-hooks-checks</code></a></li>
940 <li><a href="#cli_no_warnings"><code>--no-warnings</code></a></li>
941 <li><a href="#cli_node_memory_debug"><code>--node-memory-debug</code></a></li>
942 <li><a href="#cli_openssl_config_file"><code>--openssl-config=file</code></a></li>
943 <li><a href="#cli_openssl_shared_config"><code>--openssl-shared-config</code></a></li>
944 <li><a href="#cli_pending_deprecation"><code>--pending-deprecation</code></a></li>
945 <li><span class="stability_1"><a href="#cli_policy_integrity_sri"><code>--policy-integrity=sri</cod…
946 <li><a href="#cli_preserve_symlinks"><code>--preserve-symlinks</code></a></li>
947 <li><a href="#cli_preserve_symlinks_main"><code>--preserve-symlinks-main</code></a></li>
948 <li><a href="#cli_prof"><code>--prof</code></a></li>
949 <li><a href="#cli_prof_process"><code>--prof-process</code></a></li>
950 <li><a href="#cli_redirect_warnings_file"><code>--redirect-warnings=file</code></a></li>
951 <li><a href="#cli_report_compact"><code>--report-compact</code></a></li>
952 …eport_dir_directory_report_directory_directory"><code>--report-dir=directory</code>, <code>report-
953 <li><a href="#cli_report_filename_filename"><code>--report-filename=filename</code></a></li>
954 <li><a href="#cli_report_on_fatalerror"><code>--report-on-fatalerror</code></a></li>
955 <li><a href="#cli_report_on_signal"><code>--report-on-signal</code></a></li>
956 <li><a href="#cli_report_signal_signal"><code>--report-signal=signal</code></a></li>
957 <li><a href="#cli_report_uncaught_exception"><code>--report-uncaught-exception</code></a></li>
958 <li><a href="#cli_throw_deprecation"><code>--throw-deprecation</code></a></li>
959 <li><a href="#cli_title_title"><code>--title=title</code></a></li>
960 <li><a href="#cli_tls_cipher_list_list"><code>--tls-cipher-list=list</code></a></li>
961 <li><a href="#cli_tls_keylog_file"><code>--tls-keylog=file</code></a></li>
962 <li><a href="#cli_tls_max_v1_2"><code>--tls-max-v1.2</code></a></li>
963 <li><a href="#cli_tls_max_v1_3"><code>--tls-max-v1.3</code></a></li>
964 <li><a href="#cli_tls_min_v1_0"><code>--tls-min-v1.0</code></a></li>
965 <li><a href="#cli_tls_min_v1_1"><code>--tls-min-v1.1</code></a></li>
966 <li><a href="#cli_tls_min_v1_2"><code>--tls-min-v1.2</code></a></li>
967 <li><a href="#cli_tls_min_v1_3"><code>--tls-min-v1.3</code></a></li>
968 <li><a href="#cli_trace_atomics_wait"><code>--trace-atomics-wait</code></a></li>
969 <li><a href="#cli_trace_deprecation"><code>--trace-deprecation</code></a></li>
970 <li><a href="#cli_trace_event_categories"><code>--trace-event-categories</code></a></li>
971 <li><a href="#cli_trace_event_file_pattern"><code>--trace-event-file-pattern</code></a></li>
972 <li><a href="#cli_trace_events_enabled"><code>--trace-events-enabled</code></a></li>
973 <li><a href="#cli_trace_exit"><code>--trace-exit</code></a></li>
974 <li><a href="#cli_trace_sigint"><code>--trace-sigint</code></a></li>
975 <li><a href="#cli_trace_sync_io"><code>--trace-sync-io</code></a></li>
976 <li><a href="#cli_trace_tls"><code>--trace-tls</code></a></li>
977 <li><a href="#cli_trace_uncaught"><code>--trace-uncaught</code></a></li>
978 <li><a href="#cli_trace_warnings"><code>--trace-warnings</code></a></li>
979 <li><a href="#cli_track_heap_objects"><code>--track-heap-objects</code></a></li>
980 <li><a href="#cli_unhandled_rejections_mode"><code>--unhandled-rejections=mode</code></a></li>
981 …li><a href="#cli_use_bundled_ca_use_openssl_ca"><code>--use-bundled-ca</code>, <code>--use-openssl
982 <li><a href="#cli_use_largepages_mode"><code>--use-largepages=mode</code></a></li>
983 <li><a href="#cli_v8_options"><code>--v8-options</code></a></li>
984 <li><a href="#cli_v8_pool_size_num"><code>--v8-pool-size=num</code></a></li>
985 <li><a href="#cli_zero_fill_buffers"><code>--zero-fill-buffers</code></a></li>
986 <li><a href="#cli_c_check"><code>-c</code>, <code>--check</code></a></li>
987 <li><a href="#cli_e_eval_script"><code>-e</code>, <code>--eval "script"</code></a></li>
988 <li><a href="#cli_h_help"><code>-h</code>, <code>--help</code></a></li>
989 <li><a href="#cli_i_interactive"><code>-i</code>, <code>--interactive</code></a></li>
990 <li><a href="#cli_p_print_script"><code>-p</code>, <code>--print "script"</code></a></li>
991 <li><a href="#cli_r_require_module"><code>-r</code>, <code>--require module</code></a></li>
992 <li><a href="#cli_v_version"><code>-v</code>, <code>--version</code></a></li>
1029 <li><a href="#cli_max_old_space_size_size_in_megabytes"><code>--max-old-space-size=SIZE</code> (in …
1084 <li><a href="#corepack_running_npm_install_g_yarn_doesn_t_work">Running <code>npm install -g yarn</…
1283 <li><a href="#DEP0011">DEP0011: <code>crypto.Credentials</code></a></li>
1334 <li><a href="#DEP0062">DEP0062: <code>node --debug</code></a></li>
1362 <li><a href="#DEP0092">DEP0092: Top-level <code>this</code> bound to <code>module.exports</code></a…
1369 <li><a href="#DEP0099">DEP0099: Async context-unaware <code>node::MakeCallback</code> C++ APIs</a><…
1371 <li><a href="#DEP0101">DEP0101: <code>--with-lttng</code></a></li>
1537 <li><a href="#errors_error_first_callbacks">Error-first callbacks</a></li>
2276 …g_of_callback_and_promise_based_operations">Ordering of callback and promise-based operations</a><…
2282 <li><a href="#fs_platform_specific_considerations">Platform-specific considerations</a></li>
2286 <li><a href="#fs_per_drive_working_directories_on_windows">Per-drive working directories on Windows…
2513 <li><a href="#http2_server_side_example">Server-side example</a></li>
2514 <li><a href="#http2_client_side_example">Client-side example</a></li>
2775 <li><a href="#inspector_event_inspector_protocol_method">Event: <code>&#x3C;inspector-protocol-meth…
2795 …tl_build_with_a_pre_installed_icu_system_icu">Build with a pre-installed ICU (<code>system-icu</co…
2796 …d_a_limited_set_of_icu_data_small_icu">Embed a limited set of ICU data (<code>small-icu</code>)</a>
2801 <li><a href="#intl_embed_the_entire_icu_full_icu">Embed the entire ICU (<code>full-icu</code>)</a><…
2914 <li><span class="stability_1"><a href="#esm_top_level_await">Top-level <code>await</code></a></span…
2974 <li><a href="#packages_input_type_flag"><code>--input-type</code> flag</a></li>
2989 <li><a href="#packages_self_referencing_a_package_using_its_name">Self-referencing a package using …
3160 <li><a href="#os_windows_specific_error_constants">Windows-specific error constants</a></li>
3489 <li><a href="#readline_example_read_file_stream_line_by_line">Example: Read file stream line-by-Lin…
3508 <li><a href="#repl_reverse_i_search">Reverse-i-search</a></li>
3534 …node_js_repl_with_advanced_line_editors">Using the Node.js REPL with advanced line-editors</a></li>
3753 <li><a href="#tls_pre_shared_keys">Pre-shared keys</a></li>
3754 <li><a href="#tls_client_initiated_renegotiation_attack_mitigation">Client-initiated renegotiation …
4026 <li><a href="#url_percent_encoding_in_urls">Percent-encoding in URLs</a>
4076 …l_icu_option">Encodings supported when Node.js is built with the <code>small-icu</code> option</a>…
4341 <li><a href="#zlib_for_zlib_based_streams">For zlib-based streams</a></li>
4342 <li><a href="#zlib_for_brotli_based_streams">For Brotli-based streams</a></li>
4432 <p></p><div class="api_stability api_stability_0">Stability: 0 - Deprecated. The feature may emit w…
4434 <!-- separator -->
4435 <p></p><div class="api_stability api_stability_1">Stability: 1 - Experimental. The feature is not s…
4436 <a href="https://semver.org/">Semantic Versioning</a> rules. Non-backward compatible changes or rem…
4439 <!-- separator -->
4440 <p></p><div class="api_stability api_stability_2">Stability: 2 - Stable. Compatibility with the npm…
4442 <!-- separator -->
4443 <p></p><div class="api_stability api_stability_3">Stability: 3 - Legacy. The feature is no longer r…
4444 likely will not be removed, and is still covered by semantic-versioning
4450 a command-line flag. Experimental features may also emit a <a href="#process_event_warning">warning…
4452 <!-- STABILITY_OVERVIEW_SLOT_BEGIN -->
4453 <!-- STABILITY_OVERVIEW_SLOT_END -->
4470 <p><code>node [options] [V8 options] [script.js | -e "script" | - ] [arguments]</code></p>
4471 <p>Please see the <a href="#cli_command_line_options">Command-line options</a> document for more in…
4481 <a href="https://nodejs.org/en/download/package-manager/">Installing Node.js via package manager</a…
4484 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">mk…
4485 <span class="hljs-meta">$ </span><span class="language-bash"><span class="hljs-built_in">cd</span> …
4487 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">mk…
4488 <span class="hljs-meta">> </span><span class="language-bash"><span class="hljs-built_in">cd</span> …
4490 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">mk…
4491 <span class="hljs-meta">> </span><span class="language-bash"><span class="hljs-built_in">cd</span> …
4493 folder and call it <code>hello-world.js</code>.</p>
4494 <p>Open <code>hello-world.js</code> in any preferred text editor and
4496 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
4498 <span class="hljs-keyword">const</span> hostname = <span class="hljs-string">'127.0.0.1'</span>;
4499 <span class="hljs-keyword">const</span> port = <span class="hljs-number">3000</span>;
4501 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
4502 res.<span class="hljs-property">statusCode</span> = <span class="hljs-number">200</span>;
4503 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</…
4504 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Hello, World!\n'</spa…
4507 server.<span class="hljs-title function_">listen</span>(port, hostname, <span class="hljs-function"…
4508-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
4511 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
4513 <pre><code class="language-console">Server running at http://127.0.0.1:3000/</code></pre>
4520 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
4540 <p>In strict assertion mode, non-strict methods behave like their corresponding
4546-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
4548-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
4550-title function_">deepEqual</span>([[[<span class="hljs-number">1</span>, <span class="hljs-number…
4551 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
4552 <span class="hljs-comment">// + actual - expected ... Lines skipped</span>
4553 <span class="hljs-comment">//</span>
4554 <span class="hljs-comment">// [</span>
4555 <span class="hljs-comment">// [</span>
4556 <span class="hljs-comment">// ...</span>
4557 <span class="hljs-comment">// 2,</span>
4558 <span class="hljs-comment">// + 3</span>
4559 <span class="hljs-comment">// - '3'</span>
4560 <span class="hljs-comment">// ],</span>
4561 <span class="hljs-comment">// ...</span>
4562 <span class="hljs-comment">// 5</span>
4563 <span class="hljs-comment">// ]</span></code></pre>
4569 <p>Legacy assertion mode uses the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-co…
4577 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4579 <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Compar…
4582 <pre><code class="language-js"><span class="hljs-comment">// WARNING: This does not throw an Assert…
4583 …class="hljs-title function_">deepEqual</span>(<span class="hljs-regexp">/a/gi</span>, <span class=…
4595 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
4597 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
4598 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
4599 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4600 <li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4601 <li><code>stackStartFn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
4607 <p>All instances contain the built-in <code>Error</code> properties (<code>message</code> and <code…
4610 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
4612 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4614 …r.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean><…
4616 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
4618 <li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4620 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4622 <span class="hljs-comment">// Generate an AssertionError to compare the error message later:</span>
4623 <span class="hljs-keyword">const</span> { message } = <span class="hljs-keyword">new</span> assert.…
4624 <span class="hljs-attr">actual</span>: <span class="hljs-number">1</span>,
4625 <span class="hljs-attr">expected</span>: <span class="hljs-number">2</span>,
4626 <span class="hljs-attr">operator</span>: <span class="hljs-string">'strictEqual'</span>
4629 <span class="hljs-comment">// Verify error output:</span>
4630 <span class="hljs-keyword">try</span> {
4631 …assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <…
4632 } <span class="hljs-keyword">catch</span> (err) {
4633 …<span class="hljs-title function_">assert</span>(err <span class="hljs-keyword">instanceof</span> …
4634 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">messa…
4635 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">name<…
4636 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">actua…
4637 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">expec…
4638 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">code<…
4639 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">opera…
4640 …rt.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">generated…
4646 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
4656 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4658 <span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<spa…
4660 <span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span cla…
4662 <span class="hljs-comment">// callsfunc() must be called exactly 1 time before tracker.verify().</s…
4663 <span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">call…
4665 <span class="hljs-title function_">callsfunc</span>();
4667 <span class="hljs-comment">// Calls tracker.verify() and verifies if all tracker.calls() functions …
4668 <span class="hljs-comment">// been called exact times.</span>
4669 process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span…
4670 tracker.<span class="hljs-title function_">verify</span>();
4677 ….mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Funct…
4678 <li><code>exact</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
4679 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
4685 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4687 <span class="hljs-comment">// Creates call tracker.</span>
4688 <span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<spa…
4690 <span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span cla…
4692 <span class="hljs-comment">// Returns a function that wraps func() that must be called exact times<…
4693 <span class="hljs-comment">// before tracker.verify().</span>
4694 <span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">call…
4700 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
4702 <li>Object <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Object…
4704 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
4705 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
4706 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4708 <li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4709 <li><code>stack</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
4715 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4717 <span class="hljs-comment">// Creates call tracker.</span>
4718 <span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<spa…
4720 <span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span cla…
4722 <span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span clas…
4724 <span class="hljs-comment">// Returns a function that wraps func() that must be called exact times<…
4725 <span class="hljs-comment">// before tracker.verify().</span>
4726 <span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">call…
4728 <span class="hljs-comment">// Returns an array containing information on callsfunc()</span>
4729 tracker.<span class="hljs-title function_">report</span>();
4730 <span class="hljs-comment">// [</span>
4731 <span class="hljs-comment">// {</span>
4732 <span class="hljs-comment">// message: 'Expected the func function to be executed 2 time(s) but …
4733 <span class="hljs-comment">// executed 0 time(s).',</span>
4734 <span class="hljs-comment">// actual: 0,</span>
4735 <span class="hljs-comment">// expected: 2,</span>
4736 <span class="hljs-comment">// operator: 'func',</span>
4737 <span class="hljs-comment">// stack: stack trace</span>
4738 <span class="hljs-comment">// }</span>
4739 <span class="hljs-comment">// ]</span></code></pre>
4747 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4749 <span class="hljs-comment">// Creates call tracker.</span>
4750 <span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<spa…
4752 <span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span cla…
4754 <span class="hljs-comment">// Returns a function that wraps func() that must be called exact times<…
4755 <span class="hljs-comment">// before tracker.verify().</span>
4756 <span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">call…
4758 <span class="hljs-title function_">callsfunc</span>();
4760 <span class="hljs-comment">// Will throw an error since callsfunc() was only called once.</span>
4761 tracker.<span class="hljs-title function_">verify</span>();</code></pre>
4767 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
4768 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
4791 <td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
4798 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
4799 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4800 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
4805 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use <a href=…
4813 … are compared with the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">…
4816 <li><a href="https://tc39.github.io/ecma262/#sec-object.prototype.tostring">Type tags</a> of object…
4817 <li>Only <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownersh…
4820 <li><a href="https://developer.mozilla.org/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_…
4822 …ozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><code>Map</code></a> keys and <…
4825 …test the <a href="https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-interna…
4827 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbo…
4828 …zilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and…
4831 …onsidered equal by the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">…
4833 <pre><code class="language-js"><span class="hljs-comment">// WARNING: This does not throw an Assert…
4834 assert.<span class="hljs-title function_">deepEqual</span>(<span class="hljs-string">'+00000000'</s…
4837 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4839 <span class="hljs-keyword">const</span> obj1 = {
4840 <span class="hljs-attr">a</span>: {
4841 <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>
4844 <span class="hljs-keyword">const</span> obj2 = {
4845 <span class="hljs-attr">a</span>: {
4846 <span class="hljs-attr">b</span>: <span class="hljs-number">2</span>
4849 <span class="hljs-keyword">const</span> obj3 = {
4850 <span class="hljs-attr">a</span>: {
4851 <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>
4854 <span class="hljs-keyword">const</span> obj4 = <span class="hljs-title class_">Object</span>.<span …
4856 assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj1);
4857 <span class="hljs-comment">// OK</span>
4859 <span class="hljs-comment">// Values of b are different:</span>
4860 assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj2);
4861 <span class="hljs-comment">// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }</span>
4863 assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj3);
4864 <span class="hljs-comment">// OK</span>
4866 <span class="hljs-comment">// Prototypes are ignored:</span>
4867 assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj4);
4868 <span class="hljs-comment">// AssertionError: { a: { b: 1 } } deepEqual {}</span></code></pre>
4882 <td><p>The <code>NaN</code> is now compared using the <a href="https://tc39.github.io/ecma262/#sec-
4892 <td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
4899 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
4900 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4901 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
4908 <li>Primitive values are compared using the <a href="https://tc39.github.io/ecma262/#sec-samevalue"…
4909 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is…
4910 <li><a href="https://tc39.github.io/ecma262/#sec-object.prototype.tostring">Type tags</a> of object…
4911 <li><a href="https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slot…
4912 the <a href="https://tc39.github.io/ecma262/#sec-strict-equality-comparison">Strict Equality Compar…
4913 <li>Only <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownersh…
4916 <li>Enumerable own <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Globa…
4917 <li><a href="https://developer.mozilla.org/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_…
4919 …ozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><code>Map</code></a> keys and <…
4922 …zilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and…
4925-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
4927 <span class="hljs-comment">// This fails because 1 !== '1'.</span>
4928-title function_">deepStrictEqual</span>({ <span class="hljs-attr">a</span>: <span class="hljs-num…
4929 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
4930 <span class="hljs-comment">// + actual - expected</span>
4931 <span class="hljs-comment">//</span>
4932 <span class="hljs-comment">// {</span>
4933 <span class="hljs-comment">// + a: 1</span>
4934 <span class="hljs-comment">// - a: '1'</span>
4935 <span class="hljs-comment">// }</span>
4937 <span class="hljs-comment">// The following objects don't have own properties</span>
4938 <span class="hljs-keyword">const</span> date = <span class="hljs-keyword">new</span> <span class="h…
4939 <span class="hljs-keyword">const</span> object = {};
4940 <span class="hljs-keyword">const</span> fakeDate = {};
4941-title class_">Object</span>.<span class="hljs-title function_">setPrototypeOf</span>(fakeDate, <s…
4943 <span class="hljs-comment">// Different [[Prototype]]:</span>
4944 assert.<span class="hljs-title function_">deepStrictEqual</span>(object, fakeDate);
4945 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
4946 <span class="hljs-comment">// + actual - expected</span>
4947 <span class="hljs-comment">//</span>
4948 <span class="hljs-comment">// + {}</span>
4949 <span class="hljs-comment">// - Date {}</span>
4951 <span class="hljs-comment">// Different type tags:</span>
4952 assert.<span class="hljs-title function_">deepStrictEqual</span>(date, fakeDate);
4953 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
4954 <span class="hljs-comment">// + actual - expected</span>
4955 <span class="hljs-comment">//</span>
4956 <span class="hljs-comment">// + 2018-04-26T00:49:08.604Z</span>
4957 <span class="hljs-comment">// - Date {}</span>
4959 assert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-title class_">Na…
4960 <span class="hljs-comment">// OK, because of the SameValue comparison</span>
4962 <span class="hljs-comment">// Different unwrapped numbers:</span>
4963-title function_">deepStrictEqual</span>(<span class="hljs-keyword">new</span> <span class="hljs-t…
4964 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
4965 <span class="hljs-comment">// + actual - expected</span>
4966 <span class="hljs-comment">//</span>
4967 <span class="hljs-comment">// + [Number: 1]</span>
4968 <span class="hljs-comment">// - [Number: 2]</span>
4970-title function_">deepStrictEqual</span>(<span class="hljs-keyword">new</span> <span class="hljs-t…
4971 <span class="hljs-comment">// OK because the object and the string are identical when unwrapped.</s…
4973 assert.<span class="hljs-title function_">deepStrictEqual</span>(-<span class="hljs-number">0</span…
4974 <span class="hljs-comment">// OK</span>
4976 <span class="hljs-comment">// Different zeros using the SameValue Comparison:</span>
4977 assert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-number">0</span>…
4978 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
4979 <span class="hljs-comment">// + actual - expected</span>
4980 <span class="hljs-comment">//</span>
4981 <span class="hljs-comment">// + 0</span>
4982 <span class="hljs-comment">// - -0</span>
4984 <span class="hljs-keyword">const</span> symbol1 = <span class="hljs-title class_">Symbol</span>();
4985 <span class="hljs-keyword">const</span> symbol2 = <span class="hljs-title class_">Symbol</span>();
4986 …pan class="hljs-title function_">deepStrictEqual</span>({ [symbol1]: <span class="hljs-number">1</…
4987 <span class="hljs-comment">// OK, because it is the same symbol on both objects.</span>
4989 …pan class="hljs-title function_">deepStrictEqual</span>({ [symbol1]: <span class="hljs-number">1</…
4990 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Inputs identical but not reference eq…
4991 <span class="hljs-comment">//</span>
4992 <span class="hljs-comment">// {</span>
4993 <span class="hljs-comment">// [Symbol()]: 1</span>
4994 <span class="hljs-comment">// }</span>
4996 <span class="hljs-keyword">const</span> weakMap1 = <span class="hljs-keyword">new</span> <span clas…
4997 <span class="hljs-keyword">const</span> weakMap2 = <span class="hljs-keyword">new</span> <span clas…
4998 <span class="hljs-keyword">const</span> weakMap3 = <span class="hljs-keyword">new</span> <span clas…
4999 weakMap3.<span class="hljs-property">unequal</span> = <span class="hljs-literal">true</span>;
5001 assert.<span class="hljs-title function_">deepStrictEqual</span>(weakMap1, weakMap2);
5002 <span class="hljs-comment">// OK, because it is impossible to compare the entries</span>
5004 <span class="hljs-comment">// Fails because weakMap3 has a property that weakMap1 does not contain:…
5005 assert.<span class="hljs-title function_">deepStrictEqual</span>(weakMap1, weakMap3);
5006 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
5007 <span class="hljs-comment">// + actual - expected</span>
5008 <span class="hljs-comment">//</span>
5009 <span class="hljs-comment">// WeakMap {</span>
5010 <span class="hljs-comment">// + [items unknown]</span>
5011 <span class="hljs-comment">// - [items unknown],</span>
5012 <span class="hljs-comment">// - unequal: true</span>
5013 <span class="hljs-comment">// }</span></code></pre>
5024 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5025 <li><code>regexp</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
5026 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5028 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
5032-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5034 assert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-string">'I will fai…
5035 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The input was expected to not match t…
5037 assert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-number">123</span>,…
5038 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The "string" argument must be of type…
5040 assert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-string">'I will pas…
5041 <span class="hljs-comment">// OK</span></code></pre>
5053 …a.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a…
5054 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type">&#x3C;RegExp></a> …
5055 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
5069 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code>Class</code></a>, <a href="…
5073 <!-- eslint-disable no-restricted-syntax -->
5074 <pre><code class="language-js">(<span class="hljs-keyword">async</span> () => {
5075 …<span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">doesNotReject</s…
5076 <span class="hljs-keyword">async</span> () => {
5077 … class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title c…
5079 <span class="hljs-title class_">SyntaxError</span>
5082 <!-- eslint-disable no-restricted-syntax -->
5083-js">assert.<span class="hljs-title function_">doesNotReject</span>(<span class="hljs-title class_…
5084 .<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> {
5085 <span class="hljs-comment">// ...</span>
5102 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
5103 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type">&#x3C;RegExp></a> …
5104 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
5117 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code>Class</code></a>, <a href="…
5121 <!-- eslint-disable no-restricted-syntax -->
5122 <pre><code class="language-js">assert.<span class="hljs-title function_">doesNotThrow</span>(
5123 <span class="hljs-function">() =></span> {
5124 … class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title c…
5126 <span class="hljs-title class_">SyntaxError</span>
5130 <!-- eslint-disable no-restricted-syntax -->
5131 <pre><code class="language-js">assert.<span class="hljs-title function_">doesNotThrow</span>(
5132 <span class="hljs-function">() =></span> {
5133 … class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title c…
5135 <span class="hljs-title class_">TypeError</span>
5140 <!-- eslint-disable no-restricted-syntax -->
5141 <pre><code class="language-js">assert.<span class="hljs-title function_">doesNotThrow</span>(
5142 <span class="hljs-function">() =></span> {
5143 … class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title c…
5145 <span class="hljs-regexp">/Wrong value/</span>,
5146 <span class="hljs-string">'Whoops'</span>
5148 <span class="hljs-comment">// Throws: AssertionError: Got unwanted exception: Whoops</span></code><…
5164 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5165 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5166 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5171 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use <a href=…
5173 using the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equal…
5175 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
5177 assert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span cl…
5178 <span class="hljs-comment">// OK, 1 == 1</span>
5179 assert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span cl…
5180 <span class="hljs-comment">// OK, 1 == '1'</span>
5181 assert.<span class="hljs-title function_">equal</span>(<span class="hljs-title class_">Na</span>N, …
5182 <span class="hljs-comment">// OK</span>
5184 assert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span cl…
5185 <span class="hljs-comment">// AssertionError: 1 == 2</span>
5186-title function_">equal</span>({ <span class="hljs-attr">a</span>: { <span class="hljs-attr">b</sp…
5187 <span class="hljs-comment">// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }</span></code></pre>
5198 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5203-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5205 assert.<span class="hljs-title function_">fail</span>();
5206 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Failed</span>
5208 assert.<span class="hljs-title function_">fail</span>(<span class="hljs-string">'boom'</span>);
5209 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: boom</span>
5211 …ass="hljs-title function_">fail</span>(<span class="hljs-keyword">new</span> <span class="hljs-tit…
5212 <span class="hljs-comment">// TypeError: need array</span></code></pre>
5227 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
5230 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5231 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5232 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5233 <li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5234 <li><code>stackStartFn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
5244-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5246 assert.<span class="hljs-title function_">fail</span>(<span class="hljs-string">'a'</span>, <span c…
5247 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 'a' != 'b'</span>
5249-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</spa…
5250 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 1 > 2</span>
5252 …span class="hljs-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hlj…
5253 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: fail</span>
5255-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</spa…
5256 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: whoops</span>
5258-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</spa…
5259 <span class="hljs-comment">// TypeError: need array</span></code></pre>
5263 …class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">…
5264-title function_">fail</span>(<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'<…
5266 <span class="hljs-title function_">suppressFrame</span>();
5267 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 'a' !== 'b'</span>
5268 <span class="hljs-comment">// at repl:1:1</span>
5269 <span class="hljs-comment">// at ContextifyScript.Script.runInThisContext (vm.js:44:33)</span>
5270 <span class="hljs-comment">// ...</span></code></pre>
5286 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
5292-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5294 assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-literal">null</span>);
5295 <span class="hljs-comment">// OK</span>
5296 assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-number">0</span>);
5297 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0</sp…
5298 assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-string">'error'</span>);
5299 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'erro…
5300 assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-keyword">new</span> <spa…
5301 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error…
5303 <span class="hljs-comment">// Create some random error frames.</span>
5304 <span class="hljs-keyword">let</span> err;
5305 (<span class="hljs-keyword">function</span> <span class="hljs-title function_">errorFrame</span>(<s…
5306 …err = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span cla…
5309 (<span class="hljs-keyword">function</span> <span class="hljs-title function_">ifErrorFrame</span>(…
5310 assert.<span class="hljs-title function_">ifError</span>(err);
5312 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test …
5313 <span class="hljs-comment">// at ifErrorFrame</span>
5314 <span class="hljs-comment">// at errorFrame</span></code></pre>
5320 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5321 <li><code>regexp</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
5322 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5324 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
5328-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5330 assert.<span class="hljs-title function_">match</span>(<span class="hljs-string">'I will fail'</spa…
5331 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The input did not match the regular .…
5333 assert.<span class="hljs-title function_">match</span>(<span class="hljs-number">123</span>, <span …
5334 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The "string" argument must be of type…
5336 assert.<span class="hljs-title function_">match</span>(<span class="hljs-string">'I will pass'</spa…
5337 <span class="hljs-comment">// OK</span></code></pre>
5362 <td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
5369 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5370 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5371 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5376 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use <a href=…
5378 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
5380 <span class="hljs-keyword">const</span> obj1 = {
5381 <span class="hljs-attr">a</span>: {
5382 <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>
5385 <span class="hljs-keyword">const</span> obj2 = {
5386 <span class="hljs-attr">a</span>: {
5387 <span class="hljs-attr">b</span>: <span class="hljs-number">2</span>
5390 <span class="hljs-keyword">const</span> obj3 = {
5391 <span class="hljs-attr">a</span>: {
5392 <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>
5395 <span class="hljs-keyword">const</span> obj4 = <span class="hljs-title class_">Object</span>.<span …
5397 assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj1);
5398 <span class="hljs-comment">// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }</span>
5400 assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj2);
5401 <span class="hljs-comment">// OK</span>
5403 assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj3);
5404 <span class="hljs-comment">// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }</span>
5406 assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj4);
5407 <span class="hljs-comment">// OK</span></code></pre>
5419 <td><p>The <code>-0</code> and <code>+0</code> are not considered equal anymore.</p></td></tr>
5421 <td><p>The <code>NaN</code> is now compared using the <a href="https://tc39.github.io/ecma262/#sec-
5431 <td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
5438 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5439 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5440 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5443-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5445-title function_">notDeepStrictEqual</span>({ <span class="hljs-attr">a</span>: <span class="hljs-
5446 <span class="hljs-comment">// OK</span></code></pre>
5467 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5468 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5469 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5474 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use <a href=…
5475 …ve inequality with the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">…
5478 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
5480 assert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span…
5481 <span class="hljs-comment">// OK</span>
5483 assert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span…
5484 <span class="hljs-comment">// AssertionError: 1 != 1</span>
5486 assert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span…
5487 <span class="hljs-comment">// AssertionError: 1 != '1'</span></code></pre>
5506 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5507 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5508 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5511 determined by the <a href="https://tc39.github.io/ecma262/#sec-samevalue">SameValue Comparison</a>.…
5512-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5514 assert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>,…
5515 <span class="hljs-comment">// OK</span>
5517 assert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>,…
5518 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected "actual" to be strictly uneq…
5519 <span class="hljs-comment">//</span>
5520 <span class="hljs-comment">// 1</span>
5522 assert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>,…
5523 <span class="hljs-comment">// OK</span></code></pre>
5542 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
5543 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5556-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5558 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">true</span>);
5559 <span class="hljs-comment">// OK</span>
5560 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-number">1</span>);
5561 <span class="hljs-comment">// OK</span>
5563 assert.<span class="hljs-title function_">ok</span>();
5564 <span class="hljs-comment">// AssertionError: No value argument passed to `assert.ok()`</span>
5566 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">false</span>, <span …
5567 <span class="hljs-comment">// AssertionError: it's false</span>
5569 <span class="hljs-comment">// In the repl:</span>
5570 … class="hljs-title function_">ok</span>(<span class="hljs-keyword">typeof</span> <span class="hljs
5571 <span class="hljs-comment">// AssertionError: false == true</span>
5573 <span class="hljs-comment">// In a file (e.g. test.js):</span>
5574 … class="hljs-title function_">ok</span>(<span class="hljs-keyword">typeof</span> <span class="hljs
5575 <span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
5576 <span class="hljs-comment">//</span>
5577 <span class="hljs-comment">// assert.ok(typeof 123 === 'string')</span>
5579 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">false</span>);
5580 <span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
5581 <span class="hljs-comment">//</span>
5582 <span class="hljs-comment">// assert.ok(false)</span>
5584 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-number">0</span>);
5585 <span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
5586 <span class="hljs-comment">//</span>
5587 <span class="hljs-comment">// assert.ok(0)</span>
5589 <span class="hljs-comment">// Using `assert()` works the same:</span>
5590 <span class="hljs-title function_">assert</span>(<span class="hljs-number">0</span>);
5591 <span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
5592 <span class="hljs-comment">//</span>
5593 <span class="hljs-comment">// assert(0)</span></code></pre>
5599 …a.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a…
5600-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type">&#x3C;RegExp></a> | <a href=…
5601 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
5613 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code>Class</code></a>, <a href="…
5615 each property will be tested for including the non-enumerable <code>message</code> and
5619 <pre><code class="language-js">(<span class="hljs-keyword">async</span> () => {
5620 <span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">rejects</span>(
5621 <span class="hljs-keyword">async</span> () => {
5622 … class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title c…
5625 <span class="hljs-attr">name</span>: <span class="hljs-string">'TypeError'</span>,
5626 <span class="hljs-attr">message</span>: <span class="hljs-string">'Wrong value'</span>
5630 <pre><code class="language-js">(<span class="hljs-keyword">async</span> () => {
5631 <span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">rejects</span>(
5632 <span class="hljs-keyword">async</span> () => {
5633 … class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title c…
5635 <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
5636 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">name<…
5637 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">messa…
5638 <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
5642 <pre><code class="language-js">assert.<span class="hljs-title function_">rejects</span>(
5643-title class_">Promise</span>.<span class="hljs-title function_">reject</span>(<span class="hljs-k…
5644 <span class="hljs-title class_">Error</span>
5645 ).<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> {
5646 <span class="hljs-comment">// ...</span>
5650 <code>message</code> instead. This can lead to easy-to-miss mistakes. Please read the
5666 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5667 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5668 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5671 determined by the <a href="https://tc39.github.io/ecma262/#sec-samevalue">SameValue Comparison</a>.…
5672-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5674 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <s…
5675 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:…
5676 <span class="hljs-comment">//</span>
5677 <span class="hljs-comment">// 1 !== 2</span>
5679 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <s…
5680 <span class="hljs-comment">// OK</span>
5682 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-string">'Hello fooba…
5683 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:…
5684 <span class="hljs-comment">// + actual - expected</span>
5685 <span class="hljs-comment">//</span>
5686 <span class="hljs-comment">// + 'Hello foobar'</span>
5687 <span class="hljs-comment">// - 'Hello World!'</span>
5688 <span class="hljs-comment">// ^</span>
5690 <span class="hljs-keyword">const</span> apples = <span class="hljs-number">1</span>;
5691 <span class="hljs-keyword">const</span> oranges = <span class="hljs-number">2</span>;
5692-title function_">strictEqual</span>(apples, oranges, <span class="hljs-string">`apples <span clas…
5693 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2</span>
5695-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-string"…
5696 <span class="hljs-comment">// TypeError: Inputs are not identical</span></code></pre>
5719 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
5720-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type">&#x3C;RegExp></a> | <a href=…
5721 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
5724 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code>Class</code></a>, <a href="…
5727 equality including the non-enumerable <code>message</code> and <code>name</code> properties. When
5734-js"><span class="hljs-keyword">const</span> err = <span class="hljs-keyword">new</span> <span cla…
5735 err.<span class="hljs-property">code</span> = <span class="hljs-number">404</span>;
5736 err.<span class="hljs-property">foo</span> = <span class="hljs-string">'bar'</span>;
5737 err.<span class="hljs-property">info</span> = {
5738 <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>,
5739 <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>
5741 err.<span class="hljs-property">reg</span> = <span class="hljs-regexp">/abc/i</span>;
5743 assert.<span class="hljs-title function_">throws</span>(
5744 <span class="hljs-function">() =></span> {
5745 <span class="hljs-keyword">throw</span> err;
5748 <span class="hljs-attr">name</span>: <span class="hljs-string">'TypeError'</span>,
5749 <span class="hljs-attr">message</span>: <span class="hljs-string">'Wrong value'</span>,
5750 <span class="hljs-attr">info</span>: {
5751 <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>,
5752 <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>
5754 … <span class="hljs-comment">// Only properties on the validation object will be tested for.</span>
5755 …<span class="hljs-comment">// Using nested objects requires all properties to be present. Otherwis…
5756 <span class="hljs-comment">// the validation is going to fail.</span>
5760 <span class="hljs-comment">// Using regular expressions to validate error properties:</span>
5761 assert.<span class="hljs-title function_">throws</span>(
5762 <span class="hljs-function">() =></span> {
5763 <span class="hljs-keyword">throw</span> err;
5766 …<span class="hljs-comment">// The `name` and `message` properties are strings and using regular</s…
5767 …<span class="hljs-comment">// expressions on those will match against the string. If they fail, an…
5768 <span class="hljs-comment">// error is thrown.</span>
5769 <span class="hljs-attr">name</span>: <span class="hljs-regexp">/^TypeError$/</span>,
5770 <span class="hljs-attr">message</span>: <span class="hljs-regexp">/Wrong/</span>,
5771 <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span>,
5772 <span class="hljs-attr">info</span>: {
5773 <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>,
5774 …<span class="hljs-comment">// It is not possible to use regular expressions for nested properties!…
5775 <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>
5777 …<span class="hljs-comment">// The `reg` property contains a regular expression and only if the</sp…
5778 …<span class="hljs-comment">// validation object contains an identical regular expression, it is go…
5779 <span class="hljs-comment">// to pass.</span>
5780 <span class="hljs-attr">reg</span>: <span class="hljs-regexp">/abc/i</span>
5784 <span class="hljs-comment">// Fails due to the different `message` and `name` properties:</span>
5785 assert.<span class="hljs-title function_">throws</span>(
5786 <span class="hljs-function">() =></span> {
5787 …ass="hljs-keyword">const</span> otherErr = <span class="hljs-keyword">new</span> <span class="hljs
5788 <span class="hljs-comment">// Copy all enumerable properties from `err` to `otherErr`.</span>
5789-keyword">for</span> (<span class="hljs-keyword">const</span> [key, value] <span class="hljs-keywo…
5792 <span class="hljs-keyword">throw</span> otherErr;
5794 …<span class="hljs-comment">// The error's `message` and `name` properties will also be checked whe…
5795 <span class="hljs-comment">// an error as validation object.</span>
5799 <pre><code class="language-js">assert.<span class="hljs-title function_">throws</span>(
5800 <span class="hljs-function">() =></span> {
5801 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
5803 <span class="hljs-title class_">Error</span>
5805 <p>Validate error message using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Gu…
5808 <pre><code class="language-js">assert.<span class="hljs-title function_">throws</span>(
5809 <span class="hljs-function">() =></span> {
5810 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
5812 <span class="hljs-regexp">/^Error: Wrong value$/</span>
5817 <pre><code class="language-js">assert.<span class="hljs-title function_">throws</span>(
5818 <span class="hljs-function">() =></span> {
5819 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
5821 <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
5822 …<span class="hljs-title function_">assert</span>(err <span class="hljs-keyword">instanceof</span> …
5823 …<span class="hljs-title function_">assert</span>(<span class="hljs-regexp">/value/</span>.<span cl…
5824 …<span class="hljs-comment">// Avoid returning anything from validation functions besides `true`.</…
5825 …<span class="hljs-comment">// Otherwise, it's not clear what part of the validation failed. Instea…
5826 …<span class="hljs-comment">// throw an error about the specific validation that failed (as done in…
5827 …<span class="hljs-comment">// example) and add as much helpful debugging information to that error…
5828 <span class="hljs-comment">// possible.</span>
5829 <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
5831 <span class="hljs-string">'unexpected error'</span>
5835 <code>message</code> instead. This can lead to easy-to-miss mistakes. Using the same
5839 <!-- eslint-disable no-restricted-syntax -->
5840 …class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">…
5841 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
5844 <span class="hljs-keyword">function</span> <span class="hljs-title function_">throwingSecond</span>…
5845 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
5848 <span class="hljs-keyword">function</span> <span class="hljs-title function_">notThrowing</span>(<s…
5850 <span class="hljs-comment">// The second argument is a string and the input function threw an Error…
5851 <span class="hljs-comment">// The first case will not throw as it does not match for the error mess…
5852 <span class="hljs-comment">// thrown by the input function!</span>
5853 assert.<span class="hljs-title function_">throws</span>(throwingFirst, <span class="hljs-string">'S…
5854 <span class="hljs-comment">// In the next example the message has no benefit over the message from …
5855 <span class="hljs-comment">// error and since it is not clear if the user intended to actually matc…
5856 <span class="hljs-comment">// against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT`…
5857 assert.<span class="hljs-title function_">throws</span>(throwingSecond, <span class="hljs-string">'…
5858 <span class="hljs-comment">// TypeError [ERR_AMBIGUOUS_ARGUMENT]</span>
5860 <span class="hljs-comment">// The string is only used (as message) in case the function does not th…
5861 assert.<span class="hljs-title function_">throws</span>(notThrowing, <span class="hljs-string">'Sec…
5862 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Missing expected exception: Second</s…
5864 <span class="hljs-comment">// If it was intended to match for the error message do this instead:</s…
5865 <span class="hljs-comment">// It does not throw because the error messages match.</span>
5866 assert.<span class="hljs-title function_">throws</span>(throwingSecond, <span class="hljs-regexp">/…
5868 <span class="hljs-comment">// If the error message does not match, an AssertionError is thrown.</sp…
5869 assert.<span class="hljs-title function_">throws</span>(throwingFirst, <span class="hljs-regexp">/S…
5870 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]</span></code></pre>
5871 <p>Due to the confusing error-prone notation, avoid a string as the second
5876 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
5880 … class="language-js"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built…
5892 … class="language-js"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built…
5894 <span class="hljs-comment">// Return the ID of the current execution context.</span>
5895 <span class="hljs-keyword">const</span> eid = async_hooks.<span class="hljs-title function_">execut…
5897 <span class="hljs-comment">// Return the ID of the handle responsible for triggering the callback o…
5898 <span class="hljs-comment">// current execution scope to call.</span>
5899 <span class="hljs-keyword">const</span> tid = async_hooks.<span class="hljs-title function_">trigge…
5901 <span class="hljs-comment">// Create a new AsyncHook instance. All of these callbacks are optional.…
5902 <span class="hljs-keyword">const</span> asyncHook =
5903 …async_hooks.<span class="hljs-title function_">createHook</span>({ init, before, after, destroy, p…
5905 <span class="hljs-comment">// Allow callbacks of this AsyncHook instance to call. This is not an im…
5906 <span class="hljs-comment">// action after running the constructor, and must be explicitly run to b…
5907 <span class="hljs-comment">// executing callbacks.</span>
5908 asyncHook.<span class="hljs-title function_">enable</span>();
5910 <span class="hljs-comment">// Disable listening for new asynchronous events.</span>
5911 asyncHook.<span class="hljs-title function_">disable</span>();
5913 <span class="hljs-comment">//</span>
5914 <span class="hljs-comment">// The following are the callbacks that can be passed to createHook().</…
5915 <span class="hljs-comment">//</span>
5917 <span class="hljs-comment">// init is called during object construction. The resource may not have<…
5918 <span class="hljs-comment">// completed construction when this callback runs, therefore all fields …
5919 <span class="hljs-comment">// resource referenced by "asyncId" may not have been populated.</span>
5920 <span class="hljs-keyword">function</span> <span class="hljs-title function_">init</span>(<span cla…
5922 <span class="hljs-comment">// Before is called just before the resource's callback is called. It ca…
5923 <span class="hljs-comment">// called 0-N times for handles (such as TCPWrap), and will be called ex…
5924 <span class="hljs-comment">// time for requests (such as FSReqCallback).</span>
5925 <span class="hljs-keyword">function</span> <span class="hljs-title function_">before</span>(<span c…
5927 <span class="hljs-comment">// After is called just after the resource's callback has finished.</spa…
5928 <span class="hljs-keyword">function</span> <span class="hljs-title function_">after</span>(<span cl…
5930 <span class="hljs-comment">// Destroy is called when the resource is destroyed.</span>
5931 <span class="hljs-keyword">function</span> <span class="hljs-title function_">destroy</span>(<span …
5933 <span class="hljs-comment">// promiseResolve is called only for promise resources, when the</span>
5934 <span class="hljs-comment">// `resolve` function passed to the `Promise` constructor is invoked</sp…
5935 <span class="hljs-comment">// (either directly or through other means of resolving a promise).</spa…
5936 <span class="hljs-keyword">function</span> <span class="hljs-title function_">promiseResolve</span>…
5942 <li><code>callbacks</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
5944 <li><code>init</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
5945 <li><code>before</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
5946 <li><code>after</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
5947 <li><code>destroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
5948 <li><code>promiseResolve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
5961 … class="language-js"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built…
5963 <span class="hljs-keyword">const</span> asyncHook = async_hooks.<span class="hljs-title function_">…
5964 …<span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsy…
5965 <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) { }
5968 <pre><code class="language-js"><span class="hljs-keyword">class</span> <span class="hljs-title clas…
5969 …<span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsy…
5970 <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) {}
5973 …s="hljs-keyword">class</span> <span class="hljs-title class_">MyAddedCallbacks</span> <span class=…
5974 <span class="hljs-title function_">before</span>(<span class="hljs-params">asyncId</span>) { }
5975 <span class="hljs-title function_">after</span>(<span class="hljs-params">asyncId</span>) { }
5978-keyword">const</span> asyncHook = async_hooks.<span class="hljs-title function_">createHook</span…
5987 with <code>--abort-on-uncaught-exception</code>, in which case a stack trace will be
6004 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
6005 <span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span cla…
6007 <span class="hljs-keyword">function</span> <span class="hljs-title function_">debug</span>(<span cl…
6008 …<span class="hljs-comment">// Use a function like this one when debugging inside an AsyncHooks cal…
6009-title function_">writeFileSync</span>(<span class="hljs-string">'log.out'</span>, <span class="hl…
6024 provided, enabling is a no-op.</p>
6027 … class="language-js"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built…
6029 …an class="hljs-keyword">const</span> hook = async_hooks.<span class="hljs-title function_">createH…
6044 <li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6045 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
6046 <li><code>triggerAsyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
6048 <li><code>resource</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
6058-js"><span class="hljs-built_in">require</span>(<span class="hljs-string">'net'</span>).<span clas…
6059 <span class="hljs-comment">// OR</span>
6060 …="hljs-built_in">clearTimeout</span>(<span class="hljs-built_in">setTimeout</span>(<span class="hl…
6067 <pre><code class="language-text">FSEVENTWRAP, FSREQCALLBACK, GETADDRINFOREQWRAP, GETNAMEINFOREQWRAP…
6084 <pre><code class="language-js"><span class="hljs-keyword">const</span> { fd } = process.<span class…
6086 async_hooks.<span class="hljs-title function_">createHook</span>({
6087 …<span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsy…
6088 …<span class="hljs-keyword">const</span> eid = async_hooks.<span class="hljs-title function_">execu…
6089 fs.<span class="hljs-title function_">writeSync</span>(
6091-string">`<span class="hljs-subst">${type}</span>(<span class="hljs-subst">${asyncId}</span>): tri…
6093 }).<span class="hljs-title function_">enable</span>();
6095-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">conn<…
6097 <pre><code class="language-console">TCPSERVERWRAP(5): trigger: 1 execution: 1
6123 <pre><code class="language-js"><span class="hljs-keyword">const</span> { fd } = process.<span class…
6125 <span class="hljs-keyword">let</span> indent = <span class="hljs-number">0</span>;
6126 async_hooks.<span class="hljs-title function_">createHook</span>({
6127 …<span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsy…
6128 …<span class="hljs-keyword">const</span> eid = async_hooks.<span class="hljs-title function_">execu…
6129 …<span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span cla…
6130 fs.<span class="hljs-title function_">writeSync</span>(
6132 …<span class="hljs-string">`<span class="hljs-subst">${indentStr}</span><span class="hljs-subst">${…
6133 …<span class="hljs-string">` trigger: <span class="hljs-subst">${triggerAsyncId}</span> execution: …
6135 <span class="hljs-title function_">before</span>(<span class="hljs-params">asyncId</span>) {
6136 …<span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span cla…
6137 …ss="hljs-title function_">writeSync</span>(fd, <span class="hljs-string">`<span class="hljs-subst"…
6138 indent += <span class="hljs-number">2</span>;
6140 <span class="hljs-title function_">after</span>(<span class="hljs-params">asyncId</span>) {
6141 indent -= <span class="hljs-number">2</span>;
6142 …<span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span cla…
6143 …ss="hljs-title function_">writeSync</span>(fd, <span class="hljs-string">`<span class="hljs-subst"…
6145 <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) {
6146 …<span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span cla…
6147 …s="hljs-title function_">writeSync</span>(fd, <span class="hljs-string">`<span class="hljs-subst">…
6149 }).<span class="hljs-title function_">enable</span>();
6151-title function_">createServer</span>(<span class="hljs-function">() =></span> {}).<span class="hl…
6152 <span class="hljs-comment">// Let's wait 10ms before logging the server started.</span>
6153 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
6154-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6155 }, <span class="hljs-number">10</span>);
6158 <pre><code class="language-console">TCPSERVERWRAP(5): trigger: 1 execution: 1
6165 <span class="hljs-meta">></span><span class="language-bash">>> 7</span>
6174 <pre><code class="language-console"> root(1)
6190 <pre><code class="language-console"> bootstrap(1)
6202 <li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6217 <li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6225 <li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6238 <li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6245-js"><span class="hljs-keyword">new</span> <span class="hljs-title class_">Promise</span>(<span cl…
6247 <pre><code class="language-text">init for PROMISE with id 5, trigger id: 1
6258 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
6264 <p>Using <code>executionAsyncResource()</code> in the top-level execution context will
6266 but having an object representing the top-level can be helpful.</p>
6267 …de class="language-js"><span class="hljs-keyword">const</span> { open } = <span class="hljs-built_…
6268 …class="hljs-keyword">const</span> { executionAsyncId, executionAsyncResource } = <span class="hljs
6270-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6271 …n class="hljs-title function_">open</span>(__filename, <span class="hljs-string">'r'</span>, <span…
6272-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6276 …lass="language-js"><span class="hljs-keyword">const</span> { createServer } = <span class="hljs-bu…
6277 <span class="hljs-keyword">const</span> {
6281 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'async_hooks'</span>);
6282 …ss="hljs-keyword">const</span> sym = <span class="hljs-title class_">Symbol</span>(<span class="hl…
6284 <span class="hljs-title function_">createHook</span>({
6285 …<span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsy…
6286 …<span class="hljs-keyword">const</span> cr = <span class="hljs-title function_">executionAsyncReso…
6287 <span class="hljs-keyword">if</span> (cr) {
6291 }).<span class="hljs-title function_">enable</span>();
6293 …ass="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>(<s…
6294 …<span class="hljs-title function_">executionAsyncResource</span>()[sym] = { <span class="hljs-attr…
6295 …<span class="hljs-built_in">setTimeout</span>(<span class="hljs-keyword">function</span>(<span cla…
6296 …js-title function_">end</span>(<span class="hljs-variable constant_">JSON</span>.<span class="hljs
6297 }, <span class="hljs-number">100</span>);
6298 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code><…
6312 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
6315 … class="language-js"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built…
6317-variable language_">console</span>.<span class="hljs-title function_">log</span>(async_hooks.<spa…
6318 …span class="hljs-title function_">open</span>(path, <span class="hljs-string">'r'</span>, <span cl…
6319-variable language_">console</span>.<span class="hljs-title function_">log</span>(async_hooks.<spa…
6323-js"><span class="hljs-keyword">const</span> server = net.<span class="hljs-title function_">creat…
6324 …<span class="hljs-comment">// Returns the ID of the server, not of the new connection, because the…
6325 …<span class="hljs-comment">// callback runs in the execution scope of the server's MakeCallback().…
6326 async_hooks.<span class="hljs-title function_">executionAsyncId</span>();
6328 }).<span class="hljs-title function_">listen</span>(port, <span class="hljs-function">() =></span> {
6329 …<span class="hljs-comment">// Returns the ID of a TickObject (process.nextTick()) because all</spa…
6330 <span class="hljs-comment">// callbacks passed to .listen() are wrapped in a nextTick().</span>
6331 async_hooks.<span class="hljs-title function_">executionAsyncId</span>();
6337 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
6340-js"><span class="hljs-keyword">const</span> server = net.<span class="hljs-title function_">creat…
6341 …<span class="hljs-comment">// The resource that caused (or triggered) this callback to be called</…
6342 …<span class="hljs-comment">// was that of the new connection. Thus the return value of triggerAsyn…
6343 <span class="hljs-comment">// is the asyncId of "conn".</span>
6344 async_hooks.<span class="hljs-title function_">triggerAsyncId</span>();
6346 }).<span class="hljs-title function_">listen</span>(port, <span class="hljs-function">() =></span> {
6347 …<span class="hljs-comment">// Even though all callbacks passed to .listen() are wrapped in a nextT…
6348 …<span class="hljs-comment">// the callback itself exists because the call to the server's .listen(…
6349 <span class="hljs-comment">// was made. So the return value would be the ID of the server.</span>
6350 async_hooks.<span class="hljs-title function_">triggerAsyncId</span>();
6359 …<code class="language-js"><span class="hljs-keyword">const</span> ah = <span class="hljs-built_in"…
6360-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>(<span class="hljs-
6361-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6363 <span class="hljs-comment">// produces:</span>
6364 <span class="hljs-comment">// eid 1 tid 0</span></code></pre>
6371 …<code class="language-js"><span class="hljs-keyword">const</span> ah = <span class="hljs-built_in"…
6372-title function_">createHook</span>({ <span class="hljs-title function_">init</span>(<span class="…
6373-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>(<span class="hljs-
6374-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6376 <span class="hljs-comment">// produces:</span>
6377 <span class="hljs-comment">// eid 7 tid 6</span></code></pre>
6399-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">AsyncResource</span…
6401 <span class="hljs-comment">// AsyncResource() is meant to be extended. Instantiating a</span>
6402 <span class="hljs-comment">// new AsyncResource() also triggers init. If triggerAsyncId is omitted …
6403 <span class="hljs-comment">// async_hook.executionAsyncId() is used.</span>
6404 <span class="hljs-keyword">const</span> asyncResource = <span class="hljs-keyword">new</span> <span…
6405-attr">triggerAsyncId</span>: <span class="hljs-title function_">executionAsyncId</span>(), <span …
6408 <span class="hljs-comment">// Run a function in the execution context of the resource. This will</s…
6409 <span class="hljs-comment">// * establish the context of the resource</span>
6410 <span class="hljs-comment">// * trigger the AsyncHooks before callbacks</span>
6411 <span class="hljs-comment">// * call the provided function `fn` with the supplied arguments</span>
6412 <span class="hljs-comment">// * trigger the AsyncHooks after callbacks</span>
6413 <span class="hljs-comment">// * restore the original execution context</span>
6414 asyncResource.<span class="hljs-title function_">runInAsyncScope</span>(fn, thisArg, ...args);
6416 <span class="hljs-comment">// Call AsyncHooks destroy callbacks.</span>
6417 asyncResource.<span class="hljs-title function_">emitDestroy</span>();
6419 <span class="hljs-comment">// Return the unique ID assigned to the AsyncResource instance.</span>
6420 asyncResource.<span class="hljs-title function_">asyncId</span>();
6422 <span class="hljs-comment">// Return the trigger ID for the AsyncResource instance.</span>
6423 asyncResource.<span class="hljs-title function_">triggerAsyncId</span>();</code></pre>
6426 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
6427 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
6429 <li><code>triggerAsyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
6431 <li><code>requireManualDestroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
6442-js"><span class="hljs-keyword">class</span> <span class="hljs-title class_">DBQuery</span> <span …
6443 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">db</span>) {
6444 <span class="hljs-variable language_">super</span>(<span class="hljs-string">'DBQuery'</span>);
6445 <span class="hljs-variable language_">this</span>.<span class="hljs-property">db</span> = db;
6448 …<span class="hljs-title function_">getInfo</span>(<span class="hljs-params">query, callback</span>…
6449-variable language_">this</span>.<span class="hljs-property">db</span>.<span class="hljs-title fun…
6450 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">runInAsyncSco…
6454 <span class="hljs-title function_">close</span>(<span class="hljs-params"></span>) {
6455 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">db</span> = <span cl…
6456 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emitDestroy</…
6464 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
6465 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
6476 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
6486 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
6488 <li><code>thisArg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6489 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6505 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
6509 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
6512 <p><a id="async-resource-worker-pool"></a></p>
6519 …class="language-js"><span class="hljs-keyword">const</span> { parentPort } = <span class="hljs-bui…
6520 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
6521 …ntPort.<span class="hljs-title function_">postMessage</span>(task.<span class="hljs-property">a</s…
6524-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">AsyncResource</span…
6525 …"hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span> } = <span class=…
6526 <span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span cla…
6527 …ss="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span> } = <span class="hl…
6529 <span class="hljs-keyword">const</span> kTaskInfo = <span class="hljs-title class_">Symbol</span>(<…
6530 <span class="hljs-keyword">const</span> kWorkerFreedEvent = <span class="hljs-title class_">Symbol<…
6532 …="hljs-keyword">class</span> <span class="hljs-title class_">WorkerPoolTaskInfo</span> <span class…
6533 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">callback</span>) {
6534 …<span class="hljs-variable language_">super</span>(<span class="hljs-string">'WorkerPoolTaskInfo'<…
6535 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">callback</span> = ca…
6538 <span class="hljs-title function_">done</span>(<span class="hljs-params">err, result</span>) {
6539-variable language_">this</span>.<span class="hljs-title function_">runInAsyncScope</span>(<span c…
6540 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emitDestroy</…
6544 …lass="hljs-keyword">class</span> <span class="hljs-title class_">WorkerPool</span> <span class="hl…
6545 …<span class="hljs-title function_">constructor</span>(<span class="hljs-params">numThreads</span>)…
6546 <span class="hljs-variable language_">super</span>();
6547 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">numThreads</span> = …
6548 … <span class="hljs-variable language_">this</span>.<span class="hljs-property">workers</span> = [];
6549 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">freeWorkers</span> =…
6550 <span class="hljs-variable language_">this</span>.<span class="hljs-property">tasks</span> = [];
6552 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs
6553 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">addNewWorker<…
6555 <span class="hljs-comment">// Any time the kWorkerFreedEvent is emitted, dispatch</span>
6556 <span class="hljs-comment">// the next task pending in the queue, if any.</span>
6557 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">on</span>(kWo…
6558-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property"…
6559-keyword">const</span> { task, callback } = <span class="hljs-variable language_">this</span>.<spa…
6560 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">runTask</span…
6565 <span class="hljs-title function_">addNewWorker</span>(<span class="hljs-params"></span>) {
6566-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title clas…
6567 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
6568 …<span class="hljs-comment">// In case of success: Call the callback that was passed to `runTask`,<…
6569 …<span class="hljs-comment">// remove the `TaskInfo` associated with the Worker, and mark it as fre…
6570 <span class="hljs-comment">// again.</span>
6571 …worker[kTaskInfo].<span class="hljs-title function_">done</span>(<span class="hljs-literal">null</…
6572 worker[kTaskInfo] = <span class="hljs-literal">null</span>;
6573 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">freeWorkers</span>.<…
6574 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(k…
6576 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
6577 …<span class="hljs-comment">// In case of an uncaught exception: Call the callback that was passed …
6578 <span class="hljs-comment">// `runTask` with the error.</span>
6579 <span class="hljs-keyword">if</span> (worker[kTaskInfo])
6580 …worker[kTaskInfo].<span class="hljs-title function_">done</span>(err, <span class="hljs-literal">n…
6581 <span class="hljs-keyword">else</span>
6582 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<…
6583 …<span class="hljs-comment">// Remove the worker from the list and start a new Worker to replace th…
6584 <span class="hljs-comment">// current one.</span>
6585-variable language_">this</span>.<span class="hljs-property">workers</span>.<span class="hljs-titl…
6586 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">addNewWorker<…
6588 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">workers</span>.<span…
6589 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">freeWorkers</span>.<…
6590 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(k…
6593 …<span class="hljs-title function_">runTask</span>(<span class="hljs-params">task, callback</span>)…
6594-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property"…
6595 <span class="hljs-comment">// No free threads, wait until a worker thread becomes free.</span>
6596 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">tasks</span>.<span c…
6597 <span class="hljs-keyword">return</span>;
6600 …ljs-keyword">const</span> worker = <span class="hljs-variable language_">this</span>.<span class="…
6601 …worker[kTaskInfo] = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WorkerPo…
6602 worker.<span class="hljs-title function_">postMessage</span>(task);
6605 <span class="hljs-title function_">close</span>(<span class="hljs-params"></span>) {
6606-keyword">for</span> (<span class="hljs-keyword">const</span> worker <span class="hljs-keyword">of…
6610 <span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <s…
6617-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">WorkerPool</span> = <…
6618 <span class="hljs-keyword">const</span> os = <span class="hljs-built_in">require</span>(<span class…
6620-keyword">const</span> pool = <span class="hljs-keyword">new</span> <span class="hljs-title class_…
6622 <span class="hljs-keyword">let</span> finished = <span class="hljs-number">0</span>;
6623 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
6624-title function_">runTask</span>({ <span class="hljs-attr">a</span>: <span class="hljs-number">42<…
6625 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
6626 <span class="hljs-keyword">if</span> (++finished === <span class="hljs-number">10</span>)
6627 pool.<span class="hljs-title function_">close</span>();
6636 approach can be applied to a <a href="#stream_stream"><code>Stream</code></a> or a similar event-dr…
6637 …lass="language-js"><span class="hljs-keyword">const</span> { createServer } = <span class="hljs-bu…
6638-keyword">const</span> { <span class="hljs-title class_">AsyncResource</span>, executionAsyncId } …
6640 …ass="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>(<s…
6641-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-title clas…
6642 <span class="hljs-comment">// Execution context is bound to the current outer scope.</span>
6644 …req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span c…
6645 …<span class="hljs-comment">// Execution context is bound to the scope that caused 'close' to emit.…
6647 res.<span class="hljs-title function_">end</span>();
6648 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code><…
6655 or any other asynchronous duration. It is similar to thread-local storage
6659 implementation that involves significant optimizations that are non-obvious to
6664 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
6665 …ljs-keyword">const</span> { <span class="hljs-title class_">AsyncLocalStorage</span> } = <span cla…
6667 <span class="hljs-keyword">const</span> asyncLocalStorage = <span class="hljs-keyword">new</span> <…
6669 <span class="hljs-keyword">function</span> <span class="hljs-title function_">logWithId</span>(<spa…
6670 …<span class="hljs-keyword">const</span> id = asyncLocalStorage.<span class="hljs-title function_">…
6671-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6674 <span class="hljs-keyword">let</span> idSeq = <span class="hljs-number">0</span>;
6675 http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span clas…
6676 …asyncLocalStorage.<span class="hljs-title function_">run</span>(idSeq++, <span class="hljs-functio…
6677 <span class="hljs-title function_">logWithId</span>(<span class="hljs-string">'start'</span>);
6678 <span class="hljs-comment">// Imagine any chain of async operations here</span>
6679 … <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> {
6680 … <span class="hljs-title function_">logWithId</span>(<span class="hljs-string">'finish'</span>);
6681 res.<span class="hljs-title function_">end</span>();
6684 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8080</span>);
6686 http.<span class="hljs-title function_">get</span>(<span class="hljs-string">'http://localhost:8080…
6687 http.<span class="hljs-title function_">get</span>(<span class="hljs-string">'http://localhost:8080…
6688 <span class="hljs-comment">// Prints:</span>
6689 <span class="hljs-comment">// 0: start</span>
6690 <span class="hljs-comment">// 1: start</span>
6691 <span class="hljs-comment">// 0: finish</span>
6692 <span class="hljs-comment">// 1: finish</span></code></pre>
6721 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
6732 <li><code>store</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
6738 …><code class="language-js"><span class="hljs-keyword">const</span> store = { <span class="hljs-att…
6739 <span class="hljs-comment">// Replaces previous store with the given store object</span>
6740 asyncLocalStorage.<span class="hljs-title function_">enterWith</span>(store);
6741 asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment">…
6742 <span class="hljs-title function_">someAsyncOperation</span>(<span class="hljs-function">() =></spa…
6743 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6751 …><code class="language-js"><span class="hljs-keyword">const</span> store = { <span class="hljs-att…
6753 emitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'my-event'</span>, <…
6754 asyncLocalStorage.<span class="hljs-title function_">enterWith</span>(store);
6756 emitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'my-event'</span>, <…
6757 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6760 asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment">…
6761 emitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'my-event'</span>);
6762 asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment">…
6768 <li><code>store</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
6769 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
6770 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6780 …><code class="language-js"><span class="hljs-keyword">const</span> store = { <span class="hljs-att…
6781 <span class="hljs-keyword">try</span> {
6782 …asyncLocalStorage.<span class="hljs-title function_">run</span>(store, <span class="hljs-function"…
6783 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6784 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
6785 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6786 }, <span class="hljs-number">200</span>);
6787 …<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-ti…
6789 } <span class="hljs-keyword">catch</span> (e) {
6790 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6791 <span class="hljs-comment">// The error will be caught here</span>
6798 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
6799 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6807 The stacktrace is not impacted by this call and the context is re-entered.</p>
6809 <pre><code class="language-js"><span class="hljs-comment">// Within a call to run</span>
6810 <span class="hljs-keyword">try</span> {
6811 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6812 …asyncLocalStorage.<span class="hljs-title function_">exit</span>(<span class="hljs-function">() =>…
6813 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6814 …<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-ti…
6816 } <span class="hljs-keyword">catch</span> (e) {
6817 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6818 <span class="hljs-comment">// The error will be caught here</span>
6823 …e-js"><span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span cla…
6824-keyword">await</span> asyncLocalStorage.<span class="hljs-title function_">run</span>(<span class…
6825 …lStorage.<span class="hljs-title function_">getStore</span>().<span class="hljs-title function_">s…
6826 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">foo</span>(); <span cl…
6837 <p>If your code is callback-based, it is enough to promisify it with
6839 <p>If you need to keep using callback-based API, or your code assumes
6845 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
6847 <p><code>Buffer</code> objects are used to represent a fixed-length sequence of bytes. Many
6849 …lass is a subclass of JavaScript's <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
6851 plain <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uin…
6854 <pre><code class="language-js"><span class="hljs-comment">// Creates a zero-filled Buffer of length…
6855 …"hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs
6857 <span class="hljs-comment">// Creates a Buffer of length 10,</span>
6858 <span class="hljs-comment">// filled with bytes which all have the value `1`.</span>
6859-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
6861 <span class="hljs-comment">// Creates an uninitialized buffer of length 10.</span>
6862 <span class="hljs-comment">// This is faster than calling Buffer.alloc() but the returned</span>
6863 <span class="hljs-comment">// Buffer instance might contain old data that needs to be</span>
6864 <span class="hljs-comment">// overwritten using fill(), write(), or other functions that fill the B…
6865 <span class="hljs-comment">// contents.</span>
6866 …js-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-ti…
6868 <span class="hljs-comment">// Creates a Buffer containing the bytes [1, 2, 3].</span>
6869-keyword">const</span> buf4 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
6871 <span class="hljs-comment">// Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries</spa…
6872 <span class="hljs-comment">// are all truncated using `(value &#x26; 255)` to fit into the range 0–…
6873-keyword">const</span> buf5 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
6875 <span class="hljs-comment">// Creates a Buffer containing the UTF-8-encoded bytes for the string 't…
6876 <span class="hljs-comment">// [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation)</span>
6877 <span class="hljs-comment">// [116, 195, 169, 115, 116] (in decimal notation)</span>
6878 …="hljs-keyword">const</span> buf6 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
6880 <span class="hljs-comment">// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74…
6881-keyword">const</span> buf7 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
6897 specified. If no character encoding is specified, UTF-8 will be used as the
6899-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
6901-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
6902 <span class="hljs-comment">// Prints: 68656c6c6f20776f726c64</span>
6903-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
6904 <span class="hljs-comment">// Prints: aGVsbG8gd29ybGQ=</span>
6906-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6907 <span class="hljs-comment">// Prints: &#x3C;Buffer 66 68 71 77 68 67 61 64 73></span>
6908-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6909 <span class="hljs-comment">// Prints: &#x3C;Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 …
6913 <p><code>'utf8'</code>: Multi-byte encoded Unicode characters. Many web pages and other
6914 document formats use <a href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. This is the default c…
6916 valid UTF-8 data, the Unicode replacement character <code>U+FFFD</code> � will be used
6920 <p><code>'utf16le'</code>: Multi-byte encoded Unicode characters. Unlike <code>'utf8'</code>, each
6922 …n.wikipedia.org/wiki/Endianness">little-endian</a> variant of <a href="https://en.wikipedia.org/wi…
6925 <p><code>'latin1'</code>: Latin-1 stands for <a href="https://en.wikipedia.org/wiki/ISO-8859-1">ISO
6933 <p>Node.js also supports the following binary-to-text encodings. For
6934 binary-to-text encodings, the naming convention is reversed: Converting a
6941 specified in <a href="https://tools.ietf.org/html/rfc4648#section-5">RFC 4648, Section 5</a>. White…
6942 tabs, and new lines contained within the base64-encoded string are ignored.</p>
6945 <p><code>'base64url'</code>: <a href="https://tools.ietf.org/html/rfc4648#section-5">base64url</a> …
6946 <a href="https://tools.ietf.org/html/rfc4648#section-5">RFC 4648, Section 5</a>. When creating a <c…
6947 encoding will also correctly accept regular base64-encoded strings. When
6959 <p><code>'ascii'</code>: For 7-bit <a href="https://en.wikipedia.org/wiki/ASCII">ASCII</a> data onl…
6964 (or, if the data is known to always be ASCII-only, <code>'latin1'</code>) will be a
6965 better choice when encoding or decoding ASCII-only text. It is only provided
6969 …ias for <code>'latin1'</code>. See <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMSt…
6972 between strings and <code>Buffer</code>s, typically <code>'utf-8'</code> is the right choice.</p>
6975 <p><code>'ucs2'</code>: Alias of <code>'utf16le'</code>. UCS-2 used to refer to a variant of UTF-16
6980-js"><span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(…
6981 <span class="hljs-comment">// Prints &#x3C;Buffer 1a>, data truncated when first non-hexadecimal va…
6982 <span class="hljs-comment">// ('g') encountered.</span>
6984 …ass="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="…
6985 <span class="hljs-comment">// Prints &#x3C;Buffer 1a>, data truncated when data ends in single digi…
6987 …ass="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="…
6988 <span class="hljs-comment">// Prints &#x3C;Buffer 16 34>, all data represented.</span></code></pre>
6990 both <code>'latin1'</code> and <code>'ISO-8859-1'</code> to <code>'win-1252'</code>. This means tha…
6993 <code>'win-1252'</code>-encoded data, and using <code>'latin1'</code> encoding may incorrectly deco…
7005 …la.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> …
7006 instances. All <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Ob…
7008 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArra…
7011 <li>While <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects…
7014 compatibility. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Ob…
7020 <p>There are two ways to create new <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
7022 <li>Passing a <code>Buffer</code> to a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
7026-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7027 …s="hljs-keyword">const</span> uint32array = <span class="hljs-keyword">new</span> <span class="hlj…
7029 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7031 <span class="hljs-comment">// Prints: Uint32Array(4) [ 1, 2, 3, 4 ]</span></code></pre>
7033 <li>Passing the <code>Buffer</code>s underlying <a href="https://developer.mozilla.org/en-US/docs/W…
7034 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArra…
7036-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7037 …s="hljs-keyword">const</span> uint16array = <span class="hljs-keyword">new</span> <span class="hlj…
7038 buf.<span class="hljs-property">buffer</span>,
7039 buf.<span class="hljs-property">byteOffset</span>,
7040 …class="hljs-property">length</span> / <span class="hljs-title class_">Uint</span>16<span class="hl…
7042 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7044 <span class="hljs-comment">// Prints: Uint16Array(5) [ 104, 101, 108, 108, 111 ]</span></code></pre>
7046 memory as a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objec…
7049-js"><span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span cla…
7051 arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>;
7052 arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>;
7054 <span class="hljs-comment">// Copies the contents of `arr`.</span>
7055 <span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span …
7057 <span class="hljs-comment">// Shares memory with `arr`.</span>
7058 …hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-
7060 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7061 <span class="hljs-comment">// Prints: &#x3C;Buffer 88 a0></span>
7062 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7063 <span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 a0 0f></span>
7065 arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>;
7067 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7068 <span class="hljs-comment">// Prints: &#x3C;Buffer 88 a0></span>
7069 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7070 <span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 70 17></span></code></pre>
7071 <p>When creating a <code>Buffer</code> using a <a href="https://developer.mozilla.org/en-US/docs/We…
7072 possible to use only a portion of the underlying <a href="https://developer.mozilla.org/en-US/docs/…
7074-js"><span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span cla…
7075-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title…
7077 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7078 <span class="hljs-comment">// Prints: 16</span></code></pre>
7079 <p>The <code>Buffer.from()</code> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
7080 implementations. Specifically, the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
7096-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7098 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> b <span class="hljs-
7099 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
7101 <span class="hljs-comment">// Prints:</span>
7102 <span class="hljs-comment">// 1</span>
7103 <span class="hljs-comment">// 2</span>
7104 <span class="hljs-comment">// 3</span></code></pre>
7111 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
7112 <p>A <a href="https://developer.mozilla.org/en-US/docs/Web/API/Blob"><code>Blob</code></a> encapsul…
7119-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="h…
7120-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a>,…
7122 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
7124 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7126 …//developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;…
7133-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a>,…
7141 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
7143 <p>Returns a promise that fulfills with an <a href="https://developer.mozilla.org/en-US/docs/Web/Ja…
7155 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
7156 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
7157 …s://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3…
7166 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
7168 <p>Returns a promise that resolves the contents of the <code>Blob</code> decoded as a UTF-8
7175 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
7177 <p>The content-type of the <code>Blob</code>.</p>
7183-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Blob</span> } = <sp…
7184 …class="hljs-keyword">const</span> blob = <span class="hljs-keyword">new</span> <span class="hljs-t…
7185 …s="hljs-keyword">const</span> { <span class="hljs-attr">setTimeout</span>: delay } = <span class="…
7187 <span class="hljs-keyword">const</span> mc1 = <span class="hljs-keyword">new</span> <span class="hl…
7188 <span class="hljs-keyword">const</span> mc2 = <span class="hljs-keyword">new</span> <span class="hl…
7190 mc1.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span c…
7191 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class=…
7192 mc1.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>();
7195 mc2.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span c…
7196 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">delay</span>(<span clas…
7197 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class=…
7198 mc2.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>();
7201 mc1.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(…
7202 mc2.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(…
7204 <span class="hljs-comment">// The Blob is still usable after posting.</span>
7205 …s-title function_">text</span>().<span class="hljs-title function_">then</span>(<span class="hljs-
7215 <td><p>Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exce…
7219 <td><p>Specifying an invalid string for <code>fill</code> now results in a zero-filled buffer.</p><…
7226 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
7227-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
7229 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7233 <code>Buffer</code> will be zero-filled.</p>
7234-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7236 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7237 <span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 00></span></code></pre>
7243-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7245 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7246 <span class="hljs-comment">// Prints: &#x3C;Buffer 61 61 61 61 61></span></code></pre>
7249-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7251 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7252 <span class="hljs-comment">// Prints: &#x3C;Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64></span></code><…
7271 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
7280-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7282 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7283 <span class="hljs-comment">// Prints (contents may vary): &#x3C;Buffer a0 8b 28 3f 01 00 00 00 50 3…
7285 buf.<span class="hljs-title function_">fill</span>(<span class="hljs-number">0</span>);
7287 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7288 <span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 00 00 00 00 00 00></span></code></pr…
7290 <p>The <code>Buffer</code> module pre-allocates an internal <code>Buffer</code> instance of
7296 <p>Use of this pre-allocated internal memory pool is a key difference between
7308 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
7312 is thrown. A zero-length <code>Buffer</code> is created if <code>size</code> is 0.</p>
7318 allocations under 4KB are sliced from a single pre-allocated <code>Buffer</code>. This
7325 to create an un-pooled <code>Buffer</code> instance using <code>Buffer.allocUnsafeSlow()</code> and
7327 <pre><code class="language-js"><span class="hljs-comment">// Need to keep around a few small chunks…
7328 <span class="hljs-keyword">const</span> store = [];
7330 socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <s…
7331 <span class="hljs-keyword">let</span> data;
7332 …<span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (data = readab…
7333 <span class="hljs-comment">// Allocate for retained data.</span>
7334 …s-keyword">const</span> sb = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title…
7336 <span class="hljs-comment">// Copy the data into the new allocation.</span>
7337 …an class="hljs-title function_">copy</span>(sb, <span class="hljs-number">0</span>, <span class="h…
7339 store.<span class="hljs-title function_">push</span>(sb);
7358-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
7360 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7362 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
7365 This is not the same as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
7368 For strings that contain non-base64/hex-encoded data (e.g. whitespace), the
7371 <pre><code class="language-js"><span class="hljs-keyword">const</span> str = <span class="hljs-stri…
7373-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
7374 …<span class="hljs-string">`<span class="hljs-subst">${Buffer.byteLength(str, <span class="hljs-str…
7375 <span class="hljs-comment">// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes</span></code></pre>
7376-US/docs/Web/JavaScript/Reference/Global_Objects/DataView"><code>DataView</code></a>/<a href="http…
7377 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArr…
7392 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
7393 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
7394 …://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C…
7400-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
7401 …="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
7402 <span class="hljs-keyword">const</span> arr = [buf1, buf2];
7404-variable language_">console</span>.<span class="hljs-title function_">log</span>(arr.<span class=…
7405 <span class="hljs-comment">// Prints: [ &#x3C;Buffer 30 31 32 33>, &#x3C;Buffer 31 32 33 34> ]</spa…
7406 <span class="hljs-comment">// (This result is equal to: [buf2, buf1].)</span></code></pre>
7420-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array[]></a> …
7422 <li><code>totalLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
7428 <p>If the list has no items, or if the <code>totalLength</code> is 0, then a new zero-length
7435 <pre><code class="language-js"><span class="hljs-comment">// Create a single `Buffer` from a list o…
7437 …"hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs
7438 …"hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs
7439 …"hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs
7440-keyword">const</span> totalLength = buf1.<span class="hljs-property">length</span> + buf2.<span c…
7442 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7443 <span class="hljs-comment">// Prints: 42</span>
7445 <span class="hljs-keyword">const</span> bufA = <span class="hljs-title class_">Buffer</span>.<span …
7447 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7448 <span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 ...></span>
7449 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7450 <span class="hljs-comment">// Prints: 42</span></code></pre>
7458 <li><code>array</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
7462 <pre><code class="language-js"><span class="hljs-comment">// Creates a new Buffer containing the UT…
7463-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title…
7473-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> …
7474 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArr…
7475 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArra…
7476 <li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
7477 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
7478 <strong>Default:</strong> <code>arrayBuffer.byteLength - byteOffset</code>.</li>
7480 <p>This creates a view of the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
7482 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArra…
7483 allocated memory as the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
7484-js"><span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span cla…
7486 arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>;
7487 arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>;
7489 <span class="hljs-comment">// Shares memory with `arr`.</span>
7490 …hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-t…
7492 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7493 <span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 a0 0f></span>
7495 <span class="hljs-comment">// Changing the original Uint16Array changes the Buffer also.</span>
7496 arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>;
7498 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7499 <span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 70 17></span></code></pre>
7502-js"><span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">new</span> <span clas…
7503-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title…
7505 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7506 <span class="hljs-comment">// Prints: 2</span></code></pre>
7507 …<code>arrayBuffer</code> is not an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
7508 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArr…
7514-js"><span class="hljs-keyword">const</span> arrA = <span class="hljs-title class_">Uint</span>8<s…
7515-keyword">const</span> arrB = <span class="hljs-keyword">new</span> <span class="hljs-title class_…
7516-variable language_">console</span>.<span class="hljs-title function_">log</span>(arrA.<span class…
7518 …hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-t…
7519 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7520 <span class="hljs-comment">// Prints: &#x3C;Buffer 63 64 65 66></span></code></pre>
7526-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> An…
7530-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
7531 <span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span …
7533 buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">0x61</span>;
7535 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7536 <span class="hljs-comment">// Prints: auffer</span>
7537 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7538 <span class="hljs-comment">// Prints: buffer</span></code></pre>
7546 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
7547-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="ht…
7548 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
7552-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7553 <span class="hljs-comment">// Prints: &#x3C;Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74></span…
7556 <pre><code class="language-js"><span class="hljs-keyword">class</span> <span class="hljs-title clas…
7557 [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toPrimitive</span>]() {
7558 <span class="hljs-keyword">return</span> <span class="hljs-string">'this is a test'</span>;
7562-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title…
7563 <span class="hljs-comment">// Prints: &#x3C;Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74></span…
7571 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
7572 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7576-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
7577-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
7579 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7580 <span class="hljs-comment">// Prints: this is a tést</span>
7581 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7582 <span class="hljs-comment">// Prints: this is a tést</span>
7583-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class…
7584 <span class="hljs-comment">// Prints: this is a tést</span></code></pre>
7592 <li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
7593 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
7596-js"><span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</sp…
7597-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-
7598 …hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="h…
7599 <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>([]…
7600-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-
7606 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7607 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
7611-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
7612 <span class="hljs-comment">// Prints: true</span>
7614-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
7615 <span class="hljs-comment">// Prints: true</span>
7617-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
7618 <span class="hljs-comment">// Prints: false</span>
7620-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
7621 <span class="hljs-comment">// Prints: false</span></code></pre>
7627 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
7629 <p>This is the size (in bytes) of pre-allocated internal <code>Buffer</code> instances used
7633 <li><code>index</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
7638 <p>This operator is inherited from <code>Uint8Array</code>, so its behavior on out-of-bounds
7643 <pre><code class="language-js"><span class="hljs-comment">// Copy an ASCII string into a `Buffer` o…
7644 <span class="hljs-comment">// (This only works for ASCII-only strings. In general, one should use</…
7645 <span class="hljs-comment">// `Buffer.from()` to perform this conversion.)</span>
7647 <span class="hljs-keyword">const</span> str = <span class="hljs-string">'Node.js'</span>;
7648 …s-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
7650 …class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-numbe…
7651 buf[i] = str.<span class="hljs-title function_">charCodeAt</span>(i);
7654-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7655 <span class="hljs-comment">// Prints: Node.js</span></code></pre>
7658 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array…
7663-js"><span class="hljs-keyword">const</span> arrayBuffer = <span class="hljs-keyword">new</span> <…
7664 <span class="hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<spa…
7666 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7667 <span class="hljs-comment">// Prints: true</span></code></pre>
7670 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
7680 <pre><code class="language-js"><span class="hljs-comment">// Create a buffer smaller than `Buffer.p…
7681-keyword">const</span> nodeBuffer = <span class="hljs-keyword">new</span> <span class="hljs-title …
7683 <span class="hljs-comment">// When casting the Node.js Buffer to an Int8Array, use the byteOffset</…
7684 <span class="hljs-comment">// to refer only to the part of `nodeBuffer.buffer` that contains the me…
7685 <span class="hljs-comment">// for `nodeBuffer`.</span>
7686-keyword">new</span> <span class="hljs-title class_">Int</span>8<span class="hljs-built_in">Array<…
7702-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> A …
7704 <li><code>targetStart</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
7706 <li><code>targetEnd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
7708 <li><code>sourceStart</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
7710 <li><code>sourceEnd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
7712 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
7720 <li><code>-1</code> is returned if <code>target</code> should come <em>after</em> <code>buf</code> …
7722-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
7723 …="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
7724 …="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
7726 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7727 <span class="hljs-comment">// Prints: 0</span>
7728 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7729 <span class="hljs-comment">// Prints: -1</span>
7730 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7731 <span class="hljs-comment">// Prints: -1</span>
7732 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7733 <span class="hljs-comment">// Prints: 1</span>
7734 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7735 <span class="hljs-comment">// Prints: 1</span>
7736-variable language_">console</span>.<span class="hljs-title function_">log</span>([buf1, buf2, buf…
7737 <span class="hljs-comment">// Prints: [ &#x3C;Buffer 41 42 43>, &#x3C;Buffer 41 42 43 44>, &#x3C;Bu…
7738 <span class="hljs-comment">// (This result is equal to: [buf1, buf3, buf2].)</span></code></pre>
7742-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
7743-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
7745-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class…
7746 <span class="hljs-comment">// Prints: 0</span>
7747-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class…
7748 <span class="hljs-comment">// Prints: -1</span>
7749-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class…
7750 <span class="hljs-comment">// Prints: 1</span></code></pre>
7758-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> A …
7759 <li><code>targetStart</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
7761 <li><code>sourceStart</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
7763 <li><code>sourceEnd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
7765 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
7769 <p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedA…
7772 <pre><code class="language-js"><span class="hljs-comment">// Create two `Buffer` instances.</span>
7773 …js-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-ti…
7774-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
7776 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
7777 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
7778 buf1[i] = i + <span class="hljs-number">97</span>;
7781 <span class="hljs-comment">// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `bu…
7782 …n class="hljs-title function_">copy</span>(buf2, <span class="hljs-number">8</span>, <span class="…
7783 <span class="hljs-comment">// This is equivalent to:</span>
7784 <span class="hljs-comment">// buf2.set(buf1.subarray(16, 20), 8);</span>
7786-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class…
7787 <span class="hljs-comment">// Prints: !!!!!!!!qrst!!!!!!!!!!!!!</span></code></pre>
7788 <pre><code class="language-js"><span class="hljs-comment">// Create a `Buffer` and copy data from o…
7789 <span class="hljs-comment">// within the same `Buffer`.</span>
7791 …ljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-ti…
7793 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
7794 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
7795 buf[i] = i + <span class="hljs-number">97</span>;
7798 …an class="hljs-title function_">copy</span>(buf, <span class="hljs-number">0</span>, <span class="…
7800 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7801 <span class="hljs-comment">// Prints: efghijghijklmnopqrstuvwxyz</span></code></pre>
7807 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
7809 <p>Creates and returns an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
7811 <pre><code class="language-js"><span class="hljs-comment">// Log the entire contents of a `Buffer`.…
7813 …="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs
7815 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> pair <span class="hljs-
7816 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
7818 <span class="hljs-comment">// Prints:</span>
7819 <span class="hljs-comment">// [0, 98]</span>
7820 <span class="hljs-comment">// [1, 117]</span>
7821 <span class="hljs-comment">// [2, 102]</span>
7822 <span class="hljs-comment">// [3, 102]</span>
7823 <span class="hljs-comment">// [4, 101]</span>
7824 <span class="hljs-comment">// [5, 114]</span></code></pre>
7838-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> A …
7840 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
7845-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
7846-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
7847 …="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
7849 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7850 <span class="hljs-comment">// Prints: true</span>
7851 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7852 <span class="hljs-comment">// Prints: false</span></code></pre>
7863 <td><p>Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exce…
7874-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
7875 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
7877 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
7879 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7885 <pre><code class="language-js"><span class="hljs-comment">// Fill a `Buffer` with the ASCII charact…
7887-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title f…
7889 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7890 <span class="hljs-comment">// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh</span></co…
7894 <p>If the final write of a <code>fill()</code> operation falls on a multi-byte character,
7896 <pre><code class="language-js"><span class="hljs-comment">// Fill a `Buffer` with character that ta…
7898-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
7899 <span class="hljs-comment">// Prints: &#x3C;Buffer c8 a2 c8 a2 c8></span></code></pre>
7902-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7904 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class…
7905 <span class="hljs-comment">// Prints: &#x3C;Buffer 61 61 61 61 61></span>
7906-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7907 <span class="hljs-comment">// Prints: &#x3C;Buffer aa aa aa aa aa></span>
7908-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7909 <span class="hljs-comment">// Throws an exception.</span></code></pre>
7915-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
7916 <li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
7918 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7920 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
7922 <p>Equivalent to <a href="#buffer_buf_indexof_value_byteoffset_encoding"><code>buf.indexOf() !== -1…
7923-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7925-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7926 <span class="hljs-comment">// Prints: true</span>
7927-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7928 <span class="hljs-comment">// Prints: true</span>
7929-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7930 <span class="hljs-comment">// Prints: true</span>
7931-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7932 <span class="hljs-comment">// Prints: true (97 is the decimal ASCII value for 'a')</span>
7933-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7934 <span class="hljs-comment">// Prints: false</span>
7935-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7936 <span class="hljs-comment">// Prints: true</span>
7937-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7938 <span class="hljs-comment">// Prints: false</span></code></pre>
7954-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
7955 <li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
7957 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7960 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
7961 <code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li>
7967 <li>a <code>Buffer</code> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer…
7969 <li>a number, <code>value</code> will be interpreted as an unsigned 8-bit integer
7972-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7974-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7975 <span class="hljs-comment">// Prints: 0</span>
7976-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7977 <span class="hljs-comment">// Prints: 2</span>
7978-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7979 <span class="hljs-comment">// Prints: 8</span>
7980-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7981 <span class="hljs-comment">// Prints: 8 (97 is the decimal ASCII value for 'a')</span>
7982-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7983 <span class="hljs-comment">// Prints: -1</span>
7984-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7985 <span class="hljs-comment">// Prints: 8</span>
7987-keyword">const</span> utf16Buffer = <span class="hljs-title class_">Buffer</span>.<span class="hl…
7989-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<spa…
7990 <span class="hljs-comment">// Prints: 4</span>
7991-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<spa…
7992 <span class="hljs-comment">// Prints: 6</span></code></pre>
7998 behavior matches <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_…
7999-js"><span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<sp…
8001 <span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span>
8002 <span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span>
8003-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8004-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8006 <span class="hljs-comment">// Passing a byteOffset that coerces to NaN or 0.</span>
8007 <span class="hljs-comment">// Prints: 1, searching the whole buffer.</span>
8008-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8009-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8010-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8011-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8020 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
8022 <p>Creates and returns an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
8023-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8025 …an class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> key <span class="hljs-
8026 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
8028 <span class="hljs-comment">// Prints:</span>
8029 <span class="hljs-comment">// 0</span>
8030 <span class="hljs-comment">// 1</span>
8031 <span class="hljs-comment">// 2</span>
8032 <span class="hljs-comment">// 3</span>
8033 <span class="hljs-comment">// 4</span>
8034 <span class="hljs-comment">// 5</span></code></pre>
8048-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
8049 <li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
8051 <code>buf.length - 1</code>.</li>
8052 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
8055 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8056 <code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li>
8060-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8062-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8063 <span class="hljs-comment">// Prints: 0</span>
8064-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8065 <span class="hljs-comment">// Prints: 17</span>
8066-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8067 <span class="hljs-comment">// Prints: 17</span>
8068-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8069 <span class="hljs-comment">// Prints: 15 (97 is the decimal ASCII value for 'a')</span>
8070-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8071 <span class="hljs-comment">// Prints: -1</span>
8072-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8073 <span class="hljs-comment">// Prints: 5</span>
8074-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8075 <span class="hljs-comment">// Prints: -1</span>
8077-keyword">const</span> utf16Buffer = <span class="hljs-title class_">Buffer</span>.<span class="hl…
8079-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<spa…
8080 <span class="hljs-comment">// Prints: 6</span>
8081-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<spa…
8082 <span class="hljs-comment">// Prints: 4</span></code></pre>
8088 This behavior matches <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
8089-js"><span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<sp…
8091 <span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span>
8092 <span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span>
8093-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8094-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8096 <span class="hljs-comment">// Passing a byteOffset that coerces to NaN.</span>
8097 <span class="hljs-comment">// Prints: 1, searching the whole buffer.</span>
8098-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8099-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8101 <span class="hljs-comment">// Passing a byteOffset that coerces to 0.</span>
8102 <span class="hljs-comment">// Prints: -1, equivalent to passing 0.</span>
8103-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8104-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8111 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
8114 <pre><code class="language-js"><span class="hljs-comment">// Create a `Buffer` and write a shorter …
8116 …="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs
8118 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8119 <span class="hljs-comment">// Prints: 1234</span>
8121 …lass="hljs-title function_">write</span>(<span class="hljs-string">'some string'</span>, <span cla…
8123 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8124 <span class="hljs-comment">// Prints: 1234</span></code></pre>
8129 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
8136 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8137 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
8138 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
8140 <p>Reads a signed, big-endian 64-bit integer from <code>buf</code> at the specified <code>offset</c…
8148 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8149 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
8150 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
8152 <p>Reads a signed, little-endian 64-bit integer from <code>buf</code> at the specified
8169 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8170 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
8171 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
8173 <p>Reads an unsigned, big-endian 64-bit integer from <code>buf</code> at the specified
8176-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8178-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8179 <span class="hljs-comment">// Prints: 4294967295n</span></code></pre>
8193 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8194 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
8195 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
8197 <p>Reads an unsigned, little-endian 64-bit integer from <code>buf</code> at the specified
8200-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8202-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8203 <span class="hljs-comment">// Prints: 18446744069414584320n</span></code></pre>
8217 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8218 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</co…
8219 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8221 <p>Reads a 64-bit, big-endian double from <code>buf</code> at the specified <code>offset</code>.</p>
8222-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8224-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8225 <span class="hljs-comment">// Prints: 8.20788039913184e-304</span></code></pre>
8239 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8240 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</co…
8241 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8243 <p>Reads a 64-bit, little-endian double from <code>buf</code> at the specified <code>offset</code>.…
8244-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8246-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8247 <span class="hljs-comment">// Prints: 5.447603722011605e-270</span>
8248-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8249 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8263 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8264 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
8265 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8267 <p>Reads a 32-bit, big-endian float from <code>buf</code> at the specified <code>offset</code>.</p>
8268-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8270-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8271 <span class="hljs-comment">// Prints: 2.387939260590663e-38</span></code></pre>
8285 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8286 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
8287 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8289 <p>Reads a 32-bit, little-endian float from <code>buf</code> at the specified <code>offset</code>.<…
8290-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8292-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8293 <span class="hljs-comment">// Prints: 1.539989614439558e-36</span>
8294-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8295 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8309 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8310 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</co…
8311 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8313 <p>Reads a signed 8-bit integer from <code>buf</code> at the specified <code>offset</code>.</p>
8315-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8317-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8318 <span class="hljs-comment">// Prints: -1</span>
8319-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8320 <span class="hljs-comment">// Prints: 5</span>
8321-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8322 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8336 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8337 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
8338 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8340 <p>Reads a signed, big-endian 16-bit integer from <code>buf</code> at the specified <code>offset</c…
8342-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8344-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8345 <span class="hljs-comment">// Prints: 5</span></code></pre>
8359 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8360 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
8361 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8363 <p>Reads a signed, little-endian 16-bit integer from <code>buf</code> at the specified
8366-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8368-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8369 <span class="hljs-comment">// Prints: 1280</span>
8370-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8371 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8385 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8386 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
8387 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8389 <p>Reads a signed, big-endian 32-bit integer from <code>buf</code> at the specified <code>offset</c…
8391-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8393-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8394 <span class="hljs-comment">// Prints: 5</span></code></pre>
8408 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8409 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
8410 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8412 <p>Reads a signed, little-endian 32-bit integer from <code>buf</code> at the specified
8415-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8417-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8418 <span class="hljs-comment">// Prints: 83886080</span>
8419-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8420 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8434 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8435 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
8436 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
8438 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8441 and interprets the result as a big-endian, two's complement signed value
8443-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8445-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8446 <span class="hljs-comment">// Prints: 1234567890ab</span>
8447-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8448 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span>
8449-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8450 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8464 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8465 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
8466 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
8468 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8471 and interprets the result as a little-endian, two's complement signed value
8473-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8475-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8476 <span class="hljs-comment">// Prints: -546f87a9cbee</span></code></pre>
8492 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8493 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</co…
8494 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8496 <p>Reads an unsigned 8-bit integer from <code>buf</code> at the specified <code>offset</code>.</p>
8498-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8500-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8501 <span class="hljs-comment">// Prints: 1</span>
8502-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8503 <span class="hljs-comment">// Prints: 254</span>
8504-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8505 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8521 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8522 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
8523 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8525 <p>Reads an unsigned, big-endian 16-bit integer from <code>buf</code> at the specified
8528-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8530-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8531 <span class="hljs-comment">// Prints: 1234</span>
8532-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8533 <span class="hljs-comment">// Prints: 3456</span></code></pre>
8549 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8550 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
8551 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8553 <p>Reads an unsigned, little-endian 16-bit integer from <code>buf</code> at the specified
8556-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8558-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8559 <span class="hljs-comment">// Prints: 3412</span>
8560-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8561 <span class="hljs-comment">// Prints: 5634</span>
8562-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8563 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8579 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8580 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
8581 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8583 <p>Reads an unsigned, big-endian 32-bit integer from <code>buf</code> at the specified
8586-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8588-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8589 <span class="hljs-comment">// Prints: 12345678</span></code></pre>
8605 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8606 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
8607 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8609 <p>Reads an unsigned, little-endian 32-bit integer from <code>buf</code> at the specified
8612-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8614-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8615 <span class="hljs-comment">// Prints: 78563412</span>
8616-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8617 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8633 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8634 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
8635 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
8637 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8640 and interprets the result as an unsigned big-endian integer supporting
8643-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8645-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8646 <span class="hljs-comment">// Prints: 1234567890ab</span>
8647-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8648 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8664 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8665 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
8666 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
8668 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8671 and interprets the result as an unsigned, little-endian integer supporting
8674-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8676-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8677 <span class="hljs-comment">// Prints: ab9078563412</span></code></pre>
8683 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
8684 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
8692 <p>This method is inherited from <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
8695 <pre><code class="language-js"><span class="hljs-comment">// Create a `Buffer` with the ASCII alpha…
8696 <span class="hljs-comment">// from the original `Buffer`.</span>
8698 …js-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-ti…
8700 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
8701 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
8702 buf1[i] = i + <span class="hljs-number">97</span>;
8705 …="hljs-keyword">const</span> buf2 = buf1.<span class="hljs-title function_">subarray</span>(<span …
8707-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class…
8708 <span class="hljs-comment">// Prints: abc</span>
8710 buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">33</span>;
8712-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class…
8713 <span class="hljs-comment">// Prints: !bc</span></code></pre>
8716-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8718-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8719 <span class="hljs-comment">// Prints: buffe</span>
8720 <span class="hljs-comment">// (Equivalent to buf.subarray(0, 5).)</span>
8722-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8723 <span class="hljs-comment">// Prints: buff</span>
8724 <span class="hljs-comment">// (Equivalent to buf.subarray(0, 4).)</span>
8726-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8727 <span class="hljs-comment">// Prints: uff</span>
8728 <span class="hljs-comment">// (Equivalent to buf.subarray(1, 4).)</span></code></pre>
8737 <td><p>Coercing the offsets to integers now handles values outside the 32-bit integer range properl…
8744 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
8745 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
8755-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8757-keyword">const</span> copiedBuf = <span class="hljs-title class_">Uint</span>8<span class="hljs-b…
8758 copiedBuf[<span class="hljs-number">0</span>]++;
8759 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8760 <span class="hljs-comment">// Prints: cuffer</span>
8762 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8763 <span class="hljs-comment">// Prints: buffer</span></code></pre>
8771 <p>Interprets <code>buf</code> as an array of unsigned 16-bit integers and swaps the
8772 byte order <em>in-place</em>. Throws <a href="#ERR_INVALID_BUFFER_SIZE"><code>ERR_INVALID_BUFFER_SI…
8774-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
8776 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8777 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span>
8779 buf1.<span class="hljs-title function_">swap16</span>();
8781 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8782 <span class="hljs-comment">// Prints: &#x3C;Buffer 02 01 04 03 06 05 08 07></span>
8784-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
8786 buf2.<span class="hljs-title function_">swap16</span>();
8787 <span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code></pre>
8788 <p>One convenient use of <code>buf.swap16()</code> is to perform a fast in-place conversion
8789 between UTF-16 little-endian and UTF-16 big-endian:</p>
8790-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8791 buf.<span class="hljs-title function_">swap16</span>(); <span class="hljs-comment">// Convert to bi…
8799 <p>Interprets <code>buf</code> as an array of unsigned 32-bit integers and swaps the
8800 byte order <em>in-place</em>. Throws <a href="#ERR_INVALID_BUFFER_SIZE"><code>ERR_INVALID_BUFFER_SI…
8802-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
8804 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8805 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span>
8807 buf1.<span class="hljs-title function_">swap32</span>();
8809 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8810 <span class="hljs-comment">// Prints: &#x3C;Buffer 04 03 02 01 08 07 06 05></span>
8812-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
8814 buf2.<span class="hljs-title function_">swap32</span>();
8815 <span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code></pre>
8823 <p>Interprets <code>buf</code> as an array of 64-bit numbers and swaps byte order <em>in-place</em>.
8825-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
8827 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8828 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span>
8830 buf1.<span class="hljs-title function_">swap64</span>();
8832 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8833 <span class="hljs-comment">// Prints: &#x3C;Buffer 08 07 06 05 04 03 02 01></span>
8835-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
8837 buf2.<span class="hljs-title function_">swap64</span>();
8838 <span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code></pre>
8844 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
8846 <p>Returns a JSON representation of <code>buf</code>. <a href="https://developer.mozilla.org/en-US/…
8850-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8851 <span class="hljs-keyword">const</span> json = <span class="hljs-variable constant_">JSON</span>.<s…
8853 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8854 <span class="hljs-comment">// Prints: {"type":"Buffer","data":[1,2,3,4,5]}</span>
8856-keyword">const</span> copy = <span class="hljs-variable constant_">JSON</span>.<span class="hljs-
8857 …<span class="hljs-keyword">return</span> value &#x26;&#x26; value.<span class="hljs-property">type…
8858 …<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(value…
8862 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8863 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05></span></code></pre>
8869 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
8870 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
8871 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
8873 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
8877 …f <code>encoding</code> is <code>'utf8'</code> and a byte sequence in the input is not valid UTF-8,
8879 <p>The maximum length of a string instance (in UTF-16 code units) is available
8881-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
8883 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
8884 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
8885 buf1[i] = i + <span class="hljs-number">97</span>;
8888-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class…
8889 <span class="hljs-comment">// Prints: abcdefghijklmnopqrstuvwxyz</span>
8890-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class…
8891 <span class="hljs-comment">// Prints: abcde</span>
8893 …="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
8895-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class…
8896 <span class="hljs-comment">// Prints: 74c3a97374</span>
8897-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class…
8898 <span class="hljs-comment">// Prints: té</span>
8899-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class…
8900 <span class="hljs-comment">// Prints: té</span></code></pre>
8906 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
8908 <p>Creates and returns an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
8910-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8912 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="hljs
8913 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
8915 <span class="hljs-comment">// Prints:</span>
8916 <span class="hljs-comment">// 98</span>
8917 <span class="hljs-comment">// 117</span>
8918 <span class="hljs-comment">// 102</span>
8919 <span class="hljs-comment">// 102</span>
8920 <span class="hljs-comment">// 101</span>
8921 <span class="hljs-comment">// 114</span>
8923 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="h…
8924 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
8926 <span class="hljs-comment">// Prints:</span>
8927 <span class="hljs-comment">// 98</span>
8928 <span class="hljs-comment">// 117</span>
8929 <span class="hljs-comment">// 102</span>
8930 <span class="hljs-comment">// 102</span>
8931 <span class="hljs-comment">// 101</span>
8932 <span class="hljs-comment">// 114</span></code></pre>
8938 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8939 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8941 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8942 exceed <code>buf.length - offset</code>). <strong>Default:</strong> <code>buf.length - offset</code…
8943 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
8944 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8950-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8952-keyword">const</span> len = buf.<span class="hljs-title function_">write</span>(<span class="hljs
8954-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
8955 <span class="hljs-comment">// Prints: 12 bytes: ½ + ¼ = ¾</span>
8957 …hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
8959 …ljs-keyword">const</span> length = buffer.<span class="hljs-title function_">write</span>(<span cl…
8961-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
8962 <span class="hljs-comment">// Prints: 2 bytes : ab</span></code></pre>
8968 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
8969 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8970 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
8971 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8973 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
8975-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8977 …uf.<span class="hljs-title function_">writeBigInt64BE</span>(<span class="hljs-number">0x010203040…
8979 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8980 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span></code></pre>
8986 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
8987 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8988 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
8989 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8991 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
8993-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8995 …uf.<span class="hljs-title function_">writeBigInt64LE</span>(<span class="hljs-number">0x010203040…
8997 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8998 <span class="hljs-comment">// Prints: &#x3C;Buffer 08 07 06 05 04 03 02 01></span></code></pre>
9012 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
9013 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9014 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
9015 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9017 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9019-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9021 …f.<span class="hljs-title function_">writeBigUInt64BE</span>(<span class="hljs-number">0xdecafafec…
9023 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9024 <span class="hljs-comment">// Prints: &#x3C;Buffer de ca fa fe ca ce fa de></span></code></pre>
9038 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
9039 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9040 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
9041 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9043 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9044-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9046 …f.<span class="hljs-title function_">writeBigUInt64LE</span>(<span class="hljs-number">0xdecafafec…
9048 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9049 <span class="hljs-comment">// Prints: &#x3C;Buffer de fa ce ca fe fa ca de></span></code></pre>
9064 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9065 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9066 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</co…
9067 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9069 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9072-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9074 buf.<span class="hljs-title function_">writeDoubleBE</span>(<span class="hljs-number">123.456</span…
9076 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9077 <span class="hljs-comment">// Prints: &#x3C;Buffer 40 5e dd 2f 1a 9f be 77></span></code></pre>
9091 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9092 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9093 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</co…
9094 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9096 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9099-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9101 buf.<span class="hljs-title function_">writeDoubleLE</span>(<span class="hljs-number">123.456</span…
9103 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9104 <span class="hljs-comment">// Prints: &#x3C;Buffer 77 be 9f 1a 2f dd 5e 40></span></code></pre>
9118 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9119 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9120 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
9121 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9123 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9125-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9127 buf.<span class="hljs-title function_">writeFloatBE</span>(<span class="hljs-number">0xcafebabe</sp…
9129 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9130 <span class="hljs-comment">// Prints: &#x3C;Buffer 4f 4a fe bb></span></code></pre>
9144 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9145 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9146 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
9147 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9149 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9151-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9153 buf.<span class="hljs-title function_">writeFloatLE</span>(<span class="hljs-number">0xcafebabe</sp…
9155 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9156 <span class="hljs-comment">// Prints: &#x3C;Buffer bb fe 4a 4f></span></code></pre>
9170 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9171 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9172 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</co…
9173 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9176 signed 8-bit integer. Behavior is undefined when <code>value</code> is anything other than
9177 a signed 8-bit integer.</p>
9179-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9181 buf.<span class="hljs-title function_">writeInt8</span>(<span class="hljs-number">2</span>, <span c…
9182 buf.<span class="hljs-title function_">writeInt8</span>(-<span class="hljs-number">2</span>, <span …
9184 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9185 <span class="hljs-comment">// Prints: &#x3C;Buffer 02 fe></span></code></pre>
9199 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9200 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9201 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
9202 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9204 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9205 must be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is
9206 anything other than a signed 16-bit integer.</p>
9208-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9210 buf.<span class="hljs-title function_">writeInt16BE</span>(<span class="hljs-number">0x0102</span>,…
9212 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9213 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02></span></code></pre>
9227 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9228 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9229 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
9230 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9232 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9233 must be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is
9234 anything other than a signed 16-bit integer.</p>
9236-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9238 buf.<span class="hljs-title function_">writeInt16LE</span>(<span class="hljs-number">0x0304</span>,…
9240 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9241 <span class="hljs-comment">// Prints: &#x3C;Buffer 04 03></span></code></pre>
9255 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9256 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9257 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
9258 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9260 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9261 must be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is
9262 anything other than a signed 32-bit integer.</p>
9264-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9266 buf.<span class="hljs-title function_">writeInt32BE</span>(<span class="hljs-number">0x01020304</sp…
9268 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9269 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04></span></code></pre>
9283 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9284 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9285 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
9286 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9288 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9289 must be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is
9290 anything other than a signed 32-bit integer.</p>
9292-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9294 buf.<span class="hljs-title function_">writeInt32LE</span>(<span class="hljs-number">0x05060708</sp…
9296 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9297 <span class="hljs-comment">// Prints: &#x3C;Buffer 08 07 06 05></span></code></pre>
9311 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9312 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9313 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
9314 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
9316 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9319 as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined when
9321-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9323 …s="hljs-title function_">writeIntBE</span>(<span class="hljs-number">0x1234567890ab</span>, <span …
9325 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9326 <span class="hljs-comment">// Prints: &#x3C;Buffer 12 34 56 78 90 ab></span></code></pre>
9340 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9341 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9342 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
9343 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
9345 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9348 as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined
9350-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9352 …s="hljs-title function_">writeIntLE</span>(<span class="hljs-number">0x1234567890ab</span>, <span …
9354 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9355 <span class="hljs-comment">// Prints: &#x3C;Buffer ab 90 78 56 34 12></span></code></pre>
9371 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9372 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9373 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</co…
9374 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9377 valid unsigned 8-bit integer. Behavior is undefined when <code>value</code> is anything
9378 other than an unsigned 8-bit integer.</p>
9380-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9382 buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x3</span>, <spa…
9383 buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x4</span>, <spa…
9384 buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x23</span>, <sp…
9385 buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x42</span>, <sp…
9387 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9388 <span class="hljs-comment">// Prints: &#x3C;Buffer 03 04 23 42></span></code></pre>
9404 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9405 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9406 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
9407 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9409 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9410 must be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code>
9411 is anything other than an unsigned 16-bit integer.</p>
9413-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9415 buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xdead</span>…
9416 buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xbeef</span>…
9418 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9419 <span class="hljs-comment">// Prints: &#x3C;Buffer de ad be ef></span></code></pre>
9435 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9436 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9437 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
9438 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9440 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9441 must be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code> is
9442 anything other than an unsigned 16-bit integer.</p>
9444-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9446 buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xdead</span>…
9447 buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xbeef</span>…
9449 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9450 <span class="hljs-comment">// Prints: &#x3C;Buffer ad de ef be></span></code></pre>
9466 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9467 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9468 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
9469 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9471 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9472 must be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code>
9473 is anything other than an unsigned 32-bit integer.</p>
9475-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9477 buf.<span class="hljs-title function_">writeUInt32BE</span>(<span class="hljs-number">0xfeedface</s…
9479 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9480 <span class="hljs-comment">// Prints: &#x3C;Buffer fe ed fa ce></span></code></pre>
9496 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9497 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9498 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
9499 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9501 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9502 must be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code> is
9503 anything other than an unsigned 32-bit integer.</p>
9505-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9507 buf.<span class="hljs-title function_">writeUInt32LE</span>(<span class="hljs-number">0xfeedface</s…
9509 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9510 <span class="hljs-comment">// Prints: &#x3C;Buffer ce fa ed fe></span></code></pre>
9526 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9527 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9528 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
9529 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
9531 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9534 as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined
9537-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9539 …="hljs-title function_">writeUIntBE</span>(<span class="hljs-number">0x1234567890ab</span>, <span …
9541 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9542 <span class="hljs-comment">// Prints: &#x3C;Buffer 12 34 56 78 90 ab></span></code></pre>
9558 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9559 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9560 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
9561 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
9563 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9566 as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined
9569-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9571 …="hljs-title function_">writeUIntLE</span>(<span class="hljs-number">0x1234567890ab</span>, <span …
9573 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9574 <span class="hljs-comment">// Prints: &#x3C;Buffer ab 90 78 56 34 12></span></code></pre>
9591 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
9593 <li><code>array</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9616 …bility api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use
9620-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> …
9621-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code>…
9622 <li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
9623 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9624 <strong>Default:</strong> <code>arrayBuffer.byteLength - byteOffset</code>.</li>
9644 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
9646-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> An…
9658 <td><p>The <code>new Buffer(size)</code> will return zero-filled memory by default.</p></td></tr>
9668 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
9671 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
9691 …_stability api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated:
9694 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9695 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
9700 <code>Buffer</code>-related APIs that are available only via the <code>buffer</code> module
9707 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#…
9709 <p>Decodes a string of Base64-encoded data into bytes, and encodes those bytes
9710 into a string using Latin-1 (ISO-8859-1).</p>
9711 <p>The <code>data</code> may be any JavaScript-value that can be coerced into a string.</p>
9715 For code running using Node.js APIs, converting between base64-encoded strings
9723 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
9725 <p>Decodes a string into bytes using Latin-1 (ISO-8859), and encodes those bytes
9727 <p>The <code>data</code> may be any JavaScript-value that can be coerced into a string.</p>
9731 For code running using Node.js APIs, converting between base64-encoded strings
9739 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
9749 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
9757 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
9773 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
9774 <li><code>fromEnc</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
9775 <li><code>toEnc</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9778 <p>Re-encodes the given <code>Buffer</code> or <code>Uint8Array</code> instance from one character
9786 …ode class="language-js"><span class="hljs-keyword">const</span> buffer = <span class="hljs-built_i…
9788-keyword">const</span> newBuf = buffer.<span class="hljs-title function_">transcode</span>(<span c…
9789-variable language_">console</span>.<span class="hljs-title function_">log</span>(newBuf.<span cla…
9790 <span class="hljs-comment">// Prints: '?'</span></code></pre>
9791 <p>Because the Euro (<code>€</code>) sign is not representable in US-ASCII, it is replaced
9797 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
9805 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
9807 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
9820 <td><p>Value is changed from 2<sup>31</sup> - 1 to 2<sup>32</sup> - 1 on 64-bit architectures.</p><…
9827 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
9829 <p>On 32-bit architectures, this value currently is 2<sup>30</sup> - 1 (~1GB).</p>
9830 <p>On 64-bit architectures, this value currently is 2<sup>32</sup> - 1 (~4GB).</p>
9838 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
9841 in UTF-16 code units.</p>
9856 required between creating a fast-but-uninitialized <code>Buffer</code> versus creating a
9857 slower-but-safer <code>Buffer</code>. Since Node.js 8.0.0, <code>Buffer(num)</code> and <code>new B…
9860 ….org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> …
9875 arbitrary pre-existing in-memory data, so may be used to expose in-memory
9877 occur because the data is zero-filled. However, other attacks are still
9880 <p>To make the creation of <code>Buffer</code> instances more reliable and less error-prone,
9891 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuff…
9912 <h4>The <code>--zero-fill-buffers</code> command-line option<span><a class="mark" href="#buffer_the…
9916 <p>Node.js can be started using the <code>--zero-fill-buffers</code> command-line option to
9917 cause all newly-allocated <code>Buffer</code> instances to be zero-filled upon creation by
9919 …ode>Buffer.allocUnsafeSlow()</code></a>, and <code>new SlowBuffer(size)</code> are not zero-filled.
9921 <code>--zero-fill-buffers</code> option only when necessary to enforce that newly allocated
9923 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
9924 <span class="hljs-meta">> </span><span class="language-bash">Buffer.allocUnsafe(5);</span>
9928 segment of allocated memory is <em>uninitialized</em> (it is not zeroed-out). While
9940 <p><em>Addons</em> are dynamically-linked shared objects written in C++. The
9943 <p>There are three options for implementing addons: Node-API, nan, or direct
9945 direct access to functionality which is not exposed by Node-API, use Node-API.
9946 Refer to <a href="n-api.html">C/C++ addons with Node-API</a> for more information on
9947 Node-API.</p>
9948 <p>When not using Node-API, implementing addons is complicated,
9961 serves as a cross-platform abstraction library, giving easy, POSIX-like
9967 avoid blocking the event loop with I/O or other time-intensive tasks by
9968 offloading work via libuv to non-blocking system operations, worker threads,
9979 re-exported by Node.js and may be used to various extents by addons. See
9983 <p>All of the following examples are available for <a href="https://github.com/nodejs/node-addon-ex…
9984 be used as the starting-point for an addon.</p>
9988-js"><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span…
9990 <pre><code class="language-cpp"><span class="hljs-comment">// hello.cc</span>
9991 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
9993 <span class="hljs-keyword">namespace</span> demo {
9995 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
9996 <span class="hljs-keyword">using</span> v8::Isolate;
9997 <span class="hljs-keyword">using</span> v8::Local;
9998 <span class="hljs-keyword">using</span> v8::Object;
9999 <span class="hljs-keyword">using</span> v8::String;
10000 <span class="hljs-keyword">using</span> v8::Value;
10002 …class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Method</span><s…
10003 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10004 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(St…
10005 …isolate, <span class="hljs-string">"world"</span>).<span class="hljs-built_in">ToLocalChecked</spa…
10008 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Initialize…
10009 …<span class="hljs-built_in">NODE_SET_METHOD</span>(exports, <span class="hljs-string">"hello"</spa…
10012 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Initialize)
10014 } <span class="hljs-comment">// namespace demo</span></code></pre>
10017 …s="language-cpp"><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs
10018 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Initialize)</code></pre>
10019 <p>There is no semi-colon after <code>NODE_MODULE</code> as it's not a function (see
10025 <p>When building addons with <code>node-gyp</code>, using the macro <code>NODE_GYP_MODULE_NAME</cod…
10028 <h4>Context-aware addons<span><a class="mark" href="#addons_context_aware_addons" id="addons_contex…
10035 <p>A context-aware addon can be constructed by using the macro
10039 <pre><code class="language-cpp"><span class="hljs-keyword">using</span> <span class="hljs-keyword">…
10041 …n class="hljs-keyword">extern</span> <span class="hljs-string">"C"</span> <span class="hljs-functi…
10042 <span class="hljs-title">NODE_MODULE_INITIALIZER</span><span class="hljs-params">(Local&#x3C;Object…
10043 Local&#x3C;Value> <span class="hljs-keyword">module</span>,
10045 <span class="hljs-comment">/* Perform addon initialization steps here. */</span>
10048 construct a context-aware addon. Unlike <code>NODE_MODULE()</code>, which is used to
10059 <p>The choice to build a context-aware addon carries with it the responsibility of
10067 <p>The context-aware addon can be structured to avoid global static data by
10070 <li>Define a class which will hold per-addon-instance data and which has a static
10072-cpp"><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-type"…
10073 <span class="hljs-comment">// Cast `data` to an instance of the class and delete it.</span>
10076 <li>Heap-allocate an instance of this class in the addon initializer. This can be
10078 <li>Call <code>node::AddEnvironmentCleanupHook()</code>, passing it the above-created
10084 native-backed JavaScript functions. The third parameter of
10089 <p>This will ensure that the per-addon-instance data reaches each binding that can
10090 be called from JavaScript. The per-addon-instance data must also be passed into
10092 <p>The following example illustrates the implementation of a context-aware addon:</p>
10093 <pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> …
10095 <span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> v8;
10097 <span class="hljs-keyword">class</span> <span class="hljs-title class_">AddonData</span> {
10098 <span class="hljs-keyword">public</span>:
10099 …<span class="hljs-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">Ad…
10100 call_count(<span class="hljs-number">0</span>) {</span>
10101 …<span class="hljs-comment">// Ensure this per-addon-instance data is deleted at environment cleanu…
10102 …node::<span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, DeleteInstance, <span …
10105 <span class="hljs-comment">// Per-addon data.</span>
10106 <span class="hljs-type">int</span> call_count;
10108-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10109 …<span class="hljs-keyword">delete</span> <span class="hljs-built_in">static_cast</span>&#x3C;Addon…
10113-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10114 <span class="hljs-comment">// Retrieve the per-addon-instance data.</span>
10116-built_in">reinterpret_cast</span>&#x3C;AddonData*>(info.<span class="hljs-built_in">Data</span>()…
10117 data->call_count++;
10118 …<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>((<span …
10121 <span class="hljs-comment">// Initialize this addon to be context-aware.</span>
10122 <span class="hljs-built_in">NODE_MODULE_INIT</span>(<span class="hljs-comment">/* exports, module, …
10123 Isolate* isolate = context-><span class="hljs-built_in">GetIsolate</span>();
10125 …<span class="hljs-comment">// Create a new instance of `AddonData` for this instance of the addon …
10126 <span class="hljs-comment">// tie its life cycle to that of the Node.js environment.</span>
10127 …AddonData* data = <span class="hljs-keyword">new</span> <span class="hljs-built_in">AddonData</spa…
10129 …<span class="hljs-comment">// Wrap the data in a `v8::External` so we can pass it to the method we…
10130 <span class="hljs-comment">// expose.</span>
10131 Local&#x3C;External> external = External::<span class="hljs-built_in">New</span>(isolate, data);
10133 …<span class="hljs-comment">// Expose the method `Method` to JavaScript, and make sure it receives …
10134 …<span class="hljs-comment">// per-addon-instance data we created above by passing `external` as th…
10135 <span class="hljs-comment">// third parameter to the `FunctionTemplate` constructor.</span>
10136 exports-><span class="hljs-built_in">Set</span>(context,
10137 …String::<span class="hljs-built_in">NewFromUtf8</span>(isolate, <span class="hljs-string">"method"…
10138 FunctionTemplate::<span class="hljs-built_in">New</span>(isolate, Method, external)
10139-><span class="hljs-built_in">GetFunction</span>(context).<span class="hljs-built_in">ToLocalCheck…
10152 such as a main thread and a Worker thread, an add-on needs to either:</p>
10154 <li>Be an Node-API addon, or</li>
10155 <li>Be declared as context-aware using <code>NODE_MODULE_INIT()</code> as described above</li>
10160 …uage-cpp"><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title"…
10161 … <span class="hljs-type">void</span> (*fun)(<span class="hljs-type">void</span>* arg),
10162 <span class="hljs-type">void</span>* arg)</span></span>;</code></pre>
10166 run in last-in first-out order.</p>
10172 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10173 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10174 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10175 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10177 <span class="hljs-keyword">using</span> node::AddEnvironmentCleanupHook;
10178 <span class="hljs-keyword">using</span> v8::HandleScope;
10179 <span class="hljs-keyword">using</span> v8::Isolate;
10180 <span class="hljs-keyword">using</span> v8::Local;
10181 <span class="hljs-keyword">using</span> v8::Object;
10183 <span class="hljs-comment">// Note: In a real-world application, do not rely on static/global data.…
10184 <span class="hljs-keyword">static</span> <span class="hljs-type">char</span> cookie[] = <span class…
10185 <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> cleanup_cb1_called = <s…
10186 <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> cleanup_cb2_called = <s…
10188-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10189 Isolate* isolate = <span class="hljs-built_in">static_cast</span>&#x3C;Isolate*>(arg);
10190 …<span class="hljs-function">HandleScope <span class="hljs-title">scope</span><span class="hljs-par…
10191 Local&#x3C;Object> obj = Object::<span class="hljs-built_in">New</span>(isolate);
10192 …<span class="hljs-built_in">assert</span>(!obj.<span class="hljs-built_in">IsEmpty</span>()); <sp…
10193 <span class="hljs-built_in">assert</span>(obj-><span class="hljs-built_in">IsObject</span>());
10197-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10198 …<span class="hljs-built_in">assert</span>(arg == <span class="hljs-built_in">static_cast</span>&#x…
10202-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10203 …<span class="hljs-built_in">assert</span>(cleanup_cb1_called == <span class="hljs-number">1</span>…
10204 …<span class="hljs-built_in">assert</span>(cleanup_cb2_called == <span class="hljs-number">1</span>…
10207 <span class="hljs-comment">// Initialize this addon to be context-aware.</span>
10208 <span class="hljs-built_in">NODE_MODULE_INIT</span>(<span class="hljs-comment">/* exports, module, …
10209 Isolate* isolate = context-><span class="hljs-built_in">GetIsolate</span>();
10211 …<span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, sanity_check, <span class="h…
10212 <span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, cleanup_cb2, cookie);
10213 <span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, cleanup_cb1, isolate);
10216 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10217 <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon'</span>…
10221 top-level of the project describing the build configuration of the module
10222 using a JSON-like format. This file is used by <a href="https://github.com/nodejs/node-gyp">node-gy…
10224 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
10225 …<span class="hljs-attr">"targets"</span><span class="hljs-punctuation">:</span> <span class="hljs-
10226 <span class="hljs-punctuation">{</span>
10227 …an class="hljs-attr">"target_name"</span><span class="hljs-punctuation">:</span> <span class="hljs
10228-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</s…
10229 <span class="hljs-punctuation">}</span>
10230 <span class="hljs-punctuation">]</span>
10231 <span class="hljs-punctuation">}</span></code></pre>
10232 <p>A version of the <code>node-gyp</code> utility is bundled and distributed with
10236 use <code>node-gyp</code> directly can install it using the command
10237 <code>npm install -g node-gyp</code>. See the <code>node-gyp</code> <a href="https://github.com/nod…
10238 more information, including platform-specific requirements.</p>
10239 <p>Once the <code>binding.gyp</code> file has been created, use <code>node-gyp configure</code> to
10243 <p>Next, invoke the <code>node-gyp build</code> command to generate the compiled <code>addon.node</…
10246 version of <code>node-gyp</code> to perform this same set of actions, generating a
10250 <pre><code class="language-js"><span class="hljs-comment">// hello.js</span>
10251 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
10253 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10254 <span class="hljs-comment">// Prints: 'world'</span></code></pre>
10257 the <a href="https://github.com/TooTallNate/node-bindings">bindings</a> package to load the compile…
10260 <pre><code class="language-js"><span class="hljs-keyword">try</span> {
10261 …<span class="hljs-keyword">return</span> <span class="hljs-built_in">require</span>(<span class="h…
10262 } <span class="hljs-keyword">catch</span> (err) {
10263 …<span class="hljs-keyword">return</span> <span class="hljs-built_in">require</span>(<span class="h…
10269 ….></code> statements (e.g. <code>#include &#x3C;v8.h></code>) and <code>node-gyp</code> will locate
10274 <p>When <code>node-gyp</code> runs, it will detect the specific release version of Node.js
10281 <p><code>node-gyp</code> can be run using the <code>--nodedir</code> flag pointing at a local Node.…
10289 files with the <code>.node</code> file extension and initialize those as dynamically-linked
10310 </section><section><h3>Node-API<span><a class="mark" href="#addons_node_api" id="addons_node_api">#…
10311 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
10312 <p>Node-API is an API for building native addons. It is independent from
10319 outlined in this document (node-gyp, etc.). The only difference is the
10322 in the Node-API are used.</p>
10324 provided by Node-API carries with it certain
10326 <p>To use Node-API in the above "Hello world" example, replace the content of
10328 <pre><code class="language-cpp"><span class="hljs-comment">// hello.cc using Node-API</span>
10329 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10331 <span class="hljs-keyword">namespace</span> demo {
10333 <span class="hljs-function">napi_value <span class="hljs-title">Method</span><span class="hljs-para…
10337 …status = <span class="hljs-built_in">napi_create_string_utf8</span>(env, <span class="hljs-string"…
10338 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
10339 <span class="hljs-keyword">return</span> greeting;
10342 <span class="hljs-function">napi_value <span class="hljs-title">init</span><span class="hljs-params…
10346 …js-built_in">napi_create_function</span>(env, <span class="hljs-literal">nullptr</span>, <span cla…
10347 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
10349 …status = <span class="hljs-built_in">napi_set_named_property</span>(env, exports, <span class="hlj…
10350 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
10351 <span class="hljs-keyword">return</span> exports;
10354 <span class="hljs-built_in">NAPI_MODULE</span>(NODE_GYP_MODULE_NAME, init)
10356 } <span class="hljs-comment">// namespace demo</span></code></pre>
10358 <a href="n-api.html">C/C++ addons with Node-API</a>.</p>
10366 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
10367 …<span class="hljs-attr">"targets"</span><span class="hljs-punctuation">:</span> <span class="hljs-
10368 <span class="hljs-punctuation">{</span>
10369 …an class="hljs-attr">"target_name"</span><span class="hljs-punctuation">:</span> <span class="hljs
10370-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</s…
10371 <span class="hljs-punctuation">}</span>
10372 <span class="hljs-punctuation">]</span>
10373 <span class="hljs-punctuation">}</span></code></pre>
10376-json"><span class="hljs-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class…
10378 built using <code>node-gyp</code>:</p>
10379 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
10387 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10388 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10390 <span class="hljs-keyword">namespace</span> demo {
10392 <span class="hljs-keyword">using</span> v8::Exception;
10393 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10394 <span class="hljs-keyword">using</span> v8::Isolate;
10395 <span class="hljs-keyword">using</span> v8::Local;
10396 <span class="hljs-keyword">using</span> v8::Number;
10397 <span class="hljs-keyword">using</span> v8::Object;
10398 <span class="hljs-keyword">using</span> v8::String;
10399 <span class="hljs-keyword">using</span> v8::Value;
10401 <span class="hljs-comment">// This is the implementation of the "add" method</span>
10402 <span class="hljs-comment">// Input arguments are passed using the</span>
10403 <span class="hljs-comment">// const FunctionCallbackInfo&#x3C;Value>&#x26; args struct</span>
10404 …n class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Add</span><sp…
10405 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10407 <span class="hljs-comment">// Check the number of arguments passed.</span>
10408 …<span class="hljs-keyword">if</span> (args.<span class="hljs-built_in">Length</span>() &#x3C; <spa…
10409 <span class="hljs-comment">// Throw an Error that is passed back to JavaScript</span>
10410 …isolate-><span class="hljs-built_in">ThrowException</span>(Exception::<span class="hljs-built_in">…
10411 String::<span class="hljs-built_in">NewFromUtf8</span>(isolate,
10412 …<span class="hljs-string">"Wrong number of arguments"</span>).<span class="hljs-built_in">ToLocalC…
10413 <span class="hljs-keyword">return</span>;
10416 <span class="hljs-comment">// Check the argument types</span>
10417-keyword">if</span> (!args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">IsNumb…
10418 …isolate-><span class="hljs-built_in">ThrowException</span>(Exception::<span class="hljs-built_in">…
10419 String::<span class="hljs-built_in">NewFromUtf8</span>(isolate,
10420 …<span class="hljs-string">"Wrong arguments"</span>).<span class="hljs-built_in">ToLocalChecked</sp…
10421 <span class="hljs-keyword">return</span>;
10424 <span class="hljs-comment">// Perform the operation</span>
10425 <span class="hljs-type">double</span> value =
10426-number">0</span>].<span class="hljs-built_in">As</span>&#x3C;Number>()-><span class="hljs-built_i…
10427 Local&#x3C;Number> num = Number::<span class="hljs-built_in">New</span>(isolate, value);
10429 <span class="hljs-comment">// Set the return value (using the passed in</span>
10430 <span class="hljs-comment">// FunctionCallbackInfo&#x3C;Value>&#x26;)</span>
10431 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(nu…
10434 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Init</span…
10435 …<span class="hljs-built_in">NODE_SET_METHOD</span>(exports, <span class="hljs-string">"add"</span>…
10438 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Init)
10440 } <span class="hljs-comment">// namespace demo</span></code></pre>
10442 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10443 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
10445-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
10450 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10451 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10453 <span class="hljs-keyword">namespace</span> demo {
10455 <span class="hljs-keyword">using</span> v8::Context;
10456 <span class="hljs-keyword">using</span> v8::Function;
10457 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10458 <span class="hljs-keyword">using</span> v8::Isolate;
10459 <span class="hljs-keyword">using</span> v8::Local;
10460 <span class="hljs-keyword">using</span> v8::Null;
10461 <span class="hljs-keyword">using</span> v8::Object;
10462 <span class="hljs-keyword">using</span> v8::String;
10463 <span class="hljs-keyword">using</span> v8::Value;
10465 …ass="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">RunCallback</span…
10466 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10467 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10468 …unction> cb = Local&#x3C;Function>::<span class="hljs-built_in">Cast</span>(args[<span class="hljs
10469 …<span class="hljs-keyword">const</span> <span class="hljs-keyword">unsigned</span> argc = <span cl…
10471 String::<span class="hljs-built_in">NewFromUtf8</span>(isolate,
10472 …<span class="hljs-string">"hello world"</span>).<span class="hljs-built_in">ToLocalChecked</span>(…
10473 …cb-><span class="hljs-built_in">Call</span>(context, <span class="hljs-built_in">Null</span>(isola…
10476-function"><span class="hljs-type">void</span> <span class="hljs-title">Init</span><span class="hl…
10477 …<span class="hljs-built_in">NODE_SET_METHOD</span>(<span class="hljs-keyword">module</span>, <span…
10480 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Init)
10482 } <span class="hljs-comment">// namespace demo</span></code></pre>
10483 <p>This example uses a two-argument form of <code>Init()</code> that receives the full
10488 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10489 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
10491 <span class="hljs-title function_">addon</span>(<span class="hljs-function">(<span class="hljs-para…
10492 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
10493 <span class="hljs-comment">// Prints: 'hello world'</span>
10500 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10501 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10503 <span class="hljs-keyword">namespace</span> demo {
10505 <span class="hljs-keyword">using</span> v8::Context;
10506 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10507 <span class="hljs-keyword">using</span> v8::Isolate;
10508 <span class="hljs-keyword">using</span> v8::Local;
10509 <span class="hljs-keyword">using</span> v8::Object;
10510 <span class="hljs-keyword">using</span> v8::String;
10511 <span class="hljs-keyword">using</span> v8::Value;
10513 …ss="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">CreateObject</span…
10514 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10515 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10517 Local&#x3C;Object> obj = Object::<span class="hljs-built_in">New</span>(isolate);
10518 obj-><span class="hljs-built_in">Set</span>(context,
10519 String::<span class="hljs-built_in">NewFromUtf8</span>(isolate,
10520 … <span class="hljs-string">"msg"</span>).<span class="hljs-built_in">ToLocalChecked</span>(),
10521 …args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">ToString</span>(context).<sp…
10522 .<span class="hljs-built_in">FromJust</span>();
10524 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(ob…
10527-function"><span class="hljs-type">void</span> <span class="hljs-title">Init</span><span class="hl…
10528 …<span class="hljs-built_in">NODE_SET_METHOD</span>(<span class="hljs-keyword">module</span>, <span…
10531 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Init)
10533 } <span class="hljs-comment">// namespace demo</span></code></pre>
10535 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10536 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
10538 <span class="hljs-keyword">const</span> obj1 = <span class="hljs-title function_">addon</span>(<spa…
10539 <span class="hljs-keyword">const</span> obj2 = <span class="hljs-title function_">addon</span>(<spa…
10540 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(obj1.<span clas…
10541 <span class="hljs-comment">// Prints: 'hello world'</span></code></pre>
10545 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10546 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10548 <span class="hljs-keyword">namespace</span> demo {
10550 <span class="hljs-keyword">using</span> v8::Context;
10551 <span class="hljs-keyword">using</span> v8::Function;
10552 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10553 <span class="hljs-keyword">using</span> v8::FunctionTemplate;
10554 <span class="hljs-keyword">using</span> v8::Isolate;
10555 <span class="hljs-keyword">using</span> v8::Local;
10556 <span class="hljs-keyword">using</span> v8::Object;
10557 <span class="hljs-keyword">using</span> v8::String;
10558 <span class="hljs-keyword">using</span> v8::Value;
10560 …ass="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyFunction</span>…
10561 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10562 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(St…
10563 …isolate, <span class="hljs-string">"hello world"</span>).<span class="hljs-built_in">ToLocalChecke…
10566 …s="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">CreateFunction</spa…
10567 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10569 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10570 …Local&#x3C;FunctionTemplate> tpl = FunctionTemplate::<span class="hljs-built_in">New</span>(isolat…
10571 …Local&#x3C;Function> fn = tpl-><span class="hljs-built_in">GetFunction</span>(context).<span class…
10573 <span class="hljs-comment">// omit this to make it anonymous</span>
10574 …fn-><span class="hljs-built_in">SetName</span>(String::<span class="hljs-built_in">NewFromUtf8</sp…
10575 …isolate, <span class="hljs-string">"theFunction"</span>).<span class="hljs-built_in">ToLocalChecke…
10577 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(fn…
10580-function"><span class="hljs-type">void</span> <span class="hljs-title">Init</span><span class="hl…
10581 …<span class="hljs-built_in">NODE_SET_METHOD</span>(<span class="hljs-keyword">module</span>, <span…
10584 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Init)
10586 } <span class="hljs-comment">// namespace demo</span></code></pre>
10588 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10589 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
10591 <span class="hljs-keyword">const</span> fn = <span class="hljs-title function_">addon</span>();
10592 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10593 <span class="hljs-comment">// Prints: 'hello world'</span></code></pre>
10597 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10598 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10599 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobj…
10601 <span class="hljs-keyword">namespace</span> demo {
10603 <span class="hljs-keyword">using</span> v8::Local;
10604 <span class="hljs-keyword">using</span> v8::Object;
10606 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">InitAll</s…
10607 MyObject::<span class="hljs-built_in">Init</span>(exports);
10610 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, InitAll)
10612 } <span class="hljs-comment">// namespace demo</span></code></pre>
10614 <pre><code class="language-cpp"><span class="hljs-comment">// myobject.h</span>
10615 <span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> MYOBJECT_H</span>
10616 <span class="hljs-meta">#<span class="hljs-keyword">define</span> MYOBJECT_H</span>
10618 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10619 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10621 <span class="hljs-keyword">namespace</span> demo {
10623 <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyObject</span> : <span cla…
10624 <span class="hljs-keyword">public</span>:
10625 …"hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span…
10627 <span class="hljs-keyword">private</span>:
10628-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">MyObject</span><spa…
10629 ~<span class="hljs-built_in">MyObject</span>();
10631-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10632-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10634 <span class="hljs-type">double</span> value_;
10637 } <span class="hljs-comment">// namespace demo</span>
10639 <span class="hljs-meta">#<span class="hljs-keyword">endif</span></span></code></pre>
10643 <pre><code class="language-cpp"><span class="hljs-comment">// myobject.cc</span>
10644 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobj…
10646 <span class="hljs-keyword">namespace</span> demo {
10648 <span class="hljs-keyword">using</span> v8::Context;
10649 <span class="hljs-keyword">using</span> v8::Function;
10650 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10651 <span class="hljs-keyword">using</span> v8::FunctionTemplate;
10652 <span class="hljs-keyword">using</span> v8::Isolate;
10653 <span class="hljs-keyword">using</span> v8::Local;
10654 <span class="hljs-keyword">using</span> v8::Number;
10655 <span class="hljs-keyword">using</span> v8::Object;
10656 <span class="hljs-keyword">using</span> v8::ObjectTemplate;
10657 <span class="hljs-keyword">using</span> v8::String;
10658 <span class="hljs-keyword">using</span> v8::Value;
10660 MyObject::<span class="hljs-built_in">MyObject</span>(<span class="hljs-type">double</span> value) …
10663 MyObject::~<span class="hljs-built_in">MyObject</span>() {
10666 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::…
10667 Isolate* isolate = exports-><span class="hljs-built_in">GetIsolate</span>();
10668 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10670 …Local&#x3C;ObjectTemplate> addon_data_tpl = ObjectTemplate::<span class="hljs-built_in">New</span>…
10671 …addon_data_tpl-><span class="hljs-built_in">SetInternalFieldCount</span>(<span class="hljs-number"…
10673 …addon_data_tpl-><span class="hljs-built_in">NewInstance</span>(context).<span class="hljs-built_in…
10675 <span class="hljs-comment">// Prepare constructor template</span>
10676 …Local&#x3C;FunctionTemplate> tpl = FunctionTemplate::<span class="hljs-built_in">New</span>(isolat…
10677-><span class="hljs-built_in">SetClassName</span>(String::<span class="hljs-built_in">NewFromUtf8<…
10678 …tpl-><span class="hljs-built_in">InstanceTemplate</span>()-><span class="hljs-built_in">SetInterna…
10680 <span class="hljs-comment">// Prototype</span>
10681 …<span class="hljs-built_in">NODE_SET_PROTOTYPE_METHOD</span>(tpl, <span class="hljs-string">"plusO…
10683 …Local&#x3C;Function> constructor = tpl-><span class="hljs-built_in">GetFunction</span>(context).<s…
10684 …addon_data-><span class="hljs-built_in">SetInternalField</span>(<span class="hljs-number">0</span>…
10685 …exports-><span class="hljs-built_in">Set</span>(context, String::<span class="hljs-built_in">NewFr…
10686 …isolate, <span class="hljs-string">"MyObject"</span>).<span class="hljs-built_in">ToLocalChecked</…
10687 constructor).<span class="hljs-built_in">FromJust</span>();
10690 …ss="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::New</spa…
10691 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10692 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10694 <span class="hljs-keyword">if</span> (args.<span class="hljs-built_in">IsConstructCall</span>()) {
10695 <span class="hljs-comment">// Invoked as constructor: `new MyObject(...)`</span>
10696 …<span class="hljs-type">double</span> value = args[<span class="hljs-number">0</span>]-><span clas…
10697-number">0</span> : args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">NumberVa…
10698 …MyObject* obj = <span class="hljs-keyword">new</span> <span class="hljs-built_in">MyObject</span>(…
10699 obj-><span class="hljs-built_in">Wrap</span>(args.<span class="hljs-built_in">This</span>());
10700 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(ar…
10701 } <span class="hljs-keyword">else</span> {
10702 …<span class="hljs-comment">// Invoked as plain function `MyObject(...)`, turn into construct call.…
10703 …<span class="hljs-keyword">const</span> <span class="hljs-type">int</span> argc = <span class="hlj…
10704 Local&#x3C;Value> argv[argc] = { args[<span class="hljs-number">0</span>] };
10706-built_in">Data</span>().<span class="hljs-built_in">As</span>&#x3C;Object>()-><span class="hljs-b…
10708 …cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_…
10709 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(re…
10713 …="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::PlusOne</s…
10714 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10716 …MyObject* obj = ObjectWrap::<span class="hljs-built_in">Unwrap</span>&#x3C;MyObject>(args.<span cl…
10717 obj->value_ += <span class="hljs-number">1</span>;
10719 …ass="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(Number::<span c…
10722 } <span class="hljs-comment">// namespace demo</span></code></pre>
10725 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
10726 …<span class="hljs-attr">"targets"</span><span class="hljs-punctuation">:</span> <span class="hljs-
10727 <span class="hljs-punctuation">{</span>
10728 …an class="hljs-attr">"target_name"</span><span class="hljs-punctuation">:</span> <span class="hljs
10729 …<span class="hljs-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-
10730 <span class="hljs-string">"addon.cc"</span><span class="hljs-punctuation">,</span>
10731 <span class="hljs-string">"myobject.cc"</span>
10732 <span class="hljs-punctuation">]</span>
10733 <span class="hljs-punctuation">}</span>
10734 <span class="hljs-punctuation">]</span>
10735 <span class="hljs-punctuation">}</span></code></pre>
10737 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10738 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
10740 …s="hljs-keyword">const</span> obj = <span class="hljs-keyword">new</span> addon.<span class="hljs-
10741 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10742 <span class="hljs-comment">// Prints: 11</span>
10743 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10744 <span class="hljs-comment">// Prints: 12</span>
10745 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10746 <span class="hljs-comment">// Prints: 13</span></code></pre>
10748 garbage-collected. For destructor testing, there are command-line flags that
10756 <pre><code class="language-js"><span class="hljs-keyword">const</span> obj = addon.<span class="hlj…
10757 <span class="hljs-comment">// instead of:</span>
10758 <span class="hljs-comment">// const obj = new addon.Object();</span></code></pre>
10760 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10761 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10762 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobj…
10764 <span class="hljs-keyword">namespace</span> demo {
10766 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10767 <span class="hljs-keyword">using</span> v8::Isolate;
10768 <span class="hljs-keyword">using</span> v8::Local;
10769 <span class="hljs-keyword">using</span> v8::Object;
10770 <span class="hljs-keyword">using</span> v8::String;
10771 <span class="hljs-keyword">using</span> v8::Value;
10773 …ss="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">CreateObject</span…
10774 MyObject::<span class="hljs-built_in">NewInstance</span>(args);
10777-function"><span class="hljs-type">void</span> <span class="hljs-title">InitAll</span><span class=…
10778 …MyObject::<span class="hljs-built_in">Init</span>(exports-><span class="hljs-built_in">GetIsolate<…
10780 …<span class="hljs-built_in">NODE_SET_METHOD</span>(<span class="hljs-keyword">module</span>, <span…
10783 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, InitAll)
10785 } <span class="hljs-comment">// namespace demo</span></code></pre>
10789 <pre><code class="language-cpp"><span class="hljs-comment">// myobject.h</span>
10790 <span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> MYOBJECT_H</span>
10791 <span class="hljs-meta">#<span class="hljs-keyword">define</span> MYOBJECT_H</span>
10793 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10794 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10796 <span class="hljs-keyword">namespace</span> demo {
10798 <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyObject</span> : <span cla…
10799 <span class="hljs-keyword">public</span>:
10800 …"hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span…
10801-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10803 <span class="hljs-keyword">private</span>:
10804-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">MyObject</span><spa…
10805 ~<span class="hljs-built_in">MyObject</span>();
10807-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10808-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10809 <span class="hljs-keyword">static</span> v8::Global&#x3C;v8::Function> constructor;
10810 <span class="hljs-type">double</span> value_;
10813 } <span class="hljs-comment">// namespace demo</span>
10815 <span class="hljs-meta">#<span class="hljs-keyword">endif</span></span></code></pre>
10817 <pre><code class="language-cpp"><span class="hljs-comment">// myobject.cc</span>
10818 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10819 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobj…
10821 <span class="hljs-keyword">namespace</span> demo {
10823 <span class="hljs-keyword">using</span> node::AddEnvironmentCleanupHook;
10824 <span class="hljs-keyword">using</span> v8::Context;
10825 <span class="hljs-keyword">using</span> v8::Function;
10826 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10827 <span class="hljs-keyword">using</span> v8::FunctionTemplate;
10828 <span class="hljs-keyword">using</span> v8::Global;
10829 <span class="hljs-keyword">using</span> v8::Isolate;
10830 <span class="hljs-keyword">using</span> v8::Local;
10831 <span class="hljs-keyword">using</span> v8::Number;
10832 <span class="hljs-keyword">using</span> v8::Object;
10833 <span class="hljs-keyword">using</span> v8::String;
10834 <span class="hljs-keyword">using</span> v8::Value;
10836 <span class="hljs-comment">// Warning! This is not thread-safe, this addon cannot be used for worke…
10837 <span class="hljs-comment">// threads.</span>
10840 MyObject::<span class="hljs-built_in">MyObject</span>(<span class="hljs-type">double</span> value) …
10843 MyObject::~<span class="hljs-built_in">MyObject</span>() {
10846 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::…
10847 <span class="hljs-comment">// Prepare constructor template</span>
10848 …Local&#x3C;FunctionTemplate> tpl = FunctionTemplate::<span class="hljs-built_in">New</span>(isolat…
10849-><span class="hljs-built_in">SetClassName</span>(String::<span class="hljs-built_in">NewFromUtf8<…
10850 …tpl-><span class="hljs-built_in">InstanceTemplate</span>()-><span class="hljs-built_in">SetInterna…
10852 <span class="hljs-comment">// Prototype</span>
10853 …<span class="hljs-built_in">NODE_SET_PROTOTYPE_METHOD</span>(tpl, <span class="hljs-string">"plusO…
10855 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10856 …or.<span class="hljs-built_in">Reset</span>(isolate, tpl-><span class="hljs-built_in">GetFunction<…
10858 …<span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, [](<span class="hljs-type">v…
10859 constructor.<span class="hljs-built_in">Reset</span>();
10860 }, <span class="hljs-literal">nullptr</span>);
10863 …ss="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::New</spa…
10864 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10865 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10867 <span class="hljs-keyword">if</span> (args.<span class="hljs-built_in">IsConstructCall</span>()) {
10868 <span class="hljs-comment">// Invoked as constructor: `new MyObject(...)`</span>
10869 …<span class="hljs-type">double</span> value = args[<span class="hljs-number">0</span>]-><span clas…
10870-number">0</span> : args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">NumberVa…
10871 …MyObject* obj = <span class="hljs-keyword">new</span> <span class="hljs-built_in">MyObject</span>(…
10872 obj-><span class="hljs-built_in">Wrap</span>(args.<span class="hljs-built_in">This</span>());
10873 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(ar…
10874 } <span class="hljs-keyword">else</span> {
10875 …<span class="hljs-comment">// Invoked as plain function `MyObject(...)`, turn into construct call.…
10876 …<span class="hljs-keyword">const</span> <span class="hljs-type">int</span> argc = <span class="hlj…
10877 Local&#x3C;Value> argv[argc] = { args[<span class="hljs-number">0</span>] };
10878 …Local&#x3C;Function> cons = Local&#x3C;Function>::<span class="hljs-built_in">New</span>(isolate, …
10880 …cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_…
10881 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(in…
10885 …hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::NewInstance<…
10886 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10888 …<span class="hljs-keyword">const</span> <span class="hljs-keyword">unsigned</span> argc = <span cl…
10889 Local&#x3C;Value> argv[argc] = { args[<span class="hljs-number">0</span>] };
10890 …Local&#x3C;Function> cons = Local&#x3C;Function>::<span class="hljs-built_in">New</span>(isolate, …
10891 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10893 …cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_…
10895 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(in…
10898 …="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::PlusOne</s…
10899 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10901 …MyObject* obj = ObjectWrap::<span class="hljs-built_in">Unwrap</span>&#x3C;MyObject>(args.<span cl…
10902 obj->value_ += <span class="hljs-number">1</span>;
10904 …ass="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(Number::<span c…
10907 } <span class="hljs-comment">// namespace demo</span></code></pre>
10910 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
10911 …<span class="hljs-attr">"targets"</span><span class="hljs-punctuation">:</span> <span class="hljs-
10912 <span class="hljs-punctuation">{</span>
10913 …an class="hljs-attr">"target_name"</span><span class="hljs-punctuation">:</span> <span class="hljs
10914 …<span class="hljs-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-
10915 <span class="hljs-string">"addon.cc"</span><span class="hljs-punctuation">,</span>
10916 <span class="hljs-string">"myobject.cc"</span>
10917 <span class="hljs-punctuation">]</span>
10918 <span class="hljs-punctuation">}</span>
10919 <span class="hljs-punctuation">]</span>
10920 <span class="hljs-punctuation">}</span></code></pre>
10922 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10923 <span class="hljs-keyword">const</span> createObject = <span class="hljs-built_in">require</span>(<…
10925 <span class="hljs-keyword">const</span> obj = <span class="hljs-title function_">createObject</span…
10926 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10927 <span class="hljs-comment">// Prints: 11</span>
10928 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10929 <span class="hljs-comment">// Prints: 12</span>
10930 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10931 <span class="hljs-comment">// Prints: 13</span>
10933 <span class="hljs-keyword">const</span> obj2 = <span class="hljs-title function_">createObject</spa…
10934 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10935 <span class="hljs-comment">// Prints: 21</span>
10936 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10937 <span class="hljs-comment">// Prints: 22</span>
10938 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10939 <span class="hljs-comment">// Prints: 23</span></code></pre>
10945 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10946 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10947 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10948 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobj…
10950 <span class="hljs-keyword">namespace</span> demo {
10952 <span class="hljs-keyword">using</span> v8::Context;
10953 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10954 <span class="hljs-keyword">using</span> v8::Isolate;
10955 <span class="hljs-keyword">using</span> v8::Local;
10956 <span class="hljs-keyword">using</span> v8::Number;
10957 <span class="hljs-keyword">using</span> v8::Object;
10958 <span class="hljs-keyword">using</span> v8::String;
10959 <span class="hljs-keyword">using</span> v8::Value;
10961 …ss="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">CreateObject</span…
10962 MyObject::<span class="hljs-built_in">NewInstance</span>(args);
10965 …n class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Add</span><sp…
10966 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10967 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10969 MyObject* obj1 = node::ObjectWrap::<span class="hljs-built_in">Unwrap</span>&#x3C;MyObject>(
10970 …args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">ToObject</span>(context).<sp…
10971 MyObject* obj2 = node::ObjectWrap::<span class="hljs-built_in">Unwrap</span>&#x3C;MyObject>(
10972 …args[<span class="hljs-number">1</span>]-><span class="hljs-built_in">ToObject</span>(context).<sp…
10974 …<span class="hljs-type">double</span> sum = obj1-><span class="hljs-built_in">value</span>() + obj…
10975 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(Nu…
10978 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">InitAll</s…
10979 …MyObject::<span class="hljs-built_in">Init</span>(exports-><span class="hljs-built_in">GetIsolate<…
10981 …<span class="hljs-built_in">NODE_SET_METHOD</span>(exports, <span class="hljs-string">"createObjec…
10982 …<span class="hljs-built_in">NODE_SET_METHOD</span>(exports, <span class="hljs-string">"add"</span>…
10985 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, InitAll)
10987 } <span class="hljs-comment">// namespace demo</span></code></pre>
10990 <pre><code class="language-cpp"><span class="hljs-comment">// myobject.h</span>
10991 <span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> MYOBJECT_H</span>
10992 <span class="hljs-meta">#<span class="hljs-keyword">define</span> MYOBJECT_H</span>
10994 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10995 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10997 <span class="hljs-keyword">namespace</span> demo {
10999 <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyObject</span> : <span cla…
11000 <span class="hljs-keyword">public</span>:
11001 …"hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span…
11002-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
11003-function"><span class="hljs-keyword">inline</span> <span class="hljs-type">double</span> <span cl…
11005 <span class="hljs-keyword">private</span>:
11006-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">MyObject</span><spa…
11007 ~<span class="hljs-built_in">MyObject</span>();
11009-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
11010 <span class="hljs-keyword">static</span> v8::Global&#x3C;v8::Function> constructor;
11011 <span class="hljs-type">double</span> value_;
11014 } <span class="hljs-comment">// namespace demo</span>
11016 <span class="hljs-meta">#<span class="hljs-keyword">endif</span></span></code></pre>
11018 <pre><code class="language-cpp"><span class="hljs-comment">// myobject.cc</span>
11019 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11020 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobj…
11022 <span class="hljs-keyword">namespace</span> demo {
11024 <span class="hljs-keyword">using</span> node::AddEnvironmentCleanupHook;
11025 <span class="hljs-keyword">using</span> v8::Context;
11026 <span class="hljs-keyword">using</span> v8::Function;
11027 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
11028 <span class="hljs-keyword">using</span> v8::FunctionTemplate;
11029 <span class="hljs-keyword">using</span> v8::Global;
11030 <span class="hljs-keyword">using</span> v8::Isolate;
11031 <span class="hljs-keyword">using</span> v8::Local;
11032 <span class="hljs-keyword">using</span> v8::Object;
11033 <span class="hljs-keyword">using</span> v8::String;
11034 <span class="hljs-keyword">using</span> v8::Value;
11036 <span class="hljs-comment">// Warning! This is not thread-safe, this addon cannot be used for worke…
11037 <span class="hljs-comment">// threads.</span>
11040 MyObject::<span class="hljs-built_in">MyObject</span>(<span class="hljs-type">double</span> value) …
11043 MyObject::~<span class="hljs-built_in">MyObject</span>() {
11046 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::…
11047 <span class="hljs-comment">// Prepare constructor template</span>
11048 …Local&#x3C;FunctionTemplate> tpl = FunctionTemplate::<span class="hljs-built_in">New</span>(isolat…
11049-><span class="hljs-built_in">SetClassName</span>(String::<span class="hljs-built_in">NewFromUtf8<…
11050 …tpl-><span class="hljs-built_in">InstanceTemplate</span>()-><span class="hljs-built_in">SetInterna…
11052 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
11053 …or.<span class="hljs-built_in">Reset</span>(isolate, tpl-><span class="hljs-built_in">GetFunction<…
11055 …<span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, [](<span class="hljs-type">v…
11056 constructor.<span class="hljs-built_in">Reset</span>();
11057 }, <span class="hljs-literal">nullptr</span>);
11060 …ss="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::New</spa…
11061 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
11062 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
11064 <span class="hljs-keyword">if</span> (args.<span class="hljs-built_in">IsConstructCall</span>()) {
11065 <span class="hljs-comment">// Invoked as constructor: `new MyObject(...)`</span>
11066 …<span class="hljs-type">double</span> value = args[<span class="hljs-number">0</span>]-><span clas…
11067-number">0</span> : args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">NumberVa…
11068 …MyObject* obj = <span class="hljs-keyword">new</span> <span class="hljs-built_in">MyObject</span>(…
11069 obj-><span class="hljs-built_in">Wrap</span>(args.<span class="hljs-built_in">This</span>());
11070 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(ar…
11071 } <span class="hljs-keyword">else</span> {
11072 …<span class="hljs-comment">// Invoked as plain function `MyObject(...)`, turn into construct call.…
11073 …<span class="hljs-keyword">const</span> <span class="hljs-type">int</span> argc = <span class="hlj…
11074 Local&#x3C;Value> argv[argc] = { args[<span class="hljs-number">0</span>] };
11075 …Local&#x3C;Function> cons = Local&#x3C;Function>::<span class="hljs-built_in">New</span>(isolate, …
11077 …cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_…
11078 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(in…
11082 …hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::NewInstance<…
11083 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
11085 …<span class="hljs-keyword">const</span> <span class="hljs-keyword">unsigned</span> argc = <span cl…
11086 Local&#x3C;Value> argv[argc] = { args[<span class="hljs-number">0</span>] };
11087 …Local&#x3C;Function> cons = Local&#x3C;Function>::<span class="hljs-built_in">New</span>(isolate, …
11088 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
11090 …cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_…
11092 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(in…
11095 } <span class="hljs-comment">// namespace demo</span></code></pre>
11097 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
11098 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
11100 <span class="hljs-keyword">const</span> obj1 = addon.<span class="hljs-title function_">createObjec…
11101 <span class="hljs-keyword">const</span> obj2 = addon.<span class="hljs-title function_">createObjec…
11102 <span class="hljs-keyword">const</span> result = addon.<span class="hljs-title function_">add</span…
11104 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
11105 <span class="hljs-comment">// Prints: 30</span></code></pre></section>
11107 <section><h2>Node-API<span><a class="mark" href="#n_api_node_api" id="n_api_node_api">#</a></span><…
11110 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
11111 <p>Node-API (formerly N-API) is an API for building native Addons. It is
11117 …e <a href="https://nodejs.org/en/docs/guides/abi-stability/">ABI Stability</a> guide provides a mo…
11121 APIs, the functions available in Node-API are used.</p>
11122 <p>APIs exposed by Node-API are generally used to create and manipulate
11124 in the ECMA-262 Language Specification. The APIs have the following
11127 <li>All Node-API calls return a status code of type <code>napi_status</code>. This
11136 <p>Node-API is a C API that ensures ABI stability across Node.js versions
11139 C++ wrapper module called <a href="https://github.com/nodejs/node-addon-api"><code>node-addon-api</…
11141 with <code>node-addon-api</code> will depend on the symbols for the Node-API C-based
11142 functions exported by Node.js. <code>node-addon-api</code> is a more
11143 efficient way to write code that calls Node-API. Take, for example, the
11144 following <code>node-addon-api</code> code. The first section shows the
11145 <code>node-addon-api</code> code and the second section shows what actually gets
11147 <pre><code class="language-cpp">Object obj = Object::<span class="hljs-built_in">New</span>(env);
11148 obj[<span class="hljs-string">"foo"</span>] = String::<span class="hljs-built_in">New</span>(env, <…
11149 <pre><code class="language-cpp">napi_status status;
11151 status = <span class="hljs-built_in">napi_create_object</span>(env, &#x26;object);
11152 <span class="hljs-keyword">if</span> (status != napi_ok) {
11153 <span class="hljs-built_in">napi_throw_error</span>(env, ...);
11154 <span class="hljs-keyword">return</span>;
11157 status = <span class="hljs-built_in">napi_create_string_utf8</span>(env, <span class="hljs-string">…
11158 <span class="hljs-keyword">if</span> (status != napi_ok) {
11159 <span class="hljs-built_in">napi_throw_error</span>(env, ...);
11160 <span class="hljs-keyword">return</span>;
11163 status = <span class="hljs-built_in">napi_set_named_property</span>(env, object, <span class="hljs-
11164 <span class="hljs-keyword">if</span> (status != napi_ok) {
11165 <span class="hljs-built_in">napi_throw_error</span>(env, ...);
11166 <span class="hljs-keyword">return</span>;
11170 …en using <code>node-addon-api</code> instead of the C APIs, start with the API <a href="https://gi…
11171 for <code>node-addon-api</code>.</p>
11172 <p>The <a href="https://nodejs.github.io/node-addon-examples/">Node-API Resource</a> offers
11174 Node-API and <code>node-addon-api</code>.</p>
11176 <p>Although Node-API provides an ABI stability guarantee, other parts of Node.js do
11183 <pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> …
11184 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11185 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11186 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11190 <pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> …
11194 <pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> …
11197 <p>Thus, for an addon to remain ABI-compatible across Node.js major versions, it
11198 must use Node-API exclusively by restricting itself to using</p>
11199 <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <s…
11201 library makes ABI stability guarantees similar to Node-API.</p>
11204 native addons using Node-API requires an additional set of tools. Besides the
11216 <pre><code class="language-bash">xcode-select --install</code></pre>
11220 <pre><code class="language-bash">npm install --global windows-build-tools</code></pre>
11227 <h5>node-gyp<span><a class="mark" href="#n_api_node_gyp" id="n_api_node_gyp">#</a></span></h5>
11228 …ps://github.com/nodejs/node-gyp">node-gyp</a> is a build system based on the <a href="https://gith…
11229 …<a href="https://gyp.gsrc.io">GYP</a> tool and comes bundled with npm. GYP, and therefore node-gyp,
11231 <p>Historically, node-gyp has been the tool of choice for building native
11233 developers have run into limitations in node-gyp.</p>
11235 <p><a href="https://github.com/cmake-js/cmake-js">CMake.js</a> is an alternative build system based…
11237 developers affected by limitations in node-gyp.</p>
11241 typically integrated with CI/CD build systems like <a href="https://travis-ci.org">Travis CI</a> and
11245 <h5>node-pre-gyp<span><a class="mark" href="#n_api_node_pre_gyp" id="n_api_node_pre_gyp">#</a></spa…
11246 <p><a href="https://github.com/mapbox/node-pre-gyp">node-pre-gyp</a> is a tool based on node-gyp th…
11247 upload binaries to a server of the developer's choice. node-pre-gyp has
11250 …/github.com/prebuild/prebuild">prebuild</a> is a tool that supports builds using either node-gyp or
11251 CMake.js. Unlike node-pre-gyp which supports a variety of servers, prebuild
11252 …s only to <a href="https://help.github.com/en/github/administering-a-repository/about-releases">Gi…
11255 <p><a href="https://github.com/prebuild/prebuildify">prebuildify</a> is a tool based on node-gyp. T…
11260 <p>In order to use the Node-API functions, include the file <a href="https://github.com/nodejs/node…
11262 <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <s…
11264 In order to ensure compatibility with specific versions of Node-API, the version
11266 <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAP…
11267 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11268 <p>This restricts the Node-API surface to just the functionality that was available
11270 <p>Some of the Node-API surface is experimental and requires explicit opt-in:</p>
11271 <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAP…
11272 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11275 </section><section><h3>Node-API version matrix<span><a class="mark" href="#n_api_node_api_version_m…
11276 <p>Node-API versions are additive and versioned independently from Node.js.
11281 <!-- For accessibility purposes, this table needs row headers. That means we
11282 can't do it in markdown. Hence, the raw HTML. -->
11381 <p>* Node-API was experimental.</p>
11382 <p>** Node.js 8.0.0 included Node-API as experimental. It was released as
11383 Node-API version 1 but continued to evolve until Node.js 8.6.0. The API is
11384 different in versions prior to Node.js 8.6.0. We recommend Node-API version 3 or
11386 <p>Each API documented for Node-API will have a header named <code>added in:</code>, and APIs
11387 which are stable will have the additional header <code>Node-API version:</code>.
11389 the Node-API version shown in <code>Node-API version:</code> or higher.
11391 <code>Node-API version:</code> listed or if there is no <code>Node-API version:</code> listed,
11397 <p>The Node-APIs associated strictly with accessing ECMAScript features from native
11401 implementations of Node-API outside of Node.js. For those implementations the
11403 <p>The Node.js-specific parts of an addon can be separated from the code that
11405 latter may be used with multiple implementations of Node-API. In the example
11408 implementation of Node-API or any implementation of Node-API outside of Node.js.</p>
11412 <pre><code class="language-c"><span class="hljs-comment">// addon.h</span>
11413 <span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> _ADDON_H_</span>
11414 <span class="hljs-meta">#<span class="hljs-keyword">define</span> _ADDON_H_</span>
11415 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11416 napi_value <span class="hljs-title function_">create_addon</span><span class="hljs-params">(napi_en…
11417 <span class="hljs-meta">#<span class="hljs-keyword">endif</span> <span class="hljs-comment">// _AD…
11418 <pre><code class="language-c"><span class="hljs-comment">// addon.c</span>
11419 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"addon…
11421 <span class="hljs-meta">#<span class="hljs-keyword">define</span> NAPI_CALL(env, call) …
11424 … <span class="hljs-keyword">if</span> (status != napi_ok) { \
11429 … <span class="hljs-keyword">if</span> (!is_pending) { \
11430 const char* message = (error_info->error_message == NULL) \
11431 ? <span class="hljs-string">"empty error message"</span> \
11432 : error_info->error_message; \
11439 <span class="hljs-keyword">static</span> napi_value
11440 <span class="hljs-title function_">DoSomethingUseful</span><span class="hljs-params">(napi_env env,…
11441 <span class="hljs-comment">// Do something useful.</span>
11442 <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
11445 napi_value <span class="hljs-title function_">create_addon</span><span class="hljs-params">(napi_en…
11451 <span class="hljs-string">"doSomethingUseful"</span>,
11454 <span class="hljs-literal">NULL</span>,
11459 <span class="hljs-string">"doSomethingUseful"</span>,
11462 <span class="hljs-keyword">return</span> result;
11464 <pre><code class="language-c"><span class="hljs-comment">// addon_node.c</span>
11465 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11466 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"addon…
11469 <span class="hljs-comment">// This function body is expected to return a `napi_value`.</span>
11470 …<span class="hljs-comment">// The variables `napi_env env` and `napi_value exports` may be used wi…
11471 …<span class="hljs-comment">// the body, as they are provided by the definition of `NAPI_MODULE_INI…
11472 <span class="hljs-keyword">return</span> create_addon(env);
11475 <p><a href="https://tc39.es/ecma262/#sec-agents">Section 8.7</a> of the <a href="https://tc39.githu…
11476 of an "Agent" as a self-contained environment in which JavaScript code runs.
11493 <p>To this end, Node-API provides a way to allocate data such that its life cycle
11498 <span>N-API version: 6</span>
11500 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_instance_data…
11501 <span class="hljs-type">void</span>* data,
11503 … <span class="hljs-type">void</span>* finalize_hint)</span>;</code></pre>
11505 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
11522 <span>N-API version: 6</span>
11524 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_instance_data…
11525 <span class="hljs-type">void</span>** data)</span>;</code></pre>
11527 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
11535 </section><section><h3>Basic Node-API data types<span><a class="mark" href="#n_api_basic_node_api_d…
11536 <p>Node-API exposes the following fundamental datatypes as abstractions that are
11538 introspectable only with other Node-API calls.</p>
11542 <span>N-API version: 1</span>
11544 <p>Integral status code indicating the success or failure of a Node-API call.
11546 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
11568 napi_would_deadlock, <span class="hljs-comment">/* unused */</span>
11576 <span>N-API version: 1</span>
11578 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
11579 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* error_message;
11580 <span class="hljs-type">void</span>* engine_reserved;
11581 <span class="hljs-type">uint32_t</span> engine_error_code;
11585 <li><code>error_message</code>: UTF8-encoded string containing a VM-neutral description of
11587 <li><code>engine_reserved</code>: Reserved for VM-specific error details. This is currently
11589 <li><code>engine_error_code</code>: VM-specific error code. This is currently
11591 <li><code>error_code</code>: The Node-API status code that originated with the last error.</li>
11595 <p><code>napi_env</code> is used to represent a context that the underlying Node-API
11596 implementation can use to persist VM-specific state. This structure is passed
11598 making Node-API calls. Specifically, the same <code>napi_env</code> that was passed in when
11600 nested Node-API calls. Caching the <code>napi_env</code> for the purpose of general reuse,
11611 <span>N-API version: 4</span>
11619 <span>N-API version: 4</span>
11622 the thread-safe function is to be closed immediately (<code>napi_tsfn_abort</code>) or
11625 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
11632 <span>N-API version: 4</span>
11635 the call should block whenever the queue associated with the thread-safe
11637 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
11641 <h4>Node-API memory management types<span><a class="mark" href="#n_api_node_api_memory_management_t…
11644 created within a particular scope. In general, Node-API values are created
11647 create a new handle scope, Node-API values will be created in the default handle
11660 <span>N-API version: 1</span>
11667 <span>N-API version: 1</span>
11676 <span>N-API version: 8</span>
11678 <p>A 128-bit value stored as two unsigned 64-bit integers. It serves as a UUID
11682 manipulated. Type-tagging is most useful in conjunction with <a href="#n_api_napi_wrap"><code>napi_…
11686 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
11687 <span class="hljs-type">uint64_t</span> lower;
11688 <span class="hljs-type">uint64_t</span> upper;
11697 <h4>Node-API callback types<span><a class="mark" href="#n_api_node_api_callback_types" id="n_api_no…
11701 <span>N-API version: 1</span>
11709 <span>N-API version: 1</span>
11711 <p>Function pointer type for user-provided native functions which are to be
11712 exposed to JavaScript via Node-API. Callback functions should satisfy the
11714-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">napi_value</span>…
11720 <span>N-API version: 1</span>
11722 <p>Function pointer type for add-on provided functions that allow the user to be
11723 notified when externally-owned data is ready to be cleaned up because the
11724 object with which it was associated with, has been garbage-collected. The user
11728-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span…
11729 <span class="hljs-type">void</span>* finalize_data,
11730 … <span class="hljs-type">void</span>* finalize_hint)</span>;</code></pre>
11736 <span>N-API version: 1</span>
11740-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span…
11741 <p>Implementations of this function must avoid making Node-API calls that execute
11742 JavaScript or interact with JavaScript objects. Node-API calls should be in the
11748 <span>N-API version: 1</span>
11752-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span…
11754 … <span class="hljs-type">void</span>* data)</span>;</code></pre>
11760 <span>N-API version: 4</span>
11762 <p>Function pointer used with asynchronous thread-safe function calls. The callback
11770 <p>Node-API sets up the environment prior to calling this callback, so it is
11774-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span…
11776 <span class="hljs-type">void</span>* context,
11777 … <span class="hljs-type">void</span>* data)</span>;</code></pre>
11779 …code>[in] env</code>: The environment to use for API calls, or <code>NULL</code> if the thread-safe
11782 thread-safe function is being torn down and <code>data</code> may need to be freed. It
11783 may also be <code>NULL</code> if the thread-safe function was created without
11785 <li><code>[in] context</code>: The optional data with which the thread-safe function was
11788 the callback to convert this native data to JavaScript values (with Node-API
11802-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span…
11803 … <span class="hljs-type">void</span>* data)</span>;</code></pre>
11814 <p>Node-API uses both return values and JavaScript exceptions for error handling.
11817 <p>All of the Node-API functions share the same error handling pattern. The
11830 <p>The <code>napi_status</code> return value provides a VM-independent representation of
11833 VM (engine)-specific information.</p>
11839 <span>N-API version: 1</span>
11841 …s="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hlj…
11842 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* error_message;
11843 <span class="hljs-type">void</span>* engine_reserved;
11844 <span class="hljs-type">uint32_t</span> engine_error_code;
11851 <li><code>error_code</code>: Node-API status code for the last error.</li>
11854 Node-API call that was made.</p>
11861 <span>N-API version: 1</span>
11863 <pre><code class="language-c">napi_status
11864 <span class="hljs-title function_">napi_get_last_error_info</span><span class="hljs-params">(napi_e…
11865 … <span class="hljs-keyword">const</span> napi_extended_error_info** result)</span>;</code></pre>
11875 a Node-API function is called on the same <code>env</code>.</p>
11881 <p>Any Node-API function call may result in a pending JavaScript exception. This is
11890 <p>In many cases when a Node-API function is called and an exception is
11893 for all functions. Node-API allows a subset of the functions to be
11902 code where the native method was invoked. The behavior of most Node-API calls
11914 it can be re-thrown it with <a href="#n_api_napi_throw"><code>napi_throw</code></a> where error is …
11931 SemVer applying. In order to support this model with Node-API, both
11938 <pre><code class="language-text">originalName [code]</code></pre>
11942 <pre><code class="language-text">TypeError [ERR_ERROR_1]</code></pre>
11946 <span>N-API version: 1</span>
11948 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw…
11958 <span>N-API version: 1</span>
11960 …re><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_…
11961 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* code,
11962 …<span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* msg)</span>;</code></…
11973 <span>N-API version: 1</span>
11975 …><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_ty…
11976 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* code,
11977 …<span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* msg)</span>;</code></…
11988 <span>N-API version: 1</span>
11990 …<code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_ran…
11991 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* code,
11992 …<span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* msg)</span>;</code></…
12003 <span>N-API version: 1</span>
12005 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_is_er…
12007 … <span class="hljs-type">bool</span>* result)</span>;</code></pre>
12019 <span>N-API version: 1</span>
12021 …re><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create…
12038 <span>N-API version: 1</span>
12040 …<code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_ty…
12057 <span>N-API version: 1</span>
12059 …<code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_ra…
12076 <span>N-API version: 1</span>
12078 …><code class="language-c">napi_status <span class="hljs-title function_">napi_get_and_clear_last_e…
12089 <span>N-API version: 1</span>
12091 …guage-c">napi_status <span class="hljs-title function_">napi_is_exception_pending</span><span clas…
12101 <span>N-API version: 3</span>
12103 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_fatal_exception</…
12116 <span>N-API version: 1</span>
12118-c">NAPI_NO_RETURN <span class="hljs-type">void</span> <span class="hljs-title function_">napi_fat…
12119 <span class="hljs-type">size_t</span> location_len,
12120 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* message,
12121 … <span class="hljs-type">size_t</span> message_len)</span>;</code></pre>
12125 <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li>
12128 if it is null-terminated.</li>
12133 <p>As Node-API calls are made, handles to objects in the heap for the underlying
12145 The sections which follow describe the Node-API functions that can be used
12151 …uage-c"><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class=…
12154 <span class="hljs-keyword">if</span> (status != napi_ok) {
12155 <span class="hljs-keyword">break</span>;
12157 <span class="hljs-comment">// do something with element</span>
12163 <p>To handle this case, Node-API provides the ability to establish a new 'scope' to
12168 <p>Node-API only supports a single nested hierarchy of scopes. There is only one
12176 …uage-c"><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class=…
12179 <span class="hljs-keyword">if</span> (status != napi_ok) {
12180 <span class="hljs-keyword">break</span>;
12184 <span class="hljs-keyword">if</span> (status != napi_ok) {
12185 <span class="hljs-keyword">break</span>;
12187 <span class="hljs-comment">// do something with element</span>
12189 <span class="hljs-keyword">if</span> (status != napi_ok) {
12190 <span class="hljs-keyword">break</span>;
12194 inner scope needs to live beyond the lifespan of that scope. Node-API supports
12207 <span>N-API version: 1</span>
12209 …<code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_open_hand…
12220 <span>N-API version: 1</span>
12222 …<code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_close_han…
12235 <span>N-API version: 1</span>
12237 <pre><code class="language-c">NAPI_EXTERN napi_status
12238 …<span class="hljs-title function_">napi_open_escapable_handle_scope</span><span class="hljs-params…
12250 <span>N-API version: 1</span>
12252 <pre><code class="language-c">NAPI_EXTERN napi_status
12253 …<span class="hljs-title function_">napi_close_escapable_handle_scope</span><span class="hljs-param…
12266 <span>N-API version: 1</span>
12268 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_escape_handle</sp…
12295 <p>Node-API provides methods to create persistent references to an object.
12321 <span>N-API version: 1</span>
12323 …><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_r…
12325 … <span class="hljs-type">uint32_t</span> initial_refcount,
12340 <span>N-API version: 1</span>
12342 …><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_delete_r…
12353 <span>N-API version: 1</span>
12355 …e><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_referen…
12357 … <span class="hljs-type">uint32_t</span>* result)</span>;</code></pre>
12369 <span>N-API version: 1</span>
12371 …><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_referenc…
12373 … <span class="hljs-type">uint32_t</span>* result)</span>;</code></pre>
12385 <span>N-API version: 1</span>
12387 …code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_get_refere…
12405 clean-up hooks that will be run once the current Node.js instance exits.</p>
12406 <p>Node-API provides functions for registering and un-registering such callbacks.
12412 <span>N-API version: 3</span>
12414 …code class="language-c">NODE_EXTERN napi_status <span class="hljs-title function_">napi_add_env_cl…
12415 … <span class="hljs-type">void</span> (*fun)(<span class="hljs-type">void</span>* arg),
12416 … <span class="hljs-type">void</span>* arg)</span>;</code></pre>
12432 <span>N-API version: 3</span>
12434 …de class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_remove_env_c…
12435 … <span class="hljs-type">void</span> (*fun)(<span class="hljs-type">void</span>* arg),
12436 … <span class="hljs-type">void</span>* arg)</span>;</code></pre>
12453 <span>N-API version: 8</span>
12455 …ode class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_add_async_c…
12458 <span class="hljs-type">void</span>* arg,
12489 …e class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_remove_async_…
12500 <p>Node-API modules are registered in a manner similar to other modules
12503 <pre><code class="language-c">NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)</code></pre>
12504 <p>The next difference is the signature for the <code>Init</code> method. For a Node-API
12506 <pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class=…
12510 exported by the module. Node-API modules cannot modify the <code>module</code> object but
12514 <pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class=…
12517 <span class="hljs-string">"hello"</span>,
12518 <span class="hljs-literal">NULL</span>,
12520 <span class="hljs-literal">NULL</span>,
12521 <span class="hljs-literal">NULL</span>,
12522 <span class="hljs-literal">NULL</span>,
12524 <span class="hljs-literal">NULL</span>
12526 status = napi_define_properties(env, exports, <span class="hljs-number">1</span>, &#x26;desc);
12527 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12528 <span class="hljs-keyword">return</span> exports;
12531 <pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class=…
12534 …pi_create_function(env, <span class="hljs-string">"exports"</span>, NAPI_AUTO_LENGTH, Method, <spa…
12535 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12536 <span class="hljs-keyword">return</span> method;
12540 <pre><code class="language-c"><span class="hljs-comment">// <span class="hljs-doctag">NOTE:</span> …
12541 napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, n…
12544-string">"value"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</…
12545 DECLARE_NAPI_METHOD(<span class="hljs-string">"plusOne"</span>, PlusOne),
12546 DECLARE_NAPI_METHOD(<span class="hljs-string">"multiply"</span>, Multiply),
12551 …ne_class(env, <span class="hljs-string">"MyObject"</span>, New, <span class="hljs-literal">NULL</s…
12552 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12554 status = napi_create_reference(env, cons, <span class="hljs-number">1</span>, &#x26;constructor);
12555 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12557 status = napi_set_named_property(env, exports, <span class="hljs-string">"MyObject"</span>, cons);
12558 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12560 <span class="hljs-keyword">return</span> exports;
12564 <pre><code class="language-c">NAPI_MODULE_INIT() {
12568 status = napi_create_int64(env, <span class="hljs-number">42</span>, &#x26;answer);
12569 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12571 status = napi_set_named_property(env, exports, <span class="hljs-string">"answer"</span>, answer);
12572 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12574 <span class="hljs-keyword">return</span> exports;
12576 <p>All Node-API addons are context-aware, meaning they may be loaded multiple
12578 The documentation on <a href="#addons_context_aware_addons">context-aware addons</a> provides more …
12586 <p>Node-API exposes a set of APIs to create all types of JavaScript values.
12587 …e documented under <a href="https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values">…
12592 <li>Convert from a primitive C type to a Node-API value</li>
12593 <li>Convert from Node-API value to a primitive C type</li>
12596 <p>Node-API values are represented by the type <code>napi_value</code>.
12597 Any Node-API call that requires a JavaScript value takes in a <code>napi_value</code>.
12598 In some cases, the API does check the type of the <code>napi_value</code> up-front.
12605 <span>N-API version: 6</span>
12607 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
12619 <span>N-API version: 6</span>
12621 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
12622 napi_key_all_properties = <span class="hljs-number">0</span>,
12623 napi_key_writable = <span class="hljs-number">1</span>,
12624 …napi_key_enumerable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">1<…
12625 …napi_key_configurable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">…
12626 …napi_key_skip_strings = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">…
12627 …napi_key_skip_symbols = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">…
12633 <span>N-API version: 6</span>
12635 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
12643 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
12644 <span class="hljs-comment">// ES6 types (corresponds to typeof)</span>
12657 described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types">Section 6.1</a…
12663 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
12678 <a href="https://tc39.github.io/ecma262/#sec-typedarray-objects">Section 22.2</a> of the <a href="h…
12683 <span>N-API version: 1</span>
12685 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_array</spa…
12687 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
12691 <p>This API returns a Node-API value corresponding to a JavaScript <code>Array</code> type.
12693 <a href="https://tc39.github.io/ecma262/#sec-array-objects">Section 22.1</a> of the ECMAScript Lang…
12697 <span>N-API version: 1</span>
12699 …re><code class="language-c">napi_status <span class="hljs-title function_">napi_create_array_with_…
12700 <span class="hljs-type">size_t</span> length,
12708 <p>This API returns a Node-API value corresponding to a JavaScript <code>Array</code> type.
12709 The <code>Array</code>'s length property is set to the passed-in length parameter.
12710 However, the underlying buffer is not guaranteed to be pre-allocated by the VM
12716 <a href="https://tc39.github.io/ecma262/#sec-array-objects">Section 22.1</a> of the ECMAScript Lang…
12720 <span>N-API version: 1</span>
12722 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_arraybuffe…
12723 <span class="hljs-type">size_t</span> byte_length,
12724 <span class="hljs-type">void</span>** data,
12733 <p>This API returns a Node-API value corresponding to a JavaScript <code>ArrayBuffer</code>.
12734 <code>ArrayBuffer</code>s are used to represent fixed-length binary data buffers. They are
12735 normally used as a backing-buffer for <code>TypedArray</code> objects.
12743 <a href="https://tc39.github.io/ecma262/#sec-arraybuffer-objects">Section 24.1</a> of the ECMAScrip…
12747 <span>N-API version: 1</span>
12749 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_buffer</sp…
12750 <span class="hljs-type">size_t</span> size,
12751 <span class="hljs-type">void</span>** data,
12761 fully-supported data structure, in most cases using a <code>TypedArray</code> will suffice.</p>
12765 <span>N-API version: 1</span>
12767 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_buffer_cop…
12768 <span class="hljs-type">size_t</span> length,
12769 … <span class="hljs-keyword">const</span> <span class="hljs-type">void</span>* data,
12770 <span class="hljs-type">void</span>** result_data,
12782 from the passed-in buffer. While this is still a fully-supported data
12787 <span>N-API version: 5</span>
12789 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_date</span…
12790 <span class="hljs-type">double</span> time,
12802 <a href="https://tc39.github.io/ecma262/#sec-date-objects">Section 20.3</a> of the ECMAScript Langu…
12806 <span>N-API version: 1</span>
12808 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external</…
12809 <span class="hljs-type">void</span>* data,
12811 <span class="hljs-type">void</span>* finalize_hint,
12840 <span>N-API version: 1</span>
12842 <pre><code class="language-c">napi_status
12843 <span class="hljs-title function_">napi_create_external_arraybuffer</span><span class="hljs-params"…
12844 <span class="hljs-type">void</span>* external_data,
12845 <span class="hljs-type">size_t</span> byte_length,
12847 <span class="hljs-type">void</span>* finalize_hint,
12869 includes for the node-api headers. Doing so will hide the 2 functions
12872 <p>This API returns a Node-API value corresponding to a JavaScript <code>ArrayBuffer</code>.
12885 <a href="https://tc39.github.io/ecma262/#sec-arraybuffer-objects">Section 24.1</a> of the ECMAScrip…
12889 <span>N-API version: 1</span>
12891 …pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external_b…
12892 <span class="hljs-type">size_t</span> length,
12893 <span class="hljs-type">void</span>* data,
12895 <span class="hljs-type">void</span>* finalize_hint,
12917 includes for the node-api headers. Doing so will hide the 2 functions
12921 backed by the passed in buffer. While this is still a fully-supported data
12935 <span>N-API version: 1</span>
12937 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_object</sp…
12945 …/code> type is described in <a href="https://tc39.github.io/ecma262/#sec-object-type">Section 6.1.…
12950 <span>N-API version: 1</span>
12952 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_symbol</sp…
12962 <p>This API creates a JavaScript <code>symbol</code> value from a UTF8-encoded C string.</p>
12963 …/code> type is described in <a href="https://tc39.github.io/ecma262/#sec-symbol-objects">Section 1…
12968 <span>N-API version: 1</span>
12970 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_typedarray…
12972 <span class="hljs-type">size_t</span> length,
12974 <span class="hljs-type">size_t</span> byte_offset,
12987 <code>ArrayBuffer</code>. <code>TypedArray</code> objects provide an array-like view over an
12994 <a href="https://tc39.github.io/ecma262/#sec-typedarray-objects">Section 22.2</a> of the ECMAScript…
12998 <span>N-API version: 1</span>
13000 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_dataview</…
13001 <span class="hljs-type">size_t</span> byte_length,
13003 <span class="hljs-type">size_t</span> byte_offset,
13015 <code>DataView</code> objects provide an array-like view over an underlying data buffer,
13021 <a href="https://tc39.github.io/ecma262/#sec-dataview-objects">Section 24.3</a> of the ECMAScript L…
13022 <h4>Functions to convert from C types to Node-API<span><a class="mark" href="#n_api_functions_to_co…
13026 <span>N-API version: 1</span>
13028 …"language-c">napi_status <span class="hljs-title function_">napi_create_int32</span><span class="h…
13038 <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</…
13042 <span>N-API version: 1</span>
13044 …"language-c">napi_status <span class="hljs-title function_">napi_create_uint32</span><span class="…
13054 <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</…
13058 <span>N-API version: 1</span>
13060 …"language-c">napi_status <span class="hljs-title function_">napi_create_int64</span><span class="h…
13069 …s described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type"…
13072 …//tc39.github.io/ecma262/#sec-number.min_safe_integer"><code>Number.MIN_SAFE_INTEGER</code></a> <c…
13073 …"https://tc39.github.io/ecma262/#sec-number.max_safe_integer"><code>Number.MAX_SAFE_INTEGER</code>…
13077 <span>N-API version: 1</span>
13079 …"language-c">napi_status <span class="hljs-title function_">napi_create_double</span><span class="…
13082 <li><code>[in] value</code>: Double-precision value to be represented in JavaScript.</li>
13089 <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</…
13093 <span>N-API version: 6</span>
13095 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_int…
13096 <span class="hljs-type">int64_t</span> value,
13108 <span>N-API version: 6</span>
13110 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_uin…
13111 <span class="hljs-type">uint64_t</span> value,
13123 <span>N-API version: 6</span>
13125 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_wor…
13126 <span class="hljs-type">int</span> sign_bit,
13127 <span class="hljs-type">size_t</span> word_count,
13128 … <span class="hljs-keyword">const</span> <span class="hljs-type">uint64_t</span>* words,
13135 <li><code>[in] words</code>: An array of <code>uint64_t</code> little-endian 64-bit words.</li>
13139 <p>This API converts an array of unsigned 64-bit words into a single <code>BigInt</code>
13146 <span>N-API version: 1</span>
13148 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_lat…
13149 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* str,
13150 <span class="hljs-type">size_t</span> length,
13154 <li><code>[in] str</code>: Character buffer representing an ISO-8859-1-encoded string.</li>
13156 is null-terminated.</li>
13160 <p>This API creates a JavaScript <code>string</code> value from an ISO-8859-1-encoded C
13163 <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</…
13167 <span>N-API version: 1</span>
13169 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_utf…
13170 … <span class="hljs-keyword">const</span> <span class="hljs-type">char16_t</span>* str,
13171 <span class="hljs-type">size_t</span> length,
13175 <li><code>[in] str</code>: Character buffer representing a UTF16-LE-encoded string.</li>
13176 <li><code>[in] length</code>: The length of the string in two-byte code units, or
13177 <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li>
13181 <p>This API creates a JavaScript <code>string</code> value from a UTF16-LE-encoded C string.
13184 <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</…
13188 <span>N-API version: 1</span>
13190 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_utf…
13191 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* str,
13192 <span class="hljs-type">size_t</span> length,
13196 <li><code>[in] str</code>: Character buffer representing a UTF8-encoded string.</li>
13198 is null-terminated.</li>
13202 <p>This API creates a JavaScript <code>string</code> value from a UTF8-encoded C string.
13205 <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</…
13206 <h4>Functions to convert from Node-API to C types<span><a class="mark" href="#n_api_functions_to_co…
13210 <span>N-API version: 1</span>
13212 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_array_length<…
13214 … <span class="hljs-type">uint32_t</span>* result)</span></code></pre>
13223 … is described in <a href="https://tc39.github.io/ecma262/#sec-properties-of-array-instances-length…
13228 <span>N-API version: 1</span>
13230 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_arraybuffer_i…
13232 <span class="hljs-type">void</span>** data,
13233 … <span class="hljs-type">size_t</span>* byte_length)</span></code></pre>
13254 <span>N-API version: 1</span>
13256 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_buffer_info</…
13258 <span class="hljs-type">void</span>** data,
13259 <span class="hljs-type">size_t</span>* length)</span></code></pre>
13275 <span>N-API version: 1</span>
13277 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_prototype</sp…
13291 <span>N-API version: 1</span>
13293 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_typedarray_in…
13296 <span class="hljs-type">size_t</span>* length,
13297 <span class="hljs-type">void</span>** data,
13299 … <span class="hljs-type">size_t</span>* byte_offset)</span></code></pre>
13315 <code>data - byte_offset</code>.</li>
13324 <span>N-API version: 1</span>
13326 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_dataview_info…
13328 <span class="hljs-type">size_t</span>* byte_length,
13329 <span class="hljs-type">void</span>** data,
13331 … <span class="hljs-type">size_t</span>* byte_offset)</span></code></pre>
13348 <span>N-API version: 5</span>
13350 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_date_value</s…
13352 <span class="hljs-type">double</span>* result)</span></code></pre>
13361 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-date <code>napi_value</code> is pass…
13368 <span>N-API version: 1</span>
13370-c">napi_status <span class="hljs-title function_">napi_get_value_bool</span><span class="hljs-par…
13377 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-boolean <code>napi_value</code> is
13384 <span>N-API version: 1</span>
13386 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_double<…
13388 <span class="hljs-type">double</span>* result)</span></code></pre>
13395 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code> is pa…
13402 <span>N-API version: 6</span>
13404 …pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_…
13406 <span class="hljs-type">int64_t</span>* result,
13407 … <span class="hljs-type">bool</span>* lossless)</span>;</code></pre>
13416 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>BigInt</code> is passed in it
13423 <span>N-API version: 6</span>
13425 …re><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_u…
13427 <span class="hljs-type">uint64_t</span>* result,
13428 … <span class="hljs-type">bool</span>* lossless)</span>;</code></pre>
13437 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>BigInt</code> is passed in it
13444 <span>N-API version: 6</span>
13446 …pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_…
13448 <span class="hljs-type">int</span>* sign_bit,
13449 <span class="hljs-type">size_t</span>* word_count,
13450 … <span class="hljs-type">uint64_t</span>* words)</span>;</code></pre>
13459 <li><code>[out] words</code>: Pointer to a pre-allocated 64-bit word array.</li>
13462 <p>This API converts a single <code>BigInt</code> value into a sign bit, 64-bit little-endian
13468 <span>N-API version: 1</span>
13470 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_externa…
13472 … <span class="hljs-type">void</span>** result)</span></code></pre>
13478 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-external <code>napi_value</code> is
13485 <span>N-API version: 1</span>
13487 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_int32</…
13489 <span class="hljs-type">int32_t</span>* result)</span></code></pre>
13496 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
13502 positive number becoming a negative number if the value is > 2<sup>31</sup> - 1.</p>
13503 <p>Non-finite number values (<code>NaN</code>, <code>+Infinity</code>, or <code>-Infinity</code>) s…
13508 <span>N-API version: 1</span>
13510 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_int64</…
13512 <span class="hljs-type">int64_t</span>* result)</span></code></pre>
13519 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
13523 <p><code>number</code> values outside the range of <a href="https://tc39.github.io/ecma262/#sec-num…
13524 …de>-(2**53 - 1)</code> - <a href="https://tc39.github.io/ecma262/#sec-number.max_safe_integer"><co…
13526 <p>Non-finite number values (<code>NaN</code>, <code>+Infinity</code>, or <code>-Infinity</code>) s…
13531 <span>N-API version: 1</span>
13533 …re><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_l…
13535 <span class="hljs-type">char</span>* buf,
13536 <span class="hljs-type">size_t</span> bufsize,
13537 … <span class="hljs-type">size_t</span>* result)</span></code></pre>
13541 <li><code>[in] buf</code>: Buffer to write the ISO-8859-1-encoded string into. If <code>NULL</code>…
13545 insufficient, the returned string is truncated and null-terminated.</li>
13549 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value…
13551 <p>This API returns the ISO-8859-1-encoded string corresponding the value passed
13556 <span>N-API version: 1</span>
13558 …pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_…
13560 <span class="hljs-type">char</span>* buf,
13561 <span class="hljs-type">size_t</span> bufsize,
13562 … <span class="hljs-type">size_t</span>* result)</span></code></pre>
13566 <li><code>[in] buf</code>: Buffer to write the UTF8-encoded string into. If <code>NULL</code> is pa…
13570 insufficient, the returned string is truncated and null-terminated.</li>
13574 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value…
13576 <p>This API returns the UTF8-encoded string corresponding the value passed in.</p>
13580 <span>N-API version: 1</span>
13582 …pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_…
13584 <span class="hljs-type">char16_t</span>* buf,
13585 <span class="hljs-type">size_t</span> bufsize,
13586 … <span class="hljs-type">size_t</span>* result)</span></code></pre>
13590 <li><code>[in] buf</code>: Buffer to write the UTF16-LE-encoded string into. If <code>NULL</code> is
13591 passed in, the length of the string in 2-byte code units and excluding the
13594 insufficient, the returned string is truncated and null-terminated.</li>
13595 <li><code>[out] result</code>: Number of 2-byte code units copied into the buffer, excluding
13598 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value…
13600 <p>This API returns the UTF16-encoded string corresponding the value passed in.</p>
13604 <span>N-API version: 1</span>
13606 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_uint32<…
13608 … <span class="hljs-type">uint32_t</span>* result)</span></code></pre>
13615 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
13623 <span>N-API version: 1</span>
13625 …="language-c">napi_status <span class="hljs-title function_">napi_get_boolean</span><span class="h…
13638 <span>N-API version: 1</span>
13640 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_global</span>…
13650 <span>N-API version: 1</span>
13652 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_null</span><s…
13662 <span>N-API version: 1</span>
13664 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_undefined</sp…
13672 <p>Node-API exposes a set of APIs to perform some abstract operations on JavaScript
13673 …tions are documented under <a href="https://tc39.github.io/ecma262/#sec-abstract-operations">Secti…
13685 <span>N-API version: 1</span>
13687 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_bool</s…
13697 <a href="https://tc39.github.io/ecma262/#sec-toboolean">Section 7.1.2</a> of the ECMAScript Languag…
13698 This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p>
13702 <span>N-API version: 1</span>
13704 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_number<…
13714 <a href="https://tc39.github.io/ecma262/#sec-tonumber">Section 7.1.3</a> of the ECMAScript Language…
13715 This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p>
13719 <span>N-API version: 1</span>
13721 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_object<…
13731 <a href="https://tc39.github.io/ecma262/#sec-toobject">Section 7.1.13</a> of the ECMAScript Languag…
13732 This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p>
13736 <span>N-API version: 1</span>
13738 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_string<…
13748 <a href="https://tc39.github.io/ecma262/#sec-toobject">Section 7.1.13</a> of the ECMAScript Languag…
13749 This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p>
13753 <span>N-API version: 1</span>
13755 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_typeof</span><spa…
13767 the object as defined in <a href="https://tc39.github.io/ecma262/#sec-typeof-operator">Section 12.5…
13778 <span>N-API version: 1</span>
13780 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_instanceof</span>…
13783 <span class="hljs-type">bool</span>* result)</span></code></pre>
13794 defined in <a href="https://tc39.github.io/ecma262/#sec-instanceofoperator">Section 12.10.4</a> of …
13798 <span>N-API version: 1</span>
13800 …ge-c">napi_status <span class="hljs-title function_">napi_is_array</span><span class="hljs-params"…
13808 as defined in <a href="https://tc39.github.io/ecma262/#sec-isarray">Section 7.2.2</a> of the ECMASc…
13812 <span>N-API version: 1</span>
13814-c">napi_status <span class="hljs-title function_">napi_is_arraybuffer</span><span class="hljs-par…
13825 <span>N-API version: 1</span>
13827 …ge-c">napi_status <span class="hljs-title function_">napi_is_buffer</span><span class="hljs-params…
13839 <span>N-API version: 5</span>
13841 …age-c">napi_status <span class="hljs-title function_">napi_is_date</span><span class="hljs-params"…
13853 <span>N-API version: 1</span>
13855 …ge-c">napi_status <span class="hljs-title function_">napi_is_error</span><span class="hljs-params"…
13866 <span>N-API version: 1</span>
13868-c">napi_status <span class="hljs-title function_">napi_is_typedarray</span><span class="hljs-para…
13879 <span>N-API version: 1</span>
13881 …e-c">napi_status <span class="hljs-title function_">napi_is_dataview</span><span class="hljs-param…
13892 <span>N-API version: 1</span>
13894 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_strict_equals</sp…
13897 <span class="hljs-type">bool</span>* result)</span></code></pre>
13906 defined in <a href="https://tc39.github.io/ecma262/#sec-strict-equality-comparison">Section 7.2.14<…
13910 <span>N-API version: 7</span>
13912 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_detach_arraybuffe…
13918 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-detachable <code>ArrayBuffer</code> …
13920 <p>Generally, an <code>ArrayBuffer</code> is non-detachable if it has been detached before.
13925 defined in <a href="https://tc39.es/ecma262/#sec-detacharraybuffer">Section 24.1.1.3</a> of the ECM…
13929 <span>N-API version: 7</span>
13931 …re><code class="language-c">napi_status <span class="hljs-title function_">napi_is_detached_arrayb…
13933 … <span class="hljs-type">bool</span>* result)</span></code></pre>
13942 operation as defined in <a href="https://tc39.es/ecma262/#sec-isdetachedbuffer">Section 24.1.1.2</a…
13945 <p>Node-API exposes a set of APIs to get and set properties on JavaScript
13946 …types are documented under <a href="https://tc39.github.io/ecma262/#sec-abstract-operations">Secti…
13949 Fundamentally, all property keys in Node-API can be represented in one of the
13952 <li>Named: a simple UTF8-encoded string</li>
13953 <li>Integer-Indexed: an index value represented by <code>uint32_t</code></li>
13954 <li>JavaScript value: these are represented in Node-API by <code>napi_value</code>. This can
13957 <p>Node-API values are represented by the type <code>napi_value</code>.
13958 Any Node-API call that requires a JavaScript value takes in a <code>napi_value</code>.
13965 <pre><code class="language-js"><span class="hljs-keyword">const</span> obj = {};
13966 obj.<span class="hljs-property">myProp</span> = <span class="hljs-number">123</span>;</code></pre>
13967 <p>The equivalent can be done using Node-API values with the following snippet:</p>
13968 <pre><code class="language-c">napi_status status = napi_generic_failure;
13970 <span class="hljs-comment">// const obj = {}</span>
13973 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13975 <span class="hljs-comment">// Create a napi_value for 123</span>
13976 status = napi_create_int32(env, <span class="hljs-number">123</span>, &#x26;value);
13977 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13979 <span class="hljs-comment">// obj.myProp = 123</span>
13980 status = napi_set_named_property(env, obj, <span class="hljs-string">"myProp"</span>, value);
13981 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13984 <pre><code class="language-js"><span class="hljs-keyword">const</span> arr = [];
13985 arr[<span class="hljs-number">123</span>] = <span class="hljs-string">'hello'</span>;</code></pre>
13986 <p>The equivalent can be done using Node-API values with the following snippet:</p>
13987 <pre><code class="language-c">napi_status status = napi_generic_failure;
13989 <span class="hljs-comment">// const arr = [];</span>
13992 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13994 <span class="hljs-comment">// Create a napi_value for 'hello'</span>
13995 status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &…
13996 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13998 <span class="hljs-comment">// arr[123] = 'hello';</span>
13999 status = napi_set_element(env, arr, <span class="hljs-number">123</span>, value);
14000 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
14003 <pre><code class="language-js"><span class="hljs-keyword">const</span> arr = [];
14004 <span class="hljs-keyword">const</span> value = arr[<span class="hljs-number">123</span>];</code></…
14005 <p>The following is the approximate equivalent of the Node-API counterpart:</p>
14006 <pre><code class="language-c">napi_status status = napi_generic_failure;
14008 <span class="hljs-comment">// const arr = []</span>
14011 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
14013 <span class="hljs-comment">// const value = arr[123]</span>
14014 status = napi_get_element(env, arr, <span class="hljs-number">123</span>, &#x26;value);
14015 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
14018 <pre><code class="language-js"><span class="hljs-keyword">const</span> obj = {};
14019 <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">defineProperties</…
14020-string">'foo'</span>: { <span class="hljs-attr">value</span>: <span class="hljs-number">123</span…
14021-string">'bar'</span>: { <span class="hljs-attr">value</span>: <span class="hljs-number">456</span…
14023 <p>The following is the approximate equivalent of the Node-API counterpart:</p>
14024 <pre><code class="language-c">napi_status status = napi_status_generic_failure;
14026 <span class="hljs-comment">// const obj = {};</span>
14029 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
14031 <span class="hljs-comment">// Create napi_values for 123 and 456</span>
14033 status = napi_create_int32(env, <span class="hljs-number">123</span>, &#x26;fooValue);
14034 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
14035 status = napi_create_int32(env, <span class="hljs-number">456</span>, &#x26;barValue);
14036 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
14038 <span class="hljs-comment">// Set the properties</span>
14040-string">"foo"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</sp…
14041-string">"bar"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</sp…
14045 …<span class="hljs-keyword">sizeof</span>(descriptors) / <span class="hljs-keyword">sizeof</span>(d…
14047 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
14059 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
14060 napi_default = <span class="hljs-number">0</span>,
14061 …napi_writable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">0</span>,
14062 …napi_enumerable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">1</spa…
14063 …napi_configurable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">2</s…
14065 <span class="hljs-comment">// Used with napi_define_class to distinguish static properties</span>
14066 <span class="hljs-comment">// from instance properties. Ignored by napi_define_properties.</span>
14067 napi_static = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">10</span>,
14069 <span class="hljs-comment">// Default for class methods.</span>
14072 <span class="hljs-comment">// Default for object properties, like in JS obj[prop].</span>
14079 attributes listed in <a href="https://tc39.github.io/ecma262/#table-2">Section 6.1.7.1</a>
14088 <a href="https://tc39.github.io/ecma262/#table-2">Section 6.1.7.1</a> of the <a href="https://tc39.…
14098 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
14099 <span class="hljs-comment">// One of utf8name or name should be NULL.</span>
14100 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8name;
14109 <span class="hljs-type">void</span>* data;
14125 performed using a Node-API call). <a href="#n_api_napi_callback"><code>napi_callback</code></a> pro…
14130 performed using a Node-API call). <a href="#n_api_napi_callback"><code>napi_callback</code></a> pro…
14144 <span>N-API version: 1</span>
14146 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_property_name…
14150 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14164 <span>N-API version: 6</span>
14166 <pre><code class="language-c">napi_get_all_property_names(napi_env env,
14173 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14189 <span>N-API version: 1</span>
14191 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_property</spa…
14196 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14206 <span>N-API version: 1</span>
14208 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_property</spa…
14213 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14223 <span>N-API version: 1</span>
14225 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_property</spa…
14228 <span class="hljs-type">bool</span>* result)</span>;</code></pre>
14230 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14240 <span>N-API version: 1</span>
14242 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_property</…
14245 <span class="hljs-type">bool</span>* result)</span>;</code></pre>
14247 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14258 <span>N-API version: 1</span>
14260 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_own_property<…
14263 <span class="hljs-type">bool</span>* result)</span>;</code></pre>
14265 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14272 be a <code>string</code> or a <code>symbol</code>, or an error will be thrown. Node-API will not
14277 <span>N-API version: 1</span>
14279 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_named_propert…
14281 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8Name,
14284 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14295 <span>N-API version: 1</span>
14297 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_named_propert…
14299 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8Name,
14302 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14313 <span>N-API version: 1</span>
14315 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_named_propert…
14317 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8Name,
14318 … <span class="hljs-type">bool</span>* result)</span>;</code></pre>
14320 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14331 <span>N-API version: 1</span>
14333 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_element</span…
14335 <span class="hljs-type">uint32_t</span> index,
14338 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14348 <span>N-API version: 1</span>
14350 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_element</span…
14352 <span class="hljs-type">uint32_t</span> index,
14355 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14365 <span>N-API version: 1</span>
14367 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_element</span…
14369 <span class="hljs-type">uint32_t</span> index,
14370 <span class="hljs-type">bool</span>* result)</span>;</code></pre>
14372 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14383 <span>N-API version: 1</span>
14385 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_element</s…
14387 <span class="hljs-type">uint32_t</span> index,
14388 <span class="hljs-type">bool</span>* result)</span>;</code></pre>
14390 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14401 <span>N-API version: 1</span>
14403 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_define_properties…
14405 <span class="hljs-type">size_t</span> property_count,
14406 … <span class="hljs-keyword">const</span> napi_property_descriptor* properties)</span>;</code></pre>
14408 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14418 …github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-des…
14423 <span>N-API version: 8</span>
14425 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_object_freeze</sp…
14428 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14437 in <a href="https://tc39.es/ecma262/#sec-object.freeze">Section 19.1.2.6</a> of the
14438 ECMA-262 specification.</p>
14442 <span>N-API version: 8</span>
14444 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_object_seal</span…
14447 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14452 added to it, as well as marking all existing properties as non-configurable.
14453 This is described in <a href="https://tc39.es/ecma262/#sec-object.seal">Section 19.1.2.20</a>
14454 of the ECMA-262 specification.</p>
14456 <p>Node-API provides a set of APIs that allow JavaScript code to
14457 call back into native code. Node-APIs that support calling back
14468 <p>Additionally, Node-API provides a set of functions which allow calling
14472 <p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> field of the
14474 whenever <code>object</code> is garbage-collected by passing both <code>object</code> and the data …
14479 <span>N-API version: 1</span>
14481 …e><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_call_fu…
14484 <span class="hljs-type">size_t</span> argc,
14485 <span class="hljs-keyword">const</span> napi_value* argv,
14498 add-on. This is the primary mechanism of calling back <em>from</em> the add-on's
14503 …ode class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title functio…
14504 <span class="hljs-keyword">return</span> num + <span class="hljs-number">2</span>;
14506 <p>Then, the above function can be invoked from a native add-on using the
14508 <pre><code class="language-c"><span class="hljs-comment">// Get the function named "AddTwo" on the …
14511 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14513 status = napi_get_named_property(env, global, <span class="hljs-string">"AddTwo"</span>, &#x26;add_…
14514 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14516 <span class="hljs-comment">// const arg = 1337</span>
14517 status = napi_create_int32(env, <span class="hljs-number">1337</span>, &#x26;arg);
14518 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14521 <span class="hljs-type">size_t</span> argc = <span class="hljs-number">1</span>;
14523 <span class="hljs-comment">// AddTwo(arg);</span>
14526 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14528 <span class="hljs-comment">// Convert the result back to a native type</span>
14529 <span class="hljs-type">int32_t</span> result;
14531 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;<…
14535 <span>N-API version: 1</span>
14537 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_function</…
14538 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8name,
14539 <span class="hljs-type">size_t</span> length,
14541 <span class="hljs-type">void</span>* data,
14548 it is null-terminated.</li>
14551 <li><code>[in] data</code>: User-provided data context. This will be passed back into the
14557 <p>This API allows an add-on author to create a function object in native code.
14558 This is the primary mechanism to allow calling <em>into</em> the add-on's native code
14564 add-on's module exports, set the newly created function on the exports
14566 <pre><code class="language-c">napi_value <span class="hljs-title function_">SayHello</span><span cl…
14567 <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello\n"</span>);
14568 <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
14571 napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, n…
14575 …e_function(env, <span class="hljs-literal">NULL</span>, <span class="hljs-number">0</span>, SayHel…
14576 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14578 status = napi_set_named_property(env, exports, <span class="hljs-string">"sayHello"</span>, fn);
14579 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14581 <span class="hljs-keyword">return</span> exports;
14585 <p>Given the above code, the add-on can be used from JavaScript as follows:</p>
14586 …de class="language-js"><span class="hljs-keyword">const</span> myaddon = <span class="hljs-built_i…
14587 myaddon.<span class="hljs-title function_">sayHello</span>();</code></pre>
14590 <p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> parameter c…
14592 <code>result</code> parameter) and freed whenever the function is garbage-collected by
14594 …n</code>s are described in <a href="https://tc39.github.io/ecma262/#sec-function-objects">Section …
14599 <span>N-API version: 1</span>
14601 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_cb_info</span…
14603 <span class="hljs-type">size_t</span>* argc,
14606 <span class="hljs-type">void</span>** data)</span></code></pre>
14610 <li><code>[in-out] argc</code>: Specifies the length of the provided <code>argv</code> array and
14626 <span>N-API version: 1</span>
14628 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_new_target</s…
14642 <span>N-API version: 1</span>
14644 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_new_instance</spa…
14646 <span class="hljs-type">size_t</span> argc,
14662 …de class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function…
14663 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">param</span> = param;
14666 <span class="hljs-keyword">const</span> arg = <span class="hljs-string">'hello'</span>;
14667 <span class="hljs-keyword">const</span> value = <span class="hljs-keyword">new</span> <span class="…
14668 <p>The following can be approximated in Node-API using the following snippet:</p>
14669 <pre><code class="language-c"><span class="hljs-comment">// Get the constructor function MyObject</…
14672 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14674 status = napi_get_named_property(env, global, <span class="hljs-string">"MyObject"</span>, &#x26;co…
14675 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14677 <span class="hljs-comment">// const arg = "hello"</span>
14678 status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &…
14679 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14682 <span class="hljs-type">size_t</span> argc = <span class="hljs-number">1</span>;
14684 <span class="hljs-comment">// const value = new MyObject(arg)</span>
14688 <p>Node-API offers a way to "wrap" C++ classes and instances so that the class
14706 <pre><code class="language-c">napi_value MyClass_constructor = <span class="hljs-literal">NULL</spa…
14709 <span class="hljs-type">bool</span> is_instance = <span class="hljs-literal">false</span>;
14712 <span class="hljs-keyword">if</span> (is_instance) {
14713 <span class="hljs-comment">// napi_unwrap() ...</span>
14714 } <span class="hljs-keyword">else</span> {
14715 <span class="hljs-comment">// otherwise...</span>
14723 …de class="language-js"><span class="hljs-keyword">const</span> myAddon = <span class="hljs-built_i…
14725 <span class="hljs-comment">// `openDatabase()` returns a JavaScript object that wraps a native data…
14726 <span class="hljs-comment">// handle.</span>
14727 <span class="hljs-keyword">const</span> dbHandle = myAddon.<span class="hljs-title function_">openD…
14729 <span class="hljs-comment">// `query()` returns a JavaScript object that wraps a native query handl…
14730 …span class="hljs-keyword">const</span> queryHandle = myAddon.<span class="hljs-title function_">qu…
14732 <span class="hljs-comment">// There is an accidental error in the line below. The first parameter t…
14733 <span class="hljs-comment">// `myAddon.queryHasRecords()` should be the database handle (`dbHandle`…
14734 <span class="hljs-comment">// the query handle (`query`), so the correct condition for the while-lo…
14735 <span class="hljs-comment">// should be</span>
14736 <span class="hljs-comment">//</span>
14737 <span class="hljs-comment">// myAddon.queryHasRecords(dbHandle, queryHandle)</span>
14738 <span class="hljs-comment">//</span>
14739 <span class="hljs-keyword">while</span> (myAddon.<span class="hljs-title function_">queryHasRecords…
14740 <span class="hljs-comment">// retrieve records</span>
14763 <p>To this end, Node-API provides type-tagging capabilities.</p>
14764 <p>A type tag is a 128-bit integer unique to the addon. Node-API provides the
14771 previously "marked" with the type tag. This creates a type-checking capability
14772 of a higher fidelity than <code>napi_instanceof()</code> can provide, because such type-
14777 <pre><code class="language-c"><span class="hljs-comment">// This value is the type tag for a databa…
14778 <span class="hljs-comment">//</span>
14779 <span class="hljs-comment">// uuidgen | sed -r -e 's/-//g' -e 's/(.{16})(.*)/0x\1, 0x\2/'</span>
14780 <span class="hljs-comment">//</span>
14781 <span class="hljs-comment">// can be used to obtain the two values with which to initialize the str…
14782 <span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> napi_type_tag Data…
14783 …<span class="hljs-number">0x1edf75a38336451d</span>, <span class="hljs-number">0xa5ed9ce2e4c00c38<…
14786 <span class="hljs-comment">// This value is the type tag for a query handle.</span>
14787 <span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> napi_type_tag Quer…
14788 …<span class="hljs-number">0x9c73317f9fad44a3</span>, <span class="hljs-number">0x93c3920bf3b0ad6a<…
14791 <span class="hljs-keyword">static</span> napi_value
14792 <span class="hljs-title function_">openDatabase</span><span class="hljs-params">(napi_env env, napi…
14796 …<span class="hljs-comment">// Perform the underlying action which results in a database handle.</s…
14799 <span class="hljs-comment">// Create a new, empty JS object.</span>
14801 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14803 …<span class="hljs-comment">// Tag the object to indicate that it holds a pointer to a `DatabaseHan…
14805 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14807 …<span class="hljs-comment">// Store the pointer to the `DatabaseHandle` structure inside the JS ob…
14808 … result, dbHandle, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>,…
14809 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14811 <span class="hljs-keyword">return</span> result;
14814 <span class="hljs-comment">// Later when we receive a JavaScript object purporting to be a database…
14815 <span class="hljs-comment">// we can use `napi_check_object_type_tag()` to ensure that it is indeed…
14816 <span class="hljs-comment">// handle.</span>
14818 <span class="hljs-keyword">static</span> napi_value
14819 <span class="hljs-title function_">query</span><span class="hljs-params">(napi_env env, napi_callba…
14821 <span class="hljs-type">size_t</span> argc = <span class="hljs-number">2</span>;
14822 napi_value argv[<span class="hljs-number">2</span>];
14823 <span class="hljs-type">bool</span> is_db_handle;
14825 …t_cb_info(env, info, &#x26;argc, argv, <span class="hljs-literal">NULL</span>, <span class="hljs-l…
14826 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14828 …<span class="hljs-comment">// Check that the object passed as the first parameter has the previous…
14829 <span class="hljs-comment">// applied tag.</span>
14831 argv[<span class="hljs-number">0</span>],
14834 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14836 <span class="hljs-comment">// Throw a `TypeError` if it doesn't.</span>
14837 <span class="hljs-keyword">if</span> (!is_db_handle) {
14838 <span class="hljs-comment">// Throw a TypeError.</span>
14839 <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
14845 <span>N-API version: 1</span>
14847 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_define_class</spa…
14848 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8name,
14849 <span class="hljs-type">size_t</span> length,
14851 <span class="hljs-type">void</span>* data,
14852 <span class="hljs-type">size_t</span> property_count,
14853 … <span class="hljs-keyword">const</span> napi_property_descriptor* properties,
14861 if it is null-terminated.</li>
14886 C++ class, <em>non-static</em> data properties, accessors, and methods of the C++
14899 that case, to prevent the function value from being garbage-collected, a
14902 <p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> parameter o…
14905 parameter) and freed whenever the class is garbage-collected by passing both
14910 <span>N-API version: 1</span>
14912 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_wrap</span><span …
14914 <span class="hljs-type">void</span>* native_object,
14916 <span class="hljs-type">void</span>* finalize_hint,
14925 native instance when the JavaScript object is ready for garbage-collection.
14937 <code>napi_wrap()</code> to wrap the newly constructed instance in the already-created
14958 <span>N-API version: 1</span>
14960 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_unwrap</span><spa…
14962 <span class="hljs-type">void</span>** result)</span>;</code></pre>
14979 <span>N-API version: 1</span>
14981 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_remove_wrap</span…
14983 <span class="hljs-type">void</span>** result)</span>;</code></pre>
14993 JavaScript object becomes garbage-collected.</p>
14997 <span>N-API version: 8</span>
14999 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_type_tag_object</…
15001 … <span class="hljs-keyword">const</span> napi_type_tag* type_tag)</span>;</code></pre>
15017 <span>N-API version: 8</span>
15019 …pre><code class="language-c">napi_status <span class="hljs-title function_">napi_check_object_type…
15021 … <span class="hljs-keyword">const</span> napi_type_tag* type_tag,
15022 … <span class="hljs-type">bool</span>* result)</span>;</code></pre>
15038 <span>N-API version: 5</span>
15040 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_add_finalizer</sp…
15042 <span class="hljs-type">void</span>* native_object,
15044 <span class="hljs-type">void</span>* finalize_hint,
15053 native data when the JavaScript object is ready for garbage-collection.
15080 <p>Node-API provides an ABI-stable interface for these
15082 <p>Node-API defines the <code>napi_async_work</code> structure which is used to manage
15088 <p>The <code>execute</code> function should avoid making any Node-API calls
15090 JavaScript objects. Most often, any code that needs to make Node-API
15095-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span…
15096 <span class="hljs-type">void</span>* data)</span>;
15097-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">…
15099 … <span class="hljs-type">void</span>* data)</span>;</code></pre>
15101 addon-provided <code>void*</code> data that was passed into the
15105 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_queue_async_work<…
15124 <span>N-API version: 1</span>
15126 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_async_work…
15131 <span class="hljs-type">void</span>* data,
15146 <li><code>[in] data</code>: User-provided data context. This will be passed back into the
15155 <p><code>async_resource_name</code> should be a null-terminated, UTF-8-encoded string.</p>
15163 <span>N-API version: 1</span>
15165 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_async_work…
15177 <span>N-API version: 1</span>
15179 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_queue_async_work<…
15192 <span>N-API version: 1</span>
15194 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_cancel_async_work…
15216 <span>N-API version: 1</span>
15218 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_async_init</span>…
15250 <span>N-API version: 1</span>
15252 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_async_destroy</sp…
15271 <span>N-API version: 1</span>
15273 …e><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_make_ca…
15277 <span class="hljs-type">size_t</span> argc,
15278 <span class="hljs-keyword">const</span> napi_value* argv,
15298 add-on. This API is similar to <code>napi_call_function</code>. However, it is used to call
15313 <span>N-API version: 3</span>
15315 …code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_open_callb…
15331 in place when making certain Node-API calls. If there is no other script on
15338 <span>N-API version: 3</span>
15340 …code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_close_call…
15351 <span>N-API version: 1</span>
15353 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
15354 <span class="hljs-type">uint32_t</span> major;
15355 <span class="hljs-type">uint32_t</span> minor;
15356 <span class="hljs-type">uint32_t</span> patch;
15357 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* release;
15360 napi_status <span class="hljs-title function_">napi_get_node_version</span><span class="hljs-params…
15361 … <span class="hljs-keyword">const</span> napi_node_version** version)</span>;</code></pre>
15374 <span>N-API version: 1</span>
15376 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_version</span…
15377 <span class="hljs-type">uint32_t</span>* result)</span>;</code></pre>
15380 <li><code>[out] result</code>: The highest version of Node-API supported.</li>
15383 <p>This API returns the highest Node-API version supported by the
15384 Node.js runtime. Node-API is planned to be additive such that
15401 <span>N-API version: 1</span>
15403 …ode class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_adjust_exte…
15404 … <span class="hljs-type">int64_t</span> change_in_bytes,
15405 … <span class="hljs-type">int64_t</span>* result)</span>;</code></pre>
15419 <p>Node-API provides facilities for creating <code>Promise</code> objects as described in
15420 <a href="https://tc39.github.io/ecma262/#sec-promise-objects">Section 25.4</a> of the ECMA specific…
15429 <pre><code class="language-c">napi_deferred deferred;
15433 <span class="hljs-comment">// Create the promise.</span>
15435 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <…
15437 <span class="hljs-comment">// Pass the deferred to a function that performs an asynchronous action.…
15440 <span class="hljs-comment">// Return the promise to JS</span>
15441 <span class="hljs-keyword">return</span> promise;</code></pre>
15445 <pre><code class="language-c">napi_deferred deferred;
15449 <span class="hljs-comment">// Create a value with which to conclude the deferred.</span>
15451 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <…
15453 <span class="hljs-comment">// Resolve or reject the promise associated with the deferred depending …
15454 <span class="hljs-comment">// whether the asynchronous action succeeded.</span>
15455 <span class="hljs-keyword">if</span> (asynchronous_action_succeeded) {
15457 } <span class="hljs-keyword">else</span> {
15460 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <…
15462 <span class="hljs-comment">// At this point the deferred has been freed, so we should assign NULL t…
15463 deferred = <span class="hljs-literal">NULL</span>;</code></pre>
15467 <span>N-API version: 1</span>
15469 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_promise</s…
15484 <span>N-API version: 1</span>
15486 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_resolve_deferred<…
15504 <span>N-API version: 1</span>
15506 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_reject_deferred</…
15524 <span>N-API version: 1</span>
15526 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_promise</span>…
15528 <span class="hljs-type">bool</span>* is_promise)</span>;</code></pre>
15536 <p>Node-API provides an API for executing a string containing JavaScript using the
15541 <span>N-API version: 1</span>
15543 …pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_run_s…
15556 <a href="#modules_the_module_scope">module scope</a>, meaning that pseudo-globals such as <code>req…
15565 <p>Node-API provides a function for getting the current event loop associated with
15570 <span>N-API version: 2</span>
15572 …<code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_get_uv_ev…
15573 … <span class="hljs-keyword">struct</span> uv_loop_s** loop)</span>;</code></pre>
15578 </section><section><h3>Asynchronous thread-safe function calls<span><a class="mark" href="#n_api_as…
15580 thread. If an addon creates additional threads, then Node-API functions that
15586 JavaScript function on their behalf. The thread-safe function APIs provide an
15596 provided. This callback will be invoked on the main thread when the thread-safe
15600 no threads should be using the thread-safe function after the finalize callback
15605 <h4>Calling a thread-safe function<span><a class="mark" href="#n_api_calling_a_thread_safe_function…
15609 behaves non-blockingly, returning <code>napi_queue_full</code> if the queue was full,
15612 <code>napi_call_threadsafe_function()</code> never blocks if the thread-safe function was
15630 Node.js process exits while there is a thread-safe function still active.</p>
15632 Node-API runs <code>call_js_cb</code> in a context appropriate for callbacks.</p>
15633 <h4>Reference counting of thread-safe functions<span><a class="mark" href="#n_api_reference_countin…
15637 indicate that a new thread will start making use of the thread-safe function.
15639 existing thread will stop making use of the thread-safe function.</p>
15648 reason, do not use a thread-safe function
15654 number of aquisitions of the thread-safe functions, instead of calling
15661 <p>The thread-safe function can be "aborted" by giving a value of <code>napi_tsfn_abort</code>
15663 associated with the thread-safe function except
15667 possible to make asynchronous calls to the thread-safe function. This can be
15670 the thread-safe function anymore because it is no longer guaranteed to
15673 <p>Similarly to libuv handles, thread-safe functions can be "referenced" and
15674 "unreferenced". A "referenced" thread-safe function will cause the event loop on
15675 the thread on which it is created to remain alive until the thread-safe function
15676 is destroyed. In contrast, an "unreferenced" thread-safe function will not
15679 <p>Neither does <code>napi_unref_threadsafe_function</code> mark the thread-safe functions as
15693 <span>N-API version: 4</span>
15695 <pre><code class="language-c">NAPI_EXTERN napi_status
15696 <span class="hljs-title function_">napi_create_threadsafe_function</span><span class="hljs-params">…
15700 <span class="hljs-type">size_t</span> max_queue_size,
15701 <span class="hljs-type">size_t</span> initial_thread_count,
15702 <span class="hljs-type">void</span>* thread_finalize_data,
15704 <span class="hljs-type">void</span>* context,
15730 <li><code>[out] result</code>: The asynchronous thread-safe JavaScript function.</li>
15735 <span>N-API version: 4</span>
15737 <pre><code class="language-c">NAPI_EXTERN napi_status
15738 <span class="hljs-title function_">napi_get_threadsafe_function_context</span><span class="hljs-par…
15739 … <span class="hljs-type">void</span>** result)</span>;</code></pre>
15741 <li><code>[in] func</code>: The thread-safe function for which to retrieve the context.</li>
15758 <span>N-API version: 4</span>
15760 <pre><code class="language-c">NAPI_EXTERN napi_status
15761 <span class="hljs-title function_">napi_call_threadsafe_function</span><span class="hljs-params">(n…
15762 <span class="hljs-type">void</span>* data,
15765 <li><code>[in] func</code>: The asynchronous thread-safe JavaScript function to invoke.</li>
15767 provided during the creation of the thread-safe JavaScript function.</li>
15783 <span>N-API version: 4</span>
15785 <pre><code class="language-c">NAPI_EXTERN napi_status
15786 <span class="hljs-title function_">napi_acquire_threadsafe_function</span><span class="hljs-params"…
15788 <li><code>[in] func</code>: The asynchronous thread-safe JavaScript function to start making
15791 <p>A thread should call this API before passing <code>func</code> to any other thread-safe
15799 <span>N-API version: 4</span>
15801 <pre><code class="language-c">NAPI_EXTERN napi_status
15802 <span class="hljs-title function_">napi_release_threadsafe_function</span><span class="hljs-params"…
15805 <li><code>[in] func</code>: The asynchronous thread-safe JavaScript function whose reference
15808 that the current thread will make no further calls to the thread-safe
15810 thread, no other thread should make any further calls to the thread-safe
15816 to any thread-safe APIs after having called this API has undefined results, as
15822 <span>N-API version: 4</span>
15824 <pre><code class="language-c">NAPI_EXTERN napi_status
15825 <span class="hljs-title function_">napi_ref_threadsafe_function</span><span class="hljs-params">(na…
15828 <li><code>[in] func</code>: The thread-safe function to reference.</li>
15833 <p>Neither does <code>napi_unref_threadsafe_function</code> mark the thread-safe functions as
15841 <span>N-API version: 4</span>
15843 <pre><code class="language-c">NAPI_EXTERN napi_status
15844 <span class="hljs-title function_">napi_unref_threadsafe_function</span><span class="hljs-params">(…
15847 <li><code>[in] func</code>: The thread-safe function to unreference.</li>
15858 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
15859 <pre><code class="language-c">NAPI_EXTERN napi_status
15860-title function_">node_api_get_module_file_name</span><span class="hljs-params">(napi_env env, <sp…
15865 location from which the add-on was loaded. For a file on the local
15866 file system it will start with <code>file://</code>. The string is null-terminated and
15869 <p><code>result</code> may be an empty string if the add-on loading process fails to establish
15870 the add-on's file name during loading.</p></section>
15881 <a href="deprecations.html">deprecation policy</a> and may occur on each semver-major release witho…
15886 <code>node -e &#x3C;code></code>, i.e. that will take a piece of JavaScript and run it in
15887 a Node.js-specific environment.</p>
15889 <h4>Setting up per-process state<span><a class="mark" href="#embedding_setting_up_per_process_state…
15890 <p>Node.js requires some per-process state management in order to run:</p>
15893 <li>V8 per-process requirements, such as a <code>v8::Platform</code> instance.</li>
15897-cpp"><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">main…
15898 argv = <span class="hljs-built_in">uv_setup_args</span>(argc, argv);
15899 …<span class="hljs-function">std::vector&#x3C;std::string> <span class="hljs-title">args</span><spa…
15902 …<span class="hljs-comment">// Parse Node.js CLI options, and print any errors that have occurred w…
15903 <span class="hljs-comment">// trying to parse them.</span>
15904 …<span class="hljs-type">int</span> exit_code = node::<span class="hljs-built_in">InitializeNodeWit…
15905 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> std::string&#x26; e…
15906-built_in">fprintf</span>(stderr, <span class="hljs-string">"%s: %s\n"</span>, args[<span class="h…
15907 <span class="hljs-keyword">if</span> (exit_code != <span class="hljs-number">0</span>) {
15908 <span class="hljs-keyword">return</span> exit_code;
15911 …<span class="hljs-comment">// Create a v8::Platform instance. `MultiIsolatePlatform::Create()` is …
15912 …<span class="hljs-comment">// to create a v8::Platform instance that Node.js can use when creating…
15913 …<span class="hljs-comment">// Worker threads. When no `MultiIsolatePlatform` instance is present,<…
15914 <span class="hljs-comment">// Worker threads are disabled.</span>
15916 …MultiIsolatePlatform::<span class="hljs-built_in">Create</span>(<span class="hljs-number">4</span>…
15917 …V8::<span class="hljs-built_in">InitializePlatform</span>(platform.<span class="hljs-built_in">get…
15918 V8::<span class="hljs-built_in">Initialize</span>();
15920 <span class="hljs-comment">// See below for the contents of this function.</span>
15921 …<span class="hljs-type">int</span> ret = <span class="hljs-built_in">RunNodeInstance</span>(platfo…
15923 V8::<span class="hljs-built_in">Dispose</span>();
15924 V8::<span class="hljs-built_in">ShutdownPlatform</span>();
15925 <span class="hljs-keyword">return</span> ret;
15927 <h4>Per-instance state<span><a class="mark" href="#embedding_per_instance_state" id="embedding_per_…
15949 sets it up with some Node.js-specific hooks (e.g. the Node.js error handler),
15951 …"language-cpp"><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-ti…
15952 … <span class="hljs-keyword">const</span> std::vector&#x3C;std::string>&#x26; args,
15953 …<span class="hljs-keyword">const</span> std::vector&#x3C;std::string>&#x26; exec_args)</span> </sp…
15954 <span class="hljs-type">int</span> exit_code = <span class="hljs-number">0</span>;
15955 <span class="hljs-comment">// Set up a libuv event loop.</span>
15956 <span class="hljs-type">uv_loop_t</span> loop;
15957 …<span class="hljs-type">int</span> ret = <span class="hljs-built_in">uv_loop_init</span>(&#x26;loo…
15958 <span class="hljs-keyword">if</span> (ret != <span class="hljs-number">0</span>) {
15959 …<span class="hljs-built_in">fprintf</span>(stderr, <span class="hljs-string">"%s: Failed to initia…
15960 args[<span class="hljs-number">0</span>].<span class="hljs-built_in">c_str</span>(),
15961 <span class="hljs-built_in">uv_err_name</span>(ret));
15962 <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
15966 ArrayBufferAllocator::<span class="hljs-built_in">Create</span>();
15968 Isolate* isolate = <span class="hljs-built_in">NewIsolate</span>(allocator, &#x26;loop, platform);
15969 <span class="hljs-keyword">if</span> (isolate == <span class="hljs-literal">nullptr</span>) {
15970-built_in">fprintf</span>(stderr, <span class="hljs-string">"%s: Failed to initialize V8 Isolate\n…
15971 <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
15975 …<span class="hljs-function">Locker <span class="hljs-title">locker</span><span class="hljs-params"…
15976 …<span class="hljs-function">Isolate::Scope <span class="hljs-title">isolate_scope</span><span clas…
15978 …<span class="hljs-comment">// Create a node::IsolateData instance that will later be released usin…
15979 <span class="hljs-comment">// node::FreeIsolateData().</span>
15980-function">std::unique_ptr&#x3C;IsolateData, <span class="hljs-title">decltype</span><span class="…
15984 <span class="hljs-comment">// Set up a new v8::Context.</span>
15985 …<span class="hljs-function">HandleScope <span class="hljs-title">handle_scope</span><span class="h…
15986 Local&#x3C;Context> context = node::<span class="hljs-built_in">NewContext</span>(isolate);
15987 <span class="hljs-keyword">if</span> (context.<span class="hljs-built_in">IsEmpty</span>()) {
15988-built_in">fprintf</span>(stderr, <span class="hljs-string">"%s: Failed to initialize V8 Context\n…
15989 <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
15992 …<span class="hljs-comment">// The v8::Context needs to be entered when node::CreateEnvironment() a…
15993 <span class="hljs-comment">// node::LoadEnvironment() are being called.</span>
15994 …<span class="hljs-function">Context::Scope <span class="hljs-title">context_scope</span><span clas…
15996 …<span class="hljs-comment">// Create a node::Environment instance that will later be released usin…
15997 <span class="hljs-comment">// node::FreeEnvironment().</span>
15998-function">std::unique_ptr&#x3C;Environment, <span class="hljs-title">decltype</span><span class="…
16002 …<span class="hljs-comment">// Set up the Node.js instance for execution, and run code inside of it…
16003 …<span class="hljs-comment">// There is also a variant that takes a callback and provides it with</…
16004 …<span class="hljs-comment">// the `require` and `process` objects, so that it can manually compile…
16005 <span class="hljs-comment">// and run scripts as needed.</span>
16006 …<span class="hljs-comment">// The `require` function inside this script does *not* access the file…
16007 <span class="hljs-comment">// system, and can only load built-in Node.js modules.</span>
16008 …<span class="hljs-comment">// `module.createRequire()` is being used to create one that is able to…
16009 …<span class="hljs-comment">// load files from the disk, and uses the standard CommonJS file loader…
16010 <span class="hljs-comment">// instead of the internal-only `require` function.</span>
16011 MaybeLocal&#x3C;Value> loadenv_ret = node::<span class="hljs-built_in">LoadEnvironment</span>(
16012 env.<span class="hljs-built_in">get</span>(),
16013 <span class="hljs-string">"const publicRequire ="</span>
16014 <span class="hljs-string">" require('module').createRequire(process.cwd() + '/');"</span>
16015 <span class="hljs-string">"globalThis.require = publicRequire;"</span>
16016 <span class="hljs-string">"require('vm').runInThisContext(process.argv[1]);"</span>);
16018 …<span class="hljs-keyword">if</span> (loadenv_ret.<span class="hljs-built_in">IsEmpty</span>()) <…
16019 <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
16022 … <span class="hljs-comment">// SealHandleScope protects against handle leaks from callbacks.</span>
16023 …<span class="hljs-function">SealHandleScope <span class="hljs-title">seal</span><span class="hljs-
16024 <span class="hljs-type">bool</span> more;
16025 <span class="hljs-keyword">do</span> {
16026 <span class="hljs-built_in">uv_run</span>(&#x26;loop, UV_RUN_DEFAULT);
16028 …<span class="hljs-comment">// V8 tasks on background threads may end up scheduling new tasks in th…
16029 …<span class="hljs-comment">// foreground, which in turn can keep the event loop going. For example…
16030 <span class="hljs-comment">// WebAssembly.compile() may do so.</span>
16031 platform-><span class="hljs-built_in">DrainTasks</span>(isolate);
16033 <span class="hljs-comment">// If there are new tasks, continue.</span>
16034 more = <span class="hljs-built_in">uv_loop_alive</span>(&#x26;loop);
16035 <span class="hljs-keyword">if</span> (more) <span class="hljs-keyword">continue</span>;
16037 …<span class="hljs-comment">// node::EmitProcessBeforeExit() is used to emit the 'beforeExit' event…
16038 <span class="hljs-comment">// on the `process` object.</span>
16039 …js-keyword">if</span> (node::<span class="hljs-built_in">EmitProcessBeforeExit</span>(env.<span cl…
16040 <span class="hljs-keyword">break</span>;
16042 …<span class="hljs-comment">// 'beforeExit' can also schedule new work that keeps the event loop</s…
16043 <span class="hljs-comment">// running.</span>
16044 more = <span class="hljs-built_in">uv_loop_alive</span>(&#x26;loop);
16045 } <span class="hljs-keyword">while</span> (more == <span class="hljs-literal">true</span>);
16048 <span class="hljs-comment">// node::EmitProcessExit() returns the current exit code.</span>
16049 …"hljs-built_in">EmitProcessExit</span>(env.<span class="hljs-built_in">get</span>()).<span class="…
16051 …<span class="hljs-comment">// node::Stop() can be used to explicitly stop the event loop and keep<…
16052 …<span class="hljs-comment">// further JavaScript from running. It can be called from any thread,</…
16053 …<span class="hljs-comment">// and will act like worker.terminate() if called from another thread.<…
16054 node::<span class="hljs-built_in">Stop</span>(env.<span class="hljs-built_in">get</span>());
16057 …<span class="hljs-comment">// Unregister the Isolate with the platform and add a listener that is …
16058 …<span class="hljs-comment">// when the Platform is done cleaning up any state it had associated wi…
16059 <span class="hljs-comment">// the Isolate.</span>
16060 <span class="hljs-type">bool</span> platform_finished = <span class="hljs-literal">false</span>;
16061 …platform-><span class="hljs-built_in">AddIsolateFinishedCallback</span>(isolate, [](<span class="h…
16062 …*<span class="hljs-built_in">static_cast</span>&#x3C;<span class="hljs-type">bool</span>*>(data) =…
16064 platform-><span class="hljs-built_in">UnregisterIsolate</span>(isolate);
16065 isolate-><span class="hljs-built_in">Dispose</span>();
16067 …<span class="hljs-comment">// Wait until the platform has cleaned up all relevant resources.</span>
16068 <span class="hljs-keyword">while</span> (!platform_finished)
16069 <span class="hljs-built_in">uv_run</span>(&#x26;loop, UV_RUN_ONCE);
16070 …<span class="hljs-type">int</span> err = <span class="hljs-built_in">uv_loop_close</span>(&#x26;lo…
16071 <span class="hljs-built_in">assert</span>(err == <span class="hljs-number">0</span>);
16073 <span class="hljs-keyword">return</span> exit_code;
16078 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
16081 a manner that is similar, but not identical, to <a href="http://man7.org/linux/man-pages/man3/popen…
16083 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16084-keyword">const</span> ls = <span class="hljs-title function_">spawn</span>(<span class="hljs-stri…
16086-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16087 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
16090-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16091 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span …
16094 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span clas…
16095-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
16099 limited (and platform-specific) capacity. If the subprocess writes to
16107 <p>On Windows, environment variables are case-insensitive. Node.js
16109 case-insensitively matches. Only first (in lexicographic order) entry will be
16153 …ptions_callback"><code>child_process.execFile()</code></a> can vary based on platform. On Unix-type
16164 <pre><code class="language-js"><span class="hljs-comment">// On Windows Only...</span>
16165 <span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<spa…
16166-keyword">const</span> bat = <span class="hljs-title function_">spawn</span>(<span class="hljs-str…
16168-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16169 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16172-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16173 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
16176 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
16177 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hl…
16179 <pre><code class="language-js"><span class="hljs-comment">// OR...</span>
16180 <span class="hljs-keyword">const</span> { exec, spawn } = <span class="hljs-built_in">require</span…
16181 …span class="hljs-title function_">exec</span>(<span class="hljs-string">'my.bat'</span>, <span cla…
16182 <span class="hljs-keyword">if</span> (err) {
16183 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
16184 <span class="hljs-keyword">return</span>;
16186 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16189 <span class="hljs-comment">// Script with spaces in the filename:</span>
16190-keyword">const</span> bat = <span class="hljs-title function_">spawn</span>(<span class="hljs-str…
16191 <span class="hljs-comment">// or:</span>
16192 …lass="hljs-title function_">exec</span>(<span class="hljs-string">'"my script.cmd" a b'</span>, <s…
16193 <span class="hljs-comment">// ...</span>
16212 …oper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string>…
16213 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
16215 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16217 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16218 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16219 <li><code>shell</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
16224 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16225 <li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
16229 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16230-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16231-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16232 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
16236 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
16238 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
16239 <li><code>stdout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
16240 <li><code>stderr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
16248 <a href="https://en.wikipedia.org/wiki/List_of_command-line_interpreters">shell</a>)
16250 …de class="language-js"><span class="hljs-keyword">const</span> { exec } = <span class="hljs-built_…
16252 <span class="hljs-title function_">exec</span>(<span class="hljs-string">'"/path/to/test file/test.…
16253 <span class="hljs-comment">// Double quotes are used so that the space in the path is not interpret…
16254 <span class="hljs-comment">// a delimiter of multiple arguments.</span>
16256 <span class="hljs-title function_">exec</span>(<span class="hljs-string">'echo "The \\$HOME variabl…
16257 <span class="hljs-comment">// The $HOME variable is escaped in the first instance, but not in the s…
16268 the output as UTF-8 and pass strings to the callback. The <code>encoding</code> option
16272 …de class="language-js"><span class="hljs-keyword">const</span> { exec } = <span class="hljs-built_…
16273 …"hljs-title function_">exec</span>(<span class="hljs-string">'cat *.js missing_file | wc -l'</span…
16274 <span class="hljs-keyword">if</span> (error) {
16275 …="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span cl…
16276 <span class="hljs-keyword">return</span>;
16278 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
16279 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span …
16284 <p>Unlike the <a href="http://man7.org/linux/man-pages/man3/exec.3.html"><code>exec(3)</code></a> P…
16292 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
16293-keyword">const</span> exec = util.<span class="hljs-title function_">promisify</span>(<span class…
16295 …ss="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title …
16296 …js-keyword">const</span> { stdout, stderr } = <span class="hljs-keyword">await</span> <span class=…
16297 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16298 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
16300 <span class="hljs-title function_">lsExample</span>();</code></pre>
16304 …de class="language-js"><span class="hljs-keyword">const</span> { exec } = <span class="hljs-built_…
16305 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span cl…
16306 <span class="hljs-keyword">const</span> { signal } = controller;
16307-keyword">const</span> child = <span class="hljs-title function_">exec</span>(<span class="hljs-st…
16308 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16310 controller.<span class="hljs-title function_">abort</span>();</code></pre>
16328 <li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
16329 <li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
16330 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
16332 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16333 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16334 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16335 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16336 <li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
16340 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16341-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16342-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16343 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
16345 <li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
16347 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
16355 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
16357 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
16358 <li><code>stdout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
16359 <li><code>stderr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
16371 … class="language-js"><span class="hljs-keyword">const</span> { execFile } = <span class="hljs-buil…
16372-keyword">const</span> child = <span class="hljs-title function_">execFile</span>(<span class="hlj…
16373 <span class="hljs-keyword">if</span> (error) {
16374 <span class="hljs-keyword">throw</span> error;
16376 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16380 the output as UTF-8 and pass strings to the callback. The <code>encoding</code> option
16390 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
16391-keyword">const</span> execFile = util.<span class="hljs-title function_">promisify</span>(<span c…
16392 …ss="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title …
16393-keyword">const</span> { stdout } = <span class="hljs-keyword">await</span> <span class="hljs-titl…
16394 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16396 <span class="hljs-title function_">getVersion</span>();</code></pre>
16403 … class="language-js"><span class="hljs-keyword">const</span> { execFile } = <span class="hljs-buil…
16404 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span cl…
16405 <span class="hljs-keyword">const</span> { signal } = controller;
16406-keyword">const</span> child = <span class="hljs-title function_">execFile</span>(<span class="hlj…
16407 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16409 controller.<span class="hljs-title function_">abort</span>();</code></pre>
16435 <li><code>modulePath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
16436 <li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
16437 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
16439 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16440 <li><code>detached</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16443 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16444 <li><code>execPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16445 <li><code>execArgv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16447-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16448 <li><code>serialization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
16453 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16455 <li><code>silent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
16459 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> |…
16463-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16464 <li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
16466 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16476 built-in that allows messages to be passed back and forth between the parent and
16490 <p>Unlike the <a href="http://man7.org/linux/man-pages/man2/fork.2.html"><code>fork(2)</code></a> P…
16497 …ge-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">argv</span>[<span…
16498 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
16499-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
16500 }, <span class="hljs-number">1_000</span>);
16501 } <span class="hljs-keyword">else</span> {
16502 …<span class="hljs-keyword">const</span> { fork } = <span class="hljs-built_in">require</span>(<spa…
16503 …<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span c…
16504 <span class="hljs-keyword">const</span> { signal } = controller;
16505 …<span class="hljs-keyword">const</span> child = <span class="hljs-title function_">fork</span>(__f…
16506 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
16507 …<span class="hljs-comment">// This will be called with err being an AbortError if the controller a…
16509 …controller.<span class="hljs-title function_">abort</span>(); <span class="hljs-comment">// Stops …
16539 <p><code>command</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
16542 <p><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16545 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
16547 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16548 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16549 <li><code>argv0</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
16551 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> |…
16553 <li><code>detached</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16556-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16557-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16558 <li><code>serialization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
16561 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
16565 <li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
16568 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
16572 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16574 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16583 <code>command</code>, with command-line arguments in <code>args</code>. If omitted, <code>args</cod…
16589 <pre><code class="language-js"><span class="hljs-keyword">const</span> defaults = {
16590 <span class="hljs-attr">cwd</span>: <span class="hljs-literal">undefined</span>,
16591 <span class="hljs-attr">env</span>: process.<span class="hljs-property">env</span>
16601 <p>Example of running <code>ls -lh /usr</code>, capturing <code>stdout</code>, <code>stderr</code>,…
16603 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16604-keyword">const</span> ls = <span class="hljs-title function_">spawn</span>(<span class="hljs-stri…
16606-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16607 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
16610-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16611 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span …
16614 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span clas…
16615-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
16618 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16619 …ass="hljs-keyword">const</span> ps = <span class="hljs-title function_">spawn</span>(<span class="…
16620 …s="hljs-keyword">const</span> grep = <span class="hljs-title function_">spawn</span>(<span class="…
16622-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16623 …grep.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">write</span>(data…
16626-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16627 …="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span cl…
16630 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span clas…
16631 <span class="hljs-keyword">if</span> (code !== <span class="hljs-number">0</span>) {
16632-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
16634 grep.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">end</span>();
16637-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16638 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16641-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16642 …"hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span cla…
16645 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span clas…
16646 <span class="hljs-keyword">if</span> (code !== <span class="hljs-number">0</span>) {
16647-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
16651 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16652 <span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span…
16654 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
16655 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
16666 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16667 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span cl…
16668 <span class="hljs-keyword">const</span> { signal } = controller;
16669 …s="hljs-keyword">const</span> grep = <span class="hljs-title function_">spawn</span>(<span class="…
16670 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
16671 …<span class="hljs-comment">// This will be called with err being an AbortError if the controller a…
16673 controller.<span class="hljs-title function_">abort</span>(); <span class="hljs-comment">// Stops t…
16682 <p>On non-Windows platforms, if <code>options.detached</code> is set to <code>true</code>, the child
16685 they are detached or not. See <a href="http://man7.org/linux/man-pages/man2/setsid.2.html"><code>se…
16692 <p>When using the <code>detached</code> option to start a long-running process, the process
16697 <p>Example of a long-running process, by detaching and also ignoring its parent
16699 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16701-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(process.<span …
16702 <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>,
16703 <span class="hljs-attr">stdio</span>: <span class="hljs-string">'ignore'</span>
16706 subprocess.<span class="hljs-title function_">unref</span>();</code></pre>
16708 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
16709 <span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<spa…
16710 …ljs-keyword">const</span> out = fs.<span class="hljs-title function_">openSync</span>(<span class=…
16711 …ljs-keyword">const</span> err = fs.<span class="hljs-title function_">openSync</span>(<span class=…
16713 <span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span…
16714 <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>,
16715 <span class="hljs-attr">stdio</span>: [ <span class="hljs-string">'ignore'</span>, out, err ]
16718 subprocess.<span class="hljs-title function_">unref</span>();</code></pre>
16762 <a href="https://docs.microsoft.com/en-us/windows/win32/fileio/synchronous-and-asynchronous-i-o">do…
16763 for more details. This is exactly the same as <code>'pipe'</code> on non-Windows
16810 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16812 <span class="hljs-comment">// Child will use parent's stdios.</span>
16813 …lass="hljs-title function_">spawn</span>(<span class="hljs-string">'prg'</span>, [], { <span class…
16815 <span class="hljs-comment">// Spawn child sharing only stderr.</span>
16816-title function_">spawn</span>(<span class="hljs-string">'prg'</span>, [], { <span class="hljs-att…
16818 <span class="hljs-comment">// Open an extra fd=4, to interact with programs presenting a</span>
16819 <span class="hljs-comment">// startd-style interface.</span>
16820-title function_">spawn</span>(<span class="hljs-string">'prg'</span>, [], { <span class="hljs-att…
16827 <p>On Unix-like operating systems, the <a href="#child_process_child_process_spawn_command_args_opt…
16838 <p>Blocking calls like these are mostly useful for simplifying general-purpose
16862 <li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
16863 <li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
16864 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
16866 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16867-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
16870 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16873 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16874-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16875-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16876 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16878 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16880 <li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
16883 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16885 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
16887 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
16893 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
16903 <p>If the process times out or has a non-zero exit code, this method will throw an
16928 <li><code>command</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16929 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
16931 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16932-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
16935 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16938 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16939 <li><code>shell</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
16942-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16943-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16944 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16946 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16948 <li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
16952 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16954 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
16958 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
16967 <p>If the process times out or has a non-zero exit code, this method will throw.
16995 <li><code>command</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16996 <li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
16997 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
16999 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
17000-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
17003 <li><code>argv0</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
17005 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
17006 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
17007-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
17008-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
17009 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
17011 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
17013 <li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
17017 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
17019 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
17023 <li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
17026 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
17030 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
17032 <li><code>pid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
17033 <li><code>output</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
17034 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
17035 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
17036 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
17038 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
17040 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
17071 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
17072 <li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
17079 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
17080-keyword">const</span> ls = <span class="hljs-title function_">spawn</span>(<span class="hljs-stri…
17082-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
17083 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
17086 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span clas…
17087-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
17090 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
17091-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
17104 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
17121 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
17122 <li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
17127 the signal, otherwise <code>null</code>. One of the two will always be non-<code>null</code>.</p>
17133 re-raise the handled signal.</p>
17134 <p>See <a href="http://man7.org/linux/man-pages/man2/waitpid.2.html"><code>waitpid(2)</code></a>.</…
17140 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
17162 the spawned process. For example, if <code>bash some-command</code> spawns successfully,
17163 the <code>'spawn'</code> event will fire, though <code>bash</code> may fail to spawn <code>some-com…
17178 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
17199 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
17221 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
17230 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
17231 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
17235 <a href="http://man7.org/linux/man-pages/man7/signal.7.html"><code>signal(7)</code></a> for a list …
17236 <a href="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code></a> succeeds, and <…
17237 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
17238 …s="hljs-keyword">const</span> grep = <span class="hljs-title function_">spawn</span>(<span class="…
17240 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span clas…
17241 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
17242 …<span class="hljs-string">`child process terminated due to receipt of signal <span class="hljs-sub…
17245 <span class="hljs-comment">// Send SIGHUP to process.</span>
17246 grep.<span class="hljs-title function_">kill</span>(<span class="hljs-string">'SIGHUP'</span>);</co…
17254 <p>See <a href="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code></a> for refe…
17262 <pre><code class="language-js"><span class="hljs-meta">'use strict'</span>;
17263 <span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<spa…
17265 <span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span…
17266 <span class="hljs-string">'sh'</span>,
17268 <span class="hljs-string">'-c'</span>,
17269 <span class="hljs-string">`node -e "setInterval(() => {
17273 … class="hljs-attr">stdio</span>: [<span class="hljs-string">'inherit'</span>, <span class="hljs-st…
17277 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
17278 …subprocess.<span class="hljs-title function_">kill</span>(); <span class="hljs-comment">// Does no…
17279 }, <span class="hljs-number">2000</span>);</code></pre>
17285 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
17296 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
17301 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
17302 …s="hljs-keyword">const</span> grep = <span class="hljs-title function_">spawn</span>(<span class="…
17304 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
17305 grep.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">end</span>();</cod…
17313 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
17315-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(process.<span …
17316 <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>,
17317 <span class="hljs-attr">stdio</span>: <span class="hljs-string">'ignore'</span>
17320 subprocess.<span class="hljs-title function_">unref</span>();
17321 subprocess.<span class="hljs-title function_">ref</span>();</code></pre>
17339 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
17341 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
17345 <li><code>keepOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
17350 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
17351 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
17360 …<code class="language-js"><span class="hljs-keyword">const</span> cp = <span class="hljs-built_in"…
17361 …pan class="hljs-keyword">const</span> n = cp.<span class="hljs-title function_">fork</span>(<span …
17363 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
17364 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
17367 <span class="hljs-comment">// Causes the child to print: CHILD got message: { hello: 'world' }</spa…
17368 n.<span class="hljs-title function_">send</span>({ <span class="hljs-attr">hello</span>: <span clas…
17370 …ge-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</s…
17371 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
17374 <span class="hljs-comment">// Causes the parent to print: PARENT got message: { foo: 'bar', baz: nu…
17375-title function_">send</span>({ <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar…
17403-js"><span class="hljs-keyword">const</span> subprocess = <span class="hljs-built_in">require</spa…
17405 <span class="hljs-comment">// Open up the server object and send the handle.</span>
17406 …lass="hljs-keyword">const</span> server = <span class="hljs-built_in">require</span>(<span class="…
17407 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, <span cl…
17408 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'handled by parent'…
17410 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>, <spa…
17411 …subprocess.<span class="hljs-title function_">send</span>(<span class="hljs-string">'server'</span…
17414 …ge-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</s…
17415 <span class="hljs-keyword">if</span> (m === <span class="hljs-string">'server'</span>) {
17416 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, <span cl…
17417 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'handled by child'<…
17432 …de class="language-js"><span class="hljs-keyword">const</span> { fork } = <span class="hljs-built_…
17433 …js-keyword">const</span> normal = <span class="hljs-title function_">fork</span>(<span class="hljs
17434 …js-keyword">const</span> special = <span class="hljs-title function_">fork</span>(<span class="hlj…
17436 <span class="hljs-comment">// Open up the server and send sockets to child. Use pauseOnConnect to p…
17437 <span class="hljs-comment">// the sockets from being read before they are sent to the child process…
17438-keyword">const</span> server = <span class="hljs-built_in">require</span>(<span class="hljs-strin…
17439 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, <span cl…
17441 <span class="hljs-comment">// If this is special priority...</span>
17442 …<span class="hljs-keyword">if</span> (socket.<span class="hljs-property">remoteAddress</span> === …
17443 …special.<span class="hljs-title function_">send</span>(<span class="hljs-string">'socket'</span>, …
17444 <span class="hljs-keyword">return</span>;
17446 <span class="hljs-comment">// This is normal priority.</span>
17447 …normal.<span class="hljs-title function_">send</span>(<span class="hljs-string">'socket'</span>, s…
17449 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);</co…
17452 …ge-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</s…
17453 <span class="hljs-keyword">if</span> (m === <span class="hljs-string">'socket'</span>) {
17454 <span class="hljs-keyword">if</span> (socket) {
17455 <span class="hljs-comment">// Check that the client socket exists.</span>
17456 …<span class="hljs-comment">// It is possible for the socket to be closed between the time it is</s…
17457 <span class="hljs-comment">// sent and the time it is received in the child process.</span>
17458 …ass="hljs-title function_">end</span>(<span class="hljs-string">`Request handled with <span class=…
17469 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
17475 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array…
17477 <p>The <code>subprocess.spawnargs</code> property represents the full list of command-line
17481 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
17526 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array…
17536 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
17537 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
17538 <span class="hljs-keyword">const</span> child_process = <span class="hljs-built_in">require</span>(…
17540 <span class="hljs-keyword">const</span> subprocess = child_process.<span class="hljs-title function…
17541 <span class="hljs-attr">stdio</span>: [
17542 …<span class="hljs-number">0</span>, <span class="hljs-comment">// Use parent's stdin for child.</s…
17543 …<span class="hljs-string">'pipe'</span>, <span class="hljs-comment">// Pipe child's stdout to pare…
17544 …ass="hljs-title function_">openSync</span>(<span class="hljs-string">'err.out'</span>, <span class…
17548 …hljs-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span…
17549-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span cla…
17551 <span class="hljs-title function_">assert</span>(subprocess.<span class="hljs-property">stdout</spa…
17552-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span cla…
17554 …hljs-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span…
17555-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span cla…
17570 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
17572 <span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span…
17574-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
17575 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
17589 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
17591-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(process.<span …
17592 <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>,
17593 <span class="hljs-attr">stdio</span>: <span class="hljs-string">'ignore'</span>
17596 subprocess.<span class="hljs-title function_">unref</span>();</code></pre>
17600 This impacts output that includes multibyte character encodings such as UTF-8 or
17601 UTF-16. For instance, <code>console.log('中文测试')</code> will send 13 UTF-8 encoded bytes
17604 <p>The shell should understand the <code>-c</code> switch. If the shell is <code>'cmd.exe'</code>, …
17605 should understand the <code>/d /s /c</code> switches and command-line parsing should be
17619 <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorith…
17620 supports more built-in JavaScript object types, such as <code>BigInt</code>, <code>Map</code>
17623 objects of such built-in types will not be passed on through the serialization
17632 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
17635 multi-core systems, the user will sometimes want to launch a cluster of Node.js
17639 …de class="language-js"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_i…
17640 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
17641-keyword">const</span> numCPUs = <span class="hljs-built_in">require</span>(<span class="hljs-stri…
17643 <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isMaster</span>) {
17644 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
17646 <span class="hljs-comment">// Fork workers.</span>
17647 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs
17648 cluster.<span class="hljs-title function_">fork</span>();
17651 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
17652 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
17654 } <span class="hljs-keyword">else</span> {
17655 <span class="hljs-comment">// Workers can share any TCP connection</span>
17656 <span class="hljs-comment">// In this case it is an HTTP server</span>
17657 …http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span cla…
17658 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>);
17659 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>…
17660 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);
17662 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
17665 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
17680 is the round-robin approach, where the master process listens on a
17682 in a round-robin fashion, with some built-in smarts to avoid
17710 application such that it does not rely too heavily on in-memory data objects for
17713 re-spawned depending on a program's needs, without affecting other
17736-js">cluster.<span class="hljs-title function_">fork</span>().<span class="hljs-title function_">o…
17737 <span class="hljs-comment">// Worker has disconnected</span>
17750 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
17751 <li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
17755 <pre><code class="language-js"><span class="hljs-keyword">const</span> worker = cluster.<span class…
17756 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
17757 <span class="hljs-keyword">if</span> (signal) {
17758-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
17759 …} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (code !== <span clas…
17760-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
17761 } <span class="hljs-keyword">else</span> {
17762 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
17770 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
17773-js">cluster.<span class="hljs-title function_">fork</span>().<span class="hljs-title function_">o…
17774 <span class="hljs-comment">// Worker is listening</span>
17782 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
17783 …la.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a>…
17790 …de class="language-js"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_i…
17791 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
17793 <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isMaster</span>) {
17795 <span class="hljs-comment">// Keep track of http requests</span>
17796 <span class="hljs-keyword">let</span> numReqs = <span class="hljs-number">0</span>;
17797 <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> {
17798 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
17799 }, <span class="hljs-number">1000</span>);
17801 <span class="hljs-comment">// Count requests</span>
17802 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">messageHandler</span…
17803 …"hljs-keyword">if</span> (msg.<span class="hljs-property">cmd</span> &#x26;&#x26; msg.<span class=…
17804 numReqs += <span class="hljs-number">1</span>;
17808 …<span class="hljs-comment">// Start workers and listen for messages containing notifyRequest</span>
17809-keyword">const</span> numCPUs = <span class="hljs-built_in">require</span>(<span class="hljs-stri…
17810 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs
17811 cluster.<span class="hljs-title function_">fork</span>();
17814 … class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> id <span class="hljs-key…
17815 …cluster.<span class="hljs-property">workers</span>[id].<span class="hljs-title function_">on</span…
17818 } <span class="hljs-keyword">else</span> {
17820 <span class="hljs-comment">// Worker processes have a http server.</span>
17821 …http.<span class="hljs-title class_">Server</span>(<span class="hljs-function">(<span class="hljs-
17822 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>);
17823 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>…
17825 <span class="hljs-comment">// Notify master about the request</span>
17826 …process.<span class="hljs-title function_">send</span>({ <span class="hljs-attr">cmd</span>: <span…
17827 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);
17834-js">cluster.<span class="hljs-title function_">fork</span>().<span class="hljs-title function_">o…
17835 <span class="hljs-comment">// Worker is online</span>
17872 <pre><code class="language-js"><span class="hljs-keyword">if</span> (cluster.<span class="hljs-prop…
17873 …<span class="hljs-keyword">const</span> worker = cluster.<span class="hljs-title function_">fork</…
17874 <span class="hljs-keyword">let</span> timeout;
17876 …span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</span>, <span cl…
17877 …worker.<span class="hljs-title function_">send</span>(<span class="hljs-string">'shutdown'</span>);
17878 worker.<span class="hljs-title function_">disconnect</span>();
17879 … timeout = <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
17880 worker.<span class="hljs-title function_">kill</span>();
17881 }, <span class="hljs-number">2000</span>);
17884 …worker.<span class="hljs-title function_">on</span>(<span class="hljs-string">'disconnect'</span>,…
17885 <span class="hljs-built_in">clearTimeout</span>(timeout);
17888 } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (cluster.<span class=…
17889 …<span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span cla…
17890 …s="hljs-keyword">const</span> server = net.<span class="hljs-title function_">createServer</span>(…
17891 <span class="hljs-comment">// Connections never end</span>
17894 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);
17896 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
17897 <span class="hljs-keyword">if</span> (msg === <span class="hljs-string">'shutdown'</span>) {
17898 <span class="hljs-comment">// Initiate graceful close of any connections to server</span>
17907 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
17915 …age-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</spa…
17916 …<span class="hljs-keyword">if</span> (worker.<span class="hljs-property">exitedAfterDisconnect</sp…
17917 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
17921 <span class="hljs-comment">// kill worker</span>
17922 worker.<span class="hljs-title function_">kill</span>();</code></pre>
17928 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
17947 …de class="language-js"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_i…
17948 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
17949-keyword">const</span> numCPUs = <span class="hljs-built_in">require</span>(<span class="hljs-stri…
17951 <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isMaster</span>) {
17952 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
17954 <span class="hljs-comment">// Fork workers.</span>
17955 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs
17956 cluster.<span class="hljs-title function_">fork</span>();
17959 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'fork'</span>, <span class…
17960-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
17963 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
17964-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
17966 } <span class="hljs-keyword">else</span> {
17967 …<span class="hljs-comment">// Workers can share any TCP connection. In this case, it is an HTTP se…
17968 …http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span cla…
17969 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>);
17970 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">`Current process\n <sp…
17971 …process.<span class="hljs-title function_">kill</span>(process.<span class="hljs-property">pid</sp…
17972 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);
17979 <li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
18020 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
18022 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
18026 <li><code>keepOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
18031 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
18032 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
18040 <pre><code class="language-js"><span class="hljs-keyword">if</span> (cluster.<span class="hljs-prop…
18041 …<span class="hljs-keyword">const</span> worker = cluster.<span class="hljs-title function_">fork</…
18042 …worker.<span class="hljs-title function_">send</span>(<span class="hljs-string">'hi there'</span>);
18044 } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (cluster.<span class=…
18045 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
18046 process.<span class="hljs-title function_">send</span>(msg);
18061 are long-living connections.</p>
18062-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'disconnect'</…
18063 …s="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cla…
18071 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
18072 <li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
18077 …age-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</spa…
18078 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
18079 …worker.<span class="hljs-property">process</span>.<span class="hljs-property">pid</span>, signal |…
18080 cluster.<span class="hljs-title function_">fork</span>();
18092 <pre><code class="language-js"><span class="hljs-keyword">const</span> timeouts = [];
18093 <span class="hljs-keyword">function</span> <span class="hljs-title function_">errorMsg</span>(<span…
18094 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
18097 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'fork'</span>, <span class…
18098 …ts[worker.<span class="hljs-property">id</span>] = <span class="hljs-built_in">setTimeout</span>(e…
18100 …span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</span>, <span cl…
18101 …<span class="hljs-built_in">clearTimeout</span>(timeouts[worker.<span class="hljs-property">id</sp…
18103 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
18104 …<span class="hljs-built_in">clearTimeout</span>(timeouts[worker.<span class="hljs-property">id</sp…
18105 <span class="hljs-title function_">errorMsg</span>();
18113 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
18122 …e-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</…
18123 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
18124 …<span class="hljs-string">`A worker is now connected to <span class="hljs-subst">${address.address…
18130 <li><code>-1</code> (Unix domain socket)</li>
18147 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
18148 …la.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a>…
18163 …ge-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'online'</sp…
18164 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
18171 <li><code>settings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
18183 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
18197 <li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
18207 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
18217 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
18224 <p>The scheduling policy, either <code>cluster.SCHED_RR</code> for round-robin or
18255 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
18257 <li><code>execArgv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
18259 <li><code>exec</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
18260 <li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
18262 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
18264 <li><code>serialization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
18268 <li><code>silent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
18270 <li><code>stdio</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
18273-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
18274-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
18275 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
18279 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
18300 <li><code>settings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
18310 …de class="language-js"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_i…
18311 cluster.<span class="hljs-title function_">setupMaster</span>({
18312 <span class="hljs-attr">exec</span>: <span class="hljs-string">'worker.js'</span>,
18313 …<span class="hljs-attr">args</span>: [<span class="hljs-string">'--use'</span>, <span class="hljs-
18314 <span class="hljs-attr">silent</span>: <span class="hljs-literal">true</span>
18316 cluster.<span class="hljs-title function_">fork</span>(); <span class="hljs-comment">// https worke…
18317 cluster.<span class="hljs-title function_">setupMaster</span>({
18318 <span class="hljs-attr">exec</span>: <span class="hljs-string">'worker.js'</span>,
18319 …<span class="hljs-attr">args</span>: [<span class="hljs-string">'--use'</span>, <span class="hljs-
18321 cluster.<span class="hljs-title function_">fork</span>(); <span class="hljs-comment">// http worker…
18328 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
18331 …de class="language-js"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_i…
18333 <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isMaster</span>) {
18334 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
18335 cluster.<span class="hljs-title function_">fork</span>();
18336 cluster.<span class="hljs-title function_">fork</span>();
18337 } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (cluster.<span class=…
18338 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
18345 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
18354 <pre><code class="language-js"><span class="hljs-comment">// Go through all workers</span>
18355 <span class="hljs-keyword">function</span> <span class="hljs-title function_">eachWorker</span>(<sp…
18356 … class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> id <span class="hljs-key…
18357 …<span class="hljs-title function_">callback</span>(cluster.<span class="hljs-property">workers</sp…
18360 <span class="hljs-title function_">eachWorker</span>(<span class="hljs-function">(<span class="hljs
18361 …worker.<span class="hljs-title function_">send</span>(<span class="hljs-string">'big announcement …
18364 …uage-js">socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</spa…
18365 …<span class="hljs-keyword">const</span> worker = cluster.<span class="hljs-property">workers</span…
18368 <section><h2>Command-line options<span><a class="mark" href="#cli_command_line_options" id="cli_com…
18371 <p>Node.js comes with a variety of CLI options. These options expose built-in
18375 <p><code>node [options] [V8 options] [script.js | -e "script" | -] [--] [arguments]</code></p>
18376 <p><code>node inspect [script.js | -e "script" | &#x3C;host>:&#x3C;port>] …</code></p>
18377 <p><code>node --v8-options</code></p>
18391 dashes (<code>-</code>) or underscores (<code>_</code>). For example, <code>--pending-deprecation</…
18392 equivalent to <code>--pending_deprecation</code>.</p>
18393 <p>If an option that takes a single value (such as <code>--max-http-header-size</code>) is
18397 <h4><code>-</code><span><a class="mark" href="#cli" id="cli">#</a></span></h4>
18401 <p>Alias for stdin. Analogous to the use of <code>-</code> in other command-line utilities,
18404 <h4><code>--</code><span><a class="mark" href="#cli_1" id="cli_1">#</a></span></h4>
18411 <h4><code>--abort-on-uncaught-exception</code><span><a class="mark" href="#cli_abort_on_uncaught_ex…
18415 <p>Aborting instead of exiting causes a core file to be generated for post-mortem
18420 <h4><code>--completion-bash</code><span><a class="mark" href="#cli_completion_bash" id="cli_complet…
18424 <p>Print source-able bash completion script for Node.js.</p>
18425 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
18426 <span class="hljs-meta">$ </span><span class="language-bash"><span class="hljs-built_in">source</sp…
18427 <h4><code>-C=condition</code>, <code>--conditions=condition</code><span><a class="mark" href="#cli_…
18431 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18438 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
18439 <h4><code>--cpu-prof</code><span><a class="mark" href="#cli_cpu_prof" id="cli_cpu_prof">#</a></span…
18443 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18446 <p>If <code>--cpu-prof-dir</code> is not specified, the generated profile is placed
18448 <p>If <code>--cpu-prof-name</code> is not specified, the generated profile is
18450 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
18451 <span class="hljs-meta">$ </span><span class="language-bash">ls *.cpuprofile</span>
18453 <h4><code>--cpu-prof-dir</code><span><a class="mark" href="#cli_cpu_prof_dir" id="cli_cpu_prof_dir"…
18457 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18458 <p>Specify the directory where the CPU profiles generated by <code>--cpu-prof</code> will
18461 <a href="#cli_diagnostic_dir_directory">--diagnostic-dir</a> command-line option.</p>
18462 <h4><code>--cpu-prof-interval</code><span><a class="mark" href="#cli_cpu_prof_interval" id="cli_cpu…
18466 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18468 by <code>--cpu-prof</code>. The default is 1000 microseconds.</p>
18469 <h4><code>--cpu-prof-name</code><span><a class="mark" href="#cli_cpu_prof_name" id="cli_cpu_prof_na…
18473 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18474 <p>Specify the file name of the CPU profile generated by <code>--cpu-prof</code>.</p>
18475 <h4><code>--diagnostic-dir=directory</code><span><a class="mark" href="#cli_diagnostic_dir_director…
18480 <li><a href="#cli_cpu_prof_dir">--cpu-prof-dir</a></li>
18481 <li><a href="#cli_heap_prof_dir">--heap-prof-dir</a></li>
18482 <li><a href="#cli_redirect_warnings_file">--redirect-warnings</a></li>
18484 <h4><code>--disable-proto=mode</code><span><a class="mark" href="#cli_disable_proto_mode" id="cli_d…
18491 <h4><code>--disallow-code-generation-from-strings</code><span><a class="mark" href="#cli_disallow_c…
18495 <p>Make built-in language features like <code>eval</code> and <code>new Function</code> that genera…
18498 <h4><code>--dns-result-order=order</code><span><a class="mark" href="#cli_dns_result_order_order" i…
18509 priority than <code>--dns-result-order</code>.</p>
18510 <h4><code>--enable-fips</code><span><a class="mark" href="#cli_enable_fips" id="cli_enable_fips">#<…
18514 <p>Enable FIPS-compliant crypto at startup. (Requires Node.js to be built
18515 against FIPS-compatible OpenSSL.)</p>
18516 <h4><code>--enable-source-maps</code><span><a class="mark" href="#cli_enable_source_maps" id="cli_e…
18531 <code>--enable-source-maps</code> enables caching of Source Maps and makes a best
18533 <p>Overriding <code>Error.prepareStackTrace</code> prevents <code>--enable-source-maps</code> from
18535 <h4><code>--experimental-abortcontroller</code><span><a class="mark" href="#cli_experimental_abortc…
18540 <h4><code>--experimental-import-meta-resolve</code><span><a class="mark" href="#cli_experimental_im…
18545 <h4><code>--experimental-json-modules</code><span><a class="mark" href="#cli_experimental_json_modu…
18550 <h4><code>--experimental-loader=module</code><span><a class="mark" href="#cli_experimental_loader_m…
18556 <h4><code>--experimental-modules</code><span><a class="mark" href="#cli_experimental_modules" id="c…
18561 <h4><code>--experimental-policy</code><span><a class="mark" href="#cli_experimental_policy" id="cli…
18566 <h4><code>--experimental-repl-await</code><span><a class="mark" href="#cli_experimental_repl_await"…
18570 <p>Enable experimental top-level <code>await</code> keyword support in REPL.</p>
18571 <h4><code>--experimental-specifier-resolution=mode</code><span><a class="mark" href="#cli_experimen…
18581 <h4><code>--experimental-vm-modules</code><span><a class="mark" href="#cli_experimental_vm_modules"…
18586 <h4><code>--experimental-wasi-unstable-preview1</code><span><a class="mark" href="#cli_experimental…
18592 <td><p>changed from <code>--experimental-wasi-unstable-preview0</code> to <code>--experimental-wasi
18599 <h4><code>--experimental-wasm-modules</code><span><a class="mark" href="#cli_experimental_wasm_modu…
18604 <h4><code>--force-context-aware</code><span><a class="mark" href="#cli_force_context_aware" id="cli…
18608 <p>Disable loading native addons that are not <a href="#addons_context_aware_addons">context-aware<…
18609 <h4><code>--force-fips</code><span><a class="mark" href="#cli_force_fips" id="cli_force_fips">#</a>…
18613 <p>Force FIPS-compliant crypto on startup. (Cannot be disabled from script code.)
18614 (Same requirements as <code>--enable-fips</code>.)</p>
18615 <h4><code>--frozen-intrinsics</code><span><a class="mark" href="#cli_frozen_intrinsics" id="cli_fro…
18619 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18624 <p><code>--require</code> runs prior to freezing intrinsics in order to allow polyfills to
18626 <h4><code>--heapsnapshot-near-heap-limit=max_count</code><span><a class="mark" href="#cli_heapsnaps…
18630 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18632 heap limit. <code>count</code> should be a non-negative integer (in which case
18649 …ass="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node --max-old
18654 &#x3C;--- Last few GCs --->
18656 [49580:0x110000000] 4826 ms: Mark-sweep 130.6 (147.8) -> 130.5 (147.8) MB, 27.4 / 0.0 ms (aver…
18657 [49580:0x110000000] 4845 ms: Mark-sweep 130.6 (147.8) -> 130.6 (147.8) MB, 18.8 / 0.0 ms (aver…
18660 &#x3C;--- JS stacktrace --->
18662 FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of m…
18664 <h4><code>--heapsnapshot-signal=signal</code><span><a class="mark" href="#cli_heapsnapshot_signal_s…
18671 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
18672 <span class="hljs-meta">$ </span><span class="language-bash">ps aux</span>
18674 node 1 5.5 6.1 787252 247004 ? Ssl 16:43 0:02 node --heapsnapshot-signal=SIGUSR2…
18675 <span class="hljs-meta">$ </span><span class="language-bash"><span class="hljs-built_in">kill</span…
18676 <span class="hljs-meta">$ </span><span class="language-bash">ls</span>
18678 <h4><code>--heap-prof</code><span><a class="mark" href="#cli_heap_prof" id="cli_heap_prof">#</a></s…
18682 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18685 <p>If <code>--heap-prof-dir</code> is not specified, the generated profile is placed
18687 <p>If <code>--heap-prof-name</code> is not specified, the generated profile is
18689 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
18690 <span class="hljs-meta">$ </span><span class="language-bash">ls *.heapprofile</span>
18692 <h4><code>--heap-prof-dir</code><span><a class="mark" href="#cli_heap_prof_dir" id="cli_heap_prof_d…
18696 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18697 <p>Specify the directory where the heap profiles generated by <code>--heap-prof</code> will
18700 <a href="#cli_diagnostic_dir_directory">--diagnostic-dir</a> command-line option.</p>
18701 <h4><code>--heap-prof-interval</code><span><a class="mark" href="#cli_heap_prof_interval" id="cli_h…
18705 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18707 by <code>--heap-prof</code>. The default is 512 * 1024 bytes.</p>
18708 <h4><code>--heap-prof-name</code><span><a class="mark" href="#cli_heap_prof_name" id="cli_heap_prof…
18712 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18713 <p>Specify the file name of the heap profile generated by <code>--heap-prof</code>.</p>
18714 <h4><code>--icu-data-dir=file</code><span><a class="mark" href="#cli_icu_data_dir_file" id="cli_icu…
18719 <h4><code>--input-type=type</code><span><a class="mark" href="#cli_input_type_type" id="cli_input_t…
18724 module. String input is input via <code>--eval</code>, <code>--print</code>, or <code>STDIN</code>.…
18726 <h4><code>--inspect-brk[=[host:]port]</code><span><a class="mark" href="#cli_inspect_brk_host_port"…
18732 <h4><code>--inspect-port=[host:]port</code><span><a class="mark" href="#cli_inspect_port_host_port"…
18741 <h4><code>--inspect[=[host:]port]</code><span><a class="mark" href="#cli_inspect_host_port" id="cli…
18748 tcp port and communicate using the <a href="https://chromedevtools.github.io/devtools-protocol/">Ch…
18759 <p><strong>More specifically, <code>--inspect=0.0.0.0</code> is insecure if the port (<code>9229</c…
18760 default) is not firewall-protected.</strong></p>
18761 <p>See the <a href="https://nodejs.org/en/docs/guides/debugging-getting-started/#security-implicati…
18762 <h4><code>--inspect-publish-uid=stderr,http</code><span><a class="mark" href="#cli_inspect_publish_…
18766 <h4><code>--insecure-http-parser</code><span><a class="mark" href="#cli_insecure_http_parser" id="c…
18771 interoperability with non-conformant HTTP implementations. It may also allow
18774 <h4><code>--jitless</code><span><a class="mark" href="#cli_jitless" id="cli_jitless">#</a></span></…
18782 disappear in a non-semver-major release.</p>
18783 <h4><code>--max-http-header-size=size</code><span><a class="mark" href="#cli_max_http_header_size_s…
18796 <h4><code>--napi-modules</code><span><a class="mark" href="#cli_napi_modules" id="cli_napi_modules"…
18800 <p>This option is a no-op. It is kept for compatibility.</p>
18801 <h4><code>--no-addons</code><span><a class="mark" href="#cli_no_addons" id="cli_no_addons">#</a></s…
18805 <p>Disable the <code>node-addons</code> exports condition as well as disable loading
18806 native addons. When <code>--no-addons</code> is specified, calling <code>process.dlopen</code> or
18808 <h4><code>--no-deprecation</code><span><a class="mark" href="#cli_no_deprecation" id="cli_no_deprec…
18813 <h4><code>--no-force-async-hooks-checks</code><span><a class="mark" href="#cli_no_force_async_hooks…
18819 <h4><code>--no-warnings</code><span><a class="mark" href="#cli_no_warnings" id="cli_no_warnings">#<…
18824 <h4><code>--node-memory-debug</code><span><a class="mark" href="#cli_node_memory_debug" id="cli_nod…
18830 <h4><code>--openssl-config=file</code><span><a class="mark" href="#cli_openssl_config_file" id="cli…
18835 used to enable FIPS-compliant crypto if Node.js is built
18836 against FIPS-enabled OpenSSL.</p>
18837 <h4><code>--openssl-shared-config</code><span><a class="mark" href="#cli_openssl_shared_config" id=…
18844 <code>OPENSSL_CONF</code>, or by using the command line option <code>--openssl-config</code>.
18849 <h4><code>--pending-deprecation</code><span><a class="mark" href="#cli_pending_deprecation" id="cli…
18856 unless either the <code>--pending-deprecation</code> command-line flag, or the
18860 <h4><code>--policy-integrity=sri</code><span><a class="mark" href="#cli_policy_integrity_sri" id="c…
18864 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18866 the specified integrity. It expects a <a href="https://developer.mozilla.org/en-US/docs/Web/Securit…
18868 <h4><code>--preserve-symlinks</code><span><a class="mark" href="#cli_preserve_symlinks" id="cli_pre…
18875 to a different on-disk location, Node.js will dereference the link and use the
18876 actual on-disk "real path" of the module as both an identifier and as a root
18881 <pre><code class="language-text">{appDir}
18885 │ ├── moduleA -> {appDir}/moduleA
18892 <p>The <code>--preserve-symlinks</code> command-line flag instructs Node.js to use the
18895 <p>Note, however, that using <code>--preserve-symlinks</code> can have other side effects.
18900 <p>The <code>--preserve-symlinks</code> flag does not apply to the main module, which allows
18901 <code>node --preserve-symlinks node_module/.bin/&#x3C;foo></code> to work. To apply the same
18902 behavior for the main module, also use <code>--preserve-symlinks-main</code>.</p>
18903 <h4><code>--preserve-symlinks-main</code><span><a class="mark" href="#cli_preserve_symlinks_main" i…
18909 <p>This flag exists so that the main module can be opted-in to the same behavior
18910 that <code>--preserve-symlinks</code> gives to all other imports; they are separate flags,
18912 <p><code>--preserve-symlinks-main</code> does not imply <code>--preserve-symlinks</code>; use
18913 <code>--preserve-symlinks-main</code> in addition to
18914 <code>--preserve-symlinks</code> when it is not desirable to follow symlinks before
18916 <p>See <code>--preserve-symlinks</code> for more information.</p>
18917 <h4><code>--prof</code><span><a class="mark" href="#cli_prof" id="cli_prof">#</a></span></h4>
18922 <h4><code>--prof-process</code><span><a class="mark" href="#cli_prof_process" id="cli_prof_process"…
18926 <p>Process V8 profiler output generated using the V8 option <code>--prof</code>.</p>
18927 <h4><code>--redirect-warnings=file</code><span><a class="mark" href="#cli_redirect_warnings_file" i…
18937 <a href="#cli_diagnostic_dir_directory">--diagnostic-dir</a> command-line option.</p>
18938 <h4><code>--report-compact</code><span><a class="mark" href="#cli_report_compact" id="cli_report_co…
18942 <p>Write reports in a compact format, single-line JSON, more easily consumable
18943 by log processing systems than the default multi-line format designed for
18945 <h4><code>--report-dir=directory</code>, <code>report-directory=directory</code><span><a class="mar…
18953 <td><p>Changed from <code>--diagnostic-report-directory</code> to <code>--report-directory</code>.<…
18960 <h4><code>--report-filename=filename</code><span><a class="mark" href="#cli_report_filename_filenam…
18968 <td><p>changed from <code>--diagnostic-report-filename</code> to <code>--report-filename</code>.</p…
18975 <h4><code>--report-on-fatalerror</code><span><a class="mark" href="#cli_report_on_fatalerror" id="c…
18983 <td><p>changed from <code>--diagnostic-report-on-fatalerror</code> to <code>--report-on-fatalerror<…
18994 <h4><code>--report-on-signal</code><span><a class="mark" href="#cli_report_on_signal" id="cli_repor…
19002 <td><p>changed from <code>--diagnostic-report-on-signal</code> to <code>--report-on-signal</code>.<…
19010 specified through <code>--report-signal</code>.</p>
19011 <h4><code>--report-signal=signal</code><span><a class="mark" href="#cli_report_signal_signal" id="c…
19019 <td><p>changed from <code>--diagnostic-report-signal</code> to <code>--report-signal</code>.</p></t…
19027 <h4><code>--report-uncaught-exception</code><span><a class="mark" href="#cli_report_uncaught_except…
19035 <td><p>changed from <code>--diagnostic-report-uncaught-exception</code> to <code>--report-uncaught-
19044 <h4><code>--throw-deprecation</code><span><a class="mark" href="#cli_throw_deprecation" id="cli_thr…
19049 <h4><code>--title=title</code><span><a class="mark" href="#cli_title_title" id="cli_title_title">#<…
19054 <h4><code>--tls-cipher-list=list</code><span><a class="mark" href="#cli_tls_cipher_list_list" id="c…
19060 <h4><code>--tls-keylog=file</code><span><a class="mark" href="#cli_tls_keylog_file" id="cli_tls_key…
19067 <h4><code>--tls-max-v1.2</code><span><a class="mark" href="#cli_tls_max_v1_2" id="cli_tls_max_v1_2"…
19073 <h4><code>--tls-max-v1.3</code><span><a class="mark" href="#cli_tls_max_v1_3" id="cli_tls_max_v1_3"…
19079 <h4><code>--tls-min-v1.0</code><span><a class="mark" href="#cli_tls_min_v1_0" id="cli_tls_min_v1_0"…
19085 <h4><code>--tls-min-v1.1</code><span><a class="mark" href="#cli_tls_min_v1_1" id="cli_tls_min_v1_1"…
19091 <h4><code>--tls-min-v1.2</code><span><a class="mark" href="#cli_tls_min_v1_2" id="cli_tls_min_v1_2"…
19098 <h4><code>--tls-min-v1.3</code><span><a class="mark" href="#cli_tls_min_v1_3" id="cli_tls_min_v1_3"…
19104 <h4><code>--trace-atomics-wait</code><span><a class="mark" href="#cli_trace_atomics_wait" id="cli_t…
19108 <p>Print short summaries of calls to <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
19110 <pre><code class="language-text">(node:15701) [Thread 0] Atomics.wait(&#x26;lt;address> + 0, 1, inf…
19115 (node:15701) [Thread 1] Atomics.wait(&#x26;lt;address> + 4, -1, inf) started
19117 (node:15701) [Thread 1] Atomics.wait(&#x26;lt;address> + 4, -1, inf) was woken up by another thread…
19126 <h4><code>--trace-deprecation</code><span><a class="mark" href="#cli_trace_deprecation" id="cli_tra…
19131 <h4><code>--trace-event-categories</code><span><a class="mark" href="#cli_trace_event_categories" i…
19136 tracing is enabled using <code>--trace-events-enabled</code>.</p>
19137 <h4><code>--trace-event-file-pattern</code><span><a class="mark" href="#cli_trace_event_file_patter…
19143 <h4><code>--trace-events-enabled</code><span><a class="mark" href="#cli_trace_events_enabled" id="c…
19148 <h4><code>--trace-exit</code><span><a class="mark" href="#cli_trace_exit" id="cli_trace_exit">#</a>…
19154 <h4><code>--trace-sigint</code><span><a class="mark" href="#cli_trace_sigint" id="cli_trace_sigint"…
19159 <h4><code>--trace-sync-io</code><span><a class="mark" href="#cli_trace_sync_io" id="cli_trace_sync_…
19165 <h4><code>--trace-tls</code><span><a class="mark" href="#cli_trace_tls" id="cli_trace_tls">#</a></s…
19171 <h4><code>--trace-uncaught</code><span><a class="mark" href="#cli_trace_uncaught" id="cli_trace_unc…
19180 <h4><code>--trace-warnings</code><span><a class="mark" href="#cli_trace_warnings" id="cli_trace_war…
19185 <h4><code>--track-heap-objects</code><span><a class="mark" href="#cli_track_heap_objects" id="cli_t…
19190 <h4><code>--unhandled-rejections=mode</code><span><a class="mark" href="#cli_unhandled_rejections_m…
19205 <li><code>warn-with-error-code</code>: Emit <a href="#process_event_unhandledrejection"><code>unhan…
19209 <h4><code>--use-bundled-ca</code>, <code>--use-openssl-ca</code><span><a class="mark" href="#cli_us…
19215 at build-time.</p>
19224 <h4><code>--use-largepages=mode</code><span><a class="mark" href="#cli_use_largepages_mode" id="cli…
19228 <p>Re-map the Node.js static code to large memory pages at startup. If supported on
19239 <h4><code>--v8-options</code><span><a class="mark" href="#cli_v8_options" id="cli_v8_options">#</a>…
19243 <p>Print V8 command-line options.</p>
19244 <h4><code>--v8-pool-size=num</code><span><a class="mark" href="#cli_v8_pool_size_num" id="cli_v8_po…
19253 <h4><code>--zero-fill-buffers</code><span><a class="mark" href="#cli_zero_fill_buffers" id="cli_zer…
19257 <p>Automatically zero-fills all newly allocated <a href="#buffer_class_buffer"><code>Buffer</code><…
19259 <h4><code>-c</code>, <code>--check</code><span><a class="mark" href="#cli_c_check" id="cli_c_check"…
19265 <td><p>The <code>--require</code> option is now supported when checking a file.</p></td></tr>
19272 <h4><code>-e</code>, <code>--eval "script"</code><span><a class="mark" href="#cli_e_eval_script" id…
19278 <td><p>Built-in libraries are now available as predefined variables.</p></td></tr>
19289 <h4><code>-h</code>, <code>--help</code><span><a class="mark" href="#cli_h_help" id="cli_h_help">#<…
19293 <p>Print node command-line options.
19295 <h4><code>-i</code>, <code>--interactive</code><span><a class="mark" href="#cli_i_interactive" id="…
19300 <h4><code>-p</code>, <code>--print "script"</code><span><a class="mark" href="#cli_p_print_script" …
19306 <td><p>Built-in libraries are now available as predefined variables.</p></td></tr>
19312 <p>Identical to <code>-e</code> but prints the result.</p>
19313 <h4><code>-r</code>, <code>--require module</code><span><a class="mark" href="#cli_r_require_module…
19321 ES6 Module using <code>--require</code> will fail with an error.</p>
19322 <h4><code>-v</code>, <code>--version</code><span><a class="mark" href="#cli_v_version" id="cli_v_ve…
19332 <li><code>1</code>, <code>true</code>, or the empty string <code>''</code> indicate 16-color suppor…
19333 <li><code>2</code> to indicate 256-color support, or</li>
19334 <li><code>3</code> to indicate 16 million-color support.</li>
19343 <p><code>','</code>-separated list of core modules that should print debug information.</p>
19345 <p><code>','</code>-separated list of core C++ modules that should print debug information.</p>
19371 <p>Data path for ICU (<code>Intl</code> object) data. Will extend linked-in data when compiled
19372 with small-icu support.</p>
19382 <p>A space-separated list of command-line options. <code>options...</code> are interpreted
19383 before command-line options, so command-line options will override or
19385 an option that is not allowed in the environment is used, such as <code>-p</code> or a
19388 <pre><code class="language-bash">NODE_OPTIONS=<span class="hljs-string">'--require "./my path/file.…
19389 <p>A singleton flag passed as a command-line option will override the same flag
19391 <pre><code class="language-bash"><span class="hljs-comment"># The inspector will be available on po…
19392 NODE_OPTIONS=<span class="hljs-string">'--inspect=localhost:4444'</span> node --inspect=localhost:5…
19394 <code>NODE_OPTIONS</code> instances were passed first, and then its command-line
19396 …e><code class="language-bash">NODE_OPTIONS=<span class="hljs-string">'--require "./a.js"'</span> n…
19397 <span class="hljs-comment"># is equivalent to:</span>
19398 node --require <span class="hljs-string">"./a.js"</span> --require <span class="hljs-string">"./b.j…
19400 <!-- node-options-node start -->
19402 <li><code>--conditions</code>, <code>-C</code></li>
19403 <li><code>--diagnostic-dir</code></li>
19404 <li><code>--disable-proto</code></li>
19405 <li><code>--dns-result-order</code></li>
19406 <li><code>--enable-fips</code></li>
19407 <li><code>--enable-source-maps</code></li>
19408 <li><code>--experimental-abortcontroller</code></li>
19409 <li><code>--experimental-import-meta-resolve</code></li>
19410 <li><code>--experimental-json-modules</code></li>
19411 <li><code>--experimental-loader</code></li>
19412 <li><code>--experimental-modules</code></li>
19413 <li><code>--experimental-policy</code></li>
19414 <li><code>--experimental-repl-await</code></li>
19415 <li><code>--experimental-specifier-resolution</code></li>
19416 <li><code>--experimental-top-level-await</code></li>
19417 <li><code>--experimental-vm-modules</code></li>
19418 <li><code>--experimental-wasi-unstable-preview1</code></li>
19419 <li><code>--experimental-wasm-modules</code></li>
19420 <li><code>--force-context-aware</code></li>
19421 <li><code>--force-fips</code></li>
19422 <li><code>--frozen-intrinsics</code></li>
19423 <li><code>--heapsnapshot-near-heap-limit</code></li>
19424 <li><code>--heapsnapshot-signal</code></li>
19425 <li><code>--http-parser</code></li>
19426 <li><code>--icu-data-dir</code></li>
19427 <li><code>--input-type</code></li>
19428 <li><code>--insecure-http-parser</code></li>
19429 <li><code>--inspect-brk</code></li>
19430 <li><code>--inspect-port</code>, <code>--debug-port</code></li>
19431 <li><code>--inspect-publish-uid</code></li>
19432 <li><code>--inspect</code></li>
19433 <li><code>--max-http-header-size</code></li>
19434 <li><code>--napi-modules</code></li>
19435 <li><code>--no-addons</code></li>
19436 <li><code>--no-deprecation</code></li>
19437 <li><code>--no-force-async-hooks-checks</code></li>
19438 <li><code>--no-warnings</code></li>
19439 <li><code>--node-memory-debug</code></li>
19440 <li><code>--openssl-config</code></li>
19441 <li><code>--openssl-shared-config</code></li>
19442 <li><code>--pending-deprecation</code></li>
19443 <li><code>--policy-integrity</code></li>
19444 <li><code>--preserve-symlinks-main</code></li>
19445 <li><code>--preserve-symlinks</code></li>
19446 <li><code>--prof-process</code></li>
19447 <li><code>--redirect-warnings</code></li>
19448 <li><code>--report-compact</code></li>
19449 <li><code>--report-dir</code>, <code>--report-directory</code></li>
19450 <li><code>--report-filename</code></li>
19451 <li><code>--report-on-fatalerror</code></li>
19452 <li><code>--report-on-signal</code></li>
19453 <li><code>--report-signal</code></li>
19454 <li><code>--report-uncaught-exception</code></li>
19455 <li><code>--require</code>, <code>-r</code></li>
19456 <li><code>--throw-deprecation</code></li>
19457 <li><code>--title</code></li>
19458 <li><code>--tls-cipher-list</code></li>
19459 <li><code>--tls-keylog</code></li>
19460 <li><code>--tls-max-v1.2</code></li>
19461 <li><code>--tls-max-v1.3</code></li>
19462 <li><code>--tls-min-v1.0</code></li>
19463 <li><code>--tls-min-v1.1</code></li>
19464 <li><code>--tls-min-v1.2</code></li>
19465 <li><code>--tls-min-v1.3</code></li>
19466 <li><code>--trace-atomics-wait</code></li>
19467 <li><code>--trace-deprecation</code></li>
19468 <li><code>--trace-event-categories</code></li>
19469 <li><code>--trace-event-file-pattern</code></li>
19470 <li><code>--trace-events-enabled</code></li>
19471 <li><code>--trace-exit</code></li>
19472 <li><code>--trace-sigint</code></li>
19473 <li><code>--trace-sync-io</code></li>
19474 <li><code>--trace-tls</code></li>
19475 <li><code>--trace-uncaught</code></li>
19476 <li><code>--trace-warnings</code></li>
19477 <li><code>--track-heap-objects</code></li>
19478 <li><code>--unhandled-rejections</code></li>
19479 <li><code>--use-bundled-ca</code></li>
19480 <li><code>--use-largepages</code></li>
19481 <li><code>--use-openssl-ca</code></li>
19482 <li><code>--v8-pool-size</code></li>
19483 <li><code>--zero-fill-buffers</code></li>
19485 <!-- node-options-node end -->
19487 <!-- node-options-v8 start -->
19489 <li><code>--abort-on-uncaught-exception</code></li>
19490 <li><code>--disallow-code-generation-from-strings</code></li>
19491 <li><code>--huge-max-old-generation-size</code></li>
19492 <li><code>--interpreted-frames-native-stack</code></li>
19493 <li><code>--jitless</code></li>
19494 <li><code>--max-old-space-size</code></li>
19495 <li><code>--perf-basic-prof-only-functions</code></li>
19496 <li><code>--perf-basic-prof</code></li>
19497 <li><code>--perf-prof-unwinding-info</code></li>
19498 <li><code>--perf-prof</code></li>
19499 <li><code>--stack-trace-limit</code></li>
19501 <!-- node-options-v8 end -->
19502 <p><code>--perf-basic-prof-only-functions</code>, <code>--perf-basic-prof</code>,
19503 <code>--perf-prof-unwinding-info</code>, and <code>--perf-prof</code> are only available on Linux.<…
19508 <p><code>':'</code>-separated list of directories prefixed to the module search path.</p>
19509 <p>On Windows, this is a <code>';'</code>-separated list instead.</p>
19517 unless either the <code>--pending-deprecation</code> command-line flag, or the
19538 equivalent to using the <code>--redirect-warnings=file</code> command-line flag.</p>
19550 <p>Path to a Node.js module which will be loaded in place of the built-in REPL.
19551 Overriding this value to an empty string (<code>''</code>) will use the built-in REPL.</p>
19564 …gin outputting <a href="https://v8project.blogspot.com/2017/12/javascript-code-coverage.html">V8 J…
19572 …//chromedevtools.github.io/devtools-protocol/tot/Profiler#type-ScriptCoverage">ScriptCoverage</a> …
19574 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
19575 …<span class="hljs-attr">"result"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
19576 <span class="hljs-punctuation">{</span>
19577 …<span class="hljs-attr">"scriptId"</span><span class="hljs-punctuation">:</span> <span class="hljs
19578 …n class="hljs-attr">"url"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
19579 …pan class="hljs-attr">"functions"</span><span class="hljs-punctuation">:</span> <span class="hljs-
19580 <span class="hljs-punctuation">}</span>
19581 <span class="hljs-punctuation">]</span>
19582 <span class="hljs-punctuation">}</span></code></pre>
19584 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
19585 <p>If found, source map data is appended to the top-level key <code>source-map-cache</code>
19587 <p><code>source-map-cache</code> is an object with keys representing the files source maps
19588 were extracted from, and values which include the raw source-map URL
19591 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
19592 …<span class="hljs-attr">"result"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
19593 <span class="hljs-punctuation">{</span>
19594 …<span class="hljs-attr">"scriptId"</span><span class="hljs-punctuation">:</span> <span class="hljs
19595 …ljs-attr">"url"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"file:///a…
19596 …pan class="hljs-attr">"functions"</span><span class="hljs-punctuation">:</span> <span class="hljs-
19597 <span class="hljs-punctuation">}</span>
19598 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
19599 …<span class="hljs-attr">"source-map-cache"</span><span class="hljs-punctuation">:</span> <span cla…
19600 …<span class="hljs-attr">"file:///absolute/path/to/source.js"</span><span class="hljs-punctuation">…
19601 …class="hljs-attr">"url"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">".…
19602 …<span class="hljs-attr">"data"</span><span class="hljs-punctuation">:</span> <span class="hljs-pun…
19603 …<span class="hljs-attr">"version"</span><span class="hljs-punctuation">:</span> <span class="hljs-
19604 …<span class="hljs-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-
19605 <span class="hljs-string">"file:///absolute/path/to/original.js"</span>
19606 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
19607 …<span class="hljs-attr">"names"</span><span class="hljs-punctuation">:</span> <span class="hljs-pu…
19608 <span class="hljs-string">"Foo"</span><span class="hljs-punctuation">,</span>
19609 <span class="hljs-string">"console"</span><span class="hljs-punctuation">,</span>
19610 <span class="hljs-string">"info"</span>
19611 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
19612 …ass="hljs-attr">"mappings"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
19613 …<span class="hljs-attr">"sourceRoot"</span><span class="hljs-punctuation">:</span> <span class="hl…
19614 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
19615 …<span class="hljs-attr">"lineLengths"</span><span class="hljs-punctuation">:</span> <span class="h…
19616 <span class="hljs-number">13</span><span class="hljs-punctuation">,</span>
19617 <span class="hljs-number">62</span><span class="hljs-punctuation">,</span>
19618 <span class="hljs-number">38</span><span class="hljs-punctuation">,</span>
19619 <span class="hljs-number">27</span>
19620 <span class="hljs-punctuation">]</span>
19621 <span class="hljs-punctuation">}</span>
19622 <span class="hljs-punctuation">}</span>
19623 <span class="hljs-punctuation">}</span></code></pre>
19625 <p><a href="https://no-color.org"><code>NO_COLOR</code></a> is an alias for <code>NODE_DISABLE_COL…
19632 used to enable FIPS-compliant crypto if Node.js is built with <code>./configure --openssl-fips</cod…
19633 <p>If the <a href="#cli_openssl_config_file"><code>--openssl-config</code></a> command-line option …
19639 <p>If <code>--use-openssl-ca</code> is enabled, this overrides and sets OpenSSL's directory
19648 <p>If <code>--use-openssl-ca</code> is enabled, this overrides and sets OpenSSL's file
19682 <h4><code>--max-old-space-size=SIZE</code> (in megabytes)<span><a class="mark" href="#cli_max_old_s…
19688 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
19692 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
19709 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
19710 <span class="hljs-comment">// Prints: hello world, to stdout</span>
19711 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class=…
19712 <span class="hljs-comment">// Prints: hello world, to stdout</span>
19713-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
19714 <span class="hljs-comment">// Prints error message and stack trace to stderr:</span>
19715 <span class="hljs-comment">// Error: Whoops, something bad happened</span>
19716 <span class="hljs-comment">// at [eval]:5:15</span>
19717 <span class="hljs-comment">// at Script.runInThisContext (node:vm:132:18)</span>
19718 <span class="hljs-comment">// at Object.runInThisContext (node:vm:309:38)</span>
19719 <span class="hljs-comment">// at node:internal/process/execution:77:19</span>
19720 <span class="hljs-comment">// at [eval]-wrapper:6:22</span>
19721 <span class="hljs-comment">// at evalScript (node:internal/process/execution:76:60)</span>
19722 <span class="hljs-comment">// at node:internal/main/eval_string:23:3</span>
19724 <span class="hljs-keyword">const</span> name = <span class="hljs-string">'Will Robinson'</span>;
19725 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span>(<span …
19726 <span class="hljs-comment">// Prints: Danger Will Robinson! Danger!, to stderr</span></code></pre>
19728 <pre><code class="language-js"><span class="hljs-keyword">const</span> out = <span class="hljs-titl…
19729 <span class="hljs-keyword">const</span> err = <span class="hljs-title function_">getStreamSomehow</…
19730 …hljs-keyword">const</span> myConsole = <span class="hljs-keyword">new</span> <span class="hljs-var…
19732 myConsole.<span class="hljs-title function_">log</span>(<span class="hljs-string">'hello world'</sp…
19733 <span class="hljs-comment">// Prints: hello world, to out</span>
19734 myConsole.<span class="hljs-title function_">log</span>(<span class="hljs-string">'hello %s'</span>…
19735 <span class="hljs-comment">// Prints: hello world, to out</span>
19736 …lass="hljs-title function_">error</span>(<span class="hljs-keyword">new</span> <span class="hljs-t…
19737 <span class="hljs-comment">// Prints: [Error: Whoops, something bad happened], to err</span>
19739 <span class="hljs-keyword">const</span> name = <span class="hljs-string">'Will Robinson'</span>;
19740 myConsole.<span class="hljs-title function_">warn</span>(<span class="hljs-string">`Danger <span cl…
19741 <span class="hljs-comment">// Prints: Danger Will Robinson! Danger!, to err</span></code></pre>
19756-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Console</span> } = …
19757 …de class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">C…
19776 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
19780 <li><code>ignoreErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
19782 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
19789 <li><code>inspectOptions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
19791 <li><code>groupIndentation</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
19799 …"language-js"><span class="hljs-keyword">const</span> output = fs.<span class="hljs-title function…
19800 <span class="hljs-keyword">const</span> errorOutput = fs.<span class="hljs-title function_">createW…
19801 <span class="hljs-comment">// Custom simple logger</span>
19802-keyword">const</span> logger = <span class="hljs-keyword">new</span> <span class="hljs-title clas…
19803 <span class="hljs-comment">// use it like console</span>
19804 <span class="hljs-keyword">const</span> count = <span class="hljs-number">5</span>;
19805 logger.<span class="hljs-title function_">log</span>(<span class="hljs-string">'count: %d'</span>, …
19806 <span class="hljs-comment">// In stdout.log: count 5</span></code></pre>
19809-js"><span class="hljs-keyword">new</span> <span class="hljs-title class_">Console</span>({ <span …
19823 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
19824 <li><code>...message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
19826 …a message if <code>value</code> is <a href="https://developer.mozilla.org/en-US/docs/Glossary/Fals…
19830 <p>If <code>value</code> is <a href="https://developer.mozilla.org/en-US/docs/Glossary/Truthy">trut…
19831-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">asser…
19833-variable language_">console</span>.<span class="hljs-title function_">assert</span>(<span class="…
19834 <span class="hljs-comment">// Assertion failed: Whoops didn't work</span>
19836 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">assert</spa…
19837 <span class="hljs-comment">// Assertion failed</span></code></pre>
19854 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
19858 <!-- eslint-skip -->
19859 <pre><code class="language-js">> <span class="hljs-variable language_">console</span>.<span class="…
19860 <span class="hljs-attr">default</span>: <span class="hljs-number">1</span>
19861 <span class="hljs-literal">undefined</span>
19862 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</sp…
19863 <span class="hljs-attr">default</span>: <span class="hljs-number">2</span>
19864 <span class="hljs-literal">undefined</span>
19865 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</sp…
19866 <span class="hljs-attr">abc</span>: <span class="hljs-number">1</span>
19867 <span class="hljs-literal">undefined</span>
19868 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</sp…
19869 <span class="hljs-attr">xyz</span>: <span class="hljs-number">1</span>
19870 <span class="hljs-literal">undefined</span>
19871 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</sp…
19872 <span class="hljs-attr">abc</span>: <span class="hljs-number">2</span>
19873 <span class="hljs-literal">undefined</span>
19874 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</sp…
19875 <span class="hljs-attr">default</span>: <span class="hljs-number">3</span>
19876 <span class="hljs-literal">undefined</span>
19883 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
19886 <!-- eslint-skip -->
19887 …ass="language-js">> <span class="hljs-variable language_">console</span>.<span class="hljs-title f…
19888 <span class="hljs-attr">abc</span>: <span class="hljs-number">1</span>
19889 <span class="hljs-literal">undefined</span>
19890 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">countRese…
19891 <span class="hljs-literal">undefined</span>
19892 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</sp…
19893 <span class="hljs-attr">abc</span>: <span class="hljs-number">1</span>
19894 <span class="hljs-literal">undefined</span>
19909 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
19910 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
19918 <li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
19919 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
19921 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> …
19923 <li><code>depth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
19926 <li><code>colors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
19947 <li><code>...data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
19956 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
19957 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
19961 values similar to <a href="http://man7.org/linux/man-pages/man3/printf.3.html"><code>printf(3)</cod…
19963 <pre><code class="language-js"><span class="hljs-keyword">const</span> code = <span class="hljs-num…
19964 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span…
19965 <span class="hljs-comment">// Prints: error #5, to stderr</span>
19966 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span…
19967 <span class="hljs-comment">// Prints: error 5, to stderr</span></code></pre>
19976 <li><code>...label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
19998 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
19999 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
20007 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
20008 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
20012 values similar to <a href="http://man7.org/linux/man-pages/man3/printf.3.html"><code>printf(3)</cod…
20014 <pre><code class="language-js"><span class="hljs-keyword">const</span> count = <span class="hljs-nu…
20015 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
20016 <span class="hljs-comment">// Prints: count: 5, to stdout</span>
20017 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
20018 <span class="hljs-comment">// Prints: count: 5, to stdout</span></code></pre>
20025 <li><code>tabularData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
20026 <li><code>properties</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
20031 <pre><code class="language-js"><span class="hljs-comment">// These can't be parsed as tabular data<…
20032 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">table</span…
20033 <span class="hljs-comment">// Symbol()</span>
20035 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">table</span…
20036 <span class="hljs-comment">// undefined</span>
20038-variable language_">console</span>.<span class="hljs-title function_">table</span>([{ <span class…
20039 <span class="hljs-comment">// ┌─────────┬─────┬─────┐</span>
20040 <span class="hljs-comment">// │ (index) │ a │ b │</span>
20041 <span class="hljs-comment">// ├─────────┼─────┼─────┤</span>
20042 <span class="hljs-comment">// │ 0 │ 1 │ 'Y' │</span>
20043 <span class="hljs-comment">// │ 1 │ 'Z' │ 2 │</span>
20044 <span class="hljs-comment">// └─────────┴─────┴─────┘</span>
20046-variable language_">console</span>.<span class="hljs-title function_">table</span>([{ <span class…
20047 <span class="hljs-comment">// ┌─────────┬─────┐</span>
20048 <span class="hljs-comment">// │ (index) │ a │</span>
20049 <span class="hljs-comment">// ├─────────┼─────┤</span>
20050 <span class="hljs-comment">// │ 0 │ 1 │</span>
20051 <span class="hljs-comment">// │ 1 │ 'Z' │</span>
20052 <span class="hljs-comment">// └─────────┴─────┘</span></code></pre>
20058 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20080 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20084 …language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function…
20085 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
20086 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">timeEnd</sp…
20087 <span class="hljs-comment">// prints 100-elements: 225.438ms</span></code></pre>
20093 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20094 <li><code>...data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
20098 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
20099 <span class="hljs-keyword">const</span> value = <span class="hljs-title function_">expensiveProcess…
20100 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">timeLog</sp…
20101 <span class="hljs-comment">// Prints "process: 365.227ms 42".</span>
20102 <span class="hljs-title function_">doExpensiveProcess2</span>(value);
20103 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">timeEnd</sp…
20109 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
20110 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
20114 …lass="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title fu…
20115 <span class="hljs-comment">// Prints: (stack trace will vary based on where trace is called)</span>
20116 <span class="hljs-comment">// Trace: Show me</span>
20117 <span class="hljs-comment">// at repl:2:9</span>
20118 <span class="hljs-comment">// at REPLServer.defaultEval (repl.js:248:27)</span>
20119 <span class="hljs-comment">// at bound (domain.js:287:14)</span>
20120 <span class="hljs-comment">// at REPLServer.runBound [as eval] (domain.js:300:12)</span>
20121 <span class="hljs-comment">// at REPLServer.&#x3C;anonymous> (repl.js:412:12)</span>
20122 <span class="hljs-comment">// at emitOne (events.js:82:20)</span>
20123 <span class="hljs-comment">// at REPLServer.emit (events.js:169:7)</span>
20124 <span class="hljs-comment">// at REPLServer.Interface._onLine (readline.js:210:10)</span>
20125 <span class="hljs-comment">// at REPLServer.Interface._line (readline.js:549:8)</span>
20126 <span class="hljs-comment">// at REPLServer.Interface._ttyWrite (readline.js:826:14)</span></cod…
20132 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
20133 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
20139 (<code>--inspect</code> flag).</p>
20145 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20151 …ass="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title fun…
20152 <span class="hljs-comment">// Some code</span>
20153 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">profileEnd<…
20154 <span class="hljs-comment">// Adds the profile 'MyLabel' to the Profiles panel of the inspector.</s…
20160 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20173 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20182 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
20192 system-specific installation processes anymore just to have the package
20204 … do that simply run <a href="https://github.com/nodejs/corepack#corepack-enable--name"><code>corep…
20209 <a href="https://github.com/nodejs/corepack#corepack-disable--name"><code>corepack disable</code></…
20222 be easily overriden by running the <a href="https://github.com/nodejs/corepack#corepack-prepare--na…
20224 <pre><code class="language-bash">corepack prepare yarn@x.y.z --activate</code></pre>
20229 <a href="https://github.com/nodejs/corepack#corepack-prepare--nameversion"><code>corepack prepare</…
20232 <p>The <code>prepare</code> command has <a href="https://github.com/nodejs/corepack#utility-command…
20266 <h4>Running <code>npm install -g yarn</code> doesn't work<span><a class="mark" href="#corepack_runn…
20275 <li>Add the <code>--force</code> flag to <code>npm install</code>; this will tell npm that it's fin…
20278 that happen, run <a href="https://github.com/nodejs/corepack#corepack-enable--name"><code>corepack …
20282 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
20287 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20289 <span class="hljs-keyword">const</span> secret = <span class="hljs-string">'abcdefg'</span>;
20290 <span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHmac…
20291 ….<span class="hljs-title function_">update</span>(<span class="hljs-string">'I love cupcakes'</spa…
20292 … .<span class="hljs-title function_">digest</span>(<span class="hljs-string">'hex'</span>);
20293 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
20294 <span class="hljs-comment">// Prints:</span>
20295 <span class="hljs-comment">// c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e</s…
20300 <pre><code class="language-js"><span class="hljs-keyword">let</span> crypto;
20301 <span class="hljs-keyword">try</span> {
20302 crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
20303 } <span class="hljs-keyword">catch</span> (err) {
20304 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
20311 Netscape and was specified formally as part of <a href="https://developer.mozilla.org/en-US/docs/We…
20312 …deprecated since <a href="https://www.w3.org/TR/html52/changes.html#features-removed">HTML 5.2</a>…
20316 …nt. Node.js uses <a href="https://www.openssl.org/docs/man1.1.0/apps/openssl-spkac.html">OpenSSL's…
20322-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20326-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> …
20327 <span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow<…
20328 <span class="hljs-keyword">const</span> challenge = <span class="hljs-title class_">Certificate</sp…
20329-variable language_">console</span>.<span class="hljs-title function_">log</span>(challenge.<span …
20330 <span class="hljs-comment">// Prints: the challenge as a UTF8 string</span></code></pre>
20336-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20337 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20341-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> …
20342 <span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow<…
20343 <span class="hljs-keyword">const</span> publicKey = <span class="hljs-title class_">Certificate</sp…
20344 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
20345 <span class="hljs-comment">// Prints: the public key as &#x3C;Buffer ...></span></code></pre>
20351 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
20352 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
20355-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> …
20356 <span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow<…
20357-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
20358 <span class="hljs-comment">// Prints: true or false</span></code></pre>
20360 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
20366 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20368 <span class="hljs-keyword">const</span> cert1 = <span class="hljs-keyword">new</span> crypto.<span …
20369 <span class="hljs-keyword">const</span> cert2 = crypto.<span class="hljs-title class_">Certificate<…
20375-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20379-js"><span class="hljs-keyword">const</span> cert = <span class="hljs-built_in">require</span>(<sp…
20380 <span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow<…
20381 <span class="hljs-keyword">const</span> challenge = cert.<span class="hljs-title function_">exportC…
20382-variable language_">console</span>.<span class="hljs-title function_">log</span>(challenge.<span …
20383 <span class="hljs-comment">// Prints: the challenge as a UTF8 string</span></code></pre>
20389-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20393-js"><span class="hljs-keyword">const</span> cert = <span class="hljs-built_in">require</span>(<sp…
20394 <span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow<…
20395 <span class="hljs-keyword">const</span> publicKey = cert.<span class="hljs-title function_">exportP…
20396 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
20397 <span class="hljs-comment">// Prints: the public key as &#x3C;Buffer ...></span></code></pre>
20403 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
20404 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
20407-js"><span class="hljs-keyword">const</span> cert = <span class="hljs-built_in">require</span>(<sp…
20408 <span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow<…
20409-variable language_">console</span>.<span class="hljs-title function_">log</span>(cert.<span class…
20410 <span class="hljs-comment">// Prints: true or false</span></code></pre>
20430 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20432 <span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
20433 <span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to gene…
20434 <span class="hljs-comment">// Key length is dependent on the algorithm. In this case for aes192, it…
20435 <span class="hljs-comment">// 24 bytes (192 bits).</span>
20436 <span class="hljs-comment">// Use async `crypto.scrypt()` instead.</span>
20437-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password,…
20438 <span class="hljs-comment">// Use `crypto.randomBytes()` to generate a random iv instead of the sta…
20439 <span class="hljs-comment">// shown here.</span>
20440-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title …
20442 <span class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCi…
20444 <span class="hljs-keyword">let</span> encrypted = <span class="hljs-string">''</span>;
20445 cipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <s…
20446 <span class="hljs-keyword">let</span> chunk;
20447 …<span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = ciphe…
20448 …encrypted += chunk.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'h…
20451 cipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span c…
20452 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
20453 …<span class="hljs-comment">// Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda…
20456 cipher.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some clear text d…
20457 cipher.<span class="hljs-title function_">end</span>();</code></pre>
20459 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20460 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
20462 <span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
20463 <span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to gene…
20464 <span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
20465-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password,…
20466 <span class="hljs-comment">// Use `crypto.randomBytes()` to generate a random iv instead of the sta…
20467 <span class="hljs-comment">// shown here.</span>
20468-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title …
20470 <span class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCi…
20472 <span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStr…
20473 <span class="hljs-keyword">const</span> output = fs.<span class="hljs-title function_">createWriteS…
20475 input.<span class="hljs-title function_">pipe</span>(cipher).<span class="hljs-title function_">pip…
20477 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20479 <span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
20480 <span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to gene…
20481 <span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
20482-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password,…
20483 <span class="hljs-comment">// Use `crypto.randomBytes` to generate a random iv instead of the stati…
20484 <span class="hljs-comment">// shown here.</span>
20485-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title …
20487 <span class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCi…
20489-keyword">let</span> encrypted = cipher.<span class="hljs-title function_">update</span>(<span cla…
20490 encrypted += cipher.<span class="hljs-title function_">final</span>(<span class="hljs-string">'hex'…
20491 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
20492 <span class="hljs-comment">// Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0…
20498 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20499 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20523 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
20524 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
20526 <li><code>plaintextLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
20543 <li><code>autoPadding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
20551 Disabling automatic padding is useful for non-standard padding, for instance
20568-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20569 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
20570 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20571 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20605 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20607 <span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
20608 <span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to gene…
20609 <span class="hljs-comment">// Key length is dependent on the algorithm. In this case for aes192, it…
20610 <span class="hljs-comment">// 24 bytes (192 bits).</span>
20611 <span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
20612-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password,…
20613 <span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span>
20614-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title …
20616 <span class="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">create…
20618 <span class="hljs-keyword">let</span> decrypted = <span class="hljs-string">''</span>;
20619 decipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, …
20620 …<span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = decip…
20621 …decrypted += chunk.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'u…
20624 decipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span…
20625 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
20626 <span class="hljs-comment">// Prints: some clear text data</span>
20629 <span class="hljs-comment">// Encrypted with same algorithm, key and iv.</span>
20630 <span class="hljs-keyword">const</span> encrypted =
20631 …<span class="hljs-string">'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'</span…
20632 decipher.<span class="hljs-title function_">write</span>(encrypted, <span class="hljs-string">'hex'…
20633 decipher.<span class="hljs-title function_">end</span>();</code></pre>
20635 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20636 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
20638 <span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
20639 <span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to gene…
20640 <span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
20641-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password,…
20642 <span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span>
20643-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title …
20645 <span class="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">create…
20647 <span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStr…
20648 <span class="hljs-keyword">const</span> output = fs.<span class="hljs-title function_">createWriteS…
20650 input.<span class="hljs-title function_">pipe</span>(decipher).<span class="hljs-title function_">p…
20652 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20654 <span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
20655 <span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to gene…
20656 <span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
20657-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password,…
20658 <span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span>
20659-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title …
20661 <span class="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">create…
20663 <span class="hljs-comment">// Encrypted using same algorithm, key and iv.</span>
20664 <span class="hljs-keyword">const</span> encrypted =
20665 …<span class="hljs-string">'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'</span…
20666-keyword">let</span> decrypted = decipher.<span class="hljs-title function_">update</span>(encrypt…
20667 decrypted += decipher.<span class="hljs-title function_">final</span>(<span class="hljs-string">'ut…
20668 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
20669 <span class="hljs-comment">// Prints: some clear text data</span></code></pre>
20675 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20676 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20696 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
20697 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
20699 <li><code>plaintextLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
20726 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
20734 …s://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf">NIST SP 800-38D</a> or …
20744 <li><code>autoPadding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
20767-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20768 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
20769 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20770 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20788 <p>The <code>DiffieHellman</code> class is a utility for creating Diffie-Hellman key
20792 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20793 <span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span c…
20795 <span class="hljs-comment">// Generate Alice's keys...</span>
20796 <span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">createDif…
20797 <span class="hljs-keyword">const</span> aliceKey = alice.<span class="hljs-title function_">generat…
20799 <span class="hljs-comment">// Generate Bob's keys...</span>
20800-keyword">const</span> bob = crypto.<span class="hljs-title function_">createDiffieHellman</span>(…
20801 <span class="hljs-keyword">const</span> bobKey = bob.<span class="hljs-title function_">generateKey…
20803 <span class="hljs-comment">// Exchange and generate the secret...</span>
20804 <span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">comp…
20805 <span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeS…
20807 <span class="hljs-comment">// OK</span>
20808-title function_">strictEqual</span>(aliceSecret.<span class="hljs-title function_">toString</span…
20814-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20815 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
20816 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20817 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20833 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20834 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20836 <p>Generates private and public Diffie-Hellman key values, and returns
20846 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20847 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20849 <p>Returns the Diffie-Hellman generator in the specified <code>encoding</code>.
20857 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20858 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20860 <p>Returns the Diffie-Hellman prime in the specified <code>encoding</code>.
20868 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20869 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20871 <p>Returns the Diffie-Hellman private key in the specified <code>encoding</code>.
20879 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20880 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20882 <p>Returns the Diffie-Hellman public key in the specified <code>encoding</code>.
20890-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20891 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20893 <p>Sets the Diffie-Hellman private key. If the <code>encoding</code> argument is provided,
20902-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20903 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20905 <p>Sets the Diffie-Hellman public key. If the <code>encoding</code> argument is provided,
20927 <p>The <code>DiffieHellmanGroup</code> class takes a well-known modp group as its argument.
20931 <pre><code class="language-js"><span class="hljs-keyword">const</span> name = <span class="hljs-str…
20932 <span class="hljs-keyword">const</span> dh = crypto.<span class="hljs-title function_">createDiffie…
20933 …aken from <a href="https://www.rfc-editor.org/rfc/rfc2412.txt">RFC 2412</a> (modp1 and 2) and <a h…
20934 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">pe…
20947 <p>The <code>ECDH</code> class is a utility for creating Elliptic Curve Diffie-Hellman (ECDH)
20951 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20952 <span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span c…
20954 <span class="hljs-comment">// Generate Alice's keys...</span>
20955 <span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">createECD…
20956 <span class="hljs-keyword">const</span> aliceKey = alice.<span class="hljs-title function_">generat…
20958 <span class="hljs-comment">// Generate Bob's keys...</span>
20959 <span class="hljs-keyword">const</span> bob = crypto.<span class="hljs-title function_">createECDH<…
20960 <span class="hljs-keyword">const</span> bobKey = bob.<span class="hljs-title function_">generateKey…
20962 <span class="hljs-comment">// Exchange and generate the secret...</span>
20963 <span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">comp…
20964 <span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeS…
20966-title function_">strictEqual</span>(aliceSecret.<span class="hljs-title function_">toString</span…
20967 <span class="hljs-comment">// OK</span></code></pre>
20973-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20974 <li><code>curve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20975 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
20976 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20977 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
20978 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20980 <p>Converts the EC Diffie-Hellman public key specified by <code>key</code> and <code>curve</code> t…
20986 On recent OpenSSL releases, <code>openssl ecparam -list_curves</code> will also display
20993-js"><span class="hljs-keyword">const</span> { createECDH, <span class="hljs-variable constant_">E…
20995 <span class="hljs-keyword">const</span> ecdh = <span class="hljs-title function_">createECDH</span>…
20996 ecdh.<span class="hljs-title function_">generateKeys</span>();
20998-keyword">const</span> compressedKey = ecdh.<span class="hljs-title function_">getPublicKey</span>…
21000 <span class="hljs-keyword">const</span> uncompressedKey = <span class="hljs-variable constant_">ECD…
21001 <span class="hljs-string">'secp256k1'</span>,
21002 <span class="hljs-string">'hex'</span>,
21003 <span class="hljs-string">'hex'</span>,
21004 <span class="hljs-string">'uncompressed'</span>);
21006 <span class="hljs-comment">// The converted key and the uncompressed public key should be the same<…
21007-variable language_">console</span>.<span class="hljs-title function_">log</span>(uncompressedKey …
21023-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21024 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
21025 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
21026 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
21047 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21048 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
21049 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
21051 <p>Generates private and public EC Diffie-Hellman key values, and returns
21064 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21065 …://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C…
21074 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21075 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
21076 …://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C…
21089-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21090 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21092 <p>Sets the EC Diffie-Hellman private key.
21103 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
21105-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21106 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21108 <p>Sets the EC Diffie-Hellman public key.
21118 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21119 <span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">createECD…
21120 <span class="hljs-keyword">const</span> bob = crypto.<span class="hljs-title function_">createECDH<…
21122 <span class="hljs-comment">// This is a shortcut way of specifying one of Alice's previous private<…
21123 <span class="hljs-comment">// keys. It would be unwise to use such a predictable private key in a r…
21124 <span class="hljs-comment">// application.</span>
21125 alice.<span class="hljs-title function_">setPrivateKey</span>(
21126-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>).<span class="hljs-t…
21129 <span class="hljs-comment">// Bob uses a newly generated cryptographically strong</span>
21130 <span class="hljs-comment">// pseudorandom key pair</span>
21131 bob.<span class="hljs-title function_">generateKeys</span>();
21133-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>…
21134-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(ali…
21136 <span class="hljs-comment">// aliceSecret and bobSecret should be the same shared secret value</spa…
21137 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
21156 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21157 <span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash…
21159 hash.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <spa…
21160 <span class="hljs-comment">// Only one element is going to be produced by the</span>
21161 <span class="hljs-comment">// hash stream.</span>
21162 …<span class="hljs-keyword">const</span> data = hash.<span class="hljs-title function_">read</span>…
21163 <span class="hljs-keyword">if</span> (data) {
21164-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span class…
21165 <span class="hljs-comment">// Prints:</span>
21166 …<span class="hljs-comment">// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50</…
21170 hash.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to hash'<…
21171 hash.<span class="hljs-title function_">end</span>();</code></pre>
21173 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21174 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
21175 <span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash…
21177 <span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStr…
21178-title function_">pipe</span>(hash).<span class="hljs-title function_">setEncoding</span>(<span cl…
21180 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21181 <span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash…
21183 hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to hash'…
21184-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash.<span class…
21185 <span class="hljs-comment">// Prints:</span>
21186 <span class="hljs-comment">// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50</s…
21192 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
21202 <pre><code class="language-js"><span class="hljs-comment">// Calculate a rolling hash.</span>
21203 <span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span c…
21204 <span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash…
21206 hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'one'</span>);
21207-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash.<span class…
21209 hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'two'</span>);
21210-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash.<span class…
21212 hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'three'</span>);
21213-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash.<span class…
21215 <span class="hljs-comment">// Etc.</span></code></pre>
21221 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21222 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
21243-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21244 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
21270 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21271 …s-keyword">const</span> hmac = crypto.<span class="hljs-title function_">createHmac</span>(<span c…
21273 hmac.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <spa…
21274 <span class="hljs-comment">// Only one element is going to be produced by the</span>
21275 <span class="hljs-comment">// hash stream.</span>
21276 …<span class="hljs-keyword">const</span> data = hmac.<span class="hljs-title function_">read</span>…
21277 <span class="hljs-keyword">if</span> (data) {
21278-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span class…
21279 <span class="hljs-comment">// Prints:</span>
21280 …<span class="hljs-comment">// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e</…
21284 hmac.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to hash'<…
21285 hmac.<span class="hljs-title function_">end</span>();</code></pre>
21287 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21288 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
21289 …s-keyword">const</span> hmac = crypto.<span class="hljs-title function_">createHmac</span>(<span c…
21291 <span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStr…
21292 …nput.<span class="hljs-title function_">pipe</span>(hmac).<span class="hljs-title function_">pipe<…
21294 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21295 …s-keyword">const</span> hmac = crypto.<span class="hljs-title function_">createHmac</span>(<span c…
21297 hmac.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to hash'…
21298-variable language_">console</span>.<span class="hljs-title function_">log</span>(hmac.<span class…
21299 <span class="hljs-comment">// Prints:</span>
21300 <span class="hljs-comment">// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e</s…
21306 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21307 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
21327-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21328 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
21369 <td><p>Added support for <code>'rsa-pss'</code>.</p></td></tr>
21382 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
21388 <li><code>'rsa-pss'</code> (OID 1.2.840.113549.1.1.10)</li>
21404 <li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
21405 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
21413 <li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
21414 <li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
21418 <li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
21420 <li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
21421 <li><code>cipher</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
21424 <li><code>passphrase</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
21435 encryption mechanism, PEM-level encryption is not supported when encrypting
21436 …. See <a href="https://www.rfc-editor.org/rfc/rfc5208.txt">RFC 5208</a> for PKCS#8 encryption and …
21443 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
21452 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
21476 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21478 …s="hljs-keyword">const</span> { privateKey, publicKey } = crypto.<span class="hljs-title function_…
21479 <span class="hljs-attr">namedCurve</span>: <span class="hljs-string">'sect239k1'</span>
21482 <span class="hljs-keyword">const</span> sign = crypto.<span class="hljs-title function_">createSign…
21483 sign.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to sign'<…
21484 sign.<span class="hljs-title function_">end</span>();
21485 <span class="hljs-keyword">const</span> signature = sign.<span class="hljs-title function_">sign</s…
21487 <span class="hljs-keyword">const</span> verify = crypto.<span class="hljs-title function_">createVe…
21488 verify.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to sign…
21489 verify.<span class="hljs-title function_">end</span>();
21490-variable language_">console</span>.<span class="hljs-title function_">log</span>(verify.<span cla…
21491 <span class="hljs-comment">// Prints: true</span></code></pre>
21493 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21495 …s="hljs-keyword">const</span> { privateKey, publicKey } = crypto.<span class="hljs-title function_…
21496 <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">2048</span>,
21499 <span class="hljs-keyword">const</span> sign = crypto.<span class="hljs-title function_">createSign…
21500 sign.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to sign'…
21501 sign.<span class="hljs-title function_">end</span>();
21502 <span class="hljs-keyword">const</span> signature = sign.<span class="hljs-title function_">sign</s…
21504 <span class="hljs-keyword">const</span> verify = crypto.<span class="hljs-title function_">createVe…
21505 verify.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to sig…
21506 verify.<span class="hljs-title function_">end</span>();
21507 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
21508 <span class="hljs-comment">// Prints: true</span></code></pre>
21515 <td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr>
21517 <td><p>This function now supports RSA-PSS keys.</p></td></tr>
21521 <td><p>Support for RSASSA-PSS and additional options was added.</p></td></tr>
21528 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
21530 <li><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
21531 <li><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
21532 <li><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
21535 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
21536 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
21545 <p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
21548 <li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code…
21549 <li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li>
21553 <p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
21559 used to sign the message as specified in section 3.1 of <a href="https://www.rfc-editor.org/rfc/rfc…
21561 section 3.3 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>.</p>
21564 <p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21588-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21589 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
21628-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21629 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
21643 <td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr>
21645 <td><p>This function now supports RSA-PSS keys.</p></td></tr>
21649 <td><p>Support for RSASSA-PSS and additional options was added.</p></td></tr>
21656 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
21658 <li><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
21659 <li><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
21660 <li><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
21663-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21664 <li><code>signatureEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
21665 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
21674 <p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
21677 <li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code…
21678 <li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li>
21682 <p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
21688 used to verify the message as specified in section 3.1 of <a href="https://www.rfc-editor.org/rfc/r…
21690 section 3.3 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>.</p>
21693 <p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21716 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
21724 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
21736 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
21757 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
21759 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21760-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21761 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
21767 cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the
21773 recent OpenSSL releases, <code>openssl list -cipher-algorithms</code>
21774 (<code>openssl list-cipher-algorithms</code> for older versions of OpenSSL) will
21783 non-cryptographically secure hash algorithm allow passwords to be tested very
21791 …IV is reused in GCM, see <a href="https://github.com/nonce-disrespect/nonce-disrespect">Nonce-Disr…
21801 <td><p>The cipher <code>chacha20-poly1305</code> is now supported.</p></td></tr>
21814 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21815-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21816-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21817 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
21823 cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the
21829 recent OpenSSL releases, <code>openssl list -cipher-algorithms</code>
21830 (<code>openssl list-cipher-algorithms</code> for older versions of OpenSSL) will
21857 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
21859 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21860-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21861 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
21867 cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the
21874 non-cryptographically secure hash algorithm allow passwords to be tested very
21888 <td><p>The cipher <code>chacha20-poly1305</code> is now supported.</p></td></tr>
21901 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21902-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21903-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21904 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
21910 cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the
21916 recent OpenSSL releases, <code>openssl list -cipher-algorithms</code>
21917 (<code>openssl list-cipher-algorithms</code> for older versions of OpenSSL) will
21947-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21948 <li><code>primeEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
21949-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
21951 <li><code>generatorEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
21967 <li><code>primeLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
21968 <li><code>generator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21979 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
21988 <li><code>curveName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21991 <p>Creates an Elliptic Curve Diffie-Hellman (<code>ECDH</code>) key exchange object using a
21994 OpenSSL releases, <code>openssl ecparam -list_curves</code> will also display the name
22009 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22010 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22019 On recent releases of OpenSSL, <code>openssl list -digest-algorithms</code>
22020 (<code>openssl list-message-digest-algorithms</code> for older versions of OpenSSL) will
22023 …class="language-js"><span class="hljs-keyword">const</span> filename = process.<span class="hljs-p…
22024 <span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span c…
22025 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
22027 <span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash…
22029 <span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStr…
22030 input.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <sp…
22031 <span class="hljs-comment">// Only one element is going to be produced by the</span>
22032 <span class="hljs-comment">// hash stream.</span>
22033 …<span class="hljs-keyword">const</span> data = input.<span class="hljs-title function_">read</span…
22034 <span class="hljs-keyword">if</span> (data)
22035 hash.<span class="hljs-title function_">update</span>(data);
22036 <span class="hljs-keyword">else</span> {
22037-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22053 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22054-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22055 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22062 On recent releases of OpenSSL, <code>openssl list -digest-algorithms</code>
22063 (<code>openssl list-message-digest-algorithms</code> for older versions of OpenSSL) will
22068 …class="language-js"><span class="hljs-keyword">const</span> filename = process.<span class="hljs-p…
22069 <span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span c…
22070 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
22072 …s-keyword">const</span> hmac = crypto.<span class="hljs-title function_">createHmac</span>(<span c…
22074 <span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStr…
22075 input.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <sp…
22076 <span class="hljs-comment">// Only one element is going to be produced by the</span>
22077 <span class="hljs-comment">// hash stream.</span>
22078 …<span class="hljs-keyword">const</span> data = input.<span class="hljs-title function_">read</span…
22079 <span class="hljs-keyword">if</span> (data)
22080 hmac.<span class="hljs-title function_">update</span>(data);
22081 <span class="hljs-keyword">else</span> {
22082-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22090 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
22092 <li><code>key</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
22093 <li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
22094 <li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
22096 <li><code>passphrase</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
22121 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
22123 <li><code>key</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
22124 <li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
22125 <li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
22148 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22158 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22159 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22166 algorithm, such as <code>'RSA-SHA256'</code>, instead of a digest algorithm. This will use
22168 algorithms, such as <code>'ecdsa-with-SHA256'</code>, so it is best to always use digest
22175 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22176 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22184 algorithm, such as <code>'RSA-SHA256'</code>, instead of a digest algorithm. This will use
22186 algorithms, such as <code>'ecdsa-with-SHA256'</code>, so it is best to always use digest
22193 <li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22201 <p>Computes the Diffie-Hellman secret based on a <code>privateKey</code> and a <code>publicKey</cod…
22203 (for Diffie-Hellman), <code>'ec'</code> (for ECDH), <code>'x448'</code>, or <code>'x25519'</code> (…
22210 <td><p>Add support for Diffie-Hellman.</p></td></tr>
22223 <li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
22225 <li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22227 <li><code>modulusLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
22228 <li><code>publicExponent</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
22229 <li><code>divisorLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
22230 <li><code>namedCurve</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
22232 <li><code>primeLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
22233 <li><code>generator</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22234 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&…
22236 <li><code>publicKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
22237 <li><code>privateKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
22240 <li><code>callback</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
22242 <li><code>err</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
22243 <li><code>publicKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22244 <li><code>privateKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
22254 <code>'pkcs8'</code> with encryption for long-term storage:</p>
22255 …ss="language-js"><span class="hljs-keyword">const</span> { generateKeyPair } = <span class="hljs-b…
22256 <span class="hljs-title function_">generateKeyPair</span>(<span class="hljs-string">'rsa'</span>, {
22257 <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">4096</span>,
22258 <span class="hljs-attr">publicKeyEncoding</span>: {
22259 <span class="hljs-attr">type</span>: <span class="hljs-string">'spki'</span>,
22260 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>
22262 <span class="hljs-attr">privateKeyEncoding</span>: {
22263 <span class="hljs-attr">type</span>: <span class="hljs-string">'pkcs8'</span>,
22264 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>,
22265 <span class="hljs-attr">cipher</span>: <span class="hljs-string">'aes-256-cbc'</span>,
22266 <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'top secret'</span>
22268 }, <span class="hljs-function">(<span class="hljs-params">err, publicKey, privateKey</span>) =></sp…
22269 <span class="hljs-comment">// Handle errors and use the generated key pair.</span>
22281 <td><p>Add support for Diffie-Hellman.</p></td></tr>
22292 <li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
22294 <li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22296 <li><code>modulusLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
22297 <li><code>publicExponent</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
22298 <li><code>divisorLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
22299 <li><code>namedCurve</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
22301 <li><code>primeLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
22302 <li><code>generator</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22303 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&…
22305 <li><code>publicKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
22306 <li><code>privateKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
22309 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
22311 <li><code>publicKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22312 <li><code>privateKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
22324 …="language-js"><span class="hljs-keyword">const</span> { generateKeyPairSync } = <span class="hljs
22325 …class="hljs-keyword">const</span> { publicKey, privateKey } = <span class="hljs-title function_">g…
22326 <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">4096</span>,
22327 <span class="hljs-attr">publicKeyEncoding</span>: {
22328 <span class="hljs-attr">type</span>: <span class="hljs-string">'spki'</span>,
22329 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>
22331 <span class="hljs-attr">privateKeyEncoding</span>: {
22332 <span class="hljs-attr">type</span>: <span class="hljs-string">'pkcs8'</span>,
22333 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>,
22334 <span class="hljs-attr">cipher</span>: <span class="hljs-string">'aes-256-cbc'</span>,
22335 <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'top secret'</span>
22346 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
22349 <pre><code class="language-js"><span class="hljs-keyword">const</span> ciphers = crypto.<span class…
22350 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(ciphers); <sp…
22356 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
22358 <pre><code class="language-js"><span class="hljs-keyword">const</span> curves = crypto.<span class=…
22359-variable language_">console</span>.<span class="hljs-title function_">log</span>(curves); <span c…
22365 <li><code>groupName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22370 <a href="https://www.rfc-editor.org/rfc/rfc2412.txt">RFC 2412</a>, but see <a href="#crypto_support…
22371 …>'modp17'</code>, <code>'modp18'</code> (defined in <a href="https://www.rfc-editor.org/rfc/rfc352…
22379 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22380 <span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">getDiffie…
22381 <span class="hljs-keyword">const</span> bob = crypto.<span class="hljs-title function_">getDiffieHe…
22383 alice.<span class="hljs-title function_">generateKeys</span>();
22384 bob.<span class="hljs-title function_">generateKeys</span>();
22386-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>…
22387-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(ali…
22389 <span class="hljs-comment">/* aliceSecret and bobSecret should be the same */</span>
22390 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
22396 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
22397 currently in use, <code>0</code> otherwise. A future semver-major release may change
22398 the return type of this API to a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
22405 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
22406 such as <code>'RSA-SHA256'</code>. Hash algorithms are also called "digest" algorithms.</li>
22408 <pre><code class="language-js"><span class="hljs-keyword">const</span> hashes = crypto.<span class=…
22409 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(hashes); <sp…
22429-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22430-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22431 <li><code>iterations</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22432 <li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22433 <li><code>digest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22434 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22436 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
22441 <p>Provides an asynchronous Password-Based Key Derivation Function 2 (PBKDF2)
22456 …s://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST SP 800-132</a> for…
22457 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22458-title function_">pbkdf2</span>(<span class="hljs-string">'secret'</span>, <span class="hljs-strin…
22459 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22460-variable language_">console</span>.<span class="hljs-title function_">log</span>(derivedKey.<span…
22465 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22466 crypto.<span class="hljs-property">DEFAULT_ENCODING</span> = <span class="hljs-string">'hex'</span>;
22467-title function_">pbkdf2</span>(<span class="hljs-string">'secret'</span>, <span class="hljs-strin…
22468 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22469 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
22493-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22494-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22495 <li><code>iterations</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22496 <li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22497 <li><code>digest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22500 <p>Provides a synchronous Password-Based Key Derivation Function 2 (PBKDF2)
22512 …s://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST SP 800-132</a> for…
22513 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22514-keyword">const</span> key = crypto.<span class="hljs-title function_">pbkdf2Sync</span>(<span cla…
22515-variable language_">console</span>.<span class="hljs-title function_">log</span>(key.<span class=…
22519 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22520 crypto.<span class="hljs-property">DEFAULT_ENCODING</span> = <span class="hljs-string">'hex'</span>;
22521-keyword">const</span> key = crypto.<span class="hljs-title function_">pbkdf2Sync</span>(<span cla…
22522 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
22542 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
22544 <li><code>oaepHash</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
22546 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22554 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22576 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
22578 <li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
22579 <li><code>passphrase</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22585 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22607 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
22609 <li><code>passphrase</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22615 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22643 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
22645 <li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
22646 <li><code>oaepHash</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
22648 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22650 <li><code>passphrase</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22657 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22682 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
22683 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22685 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
22691 <p>Generates cryptographically strong pseudo-random data. The <code>size</code> argument
22697 <pre><code class="language-js"><span class="hljs-comment">// Asynchronous</span>
22698 <span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span c…
22699 …pan class="hljs-title function_">randomBytes</span>(<span class="hljs-number">256</span>, <span cl…
22700 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22701-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22706 <pre><code class="language-js"><span class="hljs-comment">// Synchronous</span>
22707 <span class="hljs-keyword">const</span> buf = crypto.<span class="hljs-title function_">randomBytes…
22708 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
22709 …ss="hljs-string">`<span class="hljs-subst">${buf.length}</span> bytes of random data: <span class=…
22735 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22736 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22737 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <st…
22738 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22741-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
22742-variable language_">console</span>.<span class="hljs-title function_">log</span>(crypto.<span cla…
22744 crypto.<span class="hljs-title function_">randomFillSync</span>(buf, <span class="hljs-number">5</s…
22745-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
22747 <span class="hljs-comment">// The above is equivalent to the following:</span>
22748 crypto.<span class="hljs-title function_">randomFillSync</span>(buf, <span class="hljs-number">5</s…
22749-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
22751-js"><span class="hljs-keyword">const</span> a = <span class="hljs-keyword">new</span> <span class…
22752-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22753 …js-property">byteOffset</span>, a.<span class="hljs-property">byteLength</span>).<span class="hljs
22755-keyword">const</span> b = <span class="hljs-keyword">new</span> <span class="hljs-title class_">F…
22756-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22757 …js-property">byteOffset</span>, b.<span class="hljs-property">byteLength</span>).<span class="hljs
22759-keyword">const</span> c = <span class="hljs-keyword">new</span> <span class="hljs-title class_">D…
22760-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22761 …js-property">byteOffset</span>, c.<span class="hljs-property">byteLength</span>).<span class="hljs
22775 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22776 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22777 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <st…
22778 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22784-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
22785 crypto.<span class="hljs-title function_">randomFill</span>(buf, <span class="hljs-function">(<span…
22786 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22787-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
22790 …an class="hljs-title function_">randomFill</span>(buf, <span class="hljs-number">5</span>, <span c…
22791 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22792-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
22795 <span class="hljs-comment">// The above is equivalent to the following:</span>
22796-title function_">randomFill</span>(buf, <span class="hljs-number">5</span>, <span class="hljs-num…
22797 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22798-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
22801-js"><span class="hljs-keyword">const</span> a = <span class="hljs-keyword">new</span> <span class…
22802 crypto.<span class="hljs-title function_">randomFill</span>(a, <span class="hljs-function">(<span c…
22803 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22804-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22805 .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
22808-keyword">const</span> b = <span class="hljs-keyword">new</span> <span class="hljs-title class_">F…
22809 crypto.<span class="hljs-title function_">randomFill</span>(b, <span class="hljs-function">(<span c…
22810 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22811-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22812 .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
22815-keyword">const</span> c = <span class="hljs-keyword">new</span> <span class="hljs-title class_">D…
22816 crypto.<span class="hljs-title function_">randomFill</span>(c, <span class="hljs-function">(<span c…
22817 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22818-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22819 .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
22833 <li><code>min</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
22834 <li><code>max</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
22835 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22839 <p>The range (<code>max - min</code>) must be less than 2<sup>48</sup>. <code>min</code> and <code>…
22840 be <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number…
22843 <pre><code class="language-js"><span class="hljs-comment">// Asynchronous</span>
22844 …<span class="hljs-title function_">randomInt</span>(<span class="hljs-number">3</span>, <span clas…
22845 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22846-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22848 <pre><code class="language-js"><span class="hljs-comment">// Synchronous</span>
22849 <span class="hljs-keyword">const</span> n = crypto.<span class="hljs-title function_">randomInt</sp…
22850-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22851 <pre><code class="language-js"><span class="hljs-comment">// With `min` argument</span>
22852 …="hljs-keyword">const</span> n = crypto.<span class="hljs-title function_">randomInt</span>(<span …
22853 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class=…
22859 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22861 <li><code>disableEntropyCache</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
22867 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
22869 <p>Generates a random <a href="https://www.rfc-editor.org/rfc/rfc4122.txt">RFC 4122</a> version 4 U…
22886-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22887-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22888 <li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22889 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22891 <li><code>cost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
22893 <li><code>blockSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22894 <li><code>parallelization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
22895 <li><code>N</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
22896 <li><code>r</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
22897 <li><code>p</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
22898 <li><code>maxmem</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22902 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22904 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
22909 …a href="https://en.wikipedia.org/wiki/Scrypt">scrypt</a> implementation. Scrypt is a password-based
22911 memory-wise in order to make brute-force attacks unrewarding.</p>
22913 …s://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST SP 800-132</a> for…
22919 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22920 <span class="hljs-comment">// Using the factory defaults.</span>
22921-title function_">scrypt</span>(<span class="hljs-string">'password'</span>, <span class="hljs-str…
22922 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22923-variable language_">console</span>.<span class="hljs-title function_">log</span>(derivedKey.<span…
22925 <span class="hljs-comment">// Using a custom N parameter. Must be a power of two.</span>
22926-title function_">scrypt</span>(<span class="hljs-string">'password'</span>, <span class="hljs-str…
22927 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22928-variable language_">console</span>.<span class="hljs-title function_">log</span>(derivedKey.<span…
22945-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22946-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22947 <li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22948 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22950 <li><code>cost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
22952 <li><code>blockSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22953 <li><code>parallelization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
22954 <li><code>N</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
22955 <li><code>r</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
22956 <li><code>p</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
22957 <li><code>maxmem</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22963 …a href="https://en.wikipedia.org/wiki/Scrypt">scrypt</a> implementation. Scrypt is a password-based
22965 memory-wise in order to make brute-force attacks unrewarding.</p>
22967 …s://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST SP 800-132</a> for…
22972 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22973 <span class="hljs-comment">// Using the factory defaults.</span>
22974-keyword">const</span> key1 = crypto.<span class="hljs-title function_">scryptSync</span>(<span cl…
22975-variable language_">console</span>.<span class="hljs-title function_">log</span>(key1.<span class…
22976 <span class="hljs-comment">// Using a custom N parameter. Must be a power of two.</span>
22977-keyword">const</span> key2 = crypto.<span class="hljs-title function_">scryptSync</span>(<span cl…
22978-variable language_">console</span>.<span class="hljs-title function_">log</span>(key2.<span class…
22984 <li><code>engine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
23005 <p>The flags below are deprecated in OpenSSL-1.1.0.</p>
23016 <li><code>bool</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
23018 <p>Enables the FIPS compliant crypto provider in a FIPS-enabled Node.js build.
23026 <td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr>
23033-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
23034 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
23035 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
23046 <p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
23049 <li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code…
23050 <li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li>
23054 <p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
23060 used to sign the message as specified in section 3.1 of <a href="https://www.rfc-editor.org/rfc/rfc…
23063 <p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
23075 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
23076 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
23077 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
23079 <p>This function is based on a constant-time algorithm.
23083 <a href="https://www.w3.org/TR/capability-urls/">capability urls</a>.</p>
23090 is timing-safe. Care should be taken to ensure that the surrounding code does
23098 <td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr>
23105-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
23106 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
23107 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
23108 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
23109 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
23119 <p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
23122 <li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code…
23123 <li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li>
23127 <p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
23133 used to sign the message as specified in section 3.1 of <a href="https://www.rfc-editor.org/rfc/rfc…
23136 <p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
23157 <p>Usage of <code>ECDH</code> with non-dynamically generated key pairs has been simplified.
23175 …ttps://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar1.pdf">NIST SP 800-131A</a>:…
23177 <li>MD5 and SHA-1 are no longer acceptable where collision resistance is
23195 <li>The length of the plaintext is limited to <code>2 ** (8 * (15 - N))</code> bytes.</li>
23199 compliance with section 2.6 of <a href="https://www.rfc-editor.org/rfc/rfc3610.txt">RFC 3610</a>.</…
23216 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
23218 <span class="hljs-keyword">const</span> key = <span class="hljs-string">'keykeykeykeykeykeykeykey'<…
23219 <span class="hljs-keyword">const</span> nonce = crypto.<span class="hljs-title function_">randomByt…
23221-keyword">const</span> aad = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title…
23223 …class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCipheri…
23224 <span class="hljs-attr">authTagLength</span>: <span class="hljs-number">16</span>
23226 <span class="hljs-keyword">const</span> plaintext = <span class="hljs-string">'Hello world'</span>;
23227 cipher.<span class="hljs-title function_">setAAD</span>(aad, {
23228 …<span class="hljs-attr">plaintextLength</span>: <span class="hljs-title class_">Buffer</span>.<spa…
23230 …span class="hljs-keyword">const</span> ciphertext = cipher.<span class="hljs-title function_">upda…
23231 cipher.<span class="hljs-title function_">final</span>();
23232 <span class="hljs-keyword">const</span> tag = cipher.<span class="hljs-title function_">getAuthTag<…
23234 <span class="hljs-comment">// Now transmit { ciphertext, nonce, tag }.</span>
23236 …ass="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">createDeciphe…
23237 <span class="hljs-attr">authTagLength</span>: <span class="hljs-number">16</span>
23239 decipher.<span class="hljs-title function_">setAuthTag</span>(tag);
23240 decipher.<span class="hljs-title function_">setAAD</span>(aad, {
23241 …<span class="hljs-attr">plaintextLength</span>: ciphertext.<span class="hljs-property">length</spa…
23243-keyword">const</span> receivedPlaintext = decipher.<span class="hljs-title function_">update</spa…
23245 <span class="hljs-keyword">try</span> {
23246 decipher.<span class="hljs-title function_">final</span>();
23247 } <span class="hljs-keyword">catch</span> (err) {
23248 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
23249 <span class="hljs-keyword">return</span>;
23252 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
23270 <td>Instructs OpenSSL to allow a non-[EC]DHE-based key exchange mode
23295 <td>Instructs OpenSSL to add server-hello extension from an early version
23322 <td>Instructs OpenSSL to disable the workaround for a man-in-the-middle
23323 protocol-version vulnerability in the SSL 2.0 server implementation.</td>
23347 <td>Instructs OpenSSL to disable encrypt-then-MAC.</td>
23573 <td>Specifies the built-in default cipher list used by Node.js.</td>
23584 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
23586 <p>Node.js includes a command-line debugging utility. To use it, start Node.js
23588 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
23589 &#x3C; Debugger listening on ws://127.0.0.1:9229/621111f9-ffcb-4e82-b718-48a145fa5db8
23597 <span class="hljs-meta">> </span><span class="language-bash">2 global.x = 5;</span>
23600 <span class="hljs-meta">debug></span></code></pre>
23601 <p>The Node.js debugger client is not a full-featured debugger, but simple step and
23605 <!-- eslint-disable no-debugger -->
23606 <pre><code class="language-js"><span class="hljs-comment">// myscript.js</span>
23607 <span class="hljs-variable language_">global</span>.<span class="hljs-property">x</span> = <span cl…
23608 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
23609 <span class="hljs-keyword">debugger</span>;
23610 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
23611 }, <span class="hljs-number">1000</span>);
23612 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
23614 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
23615 &#x3C; Debugger listening on ws://127.0.0.1:9229/621111f9-ffcb-4e82-b718-48a145fa5db8
23623 <span class="hljs-meta">> </span><span class="language-bash">2 global.x = 5;</span>
23626 <span class="hljs-meta">debug> </span><span class="language-bash">cont</span>
23632 <span class="hljs-meta">> </span><span class="language-bash">4 debugger;</span>
23635 <span class="hljs-meta">debug> </span><span class="language-bash">next</span>
23639 <span class="hljs-meta">> </span><span class="language-bash">5 console.log(<span class="hljs-stri…
23642 <span class="hljs-meta">debug> </span><span class="language-bash">repl</span>
23644 <span class="hljs-meta">> </span><span class="language-bash">x</span>
23646 <span class="hljs-meta">> </span><span class="language-bash">2 + 2</span>
23648 <span class="hljs-meta">debug> </span><span class="language-bash">next</span>
23654 <span class="hljs-meta">> </span><span class="language-bash">6 }, 1000);</span>
23657 <span class="hljs-meta">debug> </span><span class="language-bash">.<span class="hljs-built_in">exit…
23658 <span class="hljs-meta">$</span></code></pre>
23696 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
23697 &#x3C; Debugger listening on ws://127.0.0.1:9229/48a5b28a-550c-471b-b5e1-d13dd7165df9
23704 <span class="hljs-meta">> </span><span class="language-bash">1 const mod = require(<span class="hlj…
23707 <span class="hljs-meta">debug> </span><span class="language-bash">setBreakpoint(<span class="hljs-s…
23709 <span class="hljs-meta">debug> </span><span class="language-bash">c</span>
23713 <span class="hljs-meta">></span><span class="language-bash">22 exports.hello = <span class="hljs-fu…
23716 <span class="hljs-meta">debug></span></code></pre>
23719 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
23720 &#x3C; Debugger listening on ws://127.0.0.1:9229/ce24daa8-3816-44d4-b8ab-8273c8a66d35
23726 <span class="hljs-meta">> </span><span class="language-bash">7 addOne(10);</span>
23727 8 addOne(-1);
23729 … class="hljs-meta">debug> </span><span class="language-bash">setBreakpoint(<span class="hljs-strin…
23733 <span class="hljs-meta">> </span><span class="language-bash">4 <span class="hljs-built_in">return…
23737 8 addOne(-1);
23739 <span class="hljs-meta">debug> </span><span class="language-bash">cont</span>
23743 <span class="hljs-meta">> </span><span class="language-bash">4 <span class="hljs-built_in">return…
23746 <span class="hljs-meta">debug> </span><span class="language-bash"><span class="hljs-built_in">exec<…
23747 -1
23748 <span class="hljs-meta">debug></span></code></pre>
23776 <a href="https://chromedevtools.github.io/devtools-protocol/">Chrome DevTools Protocol</a>.</p>
23777 <p>V8 Inspector can be enabled by passing the <code>--inspect</code> flag when starting a
23779 e.g. <code>--inspect=9222</code> will accept DevTools connections on port 9222.</p>
23780 <p>To break on the first line of the application code, pass the <code>--inspect-brk</code>
23781 flag instead of <code>--inspect</code>.</p>
23782 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
23783 Debugger listening on ws://127.0.0.1:9229/dc9010dd-f8b8-4ac5-a510-c1a114ec7d29
23785 <p>(In the example above, the UUID dc9010dd-f8b8-4ac5-a510-c1a114ec7d29
23804 <li>Documentation-only</li>
23806 <li>End-of-Life</li>
23808 <p>A Documentation-only deprecation is one that is expressed only within the
23809 Node.js API docs. These generate no side-effects while running Node.js.
23810 Some Documentation-only deprecations trigger a runtime warning when launched
23811 with <a href="#cli_pending_deprecation"><code>--pending-deprecation</code></a> flag (or its alterna…
23813 deprecations below. Documentation-only deprecations that support that flag
23818 <a href="#cli_throw_deprecation"><code>--throw-deprecation</code></a> command-line flag is used, a …
23820 <p>An End-of-Life deprecation is used when functionality is or will soon be removed
23833 <td><p>End-of-Life.</p></td></tr>
23841 <p>Type: End-of-Life</p>
23850 <td><p>End-of-Life.</p></td></tr>
23858 <p>Type: End-of-Life</p>
23866 <td><p>End-of-Life.</p></td></tr>
23874 <p>Type: End-of-Life</p>
23883 <td><p>End-of-Life.</p></td></tr>
23887 <td><p>Documentation-only deprecation.</p></td></tr>
23891 <p>Type: End-of-Life</p>
23903 <td><p>Documentation-only deprecation.</p></td></tr>
23907 <p>Type: Runtime (supports <a href="#cli_pending_deprecation"><code>--pending-deprecation</code></a…
23920 …_arraybuffer_byteoffset_length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a> -
23926 <p>Without <code>--pending-deprecation</code>, runtime warnings occur only for code not in
23928 <code>Buffer()</code> usage in dependencies. With <code>--pending-deprecation</code>, a runtime
23936 <td><p>End-of-Life.</p></td></tr>
23942 <td><p>Documentation-only deprecation.</p></td></tr>
23946 <p>Type: End-of-Life</p>
23956 <td><p>End-of-Life.</p></td></tr>
23962 <td><p>Documentation-only deprecation.</p></td></tr>
23966 <p>Type: End-of-Life</p>
23972 precisely describe the actual semantics and was unnecessarily emotion-laden.</p>
23981 <td><p>Documentation-only deprecation.</p></td></tr>
23985 <p>Type: Documentation-only</p>
23996 <td><p>End-of-Life (for <code>digest === null</code>).</p></td></tr>
24000 <td><p>End-of-Life (for <code>digest === undefined</code>).</p></td></tr>
24008 <p>Type: End-of-Life</p>
24010 in Node.js 6.0 because the method defaulted to using the non-recommended
24024 <td><p>End-of-Life.</p></td></tr>
24032 <p>Type: End-of-Life</p>
24035 <h4 id="DEP0011">DEP0011: <code>crypto.Credentials</code><span><a class="mark" href="#deprecations_…
24041 <td><p>End-of-Life.</p></td></tr>
24049 <p>Type: End-of-Life</p>
24050 <p>The <code>crypto.Credentials</code> class was removed. Please use <a href="#tls_tls_createsecure…
24058 <td><p>End-of-Life.</p></td></tr>
24066 <p>Type: End-of-Life</p>
24075 <td><p>End-of-Life.</p></td></tr>
24081 <p>Type: End-of-Life</p>
24090 <td><p>End-of-Life.</p></td></tr>
24096 <td><p>Documentation-only deprecation.</p></td></tr>
24100 <p>Type: End-of-Life</p>
24109 <td><p>End-of-Life.</p></td></tr>
24115 <td><p>Documentation-only deprecation.</p></td></tr>
24119 <p>Type: End-of-Life</p>
24128 <td><p>End-of-Life.</p></td></tr>
24136 <p>Type: End-of-Life</p>
24145 <td><p>End-of-Life.</p></td></tr>
24151 <p>Type: End-of-Life</p>
24152 <p><code>Intl.v8BreakIterator</code> was a non-standard extension and has been removed.
24153 See <a href="https://github.com/tc39/proposal-intl-segmenter"><code>Intl.Segmenter</code></a>.</p>
24166 that are not handled will terminate the Node.js process with a non-zero exit
24182 <p>Type: End-of-Life</p>
24206 <td><p>End-of-Life.</p></td></tr>
24214 <p>Type: End-of-Life</p>
24223 <td><p>End-of-Life.</p></td></tr>
24229 <p>Type: End-of-Life</p>
24238 <td><p>End-of-Life.</p></td></tr>
24246 <p>Type: End-of-Life</p>
24255 <td><p>End-of-Life.</p></td></tr>
24261 <p>Type: End-of-Life</p>
24283 <td><p>End-of-Life.</p></td></tr>
24291 <p>Type: End-of-Life</p>
24299 <td><p>End-of-Life.</p></td></tr>
24307 <p>Type: End-of-Life</p>
24315 <td><p>End-of-Life.</p></td></tr>
24323 <p>Type: End-of-Life</p>
24331 <td><p>End-of-Life.</p></td></tr>
24339 <p>Type: End-of-Life</p>
24349 <td><p>Documentation-only deprecation.</p></td></tr>
24353 <p>Type: Documentation-only</p>
24364 <td><p>Documentation-only deprecation.</p></td></tr>
24368 <p>Type: Documentation-only</p>
24379 <td><p>Documentation-only deprecation.</p></td></tr>
24383 <p>Type: Documentation-only</p>
24393 <td><p>Documentation-only deprecation.</p></td></tr>
24397 <p>Type: Documentation-only</p>
24408 <td><p>Documentation-only deprecation.</p></td></tr>
24412 <p>Type: Documentation-only</p>
24423 <td><p>Documentation-only deprecation.</p></td></tr>
24427 <p>Type: Documentation-only</p>
24437 <td><p>Documentation-only deprecation.</p></td></tr>
24441 <p>Type: Documentation-only</p>
24453 <td><p>Documentation-only deprecation.</p></td></tr>
24471 <td><p>Documentation-only deprecation.</p></td></tr>
24486 <td><p>Documentation-only deprecation.</p></td></tr>
24490 <p>Type: Documentation-only</p>
24498 <td><p>Documentation-only deprecation.</p></td></tr>
24502 <p>Type: Documentation-only</p>
24511 <td><p>End-of-Life.</p></td></tr>
24515 <td><p>Documentation-only deprecation.</p></td></tr>
24519 <p>Type: End-of-Life</p>
24528 <td><p>End-of-Life.</p></td></tr>
24532 <td><p>Documentation-only deprecation.</p></td></tr>
24536 <p>Type: End-of-Life</p>
24549 <td><p>Documentation-only deprecation.</p></td></tr>
24557 <p>Type: Documentation-only</p>
24568 <td><p>Documentation-only deprecation.</p></td></tr>
24572 <p>Type: Documentation-only</p>
24583 <td><p>Documentation-only deprecation.</p></td></tr>
24587 <p>Type: Documentation-only</p>
24597 <td><p>Documentation-only deprecation.</p></td></tr>
24601 <p>Type: Documentation-only</p>
24612 <td><p>Documentation-only deprecation.</p></td></tr>
24616 <p>Type: Documentation-only</p>
24626 <td><p>Documentation-only deprecation.</p></td></tr>
24630 <p>Type: Documentation-only</p>
24640 <td><p>Documentation-only deprecation.</p></td></tr>
24644 <p>Type: Documentation-only</p>
24654 <td><p>Documentation-only deprecation.</p></td></tr>
24658 <p>Type: Documentation-only</p>
24668 <td><p>Documentation-only deprecation.</p></td></tr>
24672 <p>Type: Documentation-only</p>
24682 <td><p>Documentation-only deprecation.</p></td></tr>
24686 <p>Type: Documentation-only</p>
24696 <td><p>Documentation-only deprecation.</p></td></tr>
24700 <p>Type: Documentation-only</p>
24710 <td><p>Documentation-only deprecation.</p></td></tr>
24714 <p>Type: Documentation-only</p>
24724 <td><p>Documentation-only deprecation.</p></td></tr>
24728 <p>Type: Documentation-only</p>
24738 <td><p>Documentation-only deprecation.</p></td></tr>
24742 <p>Type: Documentation-only</p>
24752 <td><p>Documentation-only deprecation.</p></td></tr>
24756 <p>Type: Documentation-only</p>
24766 <td><p>Documentation-only deprecation.</p></td></tr>
24770 <p>Type: Documentation-only</p>
24780 <td><p>Documentation-only deprecation.</p></td></tr>
24784 <p>Type: Documentation-only</p>
24794 <td><p>Documentation-only deprecation.</p></td></tr>
24798 <p>Type: Documentation-only</p>
24806 <td><p>End-of-Life.</p></td></tr>
24810 <td><p>Documentation-only deprecation.</p></td></tr>
24814 <p>Type: End-of-Life</p>
24818 <h4 id="DEP0062">DEP0062: <code>node --debug</code><span><a class="mark" href="#deprecations_dep006…
24824 <td><p>End-of-Life.</p></td></tr>
24830 <p>Type: End-of-Life</p>
24831 <p><code>--debug</code> activates the legacy V8 debugger interface, which was removed as
24832 of V8 5.8. It is replaced by Inspector which is activated with <code>--inspect</code>
24840 <td><p>Documentation-only deprecation.</p></td></tr>
24844 <p>Type: Documentation-only</p>
24859 <td><p>Documentation-only deprecation.</p></td></tr>
24876 <td><p>End-of-Life.</p></td></tr>
24878 <td><p>Documentation-only deprecation.</p></td></tr>
24882 <p>Type: End-of-Life</p>
24898 <td><p>Documentation-only deprecation.</p></td></tr>
24921 <td><p>Documentation-only deprecation.</p></td></tr>
24925 <p>Type: Documentation-only</p>
24942 a V8-inspector based CLI debugger available through <code>node inspect</code>.</p>
24949 <td><p>End-of-Life.</p></td></tr>
24953 <td><p>Documentation-only deprecation.</p></td></tr>
24957 <p>Type: End-of-Life</p>
24966 <td><p>End-of-Life.</p></td></tr>
24972 <p>Type: End-of-Life</p>
24982 <td><p>End-of-Life.</p></td></tr>
24988 <p>Type: End-of-Life</p>
24998 <td><p>End-of-Life.</p></td></tr>
25004 <p>Type: End-of-Life</p>
25014 <td><p>End-of-Life.</p></td></tr>
25020 <p>Type: End-of-Life</p>
25023 <p>As the original API was undocumented and not generally useful for non-internal
25058 <td><p>Documentation-only deprecation.</p></td></tr>
25065 … class="language-js"><span class="hljs-keyword">const</span> querystring = <span class="hljs-built…
25066 querystring.<span class="hljs-title function_">parse</span>(str, <span class="hljs-string">'\n'</sp…
25069-console"><span class="hljs-meta">> </span><span class="language-bash">querystring.parse(<span cla…
25071 <span class="hljs-meta">> </span><span class="language-bash">tls.parseCertString(<span class="hljs-
25105 <td><p>End-of-Life.</p></td></tr>
25109 <td><p>Documentation-only deprecation.</p></td></tr>
25113 <p>Type: End-of-Life</p>
25124 <td><p>Documentation-only deprecation.</p></td></tr>
25128 <p>Type: Documentation-only</p>
25165 <td><p>End-of-Life.</p></td></tr>
25171 <p>Type: End-of-Life.</p>
25188 <p>Type: End-of-Life</p>
25201 <li><code>v8/tools/tickprocessor-driver</code></li>
25203 <li><code>node-inspect/lib/_inspect</code> (from 7.6.0)</li>
25204 <li><code>node-inspect/lib/internal/inspect_client</code> (from 7.6.0)</li>
25205 <li><code>node-inspect/lib/internal/inspect_repl</code> (from 7.6.0)</li>
25210 <p>On the other hand, <code>node-inspect</code> can be installed locally through a package
25219 <td><p>End-of-Life.</p></td></tr>
25225 <p>Type: End-of-Life</p>
25235 <td><p>End-of-Life.</p></td></tr>
25241 <p>Type: End-of-Life</p>
25252 <td><p>Documentation-only deprecation.</p></td></tr>
25266 <td><p>End-of-Life.</p></td></tr>
25272 <p>Type: End-of-Life</p>
25277 …s://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf">NIST SP 800-38D</a>.</p>
25290 <h4 id="DEP0092">DEP0092: Top-level <code>this</code> bound to <code>module.exports</code><span><a …
25296 <td><p>Documentation-only deprecation.</p></td></tr>
25300 <p>Type: Documentation-only</p>
25301 <p>Assigning properties to the top-level <code>this</code> as an alternative
25310 <td><p>Documentation-only deprecation.</p></td></tr>
25314 <p>Type: Documentation-only</p>
25370 <code>CallbackScope</code>, or the high-level <code>AsyncResource</code> class.</p>
25377 <td><p>End-of-Life.</p></td></tr>
25383 <p>Type: End-of-Life</p>
25390 <h4 id="DEP0099">DEP0099: Async context-unaware <code>node::MakeCallback</code> C++ APIs<span><a cl…
25396 <td><p>Compile-time deprecation.</p></td></tr>
25400 <p>Type: Compile-time</p>
25412 <td><p>Documentation-only deprecation.</p></td></tr>
25419 <h4 id="DEP0101">DEP0101: <code>--with-lttng</code><span><a class="mark" href="#deprecations_dep010…
25425 <td><p>End-of-Life.</p></td></tr>
25429 <p>Type: End-of-Life</p>
25430 <p>The <code>--with-lttng</code> compile-time option has been removed.</p>
25437 <td><p>End-of-Life.</p></td></tr>
25441 <p>Type: End-of-Life</p>
25453 <td><p>Documentation-only deprecation.</p></td></tr>
25457 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25468 <td><p>Documentation-only deprecation.</p></td></tr>
25472 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25473 <p>When assigning a non-string property to <a href="#process_process_env"><code>process.env</code><…
25484 <td><p>End-of-Life.</p></td></tr>
25490 <p>Type: End-of-Life</p>
25502 <td><p>Documentation-only deprecation.</p></td></tr>
25519 <td><p>End-of-Life.</p></td></tr>
25525 <p>Type: End-of-Life</p>
25536 <td><p>Documentation-only deprecation.</p></td></tr>
25568 <td><p>Documentation-only deprecation.</p></td></tr>
25572 <p>Type: Documentation-only</p>
25581 <td><p>Added support for <code>--pending-deprecation</code>.</p></td></tr>
25583 <td><p>Documentation-only deprecation.</p></td></tr>
25587 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25612 <td><p>End-of-Life.</p></td></tr>
25618 <p>Type: End-of-Life</p>
25627 <td><p>End-of-Life.</p></td></tr>
25633 <p>Type: End-of-Life</p>
25637 <!--lint disable nodejs-yaml-comments -->
25643 <td><p>Added documentation-only deprecation with <code>--pending-deprecation</code> support.</p></t…
25647 <!--lint enable nodejs-yaml-comments -->
25648 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25662 <td><p>Documentation-only deprecation.</p></td></tr>
25676 <td><p>End-of-Life.</p></td></tr>
25682 <p>Type: End-of-Life</p>
25709 <td><p>Documentation-only deprecation.</p></td></tr>
25713 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25722 <td><p>End-of-Life.</p></td></tr>
25728 <p>Type: End-of-Life</p>
25774 <a href="https://tools.ietf.org/html/rfc6066#section-3">RFC 6066</a>. This will be ignored in a fut…
25812 If re-referencing the timeout is necessary, <a href="#timers_timeout_ref"><code>timeout.ref()</code…
25835 <td><p>Documentation-only.</p></td></tr>
25839 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25840 <p>Modules that have an invalid <code>main</code> entry (e.g., <code>./does-not-exist.js</code>) and
25852 <td><p>Documentation-only.</p></td></tr>
25868 <td><p>Documentation-only.</p></td></tr>
25883 <td><p>Documentation-only.</p></td></tr>
25887 <p>Type: End-of-Life</p>
25890 <code>--http-parser=legacy</code> command-line flag could be used to revert to using the
25911 <td><p>Documentation-only deprecation.</p></td></tr>
25915 <p>Type: Documentation-only</p>
25924 <td><p>Documentation-only deprecation.</p></td></tr>
25928 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25952 <td><p>Documentation-only deprecation.</p></td></tr>
25956 <p>Type: Documentation-only</p>
25980-js"><span class="hljs-keyword">const</span> fsPromises = <span class="hljs-built_in">require</spa…
25981 …s="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title f…
25982 <span class="hljs-keyword">let</span> filehandle;
25983 <span class="hljs-keyword">try</span> {
25984 …ljs-keyword">await</span> fsPromises.<span class="hljs-title function_">open</span>(<span class="h…
25985 } <span class="hljs-keyword">finally</span> {
25986 … <span class="hljs-keyword">if</span> (filehandle !== <span class="hljs-literal">undefined</span>)
25987 …<span class="hljs-keyword">await</span> filehandle.<span class="hljs-title function_">close</span>…
25996 <td><p>Documentation-only deprecation.</p></td></tr>
26000 <p>Type: Documentation-only</p>
26001 <p><a href="#process_process_mainmodule"><code>process.mainModule</code></a> is a CommonJS-only fea…
26002 object is shared with non-CommonJS environment. Its use within ECMAScript
26012 <td><p>Documentation-only deprecation.</p></td></tr>
26016 <p>Type: Documentation-only</p>
26017 <p>Calling <code>process.umask()</code> with no argument causes the process-wide umask to be
26019 potential security vulnerability. There is no safe, cross-platform alternative
26027 <td><p>Documentation-only deprecation.</p></td></tr>
26031 <p>Type: Documentation-only</p>
26039 <td><p>Documentation-only (supports [<code>--pending-deprecation</code>][]).</p></td></tr>
26043 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
26052 <td><p>Documentation-only (supports [<code>--pending-deprecation</code>][]).</p></td></tr>
26056 <p>Type: Documentation-only</p>
26079 <td><p>Documentation-only deprecation.</p></td></tr>
26083 <p>Type: Documentation-only</p>
26090 …ge-js"><span class="hljs-keyword">if</span> (<span class="hljs-built_in">require</span>.<span clas…
26091 …<span class="hljs-comment">// Code section that will run only if current file is the entry point.<…
26095-js"><span class="hljs-keyword">const</span> moduleParents = <span class="hljs-title class_">Objec…
26096-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">m</span>) =…
26103 <td><p>Documentation-only deprecation.</p></td></tr>
26107 <p>Type: Documentation-only</p>
26115 <td><p>Documentation-only deprecation.</p></td></tr>
26119 <p>Type: Documentation-only</p>
26128 <td><p>Documentation-only deprecation.</p></td></tr>
26132 <p>Type: Documentation-only</p>
26142 <td><p>Documentation-only deprecation with <code>--pending-deprecation</code> support.</p></td></tr>
26146 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
26154 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
26159 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26161 will create one or many top-level channels to report messages through.
26173 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26175 <span class="hljs-comment">// Get a reusable channel object</span>
26176 …lass="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">…
26178 <span class="hljs-comment">// Subscribe to the channel</span>
26179 channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-function">(<span clas…
26180 <span class="hljs-comment">// Received data</span>
26183 <span class="hljs-comment">// Check if the channel has an active subscriber</span>
26184 <span class="hljs-keyword">if</span> (channel.<span class="hljs-property">hasSubscribers</span>) {
26185 <span class="hljs-comment">// Publish data to the channel</span>
26186 channel.<span class="hljs-title function_">publish</span>({
26187 <span class="hljs-attr">some</span>: <span class="hljs-string">'data'</span>
26192 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
26193 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
26198 performance-sensitive code.</p>
26199 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26201 … class="hljs-keyword">if</span> (diagnostics_channel.<span class="hljs-title function_">hasSubscri…
26202 <span class="hljs-comment">// There are subscribers, prepare and publish message</span>
26206 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
26209 <p>This is the primary entry-point for anyone wanting to interact with a named
26212 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26214 …lass="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">…
26225 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
26230 performance-sensitive code.</p>
26231 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26233 …lass="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">…
26235 <span class="hljs-keyword">if</span> (channel.<span class="hljs-property">hasSubscribers</span>) {
26236 <span class="hljs-comment">// There are subscribers, prepare and publish message</span>
26240 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
26244 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26246 …lass="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">…
26248 channel.<span class="hljs-title function_">publish</span>({
26249 <span class="hljs-attr">some</span>: <span class="hljs-string">'message'</span>
26253 <li><code>onMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
26255 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
26256 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
26263 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26265 …lass="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">…
26267 channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-function">(<span clas…
26268 <span class="hljs-comment">// Received data</span>
26283 <li><code>onMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
26284 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
26288 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26290 …lass="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">…
26292 <span class="hljs-keyword">function</span> <span class="hljs-title function_">onMessage</span>(<spa…
26293 <span class="hljs-comment">// Received data</span>
26296 channel.<span class="hljs-title function_">subscribe</span>(onMessage);
26298 channel.<span class="hljs-title function_">unsubscribe</span>(onMessage);</code></pre></section>
26302 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
26311 …code class="language-js"><span class="hljs-keyword">const</span> dns = <span class="hljs-built_in"…
26313 …n class="hljs-title function_">lookup</span>(<span class="hljs-string">'example.org'</span>, <span…
26314 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
26316 <span class="hljs-comment">// address: "93.184.216.34" family: IPv4</span></code></pre>
26321 DNS queries, bypassing other name-resolution facilities.</p>
26322 …code class="language-js"><span class="hljs-keyword">const</span> dns = <span class="hljs-built_in"…
26324 … class="hljs-title function_">resolve4</span>(<span class="hljs-string">'archive.org'</span>, <spa…
26325 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
26327-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
26329 …addresses.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span cla…
26330 …dns.<span class="hljs-title function_">reverse</span>(a, <span class="hljs-function">(<span class=…
26331 <span class="hljs-keyword">if</span> (err) {
26332 <span class="hljs-keyword">throw</span> err;
26334-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
26348-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Resolver</span> } =…
26349 <span class="hljs-keyword">const</span> resolver = <span class="hljs-keyword">new</span> <span clas…
26350 resolver.<span class="hljs-title function_">setServers</span>([<span class="hljs-string">'4.4.4.4'<…
26352 <span class="hljs-comment">// This request will use the server at 4.4.4.4, independent of global se…
26353 … class="hljs-title function_">resolve4</span>(<span class="hljs-string">'example.org'</span>, <spa…
26354 <span class="hljs-comment">// ...</span>
26391 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
26393 …ozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> …
26395 <li><code>tries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
26411 <li><code>ipv4</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
26413 <li><code>ipv6</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
26417 This allows programs to specify outbound interfaces when used on multi-homed
26429 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
26431 …rings, formatted according to <a href="https://tools.ietf.org/html/rfc5952#section-6">RFC 5952</a>,
26434 <!-- eslint-disable semi-->
26435 <pre><code class="language-js">[
26436 <span class="hljs-string">'4.4.4.4'</span>,
26437 <span class="hljs-string">'2001:4860:4860::8888'</span>,
26438 <span class="hljs-string">'4.4.4.4:1053'</span>,
26439 <span class="hljs-string">'[2001:4860:4860::8888]:1053'</span>,
26456 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26457 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
26459 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
26462 <li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
26464 <li><code>all</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
26466 <li><code>verbatim</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26472 <a href="#cli_dns_result_order_order"><code>--dns-result-order</code></a>. New code should use <cod…
26475 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26477 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26478 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
26479 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
26503 …code class="language-js"><span class="hljs-keyword">const</span> dns = <span class="hljs-built_in"…
26504 <span class="hljs-keyword">const</span> options = {
26505 <span class="hljs-attr">family</span>: <span class="hljs-number">6</span>,
26506 …<span class="hljs-attr">hints</span>: dns.<span class="hljs-property">ADDRCONFIG</span> | dns.<spa…
26508 …ss="hljs-title function_">lookup</span>(<span class="hljs-string">'example.com'</span>, options, <…
26509 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
26510 <span class="hljs-comment">// address: "2606:2800:220:1:248:1893:25c8:1946" family: IPv6</span>
26512 <span class="hljs-comment">// When options.all is true, the result will be an Array.</span>
26513 options.<span class="hljs-property">all</span> = <span class="hljs-literal">true</span>;
26514 …ss="hljs-title function_">lookup</span>(<span class="hljs-string">'example.com'</span>, options, <…
26515 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
26516 <span class="hljs-comment">// addresses: [{"address":"2606:2800:220:1:248:1893:25c8:1946","family":…
26532 <li><code>dns.ADDRCONFIG</code>: Limits returned address types to the types of non-loopback
26546 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
26547 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
26548 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26550 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26551 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26552 <li><code>service</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
26562 …code class="language-js"><span class="hljs-keyword">const</span> dns = <span class="hljs-built_in"…
26563-title function_">lookupService</span>(<span class="hljs-string">'127.0.0.1'</span>, <span class="…
26564 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
26565 <span class="hljs-comment">// Prints: localhost ssh</span>
26574 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26575 <li><code>rrtype</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
26576 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26578 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26579-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="h…
26670-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></td><td><a hre…
26686 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26687 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
26689 …eveloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;bo…
26695 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26697 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26698 …illa.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> |…
26719 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26720 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
26722 …eveloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;bo…
26728 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26730 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26731 …illa.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> |…
26740 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26741 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26743 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26744 <li><code>ret</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26804 <!-- eslint-disable semi -->
26805-js">[ { <span class="hljs-attr">type</span>: <span class="hljs-string">'A'</span>, <span class="h…
26806 …<span class="hljs-attr">type</span>: <span class="hljs-string">'CNAME'</span>, <span class="hljs-a…
26807-attr">type</span>: <span class="hljs-string">'MX'</span>, <span class="hljs-attr">exchange</span>…
26808 …{ <span class="hljs-attr">type</span>: <span class="hljs-string">'NS'</span>, <span class="hljs-at…
26809 …span class="hljs-attr">type</span>: <span class="hljs-string">'TXT'</span>, <span class="hljs-attr…
26810 { <span class="hljs-attr">type</span>: <span class="hljs-string">'SOA'</span>,
26811 <span class="hljs-attr">nsname</span>: <span class="hljs-string">'ns1.example.com'</span>,
26812 <span class="hljs-attr">hostmaster</span>: <span class="hljs-string">'admin.example.com'</span>,
26813 <span class="hljs-attr">serial</span>: <span class="hljs-number">156696742</span>,
26814 <span class="hljs-attr">refresh</span>: <span class="hljs-number">900</span>,
26815 <span class="hljs-attr">retry</span>: <span class="hljs-number">900</span>,
26816 <span class="hljs-attr">expire</span>: <span class="hljs-number">1800</span>,
26817 <span class="hljs-attr">minttl</span>: <span class="hljs-number">60</span> } ]</code></pre>
26826 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26827 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26829 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26830 <li><code>addresses</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
26843 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26844 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26846 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26847 <li><code>records</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
26860 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26861 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26863 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26864 <li><code>addresses</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
26877 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26878 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26880 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26881 <li><code>addresses</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
26896 <!-- eslint-skip -->
26897 <pre><code class="language-js">{
26898 <span class="hljs-attr">flags</span>: <span class="hljs-string">'s'</span>,
26899 <span class="hljs-attr">service</span>: <span class="hljs-string">'SIP+D2U'</span>,
26900 <span class="hljs-attr">regexp</span>: <span class="hljs-string">''</span>,
26901 …<span class="hljs-attr">replacement</span>: <span class="hljs-string">'_sip._udp.example.com'</spa…
26902 <span class="hljs-attr">order</span>: <span class="hljs-number">30</span>,
26903 <span class="hljs-attr">preference</span>: <span class="hljs-number">100</span>
26910 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26911 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26913 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26914 <li><code>addresses</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
26927 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26928 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26930 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26931 <li><code>addresses</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
26943 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26944 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26946 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26947 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
26963 <!-- eslint-skip -->
26964 <pre><code class="language-js">{
26965 <span class="hljs-attr">nsname</span>: <span class="hljs-string">'ns.example.com'</span>,
26966 <span class="hljs-attr">hostmaster</span>: <span class="hljs-string">'root.example.com'</span>,
26967 <span class="hljs-attr">serial</span>: <span class="hljs-number">2013101809</span>,
26968 <span class="hljs-attr">refresh</span>: <span class="hljs-number">10000</span>,
26969 <span class="hljs-attr">retry</span>: <span class="hljs-number">2400</span>,
26970 <span class="hljs-attr">expire</span>: <span class="hljs-number">604800</span>,
26971 <span class="hljs-attr">minttl</span>: <span class="hljs-number">3600</span>
26978 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26979 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26981 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26982 <li><code>addresses</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
26995 <!-- eslint-skip -->
26996 <pre><code class="language-js">{
26997 <span class="hljs-attr">priority</span>: <span class="hljs-number">10</span>,
26998 <span class="hljs-attr">weight</span>: <span class="hljs-number">5</span>,
26999 <span class="hljs-attr">port</span>: <span class="hljs-number">21223</span>,
27000 <span class="hljs-attr">name</span>: <span class="hljs-string">'service.example.com'</span>
27006 <!--lint disable no-undefined-references list-item-bullet-indent-->
27008 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27009 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
27011 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
27012 <li><code>records</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
27016 <!--lint enable no-undefined-references list-item-bullet-indent-->
27019 two-dimensional array of the text records available for <code>hostname</code> (e.g.
27020 <code>[ ['v=spf1 ip4:0.0.0.0 ', '~all' ] ]</code>). Each sub-array contains TXT chunks of
27028 <li><code>ip</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
27029 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
27031 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
27032 <li><code>hostnames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
27045 <li><code>order</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
27054 priority than <a href="#cli_dns_result_order_order"><code>--dns-result-order</code></a>. When using…
27062 …en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> array of <…
27065 …argument is an array of <a href="https://tools.ietf.org/html/rfc5952#section-6">RFC 5952</a> forma…
27067 <pre><code class="language-js">dns.<span class="hljs-title function_">setServers</span>([
27068 <span class="hljs-string">'4.4.4.4'</span>,
27069 <span class="hljs-string">'[2001:4860:4860::8888]'</span>,
27070 <span class="hljs-string">'4.4.4.4:1053'</span>,
27071 <span class="hljs-string">'[2001:4860:4860::8888]:1053'</span>,
27080 <a href="https://man7.org/linux/man-pages/man5/resolv.conf.5.html">resolve.conf</a>.
27109-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Resolver</span> } =…
27110 <span class="hljs-keyword">const</span> resolver = <span class="hljs-keyword">new</span> <span clas…
27111 resolver.<span class="hljs-title function_">setServers</span>([<span class="hljs-string">'4.4.4.4'<…
27113 <span class="hljs-comment">// This request will use the server at 4.4.4.4, independent of global se…
27114-title function_">resolve4</span>(<span class="hljs-string">'example.org'</span>).<span class="hlj…
27115 <span class="hljs-comment">// ...</span>
27118 <span class="hljs-comment">// Alternatively, the same code can be written using async-await style.<…
27119 (<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hl…
27120-keyword">const</span> addresses = <span class="hljs-keyword">await</span> resolver.<span class="h…
27152 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
27154 …rings, formatted according to <a href="https://tools.ietf.org/html/rfc5952#section-6">RFC 5952</a>,
27157 <!-- eslint-disable semi-->
27158 <pre><code class="language-js">[
27159 <span class="hljs-string">'4.4.4.4'</span>,
27160 <span class="hljs-string">'2001:4860:4860::8888'</span>,
27161 <span class="hljs-string">'4.4.4.4:1053'</span>,
27162 <span class="hljs-string">'[2001:4860:4860::8888]:1053'</span>,
27169 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27170 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
27172 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
27175 <li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
27177 <li><code>all</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
27179 <li><code>verbatim</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27185 <a href="#cli_dns_result_order_order"><code>--dns-result-order</code></a>. New code should use <cod…
27207 …code class="language-js"><span class="hljs-keyword">const</span> dns = <span class="hljs-built_in"…
27208 <span class="hljs-keyword">const</span> dnsPromises = dns.<span class="hljs-property">promises</spa…
27209 <span class="hljs-keyword">const</span> options = {
27210 <span class="hljs-attr">family</span>: <span class="hljs-number">6</span>,
27211 …<span class="hljs-attr">hints</span>: dns.<span class="hljs-property">ADDRCONFIG</span> | dns.<spa…
27214-title function_">lookup</span>(<span class="hljs-string">'example.com'</span>, options).<span cla…
27215-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
27216 <span class="hljs-comment">// address: "2606:2800:220:1:248:1893:25c8:1946" family: IPv6</span>
27219 <span class="hljs-comment">// When options.all is true, the result will be an Array.</span>
27220 options.<span class="hljs-property">all</span> = <span class="hljs-literal">true</span>;
27221-title function_">lookup</span>(<span class="hljs-string">'example.com'</span>, options).<span cla…
27222 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
27223 …<span class="hljs-comment">// addresses: [{"address":"2606:2800:220:1:248:1893:25c8:1946","family"…
27230 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
27231 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
27240-js"><span class="hljs-keyword">const</span> dnsPromises = <span class="hljs-built_in">require</sp…
27241-title function_">lookupService</span>(<span class="hljs-string">'127.0.0.1'</span>, <span class="…
27242-variable language_">console</span>.<span class="hljs-title function_">log</span>(result.<span cla…
27243 <span class="hljs-comment">// Prints: localhost ssh</span>
27250 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27251 <li><code>rrtype</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
27340-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></td><td><a hre…
27348 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27349 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
27351 …eveloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;bo…
27366 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27367 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
27369 …eveloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;bo…
27384 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27442 <!-- eslint-disable semi -->
27443-js">[ { <span class="hljs-attr">type</span>: <span class="hljs-string">'A'</span>, <span class="h…
27444 …<span class="hljs-attr">type</span>: <span class="hljs-string">'CNAME'</span>, <span class="hljs-a…
27445-attr">type</span>: <span class="hljs-string">'MX'</span>, <span class="hljs-attr">exchange</span>…
27446 …{ <span class="hljs-attr">type</span>: <span class="hljs-string">'NS'</span>, <span class="hljs-at…
27447 …span class="hljs-attr">type</span>: <span class="hljs-string">'TXT'</span>, <span class="hljs-attr…
27448 { <span class="hljs-attr">type</span>: <span class="hljs-string">'SOA'</span>,
27449 <span class="hljs-attr">nsname</span>: <span class="hljs-string">'ns1.example.com'</span>,
27450 <span class="hljs-attr">hostmaster</span>: <span class="hljs-string">'admin.example.com'</span>,
27451 <span class="hljs-attr">serial</span>: <span class="hljs-number">156696742</span>,
27452 <span class="hljs-attr">refresh</span>: <span class="hljs-number">900</span>,
27453 <span class="hljs-attr">retry</span>: <span class="hljs-number">900</span>,
27454 <span class="hljs-attr">expire</span>: <span class="hljs-number">1800</span>,
27455 <span class="hljs-attr">minttl</span>: <span class="hljs-number">60</span> } ]</code></pre>
27461 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27472 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27482 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27493 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27506 <!-- eslint-skip -->
27507 <pre><code class="language-js">{
27508 <span class="hljs-attr">flags</span>: <span class="hljs-string">'s'</span>,
27509 <span class="hljs-attr">service</span>: <span class="hljs-string">'SIP+D2U'</span>,
27510 <span class="hljs-attr">regexp</span>: <span class="hljs-string">''</span>,
27511 …<span class="hljs-attr">replacement</span>: <span class="hljs-string">'_sip._udp.example.com'</spa…
27512 <span class="hljs-attr">order</span>: <span class="hljs-number">30</span>,
27513 <span class="hljs-attr">preference</span>: <span class="hljs-number">100</span>
27520 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27531 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27541 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27555 <!-- eslint-skip -->
27556 <pre><code class="language-js">{
27557 <span class="hljs-attr">nsname</span>: <span class="hljs-string">'ns.example.com'</span>,
27558 <span class="hljs-attr">hostmaster</span>: <span class="hljs-string">'root.example.com'</span>,
27559 <span class="hljs-attr">serial</span>: <span class="hljs-number">2013101809</span>,
27560 <span class="hljs-attr">refresh</span>: <span class="hljs-number">10000</span>,
27561 <span class="hljs-attr">retry</span>: <span class="hljs-number">2400</span>,
27562 <span class="hljs-attr">expire</span>: <span class="hljs-number">604800</span>,
27563 <span class="hljs-attr">minttl</span>: <span class="hljs-number">3600</span>
27570 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27581 <!-- eslint-skip -->
27582 <pre><code class="language-js">{
27583 <span class="hljs-attr">priority</span>: <span class="hljs-number">10</span>,
27584 <span class="hljs-attr">weight</span>: <span class="hljs-number">5</span>,
27585 <span class="hljs-attr">port</span>: <span class="hljs-number">21223</span>,
27586 <span class="hljs-attr">name</span>: <span class="hljs-string">'service.example.com'</span>
27593 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27596 <code>hostname</code>. On success, the <code>Promise</code> is resolved with a two-dimensional array
27598 <code>[ ['v=spf1 ip4:0.0.0.0 ', '~all' ] ]</code>). Each sub-array contains TXT chunks of
27606 <li><code>ip</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
27617 <li><code>order</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
27626 higher priority than <a href="#cli_dns_result_order_order"><code>--dns-result-order</code></a>. Whe…
27634 …en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> array of <…
27637 …argument is an array of <a href="https://tools.ietf.org/html/rfc5952#section-6">RFC 5952</a> forma…
27639 <pre><code class="language-js">dnsPromises.<span class="hljs-title function_">setServers</span>([
27640 <span class="hljs-string">'4.4.4.4'</span>,
27641 <span class="hljs-string">'[2001:4860:4860::8888]'</span>,
27642 <span class="hljs-string">'4.4.4.4:1053'</span>,
27643 <span class="hljs-string">'[2001:4860:4860::8888]:1053'</span>,
27649 <a href="https://man7.org/linux/man-pages/man5/resolv.conf.5.html">resolve.conf</a>.
27677 <li><code>dns.NOTINITIALIZED</code>: c-ares library initialization not yet performed.</li>
27691 resolve a given name the same way as the <code>ping</code> command. On most POSIX-like
27693 …://man7.org/linux/man-pages/man5/nsswitch.conf.5.html"><code>nsswitch.conf(5)</code></a> and/or <a…
27697 perspective, it is implemented as a synchronous call to <a href="http://man7.org/linux/man-pages/ma…
27708 do not use <a href="http://man7.org/linux/man-pages/man3/getaddrinfo.3.html"><code>getaddrinfo(3)</…
27731 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
27749 <p>By the very nature of how <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer…
27759 <p>In this way, <code>domain</code> usage goes hand-in-hand with the cluster module,
27765 <pre><code class="language-js"><span class="hljs-comment">// XXX WARNING! BAD IDEA!</span>
27767 …class="hljs-keyword">const</span> d = <span class="hljs-built_in">require</span>(<span class="hljs
27768 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
27769 <span class="hljs-comment">// The error won't crash the process, but what it does is worse!</span>
27770 …<span class="hljs-comment">// Though we've prevented abrupt process restarting, we are leaking</sp…
27771 <span class="hljs-comment">// a lot of resources if this ever happens.</span>
27772 <span class="hljs-comment">// This is no better than process.on('uncaughtException')!</span>
27773 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
27775 d.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
27776-built_in">require</span>(<span class="hljs-string">'http'</span>).<span class="hljs-title functio…
27777 <span class="hljs-title function_">handleRequest</span>(req, res);
27778 …}).<span class="hljs-title function_">listen</span>(<span class="hljs-variable constant_">PORT</sp…
27783 <pre><code class="language-js"><span class="hljs-comment">// Much better!</span>
27785 <span class="hljs-keyword">const</span> cluster = <span class="hljs-built_in">require</span>(<span …
27786-keyword">const</span> <span class="hljs-variable constant_">PORT</span> = +process.<span class="h…
27788 <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isMaster</span>) {
27789 <span class="hljs-comment">// A more realistic scenario would have more than 2 workers,</span>
27790 <span class="hljs-comment">// and perhaps not put the master and worker in the same file.</span>
27791 <span class="hljs-comment">//</span>
27792 <span class="hljs-comment">// It is also possible to get a bit fancier about logging, and</span>
27793 <span class="hljs-comment">// implement whatever custom logic is needed to prevent DoS</span>
27794 <span class="hljs-comment">// attacks and other bad behavior.</span>
27795 <span class="hljs-comment">//</span>
27796 <span class="hljs-comment">// See the options in the cluster documentation.</span>
27797 <span class="hljs-comment">//</span>
27798 <span class="hljs-comment">// The important thing is that the master does very little,</span>
27799 <span class="hljs-comment">// increasing our resilience to unexpected errors.</span>
27801 cluster.<span class="hljs-title function_">fork</span>();
27802 cluster.<span class="hljs-title function_">fork</span>();
27804 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'disconnect'</span>, <span cl…
27805 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
27806 cluster.<span class="hljs-title function_">fork</span>();
27809 } <span class="hljs-keyword">else</span> {
27810 <span class="hljs-comment">// the worker</span>
27811 <span class="hljs-comment">//</span>
27812 <span class="hljs-comment">// This is where we put our bugs!</span>
27814 …<span class="hljs-keyword">const</span> domain = <span class="hljs-built_in">require</span>(<span …
27816 <span class="hljs-comment">// See the cluster documentation for more details about using</span>
27817 …<span class="hljs-comment">// worker processes to serve requests. How it works, caveats, etc.</spa…
27819-keyword">const</span> server = <span class="hljs-built_in">require</span>(<span class="hljs-strin…
27820 …<span class="hljs-keyword">const</span> d = domain.<span class="hljs-title function_">create</span…
27821 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
27822 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span…
27824 <span class="hljs-comment">// We're in dangerous territory!</span>
27825 <span class="hljs-comment">// By definition, something unexpected occurred,</span>
27826 <span class="hljs-comment">// which we probably didn't want.</span>
27827 <span class="hljs-comment">// Anything can happen now! Be very careful!</span>
27829 <span class="hljs-keyword">try</span> {
27830 <span class="hljs-comment">// Make sure we close down within 30 seconds</span>
27831 …<span class="hljs-keyword">const</span> killtimer = <span class="hljs-built_in">setTimeout</span>(…
27832 … process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">1</span>);
27833 }, <span class="hljs-number">30000</span>);
27834 <span class="hljs-comment">// But don't keep the process open just for that!</span>
27835 killtimer.<span class="hljs-title function_">unref</span>();
27837 <span class="hljs-comment">// Stop taking new requests.</span>
27838 server.<span class="hljs-title function_">close</span>();
27840 <span class="hljs-comment">// Let the master know we're dead. This will trigger a</span>
27841 … <span class="hljs-comment">// 'disconnect' in the cluster master, and then it will fork</span>
27842 <span class="hljs-comment">// a new worker.</span>
27843 …cluster.<span class="hljs-property">worker</span>.<span class="hljs-title function_">disconnect</s…
27845 …<span class="hljs-comment">// Try to send an error to the request that triggered the problem</span>
27846 res.<span class="hljs-property">statusCode</span> = <span class="hljs-number">500</span>;
27847 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'content-type'</…
27848 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Oops, there was a pro…
27849 } <span class="hljs-keyword">catch</span> (er2) {
27850 <span class="hljs-comment">// Oh well, not much we can do at this point.</span>
27851 …js-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class=…
27855 … <span class="hljs-comment">// Because req and res were created before this domain existed,</span>
27856 <span class="hljs-comment">// we need to explicitly add them.</span>
27857 <span class="hljs-comment">// See the explanation of implicit vs explicit binding below.</span>
27858 d.<span class="hljs-title function_">add</span>(req);
27859 d.<span class="hljs-title function_">add</span>(res);
27861 <span class="hljs-comment">// Now run the handler function in the domain.</span>
27862 d.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
27863 <span class="hljs-title function_">handleRequest</span>(req, res);
27866 …server.<span class="hljs-title function_">listen</span>(<span class="hljs-variable constant_">PORT…
27869 <span class="hljs-comment">// This part is not important. Just an example routing thing.</span>
27870 <span class="hljs-comment">// Put fancy application logic here.</span>
27871 <span class="hljs-keyword">function</span> <span class="hljs-title function_">handleRequest</span>(…
27872 <span class="hljs-keyword">switch</span> (req.<span class="hljs-property">url</span>) {
27873 <span class="hljs-keyword">case</span> <span class="hljs-string">'/error'</span>:
27874 <span class="hljs-comment">// We do some async stuff, and then...</span>
27875 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
27876 <span class="hljs-comment">// Whoops!</span>
27877 flerb.<span class="hljs-title function_">bark</span>();
27879 <span class="hljs-keyword">break</span>;
27880 <span class="hljs-attr">default</span>:
27881 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
27903 to <code>fs.open()</code>, or other callback-taking methods) will automatically be
27925 <pre><code class="language-js"><span class="hljs-comment">// Create a top-level domain for the serv…
27926 <span class="hljs-keyword">const</span> domain = <span class="hljs-built_in">require</span>(<span c…
27927 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
27928 <span class="hljs-keyword">const</span> serverDomain = domain.<span class="hljs-title function_">cr…
27930 serverDomain.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span>…
27931 <span class="hljs-comment">// Server is created in the scope of serverDomain</span>
27932 …http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span cla…
27933 <span class="hljs-comment">// Req and res are also created in the scope of serverDomain</span>
27934 …<span class="hljs-comment">// however, we'd prefer to have a separate domain for each request.</sp…
27935 <span class="hljs-comment">// create it first thing, and add req and res to it.</span>
27936 …<span class="hljs-keyword">const</span> reqd = domain.<span class="hljs-title function_">create</s…
27937 reqd.<span class="hljs-title function_">add</span>(req);
27938 reqd.<span class="hljs-title function_">add</span>(res);
27939 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
27940-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
27941 <span class="hljs-keyword">try</span> {
27942 … res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">500</span>);
27943 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Error occurred, sorry…
27944 } <span class="hljs-keyword">catch</span> (er2) {
27945-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
27948 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);
27963 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array…
27982 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
27983 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
27988 <pre><code class="language-js"><span class="hljs-keyword">const</span> d = domain.<span class="hljs
27990 <span class="hljs-keyword">function</span> <span class="hljs-title function_">readSomeFile</span>(<…
27991-title function_">readFile</span>(filename, <span class="hljs-string">'utf8'</span>, d.<span class…
27992 <span class="hljs-comment">// If this throws, it will also be passed to the domain.</span>
27993-keyword">return</span> <span class="hljs-title function_">cb</span>(er, data ? <span class="hljs-
27997 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
27998 …<span class="hljs-comment">// An error occurred somewhere. If we throw it now, it will crash the p…
27999 <span class="hljs-comment">// with the normal line number and stack message.</span>
28024 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
28025 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
28032 <pre><code class="language-js"><span class="hljs-keyword">const</span> d = domain.<span class="hljs
28034 <span class="hljs-keyword">function</span> <span class="hljs-title function_">readSomeFile</span>(<…
28035-title function_">readFile</span>(filename, <span class="hljs-string">'utf8'</span>, d.<span class…
28036 <span class="hljs-comment">// Note, the first argument is never passed to the</span>
28037 <span class="hljs-comment">// callback since it is assumed to be the 'Error' argument</span>
28038 <span class="hljs-comment">// and thus intercepted by the domain.</span>
28040 <span class="hljs-comment">// If this throws, it will also be passed to the domain</span>
28041 <span class="hljs-comment">// so the error-handling logic can be moved to the 'error'</span>
28042 <span class="hljs-comment">// event on the domain instead of being repeated throughout</span>
28043 <span class="hljs-comment">// the program.</span>
28044-keyword">return</span> <span class="hljs-title function_">cb</span>(<span class="hljs-literal">nu…
28048 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
28049 …<span class="hljs-comment">// An error occurred somewhere. If we throw it now, it will crash the p…
28050 <span class="hljs-comment">// with the normal line number and stack message.</span>
28060 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
28061 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
28068 …ode class="language-js"><span class="hljs-keyword">const</span> domain = <span class="hljs-built_i…
28069 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
28070 <span class="hljs-keyword">const</span> d = domain.<span class="hljs-title function_">create</span>…
28071 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
28072 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
28074 d.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
28075 …process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span…
28076 …<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { <span cla…
28077-title function_">open</span>(<span class="hljs-string">'non-existent file'</span>, <span class="h…
28078 <span class="hljs-keyword">if</span> (er) <span class="hljs-keyword">throw</span> er;
28079 <span class="hljs-comment">// proceed...</span>
28081 }, <span class="hljs-number">100</span>);
28089 <pre><code class="language-js"><span class="hljs-keyword">const</span> d1 = domain.<span class="hlj…
28090 <span class="hljs-keyword">const</span> d2 = domain.<span class="hljs-title function_">create</span…
28092 <span class="hljs-keyword">let</span> p;
28093 d1.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
28094 …p = <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</spa…
28097 d2.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
28098 …p.<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-pa…
28099 <span class="hljs-comment">// running in d2</span>
28103 <pre><code class="language-js"><span class="hljs-keyword">const</span> d1 = domain.<span class="hlj…
28104 <span class="hljs-keyword">const</span> d2 = domain.<span class="hljs-title function_">create</span…
28106 <span class="hljs-keyword">let</span> p;
28107 d1.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
28108 …p = <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</spa…
28111 d2.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
28112-title function_">then</span>(p.<span class="hljs-property">domain</span>.<span class="hljs-title …
28113 <span class="hljs-comment">// running in d1</span>
28126-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError" class="type">&#x3C;EvalError></a>, <a …
28127-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError" class="type">&#x3C;ReferenceError…
28131 <li>User-specified errors triggered by application code.</li>
28137 instances of, the standard JavaScript <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
28147 are handled using the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/St…
28149 <pre><code class="language-js"><span class="hljs-comment">// Throws with a ReferenceError because z…
28150 <span class="hljs-keyword">try</span> {
28151 <span class="hljs-keyword">const</span> m = <span class="hljs-number">1</span>;
28152 <span class="hljs-keyword">const</span> n = m + z;
28153 } <span class="hljs-keyword">catch</span> (err) {
28154 <span class="hljs-comment">// Handle the error here.</span>
28169 <!-- eslint-disable no-useless-return -->
28170 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
28171 …hljs-title function_">readFile</span>(<span class="hljs-string">'a file that does not exist'</span…
28172 <span class="hljs-keyword">if</span> (err) {
28173 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
28174 <span class="hljs-keyword">return</span>;
28176 <span class="hljs-comment">// Otherwise handle the data</span>
28182 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
28183 <span class="hljs-keyword">const</span> connection = net.<span class="hljs-title function_">connect…
28185 <span class="hljs-comment">// Adding an 'error' event handler to a stream:</span>
28186 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
28187 <span class="hljs-comment">// If the connection is reset by the server, or if it can't</span>
28188 <span class="hljs-comment">// connect at all, or on any sort of error encountered by</span>
28189 <span class="hljs-comment">// the connection, the error will be sent here.</span>
28190 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
28193 connection.<span class="hljs-title function_">pipe</span>(process.<span class="hljs-property">stdou…
28203 … the <code>'error'</code> event mechanism is most common for <a href="stream.html">stream-based</a>
28204 and <a href="#events_class_eventemitter">event emitter-based</a> APIs, which themselves represent a…
28212-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
28213 <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hlj…
28215 <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> {
28216 <span class="hljs-comment">// This will crash the process because no 'error' event</span>
28217 <span class="hljs-comment">// handler has been added.</span>
28218-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword"…
28224 <h4>Error-first callbacks<span><a class="mark" href="#errors_error_first_callbacks" id="errors_erro…
28227 pattern referred to as an <em>error-first callback</em>. With this pattern, a callback
28232 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
28234 <span class="hljs-keyword">function</span> <span class="hljs-title function_">errorFirstCallback</s…
28235 <span class="hljs-keyword">if</span> (err) {
28236 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
28237 <span class="hljs-keyword">return</span>;
28239 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
28242 fs.<span class="hljs-title function_">readFile</span>(<span class="hljs-string">'/some/file/that/do…
28243 fs.<span class="hljs-title function_">readFile</span>(<span class="hljs-string">'/some/file/that/do…
28246 use <code>throw</code> inside an error-first callback:</p>
28247 <pre><code class="language-js"><span class="hljs-comment">// THIS WILL NOT WORK:</span>
28248 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
28250 <span class="hljs-keyword">try</span> {
28251 …js-title function_">readFile</span>(<span class="hljs-string">'/some/file/that/does-not-exist'</sp…
28252 <span class="hljs-comment">// Mistaken assumption: throwing here...</span>
28253 <span class="hljs-keyword">if</span> (err) {
28254 <span class="hljs-keyword">throw</span> err;
28257 } <span class="hljs-keyword">catch</span> (err) {
28258 <span class="hljs-comment">// This will not catch the throw!</span>
28259 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
28269 <p>A generic JavaScript <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
28277 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
28283 are dependent on <a href="https://github.com/v8/v8/wiki/Stack-Trace-API">V8's stack trace API</a>. …
28288 <li><code>targetObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
28289 <li><code>constructorOpt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
28294 <pre><code class="language-js"><span class="hljs-keyword">const</span> myObject = {};
28295 <span class="hljs-title class_">Error</span>.<span class="hljs-title function_">captureStackTrace</…
28296 myObject.<span class="hljs-property">stack</span>; <span class="hljs-comment">// Similar to `new E…
28304 …de class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function…
28305-title class_">Error</span>.<span class="hljs-title function_">captureStackTrace</span>(<span clas…
28308 <span class="hljs-comment">// Without passing MyError to captureStackTrace, the MyError</span>
28309 <span class="hljs-comment">// frame would show up in the .stack property. By passing</span>
28310 <span class="hljs-comment">// the constructor, we omit that frame, and retain all frames below it.<…
28311 <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyError</span>().<span class=…
28314 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
28321 <p>If set to a non-number value, or set to a negative number, stack traces will
28325 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28330 change between any versions of Node.js. See <a href="#nodejs-error-codes">Node.js error codes</a> f…
28334 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28342-js"><span class="hljs-keyword">const</span> err = <span class="hljs-keyword">new</span> <span cla…
28343 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span…
28344 <span class="hljs-comment">// Prints: The message</span></code></pre>
28347 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28351 <pre><code class="language-console">Error: Things keep happening!
28353 at Frobnicator.refrobulate (/home/gbusey/business-logic.js:424:21)
28369 …language-js"><span class="hljs-keyword">const</span> cheetahify = <span class="hljs-built_in">requ…
28371 <span class="hljs-keyword">function</span> <span class="hljs-title function_">makeFaster</span>(<sp…
28372 <span class="hljs-comment">// `cheetahify()` *synchronously* calls speedy.</span>
28373 …ljs-title function_">cheetahify</span>(<span class="hljs-keyword">function</span> <span class="hlj…
28374 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
28378 <span class="hljs-title function_">makeFaster</span>();
28379 <span class="hljs-comment">// will throw:</span>
28380 <span class="hljs-comment">// /home/gbusey/file.js:6</span>
28381 <span class="hljs-comment">// throw new Error('oh no!');</span>
28382 <span class="hljs-comment">// ^</span>
28383 <span class="hljs-comment">// Error: oh no!</span>
28384 <span class="hljs-comment">// at speedy (/home/gbusey/file.js:6:11)</span>
28385 <span class="hljs-comment">// at makeFaster (/home/gbusey/file.js:5:3)</span>
28386 <span class="hljs-comment">// at Object.&#x3C;anonymous> (/home/gbusey/file.js:10:1)</span>
28387 <span class="hljs-comment">// at Module._compile (module.js:456:26)</span>
28388 <span class="hljs-comment">// at Object.Module._extensions..js (module.js:474:10)</span>
28389 <span class="hljs-comment">// at Module.load (module.js:356:32)</span>
28390 <span class="hljs-comment">// at Function.Module._load (module.js:312:12)</span>
28391 <span class="hljs-comment">// at Function.Module.runMain (module.js:497:10)</span>
28392 <span class="hljs-comment">// at startup (node.js:119:16)</span>
28393 <span class="hljs-comment">// at node.js:906:3</span></code></pre>
28397 <li><code>plain-filename.js:line:column</code>, if the frame represents a call internal
28420-js"><span class="hljs-built_in">require</span>(<span class="hljs-string">'net'</span>).<span clas…
28421 <span class="hljs-comment">// Throws "RangeError: "port" option should be >= 0 and &#x3C; 65536: -1…
28433 <pre><code class="language-js">doesNotExist;
28434 <span class="hljs-comment">// Throws ReferenceError, doesNotExist is not a variable in this program…
28445 <pre><code class="language-js"><span class="hljs-keyword">try</span> {
28446 …"hljs-built_in">require</span>(<span class="hljs-string">'vm'</span>).<span class="hljs-title func…
28447 } <span class="hljs-keyword">catch</span> (err) {
28448 <span class="hljs-comment">// 'err' will be a SyntaxError.</span>
28461 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
28463 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
28464 <li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
28466 …ps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x…
28467 <li><code>info</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
28468 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;stri…
28469 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
28470 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
28471 <li><code>syscall</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
28475 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28481 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28486 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28492 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
28501 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
28506 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28508 <p><code>error.message</code> is a system-provided human-readable description of the error.</p>
28511 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28516 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
28521 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28523 …ode> property is a string describing the <a href="https://man7.org/linux/man-pages/man2/syscalls.2…
28525 <p>This is a list of system errors commonly-encountered when writing a Node.js
28526 program. For a comprehensive list, see the <a href="https://man7.org/linux/man-pages/man3/errno.3.h…
28563 <code>ulimit -n 2048</code> in the same shell that will run the Node.js process.</p>
28605-js"><span class="hljs-built_in">require</span>(<span class="hljs-string">'url'</span>).<span clas…
28606 <span class="hljs-comment">// Throws TypeError, since it expected a string.</span></code></pre>
28622 OpenSSL-specific properties.</p>
28631 <p>A human-readable string describing the reason for the error.</p>
28632 <p><a id="nodejs-error-codes"></a></p>
28706 <!--
28709 - version: 11.12.0
28710 pr-url: https://github.com/nodejs/node/pull/26487
28712 - version: v14.17.1
28713 pr-url: https://github.com/nodejs/node/pull/38510
28715 -->
28721 non-writable <code>stdout</code> or <code>stderr</code> stream.</p>
28724 <!--
28726 -->
28759 <p>The <a href="#cli_force_fips"><code>--force-fips</code></a> command-line argument was used but t…
28812 <p>An unknown Diffie-Hellman group name was given. See
28819 <p>Loading native addons has been disabled using <a href="#cli_no_addons"><code>--no-addons</code><…
28850 <p><code>c-ares</code> failed to set the DNS server.</p>
28873 <p><code>--print</code> cannot be used with ESM input.</p>
28912 <p>Status code was outside the regular status code range (100-999).</p>
28925 <p>For HTTP/2 requests using the <code>CONNECT</code> method, the <code>:authority</code> pseudo-he…
28929 <p>For HTTP/2 requests using the <code>CONNECT</code> method, the <code>:path</code> pseudo-header …
28933 <p>For HTTP/2 requests using the <code>CONNECT</code> method, the <code>:scheme</code> pseudo-heade…
28937 <p>A non-specific HTTP/2 error has occurred.</p>
29026 <p>An HTTP/2 pseudo-header has been used inappropriately. Pseudo-headers are header
29043 <p>The <code>Http2Session</code> closed with a non-zero error code.</p>
29068 <p>A non-zero error code was been specified in an <code>RST_STREAM</code> frame.</p>
29093 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
29094 <p>The <code>--input-type</code> flag was used to attempt to execute a file. This flag can
29095 only be used with input via <code>--eval</code>, <code>--print</code> or <code>STDIN</code>.</p>
29142 less than -1 should never happen.</p>
29167 situation can only occur on Unix-like systems where only <code>localhost</code> or an empty
29173 semantics for determining whether a path can be used is platform-dependent.</p>
29244-js"><span class="hljs-keyword">const</span> urlSearchParams = <span class="hljs-keyword">new</spa…
29246 …="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs
29247 …SearchParams.<span class="hljs-property">has</span>.<span class="hljs-title function_">call</span>…
29248 <span class="hljs-comment">// Throws a TypeError with code 'ERR_INVALID_THIS'</span></code></pre>
29371 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
29381 <p>While using <code>Node-API</code>, a constructor passed was not a function.</p>
29395 <p>An error occurred while invoking the JavaScript portion of the thread-safe
29404 thread-safe function in an idle loop. This error indicates that an error
29420 <p>A non-context-aware native addon was loaded in a process that disallows them.</p>
29440 … href="#cli_disable_proto_mode"><code>--disable-proto=throw</code></a>. <a href="https://developer…
29441 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/se…
29445 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
29499 <a href="https://www.w3.org/TR/SRI/#the-integrity-attribute">Subresource Integrity specification</a…
29530-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">Socket</span> = <span…
29531 <span class="hljs-keyword">const</span> instance = <span class="hljs-keyword">new</span> <span clas…
29533 instance.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</sp…
29548 <p>An unspecified or non-specific system error has occurred within the Node.js
29553 <p>This error is thrown by <code>checkServerIdentity</code> if a user-supplied
29563 <p>While using TLS, the parameter offered for the Diffie-Hellman (<code>DH</code>)
29564 key-agreement protocol is too small. By default, the key length must be greater
29608 vector for denial-of-service attacks.</p>
29611 <p>An attempt was made to issue Server Name Indication from a TLS server-side
29620 the <code>--without-v8-platform</code> flag.</p>
29660 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
29665 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
29674 <a href="#packages_self_referencing_a_package_using_its_name">self-reference a package using its na…
29676 <!-- eslint-skip -->
29677 …pre><code class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-string">'…
29678 <span class="hljs-keyword">import</span> <span class="hljs-string">'./index.js'</span>; <span class…
29679 <span class="hljs-keyword">import</span> <span class="hljs-string">'package-name'</span>; <span cla…
29775 <p>Server is sending both a <code>Content-Length</code> header and <code>Transfer-Encoding: chunked…
29776 <p><code>Transfer-Encoding: chunked</code> allows the server to maintain an HTTP persistent
29778 In this case, the <code>Content-Length</code> HTTP header cannot be used.</p>
29779 <p>Use <code>Content-Length</code> or <code>Transfer-Encoding: chunked</code>.</p>
29794 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. These er…
29798 <!--
29801 -->
29809 <p>The UTF-16 encoding was used with <a href="#crypto_hash_digest_encoding"><code>hash.digest()</co…
29811 causing the method to return a string rather than a <code>Buffer</code>, the UTF-16
29867 <p>Used by the <code>Node-API</code> when <code>Constructor.prototype</code> is not an object.</p>
29933 <p>Used when a TLS renegotiation request has failed in a non-specific way.</p>
29982 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
29986 event-driven architecture in which certain kinds of objects (called "emitters")
29994 event names are camel-cased strings but any valid JavaScript property key
30002-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
30004 …lass="hljs-keyword">class</span> <span class="hljs-title class_">MyEmitter</span> <span class="hlj…
30006 <span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span cla…
30007 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <s…
30008 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30010 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);…
30017 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30018 …class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs
30019-variable language_">console</span>.<span class="hljs-title function_">log</span>(a, b, <span clas…
30020 <span class="hljs-comment">// Prints:</span>
30021 <span class="hljs-comment">// a b MyEmitter {</span>
30022 <span class="hljs-comment">// domain: null,</span>
30023 <span class="hljs-comment">// _events: { event: [Function] },</span>
30024 <span class="hljs-comment">// _eventsCount: 1,</span>
30025 <span class="hljs-comment">// _maxListeners: undefined } true</span>
30027 … class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>, <span class="h…
30030 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30031 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span clas…
30032 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30033 <span class="hljs-comment">// Prints: a b {}</span>
30035 … class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>, <span class="h…
30042 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30043 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span clas…
30044 <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> {
30045 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30048 … class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>, <span class="h…
30052 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30053 <span class="hljs-keyword">let</span> m = <span class="hljs-number">0</span>;
30054 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <s…
30055 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30057 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30058 <span class="hljs-comment">// Prints: 1</span>
30059 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30060 <span class="hljs-comment">// Prints: 2</span></code></pre>
30064 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30065 <span class="hljs-keyword">let</span> m = <span class="hljs-number">0</span>;
30066 myEmitter.<span class="hljs-title function_">once</span>(<span class="hljs-string">'event'</span>, …
30067 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30069 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30070 <span class="hljs-comment">// Prints: 1</span>
30071 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30072 <span class="hljs-comment">// Ignored</span></code></pre>
30080 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30081-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword"…
30082 <span class="hljs-comment">// Throws and crashes Node.js</span></code></pre>
30086 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30087 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
30088 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
30090-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword"…
30091 <span class="hljs-comment">// Prints: whoops! there was an error</span></code></pre>
30094-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30096 <span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span cla…
30097 myEmitter.<span class="hljs-title function_">on</span>(errorMonitor, <span class="hljs-function">(<…
30098 …<span class="hljs-title class_">MyMonitoringTool</span>.<span class="hljs-title function_">log</sp…
30100-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword"…
30101 <span class="hljs-comment">// Still throws and crashes Node.js</span></code></pre>
30103 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - captureRejections is…
30106 …e><code class="language-js"><span class="hljs-keyword">const</span> ee = <span class="hljs-keyword…
30107 ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span…
30108 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
30115-js"><span class="hljs-keyword">const</span> ee1 = <span class="hljs-keyword">new</span> <span cla…
30116 ee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <spa…
30117 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
30120 …="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-vari…
30122-keyword">const</span> ee2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
30123 ee2.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <spa…
30124 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
30127-title class_">Symbol</span>.<span class="hljs-title function_">for</span>(<span class="hljs-strin…
30130 …ode class="language-js"><span class="hljs-keyword">const</span> events = <span class="hljs-built_i…
30131 events.<span class="hljs-property">captureRejections</span> = <span class="hljs-literal">true</span…
30132 <span class="hljs-keyword">const</span> ee1 = <span class="hljs-keyword">new</span> events.<span cl…
30133 ee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <spa…
30134 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
30137 …="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-vari…
30154-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
30159 <li><code>captureRejections</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
30168 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30169 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30179-js"><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyEmitter</span> <spa…
30181 <span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span cla…
30182 <span class="hljs-comment">// Only do this once so we don't loop forever</span>
30183 …an class="hljs-title function_">once</span>(<span class="hljs-string">'newListener'</span>, <span …
30184 <span class="hljs-keyword">if</span> (event === <span class="hljs-string">'event'</span>) {
30185 <span class="hljs-comment">// Insert a new listener in front</span>
30186 …myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <…
30187 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30191 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <s…
30192 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30194 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30195 <span class="hljs-comment">// Prints:</span>
30196 <span class="hljs-comment">// B</span>
30197 <span class="hljs-comment">// A</span></code></pre>
30211 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30212 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30220 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30221 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30229 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30230 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
30231 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
30237-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
30238 <span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span cla…
30240 <span class="hljs-comment">// First listener</span>
30241-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-keyword">f…
30242 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30244 <span class="hljs-comment">// Second listener</span>
30245-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-keyword">f…
30246-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
30248 <span class="hljs-comment">// Third listener</span>
30249-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-keyword">f…
30250 …<span class="hljs-keyword">const</span> parameters = args.<span class="hljs-title function_">join<…
30251 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
30254-variable language_">console</span>.<span class="hljs-title function_">log</span>(myEmitter.<span …
30256-title function_">emit</span>(<span class="hljs-string">'event'</span>, <span class="hljs-number">…
30258 <span class="hljs-comment">// Prints:</span>
30259 <span class="hljs-comment">// [</span>
30260 <span class="hljs-comment">// [Function: firstListener],</span>
30261 <span class="hljs-comment">// [Function: secondListener],</span>
30262 <span class="hljs-comment">// [Function: thirdListener]</span>
30263 <span class="hljs-comment">// ]</span>
30264 <span class="hljs-comment">// Helloooo! first listener</span>
30265 <span class="hljs-comment">// event with parameters 1, 2 in second listener</span>
30266 <span class="hljs-comment">// event with parameters 1, 2, 3, 4, 5 in third listener</span></code></…
30272 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
30276-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
30277 <span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="h…
30278 myEE.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span cla…
30279 myEE.<span class="hljs-title function_">on</span>(<span class="hljs-string">'bar'</span>, <span cla…
30281 <span class="hljs-keyword">const</span> sym = <span class="hljs-title class_">Symbol</span>(<span c…
30282 myEE.<span class="hljs-title function_">on</span>(sym, <span class="hljs-function">() =></span> {});
30284 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
30285 <span class="hljs-comment">// Prints: [ 'foo', 'bar', Symbol(symbol) ]</span></code></pre>
30291 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
30301 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30302 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
30318 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30319 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
30322 …e-js">server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</…
30323 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30325-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class…
30326 <span class="hljs-comment">// Prints: [ [Function] ]</span></code></pre>
30332 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30333 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30342 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30343 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30351 …e-js">server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</…
30352 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30358 …><code class="language-js"><span class="hljs-keyword">const</span> myEE = <span class="hljs-keywor…
30359-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">()…
30360-title function_">prependListener</span>(<span class="hljs-string">'foo'</span>, <span class="hljs
30361 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>);
30362 <span class="hljs-comment">// Prints:</span>
30363 <span class="hljs-comment">// b</span>
30364 <span class="hljs-comment">// a</span></code></pre>
30370 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30371 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30374 <p>Adds a <strong>one-time</strong> <code>listener</code> function for the event named <code>eventN…
30376-js">server.<span class="hljs-title function_">once</span>(<span class="hljs-string">'connection'<…
30377 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30383 …><code class="language-js"><span class="hljs-keyword">const</span> myEE = <span class="hljs-keywor…
30384-title function_">once</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">…
30385-title function_">prependOnceListener</span>(<span class="hljs-string">'foo'</span>, <span class="…
30386 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>);
30387 <span class="hljs-comment">// Prints:</span>
30388 <span class="hljs-comment">// b</span>
30389 <span class="hljs-comment">// a</span></code></pre>
30395 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30396 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30404-js">server.<span class="hljs-title function_">prependListener</span>(<span class="hljs-string">'c…
30405 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30413 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30414 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30417 <p>Adds a <strong>one-time</strong> <code>listener</code> function for the event named <code>eventN…
30420-js">server.<span class="hljs-title function_">prependOnceListener</span>(<span class="hljs-string…
30421 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30429 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30442 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30443 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30448 …de class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-title function_">…
30449 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30451 server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, …
30452 <span class="hljs-comment">// ...</span>
30453 server.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'connecti…
30463 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30465 <span class="hljs-keyword">const</span> <span class="hljs-title function_">callbackA</span> = (<spa…
30466 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30467 …myEmitter.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'even…
30470 <span class="hljs-keyword">const</span> <span class="hljs-title function_">callbackB</span> = (<spa…
30471 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30474 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, ca…
30476 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, ca…
30478 <span class="hljs-comment">// callbackA removes listener callbackB but it will still be called.</sp…
30479 <span class="hljs-comment">// Internal listener array at time of emit [callbackA, callbackB]</span>
30480 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30481 <span class="hljs-comment">// Prints:</span>
30482 <span class="hljs-comment">// A</span>
30483 <span class="hljs-comment">// B</span>
30485 <span class="hljs-comment">// callbackB is now removed.</span>
30486 <span class="hljs-comment">// Internal listener array [callbackA]</span>
30487 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30488 <span class="hljs-comment">// Prints:</span>
30489 <span class="hljs-comment">// A</span></code></pre>
30499 …e><code class="language-js"><span class="hljs-keyword">const</span> ee = <span class="hljs-keyword…
30501 <span class="hljs-keyword">function</span> <span class="hljs-title function_">pong</span>(<span cla…
30502 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30505 ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'ping'</span>, pong);
30506 ee.<span class="hljs-title function_">once</span>(<span class="hljs-string">'ping'</span>, pong);
30507 ee.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'ping'</span>…
30509 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'ping'</span>);
30510 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'ping'</span>);</code><…
30517 <li><code>n</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
30531 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30532 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
30536 …<code class="language-js"><span class="hljs-keyword">const</span> emitter = <span class="hljs-keyw…
30537-title function_">once</span>(<span class="hljs-string">'log'</span>, <span class="hljs-function">…
30539 <span class="hljs-comment">// Returns a new Array with a function `onceWrapper` which has a propert…
30540 <span class="hljs-comment">// `listener` which contains the original listener bound above</span>
30541 <span class="hljs-keyword">const</span> listeners = emitter.<span class="hljs-title function_">rawL…
30542 <span class="hljs-keyword">const</span> logFnWrapper = listeners[<span class="hljs-number">0</span>…
30544 <span class="hljs-comment">// Logs "log once" to the console and does not unbind the `once` event</…
30545 logFnWrapper.<span class="hljs-title function_">listener</span>();
30547 <span class="hljs-comment">// Logs "log once" to the console and removes the listener</span>
30548 <span class="hljs-title function_">logFnWrapper</span>();
30550-title function_">on</span>(<span class="hljs-string">'log'</span>, <span class="hljs-function">()…
30551 <span class="hljs-comment">// Will return a new Array with a single function bound by `.on()` above…
30552 <span class="hljs-keyword">const</span> newListeners = emitter.<span class="hljs-title function_">r…
30554 <span class="hljs-comment">// Logs "log persistently" twice</span>
30555 newListeners[<span class="hljs-number">0</span>]();
30556 emitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'log'</span>);</co…
30561 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - captureRejections is…
30564 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30565 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
30572-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30574 …class="hljs-keyword">class</span> <span class="hljs-title class_">MyClass</span> <span class="hljs
30575 <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) {
30576 …<span class="hljs-variable language_">super</span>({ <span class="hljs-attr">captureRejections</sp…
30580-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
30581 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">destroy</span…
30584 <span class="hljs-title function_">destroy</span>(<span class="hljs-params">err</span>) {
30585 <span class="hljs-comment">// Tear the resource down here.</span>
30607-js">emitter.<span class="hljs-title function_">setMaxListeners</span>(emitter.<span class="hljs-t…
30608 emitter.<span class="hljs-title function_">once</span>(<span class="hljs-string">'event'</span>, <s…
30609 <span class="hljs-comment">// do stuff</span>
30610-title function_">setMaxListeners</span>(<span class="hljs-title class_">Math</span>.<span class="…
30612 <p>The <a href="#cli_trace_warnings"><code>--trace-warnings</code></a> command-line flag can be use…
30635 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30636 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
30643-js"><span class="hljs-keyword">const</span> { getEventListeners, <span class="hljs-title class_">…
30646 …<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hl…
30647-keyword">const</span> <span class="hljs-title function_">listener</span> = (<span class="hljs-par…
30648 ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, listener);
30649 …<span class="hljs-title function_">getEventListeners</span>(ee, <span class="hljs-string">'foo'</s…
30652 …<span class="hljs-keyword">const</span> et = <span class="hljs-keyword">new</span> <span class="hl…
30653-keyword">const</span> <span class="hljs-title function_">listener</span> = (<span class="hljs-par…
30654 …et.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</spa…
30655 …<span class="hljs-title function_">getEventListeners</span>(et, <span class="hljs-string">'foo'</s…
30671 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
30672 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
30677 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
30684 <a href="https://dom.spec.whatwg.org/#interface-eventtarget">EventTarget</a> interface, which has n…
30686-js"><span class="hljs-keyword">const</span> { once, <span class="hljs-title class_">EventEmitter<…
30688 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
30689 …<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hl…
30691 …process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span…
30692 …ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'myevent'</span>, <spa…
30695 …"hljs-keyword">const</span> [value] = <span class="hljs-keyword">await</span> <span class="hljs-ti…
30696 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30698 …class="hljs-keyword">const</span> err = <span class="hljs-keyword">new</span> <span class="hljs-ti…
30699 …process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span…
30700 … ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'error'</span>, err);
30703 <span class="hljs-keyword">try</span> {
30704 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(ee, <span c…
30705 } <span class="hljs-keyword">catch</span> (err) {
30706 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30710 <span class="hljs-title function_">run</span>();</code></pre>
30715-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30717 <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hlj…
30719 <span class="hljs-title function_">once</span>(ee, <span class="hljs-string">'error'</span>)
30720-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">[err]</span>)…
30721-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-params">err</span>) …
30723-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword"…
30725 <span class="hljs-comment">// Prints: ok boom</span></code></pre>
30727-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30729 <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hlj…
30730 <span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hlj…
30732 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
30733 <span class="hljs-keyword">try</span> {
30734 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(emitter, ev…
30735 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30736 } <span class="hljs-keyword">catch</span> (error) {
30737 …<span class="hljs-keyword">if</span> (error.<span class="hljs-property">name</span> === <span clas…
30738 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
30739 } <span class="hljs-keyword">else</span> {
30740-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
30745 <span class="hljs-title function_">foo</span>(ee, <span class="hljs-string">'foo'</span>, ac.<span …
30746 ac.<span class="hljs-title function_">abort</span>(); <span class="hljs-comment">// Abort waiting f…
30747 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); <span cl…
30755-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30757 <span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="h…
30759 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
30760 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(myEE, <span…
30761 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30763 <span class="hljs-comment">// This Promise will never resolve because the 'foo' event will</span>
30764 <span class="hljs-comment">// have already been emitted before the Promise is created.</span>
30765 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(myEE, <span…
30766 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30769 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span>…
30770 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'bar'</span>);
30771 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>);
30774-title function_">foo</span>().<span class="hljs-title function_">then</span>(<span class="hljs-fu…
30778-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30780 <span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="h…
30782 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
30783-keyword">await</span> <span class="hljs-title class_">Promise</span>.<span class="hljs-title func…
30784 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
30787 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span>…
30788 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'bar'</span>);
30789 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>);
30792-title function_">foo</span>().<span class="hljs-title function_">then</span>(<span class="hljs-fu…
30797 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - captureRejections is…
30798 <p>Value: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
30804 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - captureRejections is…
30811 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
30814 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30818-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30819 <span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span cla…
30820 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <s…
30821 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <s…
30822-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
30823 <span class="hljs-comment">// Prints: 2</span></code></pre>
30830 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30831 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
30836 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x…
30838-js"><span class="hljs-keyword">const</span> { on, <span class="hljs-title class_">EventEmitter</s…
30840 (<span class="hljs-keyword">async</span> () => {
30841 …<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hl…
30843 <span class="hljs-comment">// Emit later on</span>
30844 …process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span…
30845 …ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span cl…
30846 …ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span cl…
30849-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</sp…
30850 <span class="hljs-comment">// The execution of this inner block is synchronous and it</span>
30851 <span class="hljs-comment">// processes one event at a time (even with await). Do not use</span>
30852 <span class="hljs-comment">// if concurrent execution is required.</span>
30853 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30855 <span class="hljs-comment">// Unreachable here</span>
30862-js"><span class="hljs-keyword">const</span> { on, <span class="hljs-title class_">EventEmitter</s…
30863 <span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hlj…
30865 (<span class="hljs-keyword">async</span> () => {
30866 …<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hl…
30868 <span class="hljs-comment">// Emit later on</span>
30869 …process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span…
30870 …ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span cl…
30871 …ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span cl…
30874-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</sp…
30875 <span class="hljs-comment">// The execution of this inner block is synchronous and it</span>
30876 <span class="hljs-comment">// processes one event at a time (even with await). Do not use</span>
30877 <span class="hljs-comment">// if concurrent execution is required.</span>
30878 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30880 <span class="hljs-comment">// Unreachable here</span>
30883 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span>…
30889 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&…
30895 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
30897 <span class="hljs-title class_">EventEmitter</span>
30898 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
30900 <span class="hljs-keyword">const</span> target = <span class="hljs-keyword">new</span> <span class=…
30901 <span class="hljs-keyword">const</span> emitter = <span class="hljs-keyword">new</span> <span class…
30903 <span class="hljs-title function_">setMaxListeners</span>(<span class="hljs-number">5</span>, targe…
30904 <p><a id="event-target-and-event-api"></a></p>
30909 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
30910 <p>The <code>EventTarget</code> and <code>Event</code> objects are a Node.js-specific implementation
30914 <pre><code class="language-js"><span class="hljs-keyword">const</span> target = <span class="hljs-t…
30916 … class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, <spa…
30917 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30965 …de class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function…
30966 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span cl…
30967 event.<span class="hljs-property">a</span> = <span class="hljs-number">1</span>;
30970 …ass="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title…
30971 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span cl…
30972 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span c…
30975 <span class="hljs-keyword">const</span> handler3 = {
30976 <span class="hljs-title function_">handleEvent</span>(<span class="hljs-params">event</span>) {
30977 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span cl…
30981 <span class="hljs-keyword">const</span> handler4 = {
30982 …<span class="hljs-keyword">async</span> <span class="hljs-title function_">handleEvent</span>(<spa…
30983 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span cl…
30987 <span class="hljs-keyword">const</span> target = <span class="hljs-title function_">getEventTargetS…
30989 target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</…
30990 target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</…
30991 target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</…
30992 …js-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, handler4, { <s…
31018 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
31032 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
31039 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
31062 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
31071 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_t…
31080 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
31094 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
31127 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_t…
31135 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
31147 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31148 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
31149 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31151 <li><code>once</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31153 <li><code>passive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
31156 <li><code>capture</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
31170 …de class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function…
31172 <span class="hljs-keyword">const</span> target = <span class="hljs-title function_">getEventTargetS…
31173-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, handler, { <span…
31174-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, handler, { <span…
31176 <span class="hljs-comment">// Removes the second instance of handler</span>
31177 target.<span class="hljs-title function_">removeEventListener</span>(<span class="hljs-string">'foo…
31179 <span class="hljs-comment">// Removes the first instance of handler</span>
31180-title function_">removeEventListener</span>(<span class="hljs-string">'foo'</span>, handler, { <s…
31187 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
31198 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31199 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
31200 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31202 <li><code>capture</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
31214 <p>The <code>NodeEventTarget</code> is a Node.js-specific extension to <code>EventTarget</code>
31222 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31225 <p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31228 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
31230 <li><code>once</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31237 <p>Node.js-specific extension to the <code>EventTarget</code> class that emulates the
31246 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
31248 <p>Node.js-specific extension to the <code>EventTarget</code> class that returns an array
31256 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31259 <p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number…
31262 <p>Node.js-specific extension to the <code>EventTarget</code> class that returns the number
31270 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31273 <p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31279 <p>Node.js-specific alias for <code>eventTarget.removeListener()</code>.</p>
31286 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31289 <p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31292 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
31294 <li><code>once</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31301 <p>Node.js-specific alias for <code>eventTarget.addListener()</code>.</p>
31308 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31311 <p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31314 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
31320 <p>Node.js-specific extension to the <code>EventTarget</code> class that adds a <code>once</code>
31329 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31335 <p>Node.js-specific extension to the <code>EventTarget</code> class. If <code>type</code> is specif…
31344 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31347 <p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31353 <p>Node.js-specific extension to the <code>EventTarget</code> class that removes the
31360 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
31365 <p>To use the promise-based APIs:</p>
31367-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
31370-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
31371 <p>All file system operations have synchronous, callback, and promise-based
31374 <p>Promise-based operations return a promise that is fulfilled when the
31377-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
31379 <span class="hljs-keyword">try</span> {
31380 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">unlink</span>(<span cla…
31381 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31382 } <span class="hljs-keyword">catch</span> (error) {
31383-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
31384 …class="language-js cjs"><span class="hljs-keyword">const</span> { unlink } = <span class="hljs-bui…
31386 (<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hl…
31387 <span class="hljs-keyword">try</span> {
31388 <span class="hljs-keyword">await</span> <span class="hljs-title function_">unlink</span>(path);
31389 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
31390 } <span class="hljs-keyword">catch</span> (error) {
31391-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
31393 })(<span class="hljs-string">'/tmp/hello'</span>);</code></pre>
31401-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
31403 …n class="hljs-title function_">unlink</span>(<span class="hljs-string">'/tmp/hello'</span>, <span …
31404 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
31405 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31406 …class="language-js cjs"><span class="hljs-keyword">const</span> { unlink } = <span class="hljs-bui…
31408 …n class="hljs-title function_">unlink</span>(<span class="hljs-string">'/tmp/hello'</span>, <span …
31409 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
31410 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31412 <p>The callback-based versions of the <code>fs</code> module APIs are preferable over
31420-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
31422 <span class="hljs-keyword">try</span> {
31423 …<span class="hljs-title function_">unlinkSync</span>(<span class="hljs-string">'/tmp/hello'</span>…
31424 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31425 } <span class="hljs-keyword">catch</span> (err) {
31426 <span class="hljs-comment">// handle the error</span>
31427 …ass="language-js cjs"><span class="hljs-keyword">const</span> { unlinkSync } = <span class="hljs-b…
31429 <span class="hljs-keyword">try</span> {
31430 …<span class="hljs-title function_">unlinkSync</span>(<span class="hljs-string">'/tmp/hello'</span>…
31431 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31432 } <span class="hljs-keyword">catch</span> (err) {
31433 <span class="hljs-comment">// handle the error</span>
31485-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
31486 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
31488 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
31491 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31502 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31503 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31505 <p>Modifies the permissions on the file. See <a href="http://man7.org/linux/man-pages/man2/chmod.2.…
31511 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31512 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31513 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31515 <p>Changes the ownership of the file. A wrapper for <a href="http://man7.org/linux/man-pages/man2/c…
31521 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31525 …de class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-key…
31527 <span class="hljs-keyword">let</span> filehandle;
31528 <span class="hljs-keyword">try</span> {
31529 …ss="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-st…
31530 } <span class="hljs-keyword">finally</span> {
31531 …<span class="hljs-keyword">await</span> filehandle?.<span class="hljs-title function_">close</span…
31538 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31542 <a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html"><code>fdatasync(2)</code></a> docum…
31549 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
31556 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
31558 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31560 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31562 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31566 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31568 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
31569 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
31575 <p>If the file is not modified concurrently, the end-of-file is reached when the
31582 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31584 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
31586 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31588 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31590 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31596 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31598 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
31599 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
31605 <p>If the file is not modified concurrently, the end-of-file is reached when the
31612 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
31614 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
31615 …class_abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress readFile</li>
31618 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31634 …rg/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]>…
31635 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31638 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31640 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
31641 …rg/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]>…
31646 <p>Read from a file and write to an array of <a href="https://developer.mozilla.org/en-US/docs/Web/…
31660 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31662 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31666 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31673 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31677 Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/fsync.2.html"><code>fsync(2)</code…
31683 <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31684 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31690 …de class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-key…
31692 <span class="hljs-keyword">let</span> filehandle = <span class="hljs-literal">null</span>;
31693 <span class="hljs-keyword">try</span> {
31694 …lass="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-
31695 …<span class="hljs-keyword">await</span> filehandle.<span class="hljs-title function_">truncate</sp…
31696 } <span class="hljs-keyword">finally</span> {
31697 …<span class="hljs-keyword">await</span> filehandle?.<span class="hljs-title function_">close</span…
31707-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
31708-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
31709 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31728 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
31729 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31731 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31732 <code>buffer.byteLength - offset</code></li>
31733 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31735 the data will be written at the current position. See the POSIX <a href="http://man7.org/linux/man-
31737 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31742 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
31743 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
31765 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31766 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31768 data will be written at the current position. See the POSIX <a href="http://man7.org/linux/man-page…
31770 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31771 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31777 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
31778 <li><code>buffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31801-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
31802 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
31804 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
31808 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31811 …github.io/ecma262/#sec-asynciterable-interface" class="type">&#x3C;AsyncIterable></a> or <a href="…
31826 …rg/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]>…
31827 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31830 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31832 <p>Write an array of <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" cla…
31835 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
31836 …rg/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]>…
31849 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31850 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31851 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31860 with an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/E…
31862 …e class="language-js mjs"><span class="hljs-keyword">import</span> { access } <span class="hljs-ke…
31863 <span class="hljs-keyword">import</span> { constants } <span class="hljs-keyword">from</span> <span…
31865 <span class="hljs-keyword">try</span> {
31866-keyword">await</span> <span class="hljs-title function_">access</span>(<span class="hljs-string">…
31867 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31868 } <span class="hljs-keyword">catch</span> {
31869 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
31881 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31882 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31883 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
31885 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
31886 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31887 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31890 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31904 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31905 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
31906 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31914 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31915 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31916 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31917 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31933 <li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31934 <li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31935 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31944 a copy-on-write reflink. If the platform does not support copy-on-write,
31947 create a copy-on-write reflink. If the platform does not support
31948 copy-on-write, then the operation will fail.</li>
31951 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31958 … class="language-js mjs"><span class="hljs-keyword">import</span> { constants } <span class="hljs-
31959 <span class="hljs-keyword">import</span> { copyFile } <span class="hljs-keyword">from</span> <span …
31961 <span class="hljs-keyword">try</span> {
31962 …s="hljs-keyword">await</span> <span class="hljs-title function_">copyFile</span>(<span class="hljs
31963 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31964 } <span class="hljs-keyword">catch</span> {
31965 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31968 <span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt ex…
31969 <span class="hljs-keyword">try</span> {
31970-keyword">await</span> <span class="hljs-title function_">copyFile</span>(<span class="hljs-string…
31971 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31972 } <span class="hljs-keyword">catch</span> {
31973 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31980 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31981 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31982 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31999 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32000 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
32001 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
32002 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32010 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32011-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
32012-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
32013 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32024 <li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
32025 <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
32026 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32029 <a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a> documentation f…
32043 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32044 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
32046 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
32050 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32054 in which case the link itself is stat-ed, not the file that it refers to.
32055 Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/lstat.2.html"><code>lstat(2)</code…
32061 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32062 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
32064 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
32065 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32068 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32090 <li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
32091 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32093 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32096 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32106 …e class="language-js mjs"><span class="hljs-keyword">import</span> { mkdtemp } <span class="hljs-k…
32108 <span class="hljs-keyword">try</span> {
32109-keyword">await</span> <span class="hljs-title function_">mkdtemp</span>(path.<span class="hljs-ti…
32110 } <span class="hljs-keyword">catch</span> (err) {
32111 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32116 <code>prefix</code> must end with a trailing platform-specific path separator
32131 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32132 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32134 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32136 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32139 <p>Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/open.2.html"><code>open(2)</cod…
32141 by <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file">Naming Files, Pa…
32143 <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams">this MSDN page</a>.…
32157 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32158 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
32160 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32161 <li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
32166 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32169 <a href="http://man7.org/linux/man-pages/man3/opendir.3.html"><code>opendir(3)</code></a> documenta…
32175 …e class="language-js mjs"><span class="hljs-keyword">import</span> { opendir } <span class="hljs-k…
32177 <span class="hljs-keyword">try</span> {
32178 …ss="hljs-keyword">const</span> dir = <span class="hljs-keyword">await</span> <span class="hljs-tit…
32179 …n class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keywo…
32180 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
32181 } <span class="hljs-keyword">catch</span> (err) {
32182 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32199 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32200 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32202 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32203 <li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
32206 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32216 …e class="language-js mjs"><span class="hljs-keyword">import</span> { readdir } <span class="hljs-k…
32218 <span class="hljs-keyword">try</span> {
32219 …<span class="hljs-keyword">const</span> files = <span class="hljs-keyword">await</span> <span clas…
32220 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> file <span class="h…
32221 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
32222 } <span class="hljs-keyword">catch</span> (err) {
32223 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32238 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32239 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
32241 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32242 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32243 …class_abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress readFile</li>
32246 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32253 platform-specific. On macOS, Linux, and Windows, the promise will be rejected
32258 … class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-k…
32260 <span class="hljs-keyword">try</span> {
32261 …<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span c…
32262 <span class="hljs-keyword">const</span> { signal } = controller;
32263 …<span class="hljs-keyword">const</span> promise = <span class="hljs-title function_">readFile</spa…
32265 <span class="hljs-comment">// Abort the request before the promise settles.</span>
32266 controller.<span class="hljs-title function_">abort</span>();
32268 <span class="hljs-keyword">await</span> promise;
32269 } <span class="hljs-keyword">catch</span> (err) {
32270 <span class="hljs-comment">// When a request is aborted - err is an AbortError</span>
32271 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32281 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32282 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32284 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32287 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32290 <a href="http://man7.org/linux/man-pages/man2/readlink.2.html"><code>readlink(2)</code></a> documen…
32301 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32302 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32304 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32307 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32324 <li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
32325 <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
32326 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32346 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32347 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
32349 <li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
32354 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
32357 <li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
32362 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32369 <code>rm -rf</code>: an error will not be raised for paths that do not exist, and paths
32378 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32379 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
32381 <li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
32383 <li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
32388 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
32390 <li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
32395 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32411 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32412 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
32414 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
32418 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32426 <li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
32427 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32428 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32429 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32441 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32442 <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
32443 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32452 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32453 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32457 … link, the file is deleted. See the POSIX <a href="http://man7.org/linux/man-pages/man2/unlink.2.h…
32464 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32465-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
32466-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
32467 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32475 <code>-Infinity</code>, an <code>Error</code> will be thrown.</li>
32482 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32483 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32485 <li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
32487 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
32491 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32497 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x…
32499 <li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
32500 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32506 …e class="language-js"><span class="hljs-keyword">const</span> { watch } = <span class="hljs-built_…
32508 <span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hlj…
32509 <span class="hljs-keyword">const</span> { signal } = ac;
32510 …"hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> ac.<span class="hljs-ti…
32512 (<span class="hljs-keyword">async</span> () => {
32513 <span class="hljs-keyword">try</span> {
32514 …<span class="hljs-keyword">const</span> watcher = <span class="hljs-title function_">watch</span>(…
32515 …n class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keywo…
32516 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
32517 } <span class="hljs-keyword">catch</span> (err) {
32518 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">name</span> === <span class=…
32519 <span class="hljs-keyword">return</span>;
32520 <span class="hljs-keyword">throw</span> err;
32543 <li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32544-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
32545 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
32547 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32548 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32549 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32550 …class_abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress writeFile</l…
32553 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32556 …github.io/ecma262/#sec-asynciterable-interface" class="type">&#x3C;AsyncIterable></a> or <a href="…
32564 <p>Similarly to <code>fsPromises.readFile</code> - <code>fsPromises.writeFile</code> is a convenien…
32571 … class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-
32573 <span class="hljs-keyword">try</span> {
32574 …<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span c…
32575 <span class="hljs-keyword">const</span> { signal } = controller;
32576-keyword">const</span> data = <span class="hljs-keyword">new</span> <span class="hljs-title class_…
32577 …<span class="hljs-keyword">const</span> promise = <span class="hljs-title function_">writeFile</sp…
32579 <span class="hljs-comment">// Abort the request before the promise settles.</span>
32580 controller.<span class="hljs-title function_">abort</span>();
32582 <span class="hljs-keyword">await</span> promise;
32583 } <span class="hljs-keyword">catch</span> (err) {
32584 <span class="hljs-comment">// When a request is aborted - err is an AbortError</span>
32585 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32611 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32612 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32613 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
32615 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
32628 …ss="language-js mjs"><span class="hljs-keyword">import</span> { access, constants } <span class="h…
32630 <span class="hljs-keyword">const</span> file = <span class="hljs-string">'package.json'</span>;
32632 <span class="hljs-comment">// Check if the file exists in the current directory.</span>
32633 …ss="hljs-title function_">access</span>(file, constants.<span class="hljs-property">F_OK</span>, <…
32634-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
32637 <span class="hljs-comment">// Check if the file is readable.</span>
32638 …ss="hljs-title function_">access</span>(file, constants.<span class="hljs-property">R_OK</span>, <…
32639-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
32642 <span class="hljs-comment">// Check if the file is writable.</span>
32643 …ss="hljs-title function_">access</span>(file, constants.<span class="hljs-property">W_OK</span>, <…
32644-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
32647 <span class="hljs-comment">// Check if the file exists in the current directory, and if it is writa…
32648-title function_">access</span>(file, constants.<span class="hljs-property">F_OK</span> | constant…
32649 <span class="hljs-keyword">if</span> (err) {
32650 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32651-string">`<span class="hljs-subst">${file}</span> <span class="hljs-subst">${err.code === <span cl…
32652 } <span class="hljs-keyword">else</span> {
32653 …n class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<sp…
32662 …s="language-js mjs"><span class="hljs-keyword">import</span> { access, open, close } <span class="…
32664 …pan class="hljs-title function_">access</span>(<span class="hljs-string">'myfile'</span>, <span cl…
32665 <span class="hljs-keyword">if</span> (!err) {
32666 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32667 <span class="hljs-keyword">return</span>;
32670-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
32671 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32673 <span class="hljs-keyword">try</span> {
32674 <span class="hljs-title function_">writeMyData</span>(fd);
32675 } <span class="hljs-keyword">finally</span> {
32676 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
32677 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32683 …class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs
32685-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
32686 <span class="hljs-keyword">if</span> (err) {
32687 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class=…
32688 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32689 <span class="hljs-keyword">return</span>;
32692 <span class="hljs-keyword">throw</span> err;
32695 <span class="hljs-keyword">try</span> {
32696 <span class="hljs-title function_">writeMyData</span>(fd);
32697 } <span class="hljs-keyword">finally</span> {
32698 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
32699 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32704 …s="language-js mjs"><span class="hljs-keyword">import</span> { access, open, close } <span class="…
32705 …pan class="hljs-title function_">access</span>(<span class="hljs-string">'myfile'</span>, <span cl…
32706 <span class="hljs-keyword">if</span> (err) {
32707 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class=…
32708 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32709 <span class="hljs-keyword">return</span>;
32712 <span class="hljs-keyword">throw</span> err;
32715-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
32716 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32718 <span class="hljs-keyword">try</span> {
32719 <span class="hljs-title function_">readMyData</span>(fd);
32720 } <span class="hljs-keyword">finally</span> {
32721 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
32722 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32728 …class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs
32730-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
32731 <span class="hljs-keyword">if</span> (err) {
32732 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class=…
32733 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32734 <span class="hljs-keyword">return</span>;
32737 <span class="hljs-keyword">throw</span> err;
32740 <span class="hljs-keyword">try</span> {
32741 <span class="hljs-title function_">readMyData</span>(fd);
32742 } <span class="hljs-keyword">finally</span> {
32743 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
32744 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32754 <p>On Windows, access-control policies (ACLs) on a directory may limit access to
32777-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
32778 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32779 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
32781 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32782 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32783 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32786 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
32788 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
32796 …class="language-js mjs"><span class="hljs-keyword">import</span> { appendFile } <span class="hljs-
32798-title function_">appendFile</span>(<span class="hljs-string">'message.txt'</span>, <span class="h…
32799 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32800 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
32803 …class="language-js mjs"><span class="hljs-keyword">import</span> { appendFile } <span class="hljs-
32805-title function_">appendFile</span>(<span class="hljs-string">'message.txt'</span>, <span class="h…
32809 …"language-js mjs"><span class="hljs-keyword">import</span> { open, close, appendFile } <span class…
32811 <span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span …
32812 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
32813 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32817-title function_">open</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-st…
32818 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32820 <span class="hljs-keyword">try</span> {
32821-title function_">appendFile</span>(fd, <span class="hljs-string">'data to append'</span>, <span c…
32822 <span class="hljs-title function_">closeFd</span>(fd);
32823 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32825 } <span class="hljs-keyword">catch</span> (err) {
32826 <span class="hljs-title function_">closeFd</span>(fd);
32827 <span class="hljs-keyword">throw</span> err;
32847 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32848 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32849 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
32851 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
32857 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/chmod.2.html"><code>chmod(2)</code><…
32858 …de class="language-js mjs"><span class="hljs-keyword">import</span> { chmod } <span class="hljs-ke…
32860-title function_">chmod</span>(<span class="hljs-string">'my_file.txt'</span>, <span class="hljs-n…
32861 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32862 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
32925 octal digits (e.g. <code>765</code>). The left-most digit (<code>7</code> in the example), specifies
32927 specifies permissions for the group. The right-most digit (<code>5</code> in the example),
32978 <code>0o777</code> may result in platform-specific behaviors that are not supported to work
33001 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33002 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33003 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33004 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33006 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33012 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/chown.2.html"><code>chown(2)</code><…
33030 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33031 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33033 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33041 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/close.2.html"><code>close(2)</code><…
33055 <li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33056 <li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33057 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33058 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33073 copy-on-write reflink. If the platform does not support copy-on-write, then a
33076 create a copy-on-write reflink. If the platform does not support
33077 copy-on-write, then the operation will fail.</li>
33079 …s="language-js mjs"><span class="hljs-keyword">import</span> { copyFile, constants } <span class="…
33081 <span class="hljs-keyword">function</span> <span class="hljs-title function_">callback</span>(<span…
33082 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33083 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
33086 <span class="hljs-comment">// destination.txt will be created or overwritten by default.</span>
33087 <span class="hljs-title function_">copyFile</span>(<span class="hljs-string">'source.txt'</span>, <…
33089 <span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt ex…
33090-title function_">copyFile</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs
33116 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33117 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33119 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
33121 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
33122 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33123 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33124 <li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33125 <li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33126 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
33127 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33128 <li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
33129 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
33139 [0, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Numbe…
33145 emitted. <code>fd</code> should be blocking; non-blocking <code>fd</code>s should be passed to
33157 …ss="language-js mjs"><span class="hljs-keyword">import</span> { createReadStream } <span class="hl…
33159 <span class="hljs-comment">// Create a stream from some character device.</span>
33160 <span class="hljs-keyword">const</span> stream = <span class="hljs-title function_">createReadStrea…
33161 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
33162 …stream.<span class="hljs-title function_">close</span>(); <span class="hljs-comment">// This may n…
33163 …<span class="hljs-comment">// Artificially marking end-of-stream, as if the underlying resource ha…
33164 <span class="hljs-comment">// indicated end-of-file by itself, allows the stream to close.</span>
33165 …<span class="hljs-comment">// This does not cancel pending read operations, and if there is such a…
33166 …<span class="hljs-comment">// operation, the process may still not be able to exit successfully</s…
33167 <span class="hljs-comment">// until it finishes.</span>
33168 stream.<span class="hljs-title function_">push</span>(<span class="hljs-literal">null</span>);
33169 stream.<span class="hljs-title function_">read</span>(<span class="hljs-number">0</span>);
33170 }, <span class="hljs-number">100</span>);</code></pre>
33179 …ss="language-js mjs"><span class="hljs-keyword">import</span> { createReadStream } <span class="hl…
33181-title function_">createReadStream</span>(<span class="hljs-string">'sample.txt'</span>, { <span c…
33208 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33209 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33211 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
33213 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
33214 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33215 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33216 <li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33217 <li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33218 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
33219 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
33226 [0, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Numbe…
33244 <code>'open'</code> event will be emitted. <code>fd</code> should be blocking; non-blocking <code>f…
33261 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
33263 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33264 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33266 <li><code>exists</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33272 …e class="language-js mjs"><span class="hljs-keyword">import</span> { exists } <span class="hljs-ke…
33274 …n class="hljs-title function_">exists</span>(<span class="hljs-string">'/etc/passwd'</span>, <span…
33275 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(e ? <span class…
33288 …s="language-js mjs"><span class="hljs-keyword">import</span> { exists, open, close } <span class="…
33290 …pan class="hljs-title function_">exists</span>(<span class="hljs-string">'myfile'</span>, <span cl…
33291 <span class="hljs-keyword">if</span> (e) {
33292 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
33293 } <span class="hljs-keyword">else</span> {
33294-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
33295 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33297 <span class="hljs-keyword">try</span> {
33298 <span class="hljs-title function_">writeMyData</span>(fd);
33299 } <span class="hljs-keyword">finally</span> {
33300 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
33301 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33308 …class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs
33309-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
33310 <span class="hljs-keyword">if</span> (err) {
33311 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class=…
33312 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
33313 <span class="hljs-keyword">return</span>;
33316 <span class="hljs-keyword">throw</span> err;
33319 <span class="hljs-keyword">try</span> {
33320 <span class="hljs-title function_">writeMyData</span>(fd);
33321 } <span class="hljs-keyword">finally</span> {
33322 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
33323 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33328 …s="language-js mjs"><span class="hljs-keyword">import</span> { open, close, exists } <span class="…
33330 …pan class="hljs-title function_">exists</span>(<span class="hljs-string">'myfile'</span>, <span cl…
33331 <span class="hljs-keyword">if</span> (e) {
33332-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
33333 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33335 <span class="hljs-keyword">try</span> {
33336 <span class="hljs-title function_">readMyData</span>(fd);
33337 } <span class="hljs-keyword">finally</span> {
33338 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
33339 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33343 } <span class="hljs-keyword">else</span> {
33344 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
33348 …class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs
33350-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
33351 <span class="hljs-keyword">if</span> (err) {
33352 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class=…
33353 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
33354 <span class="hljs-keyword">return</span>;
33357 <span class="hljs-keyword">throw</span> err;
33360 <span class="hljs-keyword">try</span> {
33361 <span class="hljs-title function_">readMyData</span>(fd);
33362 } <span class="hljs-keyword">finally</span> {
33363 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
33364 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33389 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33390 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33391 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33393 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33399 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fchmod.2.html"><code>fchmod(2)</code…
33415 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33416 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33417 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33418 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33420 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33426 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fchown.2.html"><code>fchown(2)</code…
33442 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33443 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33445 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33451 <a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html"><code>fdatasync(2)</code></a> docum…
33470 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33471 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
33473 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33477 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33479 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33485 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fstat.2.html"><code>fstat(2)</code><…
33501 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33502 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33504 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33510 Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/fsync.2.html"><code>fsync(2)</code…
33527 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33528 <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33529 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33531 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33537 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/ftruncate.2.html"><code>ftruncate(2)…
33542 …"language-js mjs"><span class="hljs-keyword">import</span> { open, close, ftruncate } <span class=…
33544 <span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span …
33545 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
33546 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33550-title function_">open</span>(<span class="hljs-string">'temp.txt'</span>, <span class="hljs-strin…
33551 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33553 <span class="hljs-keyword">try</span> {
33554 …pan class="hljs-title function_">ftruncate</span>(fd, <span class="hljs-number">4</span>, <span cl…
33555 <span class="hljs-title function_">closeFd</span>(fd);
33556 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33558 } <span class="hljs-keyword">catch</span> (err) {
33559 <span class="hljs-title function_">closeFd</span>(fd);
33560 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33583 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33584-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
33585-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
33586 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33588 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33611 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33612 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33613 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33615 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33635 <td><p>Documentation-only deprecation.</p></td></tr>
33640 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33641 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33642 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33643 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33645 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33651 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/lchown.2.html"><code>lchown(2)</code…
33657 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33658-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
33659-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
33660 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33662 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33689 <li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
33690 <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
33691 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33693 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33698 <a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a> documentation f…
33719 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33720 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
33722 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33726 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33728 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33736 link, then the link itself is stat-ed, not the file that it refers to.</p>
33737 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/lstat.2.html"><code>lstat(2)</code><…
33759 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33760 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
33762 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33763 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33766 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33768 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33782 …de class="language-js mjs"><span class="hljs-keyword">import</span> { mkdir } <span class="hljs-ke…
33784 <span class="hljs-comment">// Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist.<…
33785-title function_">mkdir</span>(<span class="hljs-string">'/tmp/a/apple'</span>, { <span class="hlj…
33786 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33790 …de class="language-js mjs"><span class="hljs-keyword">import</span> { mkdir } <span class="hljs-ke…
33792-title function_">mkdir</span>(<span class="hljs-string">'/'</span>, { <span class="hljs-attr">rec…
33793 <span class="hljs-comment">// => [Error: EPERM: operation not permitted, mkdir 'C:\']</span>
33795 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/mkdir.2.html"><code>mkdir(2)</code><…
33815 <li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33816 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33818 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
33821 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33823 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33824 <li><code>directory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33838 …e class="language-js mjs"><span class="hljs-keyword">import</span> { mkdtemp } <span class="hljs-k…
33840-title function_">mkdtemp</span>(path.<span class="hljs-title function_">join</span>(os.<span clas…
33841 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33842 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
33843 …<span class="hljs-comment">// Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde…
33848 must end with a trailing platform-specific path separator
33850 …e class="language-js mjs"><span class="hljs-keyword">import</span> { tmpdir } <span class="hljs-ke…
33851 <span class="hljs-keyword">import</span> { mkdtemp } <span class="hljs-keyword">from</span> <span c…
33853 <span class="hljs-comment">// The parent directory for the new temporary directory</span>
33854 <span class="hljs-keyword">const</span> tmpDir = <span class="hljs-title function_">tmpdir</span>();
33856 <span class="hljs-comment">// This method is *INCORRECT*:</span>
33857 <span class="hljs-title function_">mkdtemp</span>(tmpDir, <span class="hljs-function">(<span class=…
33858 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33859 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
33860 <span class="hljs-comment">// Will print something similar to `/tmpabc123`.</span>
33861 <span class="hljs-comment">// A new temporary directory is created at the file system root</span>
33862 <span class="hljs-comment">// rather than *within* the /tmp directory.</span>
33865 <span class="hljs-comment">// This method is *CORRECT*:</span>
33866 <span class="hljs-keyword">import</span> { sep } <span class="hljs-keyword">from</span> <span class…
33867-title function_">mkdtemp</span>(<span class="hljs-string">`<span class="hljs-subst">${tmpDir}</sp…
33868 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33869 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
33870 <span class="hljs-comment">// Will print something similar to `/tmp/abc123`.</span>
33871 <span class="hljs-comment">// A new temporary directory is created within</span>
33872 <span class="hljs-comment">// the /tmp directory.</span>
33891 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33892 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33894 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33895 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33897 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33898 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33902 <p>Asynchronous file open. See the POSIX <a href="http://man7.org/linux/man-pages/man2/open.2.html"…
33908 by <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file">Naming Files, Pa…
33910 <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams">this MSDN page</a>.…
33926 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33927 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
33929 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33930 <li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
33935 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33937 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33942 <p>Asynchronously open a directory. See the POSIX <a href="http://man7.org/linux/man-pages/man3/ope…
33965 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33966 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
33968 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33970 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33972 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
33973 file. If <code>position</code> is <code>null</code> or <code>-1 </code>, data will be read from the…
33976 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33978 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33979 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33986 <p>If the file is not modified concurrently, the end-of-file is reached when the
34003 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
34004 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
34006 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
34007 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34008 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34009 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
34012 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34014 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34015 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
34044 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34045 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34047 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34048 <li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
34051 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34053 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34054 <li><code>files</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
34061 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</co…
34091-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
34092 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
34094 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34095 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34096 …class_abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress readFile</li>
34099 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34101 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34102 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34107 … class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-k…
34109 … class="hljs-title function_">readFile</span>(<span class="hljs-string">'/etc/passwd'</span>, <spa…
34110 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
34111 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34117 … class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-k…
34119 <span class="hljs-title function_">readFile</span>(<span class="hljs-string">'/etc/passwd'</span>, …
34121 <a href="#fs_fs_readfilesync_path_options"><code>fs.readFileSync()</code></a> is platform-specific.…
34124 … class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-k…
34126 <span class="hljs-comment">// macOS, Linux, and Windows</span>
34127 …ass="hljs-title function_">readFile</span>(<span class="hljs-string">'&#x3C;directory>'</span>, <s…
34128 …<span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, read &#x3C;dire…
34131 <span class="hljs-comment">// FreeBSD</span>
34132 …ass="hljs-title function_">readFile</span>(<span class="hljs-string">'&#x3C;directory>'</span>, <s…
34133 <span class="hljs-comment">// => null, &#x3C;data></span>
34137 … class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-k…
34139 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span cl…
34140 <span class="hljs-keyword">const</span> signal = controller.<span class="hljs-property">signal</spa…
34141-title function_">readFile</span>(fileInfo[<span class="hljs-number">0</span>].<span class="hljs-p…
34142 <span class="hljs-comment">// ...</span>
34144 <span class="hljs-comment">// When you want to abort the request</span>
34145 controller.<span class="hljs-title function_">abort</span>();</code></pre>
34171 <p>For applications that require as-fast-as-possible reading of file contents, it
34194 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34195 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34197 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34200 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34202 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34203 <li><code>linkString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34209 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/readlink.2.html"><code>readlink(2)</…
34219 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
34220 <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"…
34221 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34222 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34224 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34225 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
34226 <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"…
34262 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34263 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34265 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34268 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34270 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34271 <li><code>resolvedPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
34279 <p>This function behaves like <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>…
34282 <p>No case conversion is performed on case-insensitive file systems.</p>
34285 <p>The maximum number of symbolic links is platform-independent and generally
34286 (much) higher than what the native <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><…
34303 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34304 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34306 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34309 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34311 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34312 <li><code>resolvedPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
34316 <p>Asynchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</c…
34343 <li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
34344 <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
34345 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34347 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34356 <p>See also: <a href="http://man7.org/linux/man-pages/man2/rename.2.html"><code>rename(2)</code></a…
34357 …e class="language-js mjs"><span class="hljs-keyword">import</span> { rename } <span class="hljs-ke…
34359-title function_">rename</span>(<span class="hljs-string">'oldFile.txt'</span>, <span class="hljs-
34360 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
34361 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34386 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34387 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
34389 <li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34394 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
34397 <li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34402 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34404 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34408 <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/rmdir.2.html"><code>rmdir(2)</code></…
34413 <code>rm -rf</code>: an error will not be raised for paths that do not exist, and paths
34422 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34423 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
34425 <li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
34427 <li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34432 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
34434 <li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34439 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34441 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34467 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34468 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
34470 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34474 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34476 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34481 <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/stat.2.html"><code>stat(2)</code></a>…
34491 <pre><code class="language-text">- txtDir
34492 -- file.txt
34493 - app.js</code></pre>
34495 …de class="language-js mjs"><span class="hljs-keyword">import</span> { stat } <span class="hljs-key…
34497 <span class="hljs-keyword">const</span> pathsToCheck = [<span class="hljs-string">'./txtDir'</span>…
34499 …="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0<…
34500 …<span class="hljs-title function_">stat</span>(pathsToCheck[i], <span class="hljs-function">(<span…
34501 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34502 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34506 <pre><code class="language-console">true
34522 atime: 2019-06-22T03:37:33.072Z,
34523 mtime: 2019-06-22T03:36:54.583Z,
34524 ctime: 2019-06-22T03:37:06.624Z,
34525 birthtime: 2019-06-22T03:28:46.937Z
34543 atime: 2019-06-22T03:36:56.619Z,
34544 mtime: 2019-06-22T03:36:54.584Z,
34545 ctime: 2019-06-22T03:36:54.584Z,
34546 birthtime: 2019-06-22T03:26:47.711Z
34563 <li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34564 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34565 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34566 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34568 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34574 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/symlink.2.html"><code>symlink(2)</co…
34582 …e class="language-js mjs"><span class="hljs-keyword">import</span> { symlink } <span class="hljs-k…
34584 <span class="hljs-title function_">symlink</span>(<span class="hljs-string">'./mew'</span>, <span c…
34587 <pre><code class="language-bash">$ tree example/
34590 └── mewtwo -> ./mew</code></pre>
34606 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34607 <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
34608 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34610 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34618-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
34619 <span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span>
34620 …class="hljs-title function_">truncate</span>(<span class="hljs-string">'path/file.txt'</span>, <sp…
34621 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
34622 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34623 …lass="language-js cjs"><span class="hljs-keyword">const</span> { truncate } = <span class="hljs-bu…
34624 <span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span>
34625 …class="hljs-title function_">truncate</span>(<span class="hljs-string">'path/file.txt'</span>, <sp…
34626 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
34627 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34631 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/truncate.2.html"><code>truncate(2)</…
34649 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34650 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34652 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34658 …e class="language-js mjs"><span class="hljs-keyword">import</span> { unlink } <span class="hljs-ke…
34659 <span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span>
34660 … class="hljs-title function_">unlink</span>(<span class="hljs-string">'path/file.txt'</span>, <spa…
34661 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
34662 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34666 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/unlink.2.html"><code>unlink(2)</code…
34672 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34673 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34680 no-op, not an error.</p>
34692 <td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time…
34705 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34706-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
34707-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
34708 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34710 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34720 <code>-Infinity</code>, an <code>Error</code> will be thrown.</li>
34739 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34740 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34742 <li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34744 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
34748 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34753 …a.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a…
34755 <li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
34756 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34789 <li>On Linux systems, this uses <a href="https://man7.org/linux/man-pages/man7/inotify.7.html"><cod…
34794 …ature depends on <a href="https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-
34795 <li>On AIX systems, this feature depends on <a href="https://developer.ibm.com/articles/au-aix_even…
34821 …de class="language-js mjs"><span class="hljs-keyword">import</span> { watch } <span class="hljs-ke…
34822 …pan class="hljs-title function_">watch</span>(<span class="hljs-string">'somedir'</span>, <span cl…
34823 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
34824 <span class="hljs-keyword">if</span> (filename) {
34825 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
34826 } <span class="hljs-keyword">else</span> {
34827 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34845 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34846 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
34848 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34849 <li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34850 <li><code>interval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34853 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34870 … class="language-js mjs"><span class="hljs-keyword">import</span> { watchFile } <span class="hljs-
34872 …class="hljs-title function_">watchFile</span>(<span class="hljs-string">'message.text'</span>, <sp…
34873 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
34874 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hl…
34920 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
34921 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
34922 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34923 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34924 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34925 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34927 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34928 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
34929 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
34938 at the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwr…
34970 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
34971 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34972 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34973 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34974 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34976 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34977 <li><code>written</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
34978 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34986 the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite…
34999 or <code>stdout</code>) a string containing non-ASCII characters will not be rendered
35001 It is possible to configure the console to render UTF-8 properly by changing the
35031-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
35032-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
35033 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
35035 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35036 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35037 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35038 …class_abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress writeFile</l…
35041 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
35043 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
35057 … class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-
35059-keyword">const</span> data = <span class="hljs-keyword">new</span> <span class="hljs-title class_…
35060 …ss="hljs-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, data, <…
35061 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
35062 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
35065 … class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-
35067 …s-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, <span class="h…
35071 <p>Similarly to <code>fs.readFile</code> - <code>fs.writeFile</code> is a convenience method that
35077 … class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-
35079 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span cl…
35080 <span class="hljs-keyword">const</span> { signal } = controller;
35081-keyword">const</span> data = <span class="hljs-keyword">new</span> <span class="hljs-title class_…
35082 …js-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, data, { signa…
35083 …<span class="hljs-comment">// When a request is aborted - the callback is called with an AbortErro…
35085 <span class="hljs-comment">// When the request should be aborted</span>
35086 controller.<span class="hljs-title function_">abort</span>();</code></pre>
35092 …de class="language-js mjs"><span class="hljs-keyword">import</span> { write } <span class="hljs-ke…
35094-title function_">write</span>(fd, <span class="hljs-title class_">Buffer</span>.<span class="hljs
35114 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35115 <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"…
35116 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35117 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
35119 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
35120 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
35121 <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"…
35155 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35156 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35166 …="language-js mjs"><span class="hljs-keyword">import</span> { accessSync, constants } <span class=…
35168 <span class="hljs-keyword">try</span> {
35169-title function_">accessSync</span>(<span class="hljs-string">'etc/passwd'</span>, constants.<span…
35170 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
35171 } <span class="hljs-keyword">catch</span> (err) {
35172 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
35189-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
35190 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35191 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
35193 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35194 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35195 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35203 …ass="language-js mjs"><span class="hljs-keyword">import</span> { appendFileSync } <span class="hlj…
35205 <span class="hljs-keyword">try</span> {
35206 …<span class="hljs-title function_">appendFileSync</span>(<span class="hljs-string">'message.txt'</…
35207 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
35208 } <span class="hljs-keyword">catch</span> (err) {
35209 <span class="hljs-comment">/* Handle the error */</span>
35212 …ass="language-js mjs"><span class="hljs-keyword">import</span> { appendFileSync } <span class="hlj…
35214-title function_">appendFileSync</span>(<span class="hljs-string">'message.txt'</span>, <span clas…
35218 …age-js mjs"><span class="hljs-keyword">import</span> { openSync, closeSync, appendFileSync } <span…
35220 <span class="hljs-keyword">let</span> fd;
35222 <span class="hljs-keyword">try</span> {
35223 …fd = <span class="hljs-title function_">openSync</span>(<span class="hljs-string">'message.txt'</s…
35224 …<span class="hljs-title function_">appendFileSync</span>(fd, <span class="hljs-string">'data to ap…
35225 } <span class="hljs-keyword">catch</span> (err) {
35226 <span class="hljs-comment">/* Handle the error */</span>
35227 } <span class="hljs-keyword">finally</span> {
35228 <span class="hljs-keyword">if</span> (fd !== <span class="hljs-literal">undefined</span>)
35229 <span class="hljs-title function_">closeSync</span>(fd);
35244 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35245 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35249 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/chmod.2.html"><code>chmod(2)</code><…
35263 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35264 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35265 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35269 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/chown.2.html"><code>chown(2)</code><…
35275 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35280 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/close.2.html"><code>close(2)</code><…
35294 <li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35295 <li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35296 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35310 copy-on-write reflink. If the platform does not support copy-on-write, then a
35313 create a copy-on-write reflink. If the platform does not support
35314 copy-on-write, then the operation will fail.</li>
35316 …"language-js mjs"><span class="hljs-keyword">import</span> { copyFileSync, constants } <span class…
35318 <span class="hljs-comment">// destination.txt will be created or overwritten by default.</span>
35319 <span class="hljs-title function_">copyFileSync</span>(<span class="hljs-string">'source.txt'</span…
35320 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
35322 <span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt ex…
35323-title function_">copyFileSync</span>(<span class="hljs-string">'source.txt'</span>, <span class="…
35337 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35338 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
35346 …class="language-js mjs"><span class="hljs-keyword">import</span> { existsSync } <span class="hljs-
35348 <span class="hljs-keyword">if</span> (<span class="hljs-title function_">existsSync</span>(<span cl…
35349 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
35355 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35356 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35359 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fchmod.2.html"><code>fchmod(2)</code…
35365 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35366 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35367 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35370 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fchown.2.html"><code>fchown(2)</code…
35376 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35380 <a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html"><code>fdatasync(2)</code></a> docum…
35394 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35395 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35397 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35404 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fstat.2.html"><code>fstat(2)</code><…
35410 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35414 Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/fsync.2.html"><code>fsync(2)</code…
35420 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35421 <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35439 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35440-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
35441-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
35449 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35450 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35463 <td><p>Documentation-only deprecation.</p></td></tr>
35468 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35469 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35470 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35473 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/lchown.2.html"><code>lchown(2)</code…
35479 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35480-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
35481-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
35499 <li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
35500 <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
35503 <a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a> documentation f…
35521 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35522 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35524 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35526 <li><code>throwIfNoEntry</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
35534 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/lstat.2.html"><code>lstat(2)</code><…
35552 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35553 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
35555 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
35556 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35559 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35564 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/mkdir.2.html"><code>mkdir(2)</code><…
35578 <li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35579 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35581 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35584 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
35604 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35605 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35607 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35608 <li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
35615 <p>Synchronously open a directory. See <a href="http://man7.org/linux/man-pages/man3/opendir.3.html…
35637 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35638 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35640 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35641 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
35661 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35662 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35664 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35665 <li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
35668 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
35671 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</co…
35693-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
35694 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
35696 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35697 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35700 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
35708 <code>fs.readFileSync()</code> is platform-specific.</p>
35709 …lass="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs
35711 <span class="hljs-comment">// macOS, Linux, and Windows</span>
35712 <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'&#x3C;directory>'…
35713 <span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, read &#x3C;direc…
35715 <span class="hljs-comment">// FreeBSD</span>
35716 <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'&#x3C;directory>'…
35730 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35731 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35733 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35736 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
35739 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/readlink.2.html"><code>readlink(2)</…
35759 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35760 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
35761 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35762 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35763 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
35764 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
35782 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35783 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
35784 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35786 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35787 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35788 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
35791 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
35803 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35804 <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"…
35805 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35806 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
35829 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35830 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35832 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35835 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
35845 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35846 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35848 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35851 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
35853 <p>Synchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</co…
35875 <li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
35876 <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
35879 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/rename.2.html"><code>rename(2)</code…
35899 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35900 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35902 <li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
35907 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
35910 <li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
35916 <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/rmdir.2.html"><code>rmdir(2)</code></a…
35920 <code>rm -rf</code>: an error will not be raised for paths that do not exist, and paths
35929 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35930 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35932 <li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
35934 <li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
35939 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
35941 <li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
35966 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35967 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35969 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35971 <li><code>throwIfNoEntry</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
35994 <li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35995 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35996 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
36006 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
36007 <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
36026 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
36028 <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/unlink.2.html"><code>unlink(2)</code><…
36035 <td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time…
36046 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
36047-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
36048-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
36074-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
36075-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
36076 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
36078 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
36079 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
36080 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
36110 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
36111 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
36112 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
36113 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
36114 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
36115 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
36134 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
36135 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
36136 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
36137 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
36138 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
36147 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
36148 <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"…
36149 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
36150 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
36164 …e class="language-js mjs"><span class="hljs-keyword">import</span> { opendir } <span class="hljs-k…
36166 <span class="hljs-keyword">try</span> {
36167 …ss="hljs-keyword">const</span> dir = <span class="hljs-keyword">await</span> <span class="hljs-tit…
36168 …n class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keywo…
36169 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
36170 } <span class="hljs-keyword">catch</span> (err) {
36171 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
36180 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
36191 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
36193 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
36211 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
36213 <p>The read-only path of this directory as was provided to <a href="#fs_fs_opendir_path_options_cal…
36220-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> containi…
36222 <p>Asynchronously read the next directory entry via <a href="http://man7.org/linux/man-pages/man3/r…
36235 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
36237 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
36238 …lass="type">&#x3C;fs.Dirent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
36242 <p>Asynchronously read the next directory entry via <a href="http://man7.org/linux/man-pages/man3/r…
36255 …lass="type">&#x3C;fs.Dirent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
36258 POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</code></a> doc…
36269 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x…
36272 been read. Refer to the POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>r…
36295 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36303 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36311 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36320 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36322 … href="#fs_class_fs_dirent" class="type">&#x3C;fs.Dirent></a> object describes a first-in-first-out
36329 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36337 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36345 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36353 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
36374 <li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
36375 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
36382 <code>filename</code> will be a UTF-8 string.</p>
36383 …de class="language-js mjs"><span class="hljs-keyword">import</span> { watch } <span class="hljs-ke…
36384 <span class="hljs-comment">// Example when handled through fs.watch() listener</span>
36385-title function_">watch</span>(<span class="hljs-string">'./tmp'</span>, { <span class="hljs-attr"…
36386 <span class="hljs-keyword">if</span> (filename) {
36387 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
36388 <span class="hljs-comment">// Prints: &#x3C;Buffer ...></span>
36402 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
36490 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
36504 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
36512 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
36523 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
36544 nanosecond-precision properties suffixed with <code>Ns</code>.</p>
36545 <pre><code class="language-console">Stats {
36565 <pre><code class="language-console">BigIntStats {
36593 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36601 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36609 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36620 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36622 …="#fs_class_fs_stats" class="type">&#x3C;fs.Stats></a> object describes a first-in-first-out (FIFO)
36629 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36637 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36645 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36651 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36656 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36661 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36663 <p>A bit-field describing the file type and mode.</p>
36666 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36668 <p>The number of hard-links that exist for the file.</p>
36671 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36676 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36681 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36686 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36691 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36696 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36704 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36713 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36722 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36731 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36740 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigIn…
36751 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigIn…
36762 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigIn…
36773 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigIn…
36784 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"…
36792 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"…
36800 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"…
36808 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"…
36815 method that generates the object, the properties will be <a href="https://tc39.github.io/proposal-b…
36816 otherwise they will be <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
36818 <a href="https://tc39.github.io/proposal-bigint">bigints</a> that hold the corresponding times in n…
36822 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"><co…
36829-pages/man2/mknod.2.html"><code>mknod(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2…
36831-pages/man2/mknod.2.html"><code>mknod(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2…
36833 … href="http://man7.org/linux/man-pages/man2/chmod.2.html"><code>chmod(2)</code></a>, <a href="http…
36834-pages/man2/link.2.html"><code>link(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/m…
36835 …href="http://man7.org/linux/man-pages/man2/read.2.html"><code>read(2)</code></a>, and <a href="htt…
36839 <code>1970-01-01T00:00Z</code> (ie, Unix epoch timestamp <code>0</code>). This value may be greater
36842 <code>birthtime</code> using the <a href="http://man7.org/linux/man-pages/man2/utimes.2.html"><code…
36865 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
36885 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
36887 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
36907 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
36913 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
36922 …ass="language-js mjs"><span class="hljs-keyword">import</span> { open, constants } <span class="hl…
36924 <span class="hljs-keyword">const</span> {
36925 <span class="hljs-variable constant_">O_RDWR</span>,
36926 <span class="hljs-variable constant_">O_CREAT</span>,
36927 <span class="hljs-variable constant_">O_EXCL</span>
36930-title function_">open</span>(<span class="hljs-string">'/path/to/my/file'</span>, <span class="hl…
36931 <span class="hljs-comment">// ...</span>
36977 copy-on-write reflink. If the underlying platform does not support
36978 copy-on-write, then a fallback copy mechanism is used.</td>
36983 copy-on-write reflink. If the underlying platform does not support
36984 copy-on-write, then the operation will fail with an error.</td>
36996 <td>Flag indicating to open a file for read-only access.</td>
37000 <td>Flag indicating to open a file for write-only access.</td>
37004 <td>Flag indicating to open a file for read-write access.</td>
37100 <td>File type constant for a character-oriented device file.</td>
37104 <td>File type constant for a block-oriented device file.</td>
37177 <h4>Ordering of callback and promise-based operations<span><a class="mark" href="#fs_ordering_of_ca…
37180 promise-based methods.</p>
37183-js">fs.<span class="hljs-title function_">rename</span>(<span class="hljs-string">'/tmp/hello'</s…
37184 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37185 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
37187 …an class="hljs-title function_">stat</span>(<span class="hljs-string">'/tmp/world'</span>, <span c…
37188 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37189-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
37194-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
37196 <span class="hljs-keyword">const</span> <span class="hljs-keyword">from</span> = <span class="hljs-
37197 <span class="hljs-keyword">const</span> to = <span class="hljs-string">'/tmp/world'</span>;
37199 <span class="hljs-keyword">try</span> {
37200 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">rename</span>(<span cla…
37201 …<span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span clas…
37202-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
37203 } <span class="hljs-keyword">catch</span> (error) {
37204-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
37205 …ss="language-js cjs"><span class="hljs-keyword">const</span> { rename, stat } = <span class="hljs-
37207 (<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hl…
37208 <span class="hljs-keyword">try</span> {
37209 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">rename</span>(<span cla…
37210 …<span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span clas…
37211-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
37212 } <span class="hljs-keyword">catch</span> (error) {
37213-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
37215 })(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-string">'/tmp/world'</span>);</…
37219-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
37221-title function_">rename</span>(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-s…
37222 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37223 …an class="hljs-title function_">stat</span>(<span class="hljs-string">'/tmp/world'</span>, <span c…
37224 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37225-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
37227 …ss="language-js cjs"><span class="hljs-keyword">const</span> { rename, stat } = <span class="hljs-
37229-title function_">rename</span>(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-s…
37230 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37231 …an class="hljs-title function_">stat</span>(<span class="hljs-string">'/tmp/world'</span>, <span c…
37232 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37233-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
37240 <p>String form paths are interpreted as UTF-8 character sequences identifying
37244 …de class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-key…
37246 <span class="hljs-keyword">let</span> fd;
37247 <span class="hljs-keyword">try</span> {
37248 …hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string…
37249 <span class="hljs-comment">// Do something with the file</span>
37250 } <span class="hljs-keyword">finally</span> {
37251 <span class="hljs-keyword">await</span> fd.<span class="hljs-title function_">close</span>();
37254 …de class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-key…
37256 <span class="hljs-keyword">let</span> fd;
37257 <span class="hljs-keyword">try</span> {
37258 …lass="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-
37259 <span class="hljs-comment">// Do something with the file</span>
37260 } <span class="hljs-keyword">finally</span> {
37261 <span class="hljs-keyword">await</span> fd.<span class="hljs-title function_">close</span>();
37269 …lass="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs
37271 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37273 <h6>Platform-specific considerations<span><a class="mark" href="#fs_platform_specific_consideration…
37277 …lass="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs
37278 <span class="hljs-comment">// On Windows :</span>
37280 <span class="hljs-comment">// - WHATWG file URLs with hostname convert to UNC path</span>
37281 <span class="hljs-comment">// file://hostname/p/a/t/h/file => \\hostname\p\a\t\h\file</span>
37282 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37284 <span class="hljs-comment">// - WHATWG file URLs with drive letters convert to absolute path</span>
37285 <span class="hljs-comment">// file:///C:/tmp/hello => C:\tmp\hello</span>
37286 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37288 <span class="hljs-comment">// - WHATWG file URLs without hostname must have a drive letters</span>
37289 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37290 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37291 <span class="hljs-comment">// TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute…
37296 …lass="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs
37297 <span class="hljs-comment">// On other platforms:</span>
37299 <span class="hljs-comment">// - WHATWG file URLs with hostname are unsupported</span>
37300 <span class="hljs-comment">// file://hostname/p/a/t/h/file => throw!</span>
37301 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37302 <span class="hljs-comment">// TypeError [ERR_INVALID_FILE_URL_PATH]: must be absolute</span>
37304 <span class="hljs-comment">// - WHATWG file URLs convert to absolute path</span>
37305 <span class="hljs-comment">// file:///tmp/hello => /tmp/hello</span>
37306 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37309 …lass="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs
37311 <span class="hljs-comment">// On Windows</span>
37312 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37313 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37314 <span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include…
37317 <span class="hljs-comment">// On POSIX</span>
37318 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37319 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37320 <span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include…
37323 …lass="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs
37325 <span class="hljs-comment">// On Windows</span>
37326 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37327 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37328 <span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include…
37333 systems, it is possible for a single file path to contain sub-sequences that
37337 …de class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-key…
37339 <span class="hljs-keyword">let</span> fd;
37340 <span class="hljs-keyword">try</span> {
37341-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-title clas…
37342 <span class="hljs-comment">// Do something with the file</span>
37343 } <span class="hljs-keyword">finally</span> {
37344 <span class="hljs-keyword">await</span> fd.<span class="hljs-title function_">close</span>();
37346 <h5>Per-drive working directories on Windows<span><a class="mark" href="#fs_per_drive_working_direc…
37347 <p>On Windows, Node.js follows the concept of per-drive working directory. This
37351 <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-r…
37355 identifier called a <em>file descriptor</em>. At the system-level, all file system
37361 <p>The callback-based <code>fs.open()</code>, and synchronous <code>fs.openSync()</code> methods op…
37368 …s="language-js mjs"><span class="hljs-keyword">import</span> { open, close, fstat } <span class="h…
37370 <span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span …
37371 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
37372 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37376-title function_">open</span>(<span class="hljs-string">'/open/some/file.txt'</span>, <span class=…
37377 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37378 <span class="hljs-keyword">try</span> {
37379 …<span class="hljs-title function_">fstat</span>(fd, <span class="hljs-function">(<span class="hljs
37380 <span class="hljs-keyword">if</span> (err) {
37381 <span class="hljs-title function_">closeFd</span>(fd);
37382 <span class="hljs-keyword">throw</span> err;
37385 <span class="hljs-comment">// use stat</span>
37387 <span class="hljs-title function_">closeFd</span>(fd);
37389 } <span class="hljs-keyword">catch</span> (err) {
37390 <span class="hljs-title function_">closeFd</span>(fd);
37391 <span class="hljs-keyword">throw</span> err;
37394 <p>The promise-based APIs use a <a href="#fs_class_filehandle" class="type">&#x3C;FileHandle></a> o…
37398 …de class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-key…
37400 <span class="hljs-keyword">let</span> file;
37401 <span class="hljs-keyword">try</span> {
37402 …hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string…
37403 …<span class="hljs-keyword">const</span> stat = <span class="hljs-keyword">await</span> file.<span …
37404 <span class="hljs-comment">// use stat</span>
37405 } <span class="hljs-keyword">finally</span> {
37406 <span class="hljs-keyword">await</span> file.<span class="hljs-title function_">close</span>();
37409 <p>All callback and promise-based file system APIs ( with the exception of
37472 <p><code>flag</code> can also be a number as documented by <a href="http://man7.org/linux/man-pages…
37476 …e>'x'</code> (<code>O_EXCL</code> flag in <a href="http://man7.org/linux/man-pages/man2/open.2.htm…
37485 <p>The behavior of some flags are platform-specific. As such, opening a directory
37489 <pre><code class="language-js"><span class="hljs-comment">// macOS and Linux</span>
37490-title function_">open</span>(<span class="hljs-string">'&#x3C;directory>'</span>, <span class="hl…
37491 …<span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, open &#x3C;dire…
37494 <span class="hljs-comment">// Windows and FreeBSD</span>
37495-title function_">open</span>(<span class="hljs-string">'&#x3C;directory>'</span>, <span class="hl…
37496 <span class="hljs-comment">// => null, &#x3C;fd></span>
37517 …re are <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects">…
37524 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
37526 <p>A utility class used to signal cancelation in selected <code>Promise</code>-based APIs.
37527 The API is based on the Web API <a href="https://developer.mozilla.org/en-US/docs/Web/API/AbortCont…
37528 <p>To use, launch Node.js using the <code>--experimental-abortcontroller</code> flag.</p>
37529 …e><code class="language-js"><span class="hljs-keyword">const</span> ac = <span class="hljs-keyword…
37531-property">signal</span>.<span class="hljs-title function_">addEventListener</span>(<span class="h…
37532 … { <span class="hljs-attr">once</span>: <span class="hljs-literal">true</span> });
37534 ac.<span class="hljs-title function_">abort</span>();
37536-variable language_">console</span>.<span class="hljs-title function_">log</span>(ac.<span class="…
37574 …e><code class="language-js"><span class="hljs-keyword">const</span> ac = <span class="hljs-keyword…
37576 <span class="hljs-comment">// Use either the onabort property...</span>
37577-property">signal</span>.<span class="hljs-property">onabort</span> = <span class="hljs-function">…
37579 <span class="hljs-comment">// Or the EventTarget API...</span>
37580-property">signal</span>.<span class="hljs-title function_">addEventListener</span>(<span class="h…
37581 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span cl…
37582 }, { <span class="hljs-attr">once</span>: <span class="hljs-literal">true</span> });
37584 ac.<span class="hljs-title function_">abort</span>();</code></pre>
37599 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
37606 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects…
37616 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Funct…
37647 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
37658 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
37660 <p>In browsers, the top-level scope is the global scope. This means that
37662 Node.js this is different. The top-level scope is not the global scope;
37672 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
37681 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
37690 <pre><code class="language-js"><span class="hljs-comment">// Here, `queueMicrotask()` is used to en…
37691 <span class="hljs-comment">// emitted asynchronously, and therefore consistently. Using</span>
37692 <span class="hljs-comment">// `process.nextTick()` here would result in the 'load' event always emi…
37693 <span class="hljs-comment">// before any other promise jobs.</span>
37695-title class_">DataHandler</span>.<span class="hljs-property"><span class="hljs-keyword">prototype…
37696 …s="hljs-keyword">const</span> hit = <span class="hljs-variable language_">this</span>.<span class=…
37697 <span class="hljs-keyword">if</span> (hit !== <span class="hljs-literal">undefined</span>) {
37698 …<span class="hljs-title function_">queueMicrotask</span>(<span class="hljs-function">() =></span> {
37699 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<…
37701 <span class="hljs-keyword">return</span>;
37704 …<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> <span class…
37705 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">_cache</span>.<span …
37706 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<…
37758 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
37762 <a href="https://developer.mozilla.org/en-US/docs/WebAssembly">Mozilla Developer Network</a> for us…
37766 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
37771 In particular, large, possibly chunk-encoded, messages. The interface is
37775 <!-- eslint-skip -->
37776 <pre><code class="language-js">{ <span class="hljs-string">'content-length'</span>: <span class="hl…
37777 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain'</span>,
37778 <span class="hljs-string">'connection'</span>: <span class="hljs-string">'keep-alive'</span>,
37779 <span class="hljs-string">'host'</span>: <span class="hljs-string">'mysite.com'</span>,
37780 <span class="hljs-string">'accept'</span>: <span class="hljs-string">'*/*'</span> }</code></pre>
37783 HTTP API is very low-level. It deals with stream handling and message
37791 <!-- eslint-disable semi -->
37792 <pre><code class="language-js">[ <span class="hljs-string">'ConTent-Length'</span>, <span class="hl…
37793 <span class="hljs-string">'content-LENGTH'</span>, <span class="hljs-string">'123'</span>,
37794 <span class="hljs-string">'content-type'</span>, <span class="hljs-string">'text/plain'</span>,
37795 <span class="hljs-string">'CONNECTION'</span>, <span class="hljs-string">'keep-alive'</span>,
37796 <span class="hljs-string">'Host'</span>, <span class="hljs-string">'mysite.com'</span>,
37797 <span class="hljs-string">'accepT'</span>, <span class="hljs-string">'*/*'</span> ]</code></pre>
37809 <p>Pooled connections have TCP Keep-Alive enabled for them, but servers may
37826 …code class="language-js">http.<span class="hljs-title function_">get</span>(options, <span class="…
37827 <span class="hljs-comment">// Do stuff</span>
37828 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'socket'</span>, <span clas…
37829 …socket.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'agentRemove'</spa…
37833 functions, a one-time use <code>Agent</code> with default options will be used
37836 <pre><code class="language-js">http.<span class="hljs-title function_">get</span>({
37837 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'localhost'</span>,
37838 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
37839 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
37840 …<span class="hljs-attr">agent</span>: <span class="hljs-literal">false</span> <span class="hljs-c…
37841 }, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
37842 <span class="hljs-comment">// Do stuff with response</span>
37861 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
37864 <li><code>keepAlive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
37867 <code>keep-alive</code> value of the <code>Connection</code> header. The <code>Connection: keep-ali…
37872 <li><code>keepAliveMsecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
37874 for TCP Keep-Alive packets. Ignored when the
37876 <li><code>maxSockets</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
37885 <li><code>maxTotalSockets</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
37889 <li><code>maxFreeSockets</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
37892 <li><code>scheduling</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
37904 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
37913 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
37914-keyword">const</span> keepAliveAgent = <span class="hljs-keyword">new</span> http.<span class="hl…
37915 options.<span class="hljs-property">agent</span> = keepAliveAgent;
37916 http.<span class="hljs-title function_">request</span>(options, onResponseCallback);</code></pre>
37922 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
37924 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
37945-js">socket.<span class="hljs-title function_">setKeepAlive</span>(<span class="hljs-literal">true…
37946 socket.<span class="hljs-title function_">unref</span>();
37947 <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;</code></pre>
37962 the keep-alive options. Default behavior is to:</p>
37963 <pre><code class="language-js">socket.<span class="hljs-title function_">ref</span>();</code></pre>
37982 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
37993 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
37995 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
37997 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
37998 <li><code>localAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
38000 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
38003 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
38008 the name includes the CA, cert, ciphers, and other HTTPS/TLS-specific options
38015 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38025 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38034 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38043 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
38052 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
38064 represents an <em>in-progress</em> request whose header has already been queued. The
38085 <p>Node.js does not check whether Content-Length and the length of the
38109 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38110 <span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span clas…
38111 …s="hljs-keyword">const</span> { <span class="hljs-variable constant_">URL</span> } = <span class="…
38113 <span class="hljs-comment">// Create an HTTP tunneling proxy</span>
38114 …s="hljs-keyword">const</span> proxy = http.<span class="hljs-title function_">createServer</span>(…
38115 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
38116 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'okay'</span>);
38118 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'connect'</span>, <span cla…
38119 <span class="hljs-comment">// Connect to an origin server</span>
38120-keyword">const</span> { port, hostname } = <span class="hljs-keyword">new</span> <span class="hlj…
38121-keyword">const</span> serverSocket = net.<span class="hljs-title function_">connect</span>(port |…
38122 …clientSocket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'HTTP/1.1 2…
38123 <span class="hljs-string">'Proxy-agent: Node.js-Proxy\r\n'</span> +
38124 <span class="hljs-string">'\r\n'</span>);
38125 serverSocket.<span class="hljs-title function_">write</span>(head);
38126 serverSocket.<span class="hljs-title function_">pipe</span>(clientSocket);
38127 clientSocket.<span class="hljs-title function_">pipe</span>(serverSocket);
38131 <span class="hljs-comment">// Now that proxy is running</span>
38132 …ass="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>, <span class="hljs-
38134 <span class="hljs-comment">// Make a request to a tunneling proxy</span>
38135 <span class="hljs-keyword">const</span> options = {
38136 <span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>,
38137 <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
38138 <span class="hljs-attr">method</span>: <span class="hljs-string">'CONNECT'</span>,
38139 <span class="hljs-attr">path</span>: <span class="hljs-string">'www.google.com:80'</span>
38142 …<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</spa…
38143 req.<span class="hljs-title function_">end</span>();
38145 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'connect'</span>, <span cla…
38146 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
38148 <span class="hljs-comment">// Make a request over an HTTP tunnel</span>
38149 …socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'GET / HTTP/1.1\r…
38150 <span class="hljs-string">'Host: www.google.com:80\r\n'</span> +
38151 <span class="hljs-string">'Connection: close\r\n'</span> +
38152 <span class="hljs-string">'\r\n'</span>);
38153 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
38154 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
38156 …socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span …
38157 proxy.<span class="hljs-title function_">close</span>();
38166 the request contained 'Expect: 100-continue'. This is an instruction that
38173 <li><code>info</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
38175 <li><code>httpVersion</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
38176 <li><code>httpVersionMajor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
38177 <li><code>httpVersionMinor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
38178 <li><code>statusCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
38179 <li><code>statusMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
38180 <li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
38181 <li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
38187 HTTP version, status code, status message, key-value headers object,
38189 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38191 <span class="hljs-keyword">const</span> options = {
38192 <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
38193 <span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>,
38194 <span class="hljs-attr">path</span>: <span class="hljs-string">'/length_request'</span>
38197 <span class="hljs-comment">// Make a request</span>
38198 <span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span…
38199 req.<span class="hljs-title function_">end</span>();
38201 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'information'</span>, <span c…
38202-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
38205 traditional HTTP request/response chain, such as web sockets, in-place TLS
38251 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38253 <span class="hljs-comment">// Create an HTTP server</span>
38254 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
38255 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
38256 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'okay'</span>);
38258 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'upgrade'</span>, <span cla…
38259 …socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'HTTP/1.1 101 Web…
38260 <span class="hljs-string">'Upgrade: WebSocket\r\n'</span> +
38261 <span class="hljs-string">'Connection: Upgrade\r\n'</span> +
38262 <span class="hljs-string">'\r\n'</span>);
38264 …socket.<span class="hljs-title function_">pipe</span>(socket); <span class="hljs-comment">// echo …
38267 <span class="hljs-comment">// Now that server is running</span>
38268 …ass="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>, <span class="hljs-
38270 <span class="hljs-comment">// make a request</span>
38271 <span class="hljs-keyword">const</span> options = {
38272 <span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>,
38273 <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
38274 <span class="hljs-attr">headers</span>: {
38275 <span class="hljs-string">'Connection'</span>: <span class="hljs-string">'Upgrade'</span>,
38276 <span class="hljs-string">'Upgrade'</span>: <span class="hljs-string">'websocket'</span>
38280 …<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</spa…
38281 req.<span class="hljs-title function_">end</span>();
38283 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'upgrade'</span>, <span cla…
38284 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
38285 socket.<span class="hljs-title function_">end</span>();
38286 process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>);
38293 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
38309 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38317 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
38335 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
38336 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
38337 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
38338 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
38360 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
38361 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
38372 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38380 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
38382 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38395 <p>That's usually desired (it saves a TCP round-trip), but not when the first
38403 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
38404 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
38406 <p>Reads out a header on the request. The name is case-insensitive.
38409 …="language-js">request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-strin…
38410 …s-title function_">setHeader</span>(<span class="hljs-string">'Content-Length'</span>, <span class…
38411 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Cookie'</span>, [<span class="hl…
38412 …lass="hljs-keyword">const</span> contentType = request.<span class="hljs-title function_">getHeade…
38413 <span class="hljs-comment">// 'contentType' is 'text/html'</span>
38414 …ass="hljs-keyword">const</span> contentLength = request.<span class="hljs-title function_">getHead…
38415 <span class="hljs-comment">// 'contentLength' is of type number</span>
38416 <span class="hljs-keyword">const</span> cookie = request.<span class="hljs-title function_">getHead…
38417 <span class="hljs-comment">// 'cookie' is of type string[]</span></code></pre>
38423 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
38427 …class="language-js">request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-
38428 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class…
38430 <span class="hljs-keyword">const</span> headerNames = request.<span class="hljs-title function_">ge…
38431 <span class="hljs-comment">// headerNames === ['Foo', 'Set-Cookie']</span></code></pre>
38434 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38442 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
38449 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
38456 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
38463 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
38470 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
38473 …pre><code class="language-js">request.<span class="hljs-title function_">removeHeader</span>(<span…
38479 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38481 <p>When sending request through a keep-alive enabled agent, the underlying socket
38484 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38486 <span class="hljs-comment">// Server has a 5 seconds keep-alive timeout by default</span>
38488 ….<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="…
38489 res.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello\n'</span>);
38490 res.<span class="hljs-title function_">end</span>();
38492 .<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);
38494 <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> {
38495 <span class="hljs-comment">// Adapting a keep-alive agent</span>
38496 …hljs-title function_">get</span>(<span class="hljs-string">'http://localhost:3000'</span>, { agent…
38497 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
38498 <span class="hljs-comment">// Do nothing</span>
38501 }, <span class="hljs-number">5000</span>); <span class="hljs-comment">// Sending request on 5s inte…
38504 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38505-keyword">const</span> agent = <span class="hljs-keyword">new</span> http.<span class="hljs-title …
38507 <span class="hljs-keyword">function</span> <span class="hljs-title function_">retriableRequest</spa…
38508 <span class="hljs-keyword">const</span> req = http
38509 …hljs-title function_">get</span>(<span class="hljs-string">'http://localhost:3000'</span>, { agent…
38510 <span class="hljs-comment">// ...</span>
38512 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
38513 <span class="hljs-comment">// Check if retry is needed</span>
38514-keyword">if</span> (req.<span class="hljs-property">reusedSocket</span> &#x26;&#x26; err.<span cl…
38515 <span class="hljs-title function_">retriableRequest</span>();
38520 <span class="hljs-title function_">retriableRequest</span>();</code></pre>
38526 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
38527 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
38530 the to-be-sent headers, its value will be replaced. Use an array of strings
38531 here to send multiple headers with the same name. Non-string values will be
38533 non-string values. However, the non-string values will be converted to strings
38535 …="language-js">request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-strin…
38537-js">request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Cookie…
38543 <li><code>noDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
38552 <li><code>enable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
38553 <li><code>initialDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
38570 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
38571 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
38587 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38588 <span class="hljs-keyword">const</span> options = {
38589 <span class="hljs-attr">host</span>: <span class="hljs-string">'www.google.com'</span>,
38591 <span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">get</span>(op…
38592 req.<span class="hljs-title function_">end</span>();
38593 …pan class="hljs-title function_">once</span>(<span class="hljs-string">'response'</span>, <span cl…
38594 …<span class="hljs-keyword">const</span> ip = req.<span class="hljs-property">socket</span>.<span c…
38595 …<span class="hljs-keyword">const</span> port = req.<span class="hljs-property">socket</span>.<span…
38596-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
38597 <span class="hljs-comment">// Consume response object</span>
38607 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38617 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38626 <li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
38627 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
38628 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
38629 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
38632 <code>Content-Length</code> is set, data will automatically be encoded in HTTP Chunked
38634 <code>Transfer-Encoding: chunked</code> header is added. Calling <a href="#http_request_end_data_en…
38639 is flushed, but only if the chunk is non-empty.</p>
38660 <p>Emitted each time a request with an HTTP <code>Expect: 100-continue</code> is received.
38678 value is not <code>100-continue</code>. If this event is not listened for, the server will
38699 <li><code>exception</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
38714 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38716 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
38717 res.<span class="hljs-title function_">end</span>();
38719 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'clientError'</span>, <span c…
38720 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'HTTP/1.1 400 Bad R…
38722 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</co…
38737-js">server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'clientError'</…
38738 …hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string"…
38739 <span class="hljs-keyword">return</span>;
38742 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'HTTP/1.1 400 Bad R…
38784 <code>server.keepAliveTimeout</code> is non-zero).</p>
38797 per connection (in the case of HTTP Keep-Alive connections).</p>
38829 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
38837 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38858 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38865 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38873 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38879 <p>It must be set to a non-zero value (e.g. 120 seconds) to protect against
38880 potential Denial-of-Service attacks in case the server is deployed without a
38895 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
38896 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
38903 will be called with the timed-out socket as an argument.</p>
38920 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38932 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38936 will be destroyed. If the server receives new data before the keep-alive
38939 <p>A value of <code>0</code> will disable the keep-alive timeout behavior on incoming
38942 to 8.0.0, which did not have a keep-alive timeout.</p>
38973 <li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
38982-js">response.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</s…
38983 … <span class="hljs-string">'Trailer'</span>: <span class="hljs-string">'Content-MD5'</span> });
38984 response.<span class="hljs-title function_">write</span>(fileData);
38985 …sponse.<span class="hljs-title function_">addTrailers</span>({ <span class="hljs-string">'Content-
38986 response.<span class="hljs-title function_">end</span>();</code></pre>
38993 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
39016 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39017 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
39018 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39019 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
39032 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
39034 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39048 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39049 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
39052 The name is case-insensitive. The type of the return value depends
39054 …="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-stri…
39055 …s-title function_">setHeader</span>(<span class="hljs-string">'Content-Length'</span>, <span class…
39056 …js-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class="…
39057 …lass="hljs-keyword">const</span> contentType = response.<span class="hljs-title function_">getHead…
39058 <span class="hljs-comment">// contentType is 'text/html'</span>
39059 …ass="hljs-keyword">const</span> contentLength = response.<span class="hljs-title function_">getHea…
39060 <span class="hljs-comment">// contentLength is of type number</span>
39061 …n class="hljs-keyword">const</span> setCookie = response.<span class="hljs-title function_">getHea…
39062 <span class="hljs-comment">// setCookie is of type string[]</span></code></pre>
39068 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
39072 …lass="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-
39073 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class…
39075 <span class="hljs-keyword">const</span> headerNames = response.<span class="hljs-title function_">g…
39076 <span class="hljs-comment">// headerNames === ['foo', 'set-cookie']</span></code></pre>
39082 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
39086 header-related http module methods. The keys of the returned object are the
39093 …lass="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-
39094 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class…
39096 <span class="hljs-keyword">const</span> headers = response.<span class="hljs-title function_">getHe…
39097 <span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</spa…
39103 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39104 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
39107 outgoing headers. The header name matching is case-insensitive.</p>
39108-js"><span class="hljs-keyword">const</span> hasContentType = response.<span class="hljs-title fun…
39114 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39116 <p>Boolean (read-only). True if headers were sent, false otherwise.</p>
39122 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39125 …re><code class="language-js">response.<span class="hljs-title function_">removeHeader</span>(<span…
39131 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39142 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39143 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39148 in the to-be-sent headers, its value will be replaced. Use an array of strings
39149 here to send multiple headers with the same name. Non-string values will be
39151 non-string values. However, the non-string values will be converted to strings
39154 …="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-stri…
39156-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-C…
39162 <pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</spa…
39163 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
39164 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</…
39165 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, …
39166 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
39167 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
39180 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39181 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39202 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
39203 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
39204 …<span class="hljs-keyword">const</span> ip = res.<span class="hljs-property">socket</span>.<span c…
39205 …<span class="hljs-keyword">const</span> port = res.<span class="hljs-property">socket</span>.<span…
39206-title function_">end</span>(<span class="hljs-string">`Your IP address is <span class="hljs-subst…
39207 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code><…
39216 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
39221 <pre><code class="language-js">response.<span class="hljs-property">statusCode</span> = <span class…
39229 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39235 <pre><code class="language-js">response.<span class="hljs-property">statusMessage</span> = <span cl…
39248 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39258 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39267 <li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39268 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
39269 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39270 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
39282 <p>This is the raw HTTP body and has nothing to do with higher-level multi-part
39316 <li><code>statusCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
39317 <li><code>statusMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
39318 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
39321 <p>Sends a response header to the request. The status code is a 3-digit HTTP
39323 Optionally one can give a human-readable <code>statusMessage</code> as the second
39326 It is <em>not</em> a list of tuples. So, the even-numbered offsets are key values,
39327 and the odd-numbered offsets are the associated values. The array is in the same
39330 <pre><code class="language-js"><span class="hljs-keyword">const</span> body = <span class="hljs-str…
39332 .<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, {
39333 …<span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<…
39334 <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain'</span>
39336 .<span class="hljs-title function_">end</span>(body);</code></pre>
39350 <pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</spa…
39351 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
39352 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</…
39353 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, …
39354 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
39355 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
39357 <p><code>Content-Length</code> is given in bytes, not characters. Use
39359 does not check whether <code>Content-Length</code> and the length of the body which has
39403 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39412 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39418 <pre><code class="language-js"><span class="hljs-keyword">const</span> req = http.<span class="hljs
39419 <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
39420 <span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>,
39421 <span class="hljs-attr">method</span>: <span class="hljs-string">'POST'</span>
39422 }, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
39423 res.<span class="hljs-title function_">resume</span>();
39424 …res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span cla…
39425 <span class="hljs-keyword">if</span> (!res.<span class="hljs-property">complete</span>)
39426 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
39427 …<span class="hljs-string">'The connection was terminated while the message was still being sent'</…
39443 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
39444 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
39454 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
39457 <p>Key-value pairs of header names and values. Header names are lower-cased.</p>
39458 <pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
39459 <span class="hljs-comment">//</span>
39460 <span class="hljs-comment">// { 'user-agent': 'curl/7.22.0',</span>
39461 <span class="hljs-comment">// host: '127.0.0.1:8000',</span>
39462 <span class="hljs-comment">// accept: '*/*' }</span>
39463 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
39467 …f <code>age</code>, <code>authorization</code>, <code>content-length</code>, <code>content-type</c…
39468 …/code>, <code>from</code>, <code>host</code>, <code>if-modified-since</code>, <code>if-unmodified-
39469 <code>last-modified</code>, <code>location</code>, <code>max-forwards</code>, <code>proxy-authoriza…
39470 <code>retry-after</code>, <code>server</code>, or <code>user-agent</code> are discarded.</li>
39471 <li><code>set-cookie</code> is always an array. Duplicates are added to the array.</li>
39480 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39483 client response, the HTTP version of the connected-to server.
39492 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39501 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39505 list of tuples. So, the even-numbered offsets are key values, and the
39506 odd-numbered offsets are the associated values.</p>
39508 <pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
39509 <span class="hljs-comment">//</span>
39510 <span class="hljs-comment">// [ 'user-agent',</span>
39511 <span class="hljs-comment">// 'this is invalid because there can be only one',</span>
39512 <span class="hljs-comment">// 'User-Agent',</span>
39513 <span class="hljs-comment">// 'curl/7.22.0',</span>
39514 <span class="hljs-comment">// 'Host',</span>
39515 <span class="hljs-comment">// '127.0.0.1:8000',</span>
39516 <span class="hljs-comment">// 'ACCEPT',</span>
39517 <span class="hljs-comment">// '*/*' ]</span>
39518 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
39524 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39533 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39534 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39556 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
39559 <p>The 3-digit HTTP response status code. E.G. <code>404</code>.</p>
39565 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39574 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
39582 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39587 …ode class="language-http"><span class="hljs-keyword">GET</span> <span class="hljs-string">/status?…
39588 <span class="hljs-attribute">Accept</span><span class="hljs-punctuation">: </span>text/plain</code>…
39590-js"><span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(request.<…
39593 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
39594 <span class="hljs-meta">> </span><span class="language-bash">new URL(request.url, `http://<span cla…
39641 <li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
39648-js">message.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</sp…
39649 … <span class="hljs-string">'Trailer'</span>: <span class="hljs-string">'Content-MD5'</span> });
39650 message.<span class="hljs-title function_">write</span>(fileData);
39651 …essage.<span class="hljs-title function_">addTrailers</span>({ <span class="hljs-string">'Content-
39652 message.<span class="hljs-title function_">end</span>();</code></pre>
39659 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
39671 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
39672 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
39689 <li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39690 …illa.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Opt…
39691 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39692 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
39711 <p>It is usually desired (it saves a TCP round-trip), but not when the first
39719 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39720 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
39729 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String…
39738 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
39742 various header-related HTTP module methods. The keys of the returned
39749 …s="language-js">outgoingMessage.<span class="hljs-title function_">setHeader</span>(<span class="h…
39750 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class…
39752 <span class="hljs-keyword">const</span> headers = outgoingMessage.<span class="hljs-title function_…
39753 <span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</spa…
39759 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39760 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolea…
39763 outgoing headers. The header name is case-insensitive.</p>
39764-js"><span class="hljs-keyword">const</span> hasContentType = outgoingMessage.<span class="hljs-ti…
39770 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39772 <p>Read-only. <code>true</code> if the headers were sent, otherwise <code>false</code>.</p>
39779 <p>Since <code>OutgoingMessage</code> should be a write-only stream,
39788 …<code class="language-js">outgoingMessage.<span class="hljs-title function_">removeHeader</span>(<…
39794 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39795 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39796 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
39804 <li><code>msesc</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39805 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39809 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
39833 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
39842 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39852 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39860 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
39872 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
39881 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39897 <li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39898 …r.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a…
39899 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39900 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolea…
39916 with higher-level multi-part body encodings that may be used.</p>
39930 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39938 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
39960 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
39968 <li><code>insecureHTTPParser</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
39970 avoided. See <a href="#cli_insecure_http_parser"><code>--insecure-http-parser</code></a> for more i…
39972 <li><code>maxHeaderSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
39973 <a href="#cli_max_http_header_size_size"><code>--max-http-header-size</code></a> for requests recei…
39979 <p><code>requestListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
39989-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><pre><code cl…
39991 <span class="hljs-comment">// Create a local server to receive data from</span>
39992 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
39993 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
39994 …res.<span class="hljs-title function_">end</span>(<span class="hljs-variable constant_">JSON</span…
39995 <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>
39999-title function_">listen</span>(<span class="hljs-number">8000</span>);</code></pre><code class="l…
40001 <span class="hljs-comment">// Create a local server to receive data from</span>
40002 <span class="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServ…
40004 <span class="hljs-comment">// Listen to the request event</span>
40005 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'request'</span>, <span cla…
40006 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
40007 …res.<span class="hljs-title function_">end</span>(<span class="hljs-variable constant_">JSON</span…
40008 <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>
40012 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</co…
40029 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
40030 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
40033 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
40044-js">http.<span class="hljs-title function_">get</span>(<span class="hljs-string">'http://localhos…
40045 <span class="hljs-keyword">const</span> { statusCode } = res;
40046 …<span class="hljs-keyword">const</span> contentType = res.<span class="hljs-property">headers</spa…
40048 <span class="hljs-keyword">let</span> error;
40049 <span class="hljs-comment">// Any 2xx status code signals a successful response but</span>
40050 <span class="hljs-comment">// here we're only checking for 200.</span>
40051 <span class="hljs-keyword">if</span> (statusCode !== <span class="hljs-number">200</span>) {
40052 …error = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span c…
40053 … <span class="hljs-string">`Status Code: <span class="hljs-subst">${statusCode}</span>`</span>);
40054 …lass="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!<span class="hljs-regexp">/…
40055 …or = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span clas…
40056 …<span class="hljs-string">`Expected application/json but received <span class="hljs-subst">${conte…
40058 <span class="hljs-keyword">if</span> (error) {
40059 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
40060 <span class="hljs-comment">// Consume response data to free up memory</span>
40061 res.<span class="hljs-title function_">resume</span>();
40062 <span class="hljs-keyword">return</span>;
40065 …res.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
40066 <span class="hljs-keyword">let</span> rawData = <span class="hljs-string">''</span>;
40067 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
40068 …res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span cla…
40069 <span class="hljs-keyword">try</span> {
40070 …<span class="hljs-keyword">const</span> parsedData = <span class="hljs-variable constant_">JSON</s…
40071 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
40072 } <span class="hljs-keyword">catch</span> (e) {
40073 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
40076 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
40077 …="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span cl…
40080 <span class="hljs-comment">// Create a local server to receive data from</span>
40081 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
40082 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
40083 …res.<span class="hljs-title function_">end</span>(<span class="hljs-variable constant_">JSON</span…
40084 <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>
40088 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</co…
40103 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
40105 <p>Read-only property specifying the maximum allowed size of HTTP headers in bytes.
40106 …figurable using the <a href="#cli_max_http_header_size_size"><code>--max-http-header-size</code></…
40133 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
40134 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
40136 …ass="type">&#x3C;http.Agent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
40144 <li><code>auth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40146 <li><code>createConnection</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
40151 <li><code>defaultPort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
40153 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
40156 <li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
40157 <li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
40158 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40160 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
40162 <li><code>insecureHTTPParser</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
40164 avoided. See <a href="#cli_insecure_http_parser"><code>--insecure-http-parser</code></a> for more i…
40166 <li><code>localAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
40167 <li><code>localPort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
40168 <li><code>lookup</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
40169 <li><code>maxHeaderSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
40170 <a href="#cli_max_http_header_size_size"><code>--max-http-header-size</code></a> for requests recei…
40173 <li><code>method</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
40175 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40179 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40181 <li><code>protocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
40182 <li><code>setHost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
40184 <li><code>socketPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
40186 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
40192 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
40202 <p>The optional <code>callback</code> parameter will be added as a one-time listener for
40207 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
40209 <span class="hljs-keyword">const</span> postData = <span class="hljs-variable constant_">JSON</span…
40210 <span class="hljs-string">'msg'</span>: <span class="hljs-string">'Hello World!'</span>
40213 <span class="hljs-keyword">const</span> options = {
40214 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'www.google.com'</span>,
40215 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
40216 <span class="hljs-attr">path</span>: <span class="hljs-string">'/upload'</span>,
40217 <span class="hljs-attr">method</span>: <span class="hljs-string">'POST'</span>,
40218 <span class="hljs-attr">headers</span>: {
40219 …<span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'application/json'</spa…
40220 …<span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<…
40224 …="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options,…
40225 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
40226-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
40227 …res.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
40228 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
40229 …lass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span …
40231 …res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span cla…
40232 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
40236 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
40237 …s-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="…
40240 <span class="hljs-comment">// Write data to request body</span>
40241 req.<span class="hljs-title function_">write</span>(postData);
40242 req.<span class="hljs-title function_">end</span>();</code></pre>
40244 must always call <code>req.end()</code> to signify the end of the request -
40253 <p>Sending a 'Connection: keep-alive' will notify Node.js that the connection to
40257 <p>Sending a 'Content-Length' header will disable the default chunked encoding.</p>
40261 Usually, when sending 'Expect: 100-continue', both a timeout and a listener
40271-js"><span class="hljs-keyword">const</span> options = <span class="hljs-keyword">new</span> <span…
40273 …="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options,…
40274 <span class="hljs-comment">// ...</span>
40391 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40393 <p>Performs the low-level validations on the provided <code>name</code> that are done when
40401 …s="language-js"><span class="hljs-keyword">const</span> { validateHeaderName } = <span class="hljs
40403 <span class="hljs-keyword">try</span> {
40404 <span class="hljs-title function_">validateHeaderName</span>(<span class="hljs-string">''</span>);
40405 } <span class="hljs-keyword">catch</span> (err) {
40406 …err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">TypeError</span>;…
40407 …err.<span class="hljs-property">code</span>; <span class="hljs-comment">// --> 'ERR_INVALID_HTTP_T…
40408 …err.<span class="hljs-property">message</span>; <span class="hljs-comment">// --> 'Header name mus…
40415 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40416 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
40418 <p>Performs the low-level validations on the provided <code>value</code> that are done when
40428 …="language-js"><span class="hljs-keyword">const</span> { validateHeaderValue } = <span class="hljs
40430 <span class="hljs-keyword">try</span> {
40431 …<span class="hljs-title function_">validateHeaderValue</span>(<span class="hljs-string">'x-my-head…
40432 } <span class="hljs-keyword">catch</span> (err) {
40433 …err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">TypeError</span>;…
40434 …an class="hljs-property">code</span> === <span class="hljs-string">'ERR_HTTP_INVALID_HEADER_VALUE'…
40435 …err.<span class="hljs-property">message</span>; <span class="hljs-comment">// --> 'Invalid value "…
40438 <span class="hljs-keyword">try</span> {
40439 …<span class="hljs-title function_">validateHeaderValue</span>(<span class="hljs-string">'x-my-head…
40440 } <span class="hljs-keyword">catch</span> (err) {
40441 …err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">TypeError</span>;…
40442 …rr.<span class="hljs-property">code</span> === <span class="hljs-string">'ERR_INVALID_CHAR'</span>…
40443 …err.<span class="hljs-property">message</span>; <span class="hljs-comment">// --> 'Invalid charact…
40461 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
40465 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40467 <p>The Core API provides a low-level interface designed specifically around
40473 <code>'stream'</code>, can be emitted either by client-side code or server-side code.</p>
40474 <h4>Server-side example<span><a class="mark" href="#http2_server_side_example" id="http2_server_sid…
40477 <a href="https://http2.github.io/faq/#does-http2-require-encryption">unencrypted HTTP/2</a>, the us…
40480 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40481 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
40483 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSec…
40484 …<span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<spa…
40485 …<span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<sp…
40487-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">…
40489 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
40490 <span class="hljs-comment">// stream is a Duplex</span>
40491 stream.<span class="hljs-title function_">respond</span>({
40492 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf
40493 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
40495 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello Wor…
40498 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8443</span>);</co…
40500 <pre><code class="language-bash">openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj <span clas…
40501 -keyout localhost-privkey.pem -out localhost-cert.pem</code></pre>
40502 <h4>Client-side example<span><a class="mark" href="#http2_client_side_example" id="http2_client_sid…
40504 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40505 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
40506 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
40507 …<span class="hljs-attr">ca</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span…
40509-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">…
40511 …js-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span cl…
40513 …span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span cla…
40514 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="h…
40515-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
40519 req.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
40520 <span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
40521 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
40522 req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span clas…
40523 … class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<spa…
40524 client.<span class="hljs-title function_">close</span>();
40526 req.<span class="hljs-title function_">end</span>();</code></pre>
40544 <p>User code will not create <code>Http2Session</code> instances directly. Server-side
40546 new HTTP/2 connection is received. Client-side <code>Http2Session</code> instances are
40581 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
40590 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40591 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40592 <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
40608 <li><code>errorCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
40609 <li><code>lastStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
40628 …ass="language-js">session.<span class="hljs-title function_">settings</span>({ <span class="hljs-a…
40630 …an class="hljs-title function_">on</span>(<span class="hljs-string">'localSettings'</span>, <span …
40631 <span class="hljs-comment">/* Use the new settings */</span>
40638 …buffer_class_buffer" class="type">&#x3C;Buffer></a> The <code>PING</code> frame 8-byte payload</li>
40651-js">session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'remoteSetting…
40652 <span class="hljs-comment">/* Use the new settings */</span>
40661 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
40662 <li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere…
40666 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40667 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
40668 …<span class="hljs-keyword">const</span> method = headers[<span class="hljs-string">':method'</span…
40669 <span class="hljs-keyword">const</span> path = headers[<span class="hljs-string">':path'</span>];
40670 <span class="hljs-comment">// ...</span>
40671 stream.<span class="hljs-title function_">respond</span>({
40672 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
40673 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=ut…
40675 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
40676 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
40682 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40684 <span class="hljs-comment">// Create an unencrypted HTTP/2 server</span>
40685 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
40687 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
40688 stream.<span class="hljs-title function_">respond</span>({
40689 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf
40690 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
40692-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">…
40693 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello Wor…
40696 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">80</span>);</code…
40708 <pre><code class="language-js">session.<span class="hljs-built_in">setTimeout</span>(<span class="h…
40709 …lass="hljs-title function_">on</span>(<span class="hljs-string">'timeout'</span>, <span class="hlj…
40715 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
40726 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
40739 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
40748 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
40758 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
40760 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40776 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
40785 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
40796 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40797 <li><code>lastStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
40798 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
40810 <p>A prototype-less object describing the current local settings of this
40817 …illa.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> |…
40828 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
40839 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
40840 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
40841 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
40854 acknowledgment was received, and a <code>Buffer</code> containing the 8-byte <code>PING</code>
40856-js">session.<span class="hljs-title function_">ping</span>(<span class="hljs-title class_">Buffer…
40857 <span class="hljs-keyword">if</span> (!err) {
40858 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
40859 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
40863 64-bit timestamp (little endian) marking the start of the <code>PING</code> duration.</p>
40877 <p>A prototype-less object describing the current remote settings of this
40884 <li><code>windowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
40889 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40891 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
40892 <span class="hljs-keyword">const</span> expectedWindowSize = <span class="hljs-number">2</span> ** …
40893 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'connect'</span>, <span cla…
40895 <span class="hljs-comment">// Set local window size to be 2 ** 20</span>
40896 session.<span class="hljs-title function_">setLocalWindowSize</span>(expectedWindowSize);
40903 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
40904 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
40930 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
40932 <li><code>effectiveLocalWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
40934 <li><code>effectiveRecvDataLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
40936 <li><code>nextStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
40938 <li><code>localWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
40940 <li><code>lastProcStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
40942 <li><code>remoteWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
40944 <li><code>outboundQueueSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
40946 <li><code>deflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
40948 <li><code>inflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
40960 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
40963 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> |…
40965 <li><code>duration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
40982 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
41006 <li><code>alt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
41008-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
41014 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41016 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41017 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'session'</span>, <span cla…
41018 <span class="hljs-comment">// Set altsvc for origin https://example.org:80</span>
41019 …session.<span class="hljs-title function_">altsvc</span>(<span class="hljs-string">'h2=":8000"'</s…
41022 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41023 <span class="hljs-comment">// Set altsvc for a specific stream</span>
41024 …ljs-property">session</span>.<span class="hljs-title function_">altsvc</span>(<span class="hljs-st…
41043 ASCII string containing a comma-delimited list of "alternative" protocols
41050 … href="https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#al…
41058-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#ur…
41064 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41065 <span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getSecureOptio…
41066 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSec…
41067 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41068 stream.<span class="hljs-title function_">respond</span>();
41069 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
41071 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'session'</span>, <span cla…
41072 …ssion.<span class="hljs-title function_">origin</span>(<span class="hljs-string">'https://example.…
41085 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41086 <span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getSecureOptio…
41087 options.<span class="hljs-property">origins</span> = [<span class="hljs-string">'https://example.co…
41088 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSec…
41089 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41090 stream.<span class="hljs-title function_">respond</span>();
41091 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
41105 <li><code>alt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
41106 <li><code>origin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41107 <li><code>streamId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
41113 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41114 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
41116 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'altsvc'</span>, <span clas…
41117 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41118 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41119 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41126 <li><code>origins</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
41132 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41133 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
41135 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'origin'</span>, <span clas…
41136 …ass="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-number"…
41137 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41149 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
41151 <li><code>endStream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
41154 <li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
41158 <li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41160 <li><code>weight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41163 <li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
41178 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41179 <span class="hljs-keyword">const</span> clientSession = http2.<span class="hljs-title function_">co…
41180 <span class="hljs-keyword">const</span> {
41183 } = http2.<span class="hljs-property">constants</span>;
41185 …ass="hljs-keyword">const</span> req = clientSession.<span class="hljs-title function_">request</sp…
41186 …span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span cla…
41187 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41188-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(…
41189 … class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-
41202 <p>The <code>:method</code> and <code>:path</code> pseudo-headers are not specified within <code>he…
41217 may have up to 2<sup>31</sup>-1 <code>Http2Stream</code> instances over its lifetime.</p>
41232 <p>The default text character encoding for all <code>Http2Stream</code>s is UTF-8. As a best
41234 the <code>'content-type'</code> header should be set and should identify the character
41236 <pre><code class="language-js">stream.<span class="hljs-title function_">respond</span>({
41237 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf
41238 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
41282 abnormally aborted in mid-communication.
41300 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
41309 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
41310 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
41311 <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
41340 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
41348 …ge-js">stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'trailers'</s…
41349 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41364 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41373 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
41382 …s://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3…
41384 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
41394 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41402 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41411 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41421 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
41430 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41439 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
41441 <li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
41445 <li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41447 <li><code>weight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41450 <li><code>silent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41461 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
41506 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
41507 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
41509 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41510 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
41511 <span class="hljs-keyword">const</span> { NGHTTP2_CANCEL } = http2.<span class="hljs-property">cons…
41512 …js-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span cl…
41514 <span class="hljs-comment">// Cancel the stream if there's no activity after 5 seconds</span>
41515 …class="hljs-built_in">setTimeout</span>(<span class="hljs-number">5000</span>, <span class="hljs-f…
41523 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
41525 <li><code>localWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
41527 …eloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;numbe…
41529 <li><code>localClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
41530 <li><code>remoteClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
41532 <li><code>sumDependencyWeight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
41535 <li><code>weight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41553 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41554 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41555 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41556 …ljs-title function_">respond</span>(<span class="hljs-literal">undefined</span>, { <span class="hl…
41557 …stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span…
41558 …stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">xyz</span>…
41560 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Hello World'</span…
41562 <p>The HTTP/1 specification forbids trailers from containing HTTP/2 pseudo-header
41580 the request contained <code>Expect: 100-continue</code>. This is an instruction that
41590 …ge-js">stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'headers'</sp…
41591 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41600 …uage-js">stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'push'</spa…
41601 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41611 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41612 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
41613 …js-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span cl…
41614 …span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span cla…
41615 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41641 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41643 <p>True if headers were sent, false otherwise (read-only).</p>
41649 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41651 <p>Read-only property mapped to the <code>SETTINGS_ENABLE_PUSH</code> flag of the remote
41661 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
41663 <li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
41667 <li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41671 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
41674 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
41684 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41685 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41686 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41687 …stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</sp…
41688-title function_">pushStream</span>({ <span class="hljs-string">':path'</span>: <span class="hljs-
41689 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
41690 …pushStream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'…
41691 …pushStream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some pushed da…
41693 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
41697 <code>true</code> to enable server-side bandwidth balancing between concurrent streams.</p>
41714 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
41716 <li><code>endStream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
41718 <li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
41723 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41724 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41725 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41726 …stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</sp…
41727 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
41737 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41738 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41739 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41740-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-n…
41741 …stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span…
41742 …stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>…
41744 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
41763 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
41765 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
41767 <li><code>statCheck</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
41768 <li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
41770 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41771 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41781 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41782 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
41784 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41785 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41786 …ljs-keyword">const</span> fd = fs.<span class="hljs-title function_">openSync</span>(<span class="…
41788 …<span class="hljs-keyword">const</span> stat = fs.<span class="hljs-title function_">fstatSync</sp…
41789 <span class="hljs-keyword">const</span> headers = {
41790 <span class="hljs-string">'content-length'</span>: stat.<span class="hljs-property">size</span>,
41791 …<span class="hljs-string">'last-modified'</span>: stat.<span class="hljs-property">mtime</span>.<s…
41792 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=ut…
41794 stream.<span class="hljs-title function_">respondWithFD</span>(fd, headers);
41795 …lass="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-
41808 is not supported and may result in data loss. Re-using a file descriptor
41818 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41819 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
41821 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41822 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41823 …ljs-keyword">const</span> fd = fs.<span class="hljs-title function_">openSync</span>(<span class="…
41825 …<span class="hljs-keyword">const</span> stat = fs.<span class="hljs-title function_">fstatSync</sp…
41826 <span class="hljs-keyword">const</span> headers = {
41827 <span class="hljs-string">'content-length'</span>: stat.<span class="hljs-property">size</span>,
41828 …<span class="hljs-string">'last-modified'</span>: stat.<span class="hljs-property">mtime</span>.<s…
41829 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=ut…
41831 …span class="hljs-title function_">respondWithFD</span>(fd, headers, { <span class="hljs-attr">wait…
41832 …stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span…
41833 …stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>…
41836 …lass="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-
41853 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
41855 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
41857 <li><code>statCheck</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
41858 <li><code>onError</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
41860 <li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
41862 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41863 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41879 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41880 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41881 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41882 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">statCheck</span>(<sp…
41883 …headers[<span class="hljs-string">'last-modified'</span>] = stat.<span class="hljs-property">mtime…
41886 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">onError</span>(<span…
41887 …<span class="hljs-comment">// stream.respond() can throw if the stream has been destroyed by</span>
41888 <span class="hljs-comment">// the other side.</span>
41889 <span class="hljs-keyword">try</span> {
41890 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class=…
41891 …stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</sp…
41892 } <span class="hljs-keyword">else</span> {
41893 …stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</sp…
41895 } <span class="hljs-keyword">catch</span> (err) {
41896 <span class="hljs-comment">// Perform actual error handling.</span>
41897 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41899 stream.<span class="hljs-title function_">end</span>();
41902 …stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/…
41903 …{ <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=…
41910 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41911 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41912 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41913 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">statCheck</span>(<sp…
41914 <span class="hljs-comment">// Check the stat here...</span>
41915 …stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</sp…
41916 …<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; <span class="hlj…
41918 …stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/…
41919 …{ <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=…
41922 <p>The <code>content-length</code> header field will be automatically set.</p>
41937 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41938 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41939 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41940 …stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/…
41941 …{ <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=…
41942 … { <span class="hljs-attr">waitForTrailers</span>: <span class="hljs-literal">true</span> });
41943 …stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span…
41944 …stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>…
41967 a request with an HTTP <code>Expect: 100-continue</code> is received. If this event is
42017 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
42018 <li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere…
42024 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42025 <span class="hljs-keyword">const</span> {
42030 } = http2.<span class="hljs-property">constants</span>;
42032 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
42033 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42034 <span class="hljs-keyword">const</span> method = headers[HTTP2_HEADER_METHOD];
42035 <span class="hljs-keyword">const</span> path = headers[HTTP2_HEADER_PATH];
42036 <span class="hljs-comment">// ...</span>
42037 stream.<span class="hljs-title function_">respond</span>({
42038 [HTTP2_HEADER_STATUS]: <span class="hljs-number">200</span>,
42039 [HTTP2_HEADER_CONTENT_TYPE]: <span class="hljs-string">'text/plain; charset=utf-8'</span>
42041 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
42042 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
42064 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
42086 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
42087 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
42109 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
42146 time a request with an HTTP <code>Expect: 100-continue</code> is received. If this event
42196 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
42197 <li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere…
42203 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42204 <span class="hljs-keyword">const</span> {
42209 } = http2.<span class="hljs-property">constants</span>;
42211 <span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getOptionsSome…
42213 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSec…
42214 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42215 <span class="hljs-keyword">const</span> method = headers[HTTP2_HEADER_METHOD];
42216 <span class="hljs-keyword">const</span> path = headers[HTTP2_HEADER_PATH];
42217 <span class="hljs-comment">// ...</span>
42218 stream.<span class="hljs-title function_">respond</span>({
42219 [HTTP2_HEADER_STATUS]: <span class="hljs-number">200</span>,
42220 [HTTP2_HEADER_CONTENT_TYPE]: <span class="hljs-string">'text/plain; charset=utf-8'</span>
42222 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
42223 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
42247 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
42261 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
42262 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
42284 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
42330 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
42332 <li><code>maxDeflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/J…
42334 <li><code>maxSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
42336 <li><code>maxSessionMemory</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42345 <li><code>maxHeaderListPairs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
42349 <li><code>maxOutstandingPings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
42351 <li><code>maxSendHeaderBlockLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42355 <li><code>paddingStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42363 padding to ensure that the total frame length, including the 9-byte
42371 <li><code>peerMaxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42375 <li><code>maxSessionInvalidFrames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
42378 <li><code>maxSessionRejectedStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Ja…
42400 <li><code>unknownProtocolTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
42407 <li><code>onRequestHandler</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
42413 <a href="https://http2.github.io/faq/#does-http2-require-encryption">unencrypted HTTP/2</a>, the us…
42416 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42418 <span class="hljs-comment">// Create an unencrypted HTTP/2 server.</span>
42419 <span class="hljs-comment">// Since there are no browsers known that support</span>
42420 <span class="hljs-comment">// unencrypted HTTP/2, the use of `http2.createSecureServer()`</span>
42421 <span class="hljs-comment">// is necessary when communicating with browser clients.</span>
42422 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
42424 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42425 stream.<span class="hljs-title function_">respond</span>({
42426 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf
42427 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
42429 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello Wor…
42432 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">80</span>);</code…
42460 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
42462 <li><code>allowHTTP1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
42466 <li><code>maxDeflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/J…
42468 <li><code>maxSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
42470 <li><code>maxSessionMemory</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42479 <li><code>maxHeaderListPairs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
42483 <li><code>maxOutstandingPings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
42485 <li><code>maxSendHeaderBlockLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42489 <li><code>paddingStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42498 9-byte header, is a multiple of 8. For each frame, there is a maximum
42505 <li><code>peerMaxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42509 <li><code>maxSessionInvalidFrames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
42512 <li><code>maxSessionRejectedStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Ja…
42522 <li><code>origins</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
42524 <li><code>unknownProtocolTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
42530 <li><code>onRequestHandler</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
42535 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42536 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
42538 <span class="hljs-keyword">const</span> options = {
42539 …<span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<spa…
42540 …<span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<sp…
42543 <span class="hljs-comment">// Create a secure HTTP/2 server</span>
42544 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSec…
42546 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42547 stream.<span class="hljs-title function_">respond</span>({
42548 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf
42549 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
42551 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello Wor…
42554 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">80</span>);</code…
42576 <li><code>authority</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
42578 prefix, host name, and IP port (if a non-default port is used). Userinfo
42581 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
42583 <li><code>maxDeflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/J…
42585 <li><code>maxSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
42587 <li><code>maxSessionMemory</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42596 <li><code>maxHeaderListPairs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
42600 <li><code>maxOutstandingPings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
42602 <li><code>maxReservedRemoteStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42606 is 0. The maximum allowed value is 22<sup>32</sup>-1. A negative value sets
42608 <li><code>maxSendHeaderBlockLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42612 <li><code>paddingStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42621 9-byte header, is a multiple of 8. For each frame, there is a maximum
42628 <li><code>peerMaxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42632 <li><code>protocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
42637 <li><code>createConnection</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
42641 <li><code>unknownProtocolTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
42647 …r.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Func…
42652 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42653 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
42655 <span class="hljs-comment">/* Use the client */</span>
42657 client.<span class="hljs-title function_">close</span>();</code></pre>
42766 for use with the <code>HTTP2-Settings</code> header field.</p>
42767 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42769-keyword">const</span> packed = http2.<span class="hljs-title function_">getPackedSettings</span>(…
42771-variable language_">console</span>.<span class="hljs-title function_">log</span>(packed.<span cla…
42772 <span class="hljs-comment">// Prints: AAIAAAAA</span></code></pre>
42778 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
42788 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" c…
42794 <p>Headers are represented as own-properties on JavaScript objects. The property
42795 keys will be serialized to lower-case. Property values should be strings (if
42798 <pre><code class="language-js"><span class="hljs-keyword">const</span> headers = {
42799 <span class="hljs-string">':status'</span>: <span class="hljs-string">'200'</span>,
42800 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text-plain'</span>,
42801-string">'ABC'</span>: [<span class="hljs-string">'has'</span>, <span class="hljs-string">'more'</…
42804 stream.<span class="hljs-title function_">respond</span>(headers);</code></pre>
42813 …code>, <code>age</code>, <code>authorization</code>, <code>access-control-allow-credentials</code>,
42814 <code>access-control-max-age</code>, <code>access-control-request-method</code>, <code>content-enco…
42815 <code>content-language</code>, <code>content-length</code>, <code>content-location</code>, <code>co…
42816 <code>content-range</code>, <code>content-type</code>, <code>date</code>, <code>dnt</code>, <code>e…
42817 <code>if-match</code>, <code>if-modified-since</code>, <code>if-none-match</code>, <code>if-range</…
42818 <code>if-unmodified-since</code>, <code>last-modified</code>, <code>location</code>, <code>max-forw…
42819 <code>proxy-authorization</code>, <code>range</code>, <code>referer</code>,<code>retry-after</code>…
42820 <code>upgrade-insecure-requests</code>, <code>user-agent</code> or <code>x-content-type-options</co…
42822 <li><code>set-cookie</code> is always an array. Duplicates are added to the array.</li>
42826 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42827 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
42828 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42829 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
42830 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
42838 <pre><code class="language-js"><span class="hljs-keyword">const</span> headers = {
42839 <span class="hljs-string">':status'</span>: <span class="hljs-string">'200'</span>,
42840 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text-plain'</span>,
42841 <span class="hljs-string">'cookie'</span>: <span class="hljs-string">'some-cookie'</span>,
42842 …<span class="hljs-string">'other-sensitive-header'</span>: <span class="hljs-string">'very secret …
42843 …an class="hljs-property">sensitiveHeaders</span>]: [<span class="hljs-string">'cookie'</span>, <sp…
42846 stream.<span class="hljs-title function_">respond</span>(headers);</code></pre>
42873 <li><code>headerTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42875 is 22<sup>32</sup>-1. <strong>Default:</strong> <code>4096</code>.</li>
42876 <li><code>enablePush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
42878 <li><code>initialWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
42879 bytes for stream-level flow control. The minimum allowed value is 0. The
42880 maximum allowed value is 2<sup>32</sup>-1. <strong>Default:</strong> <code>65535</code>.</li>
42881 <li><code>maxFrameSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
42883 22<sup>24</sup>-1. <strong>Default:</strong> <code>16384</code>.</li>
42884 <li><code>maxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
42886 implies, at least theoretically, 2<sup>32</sup>-1 streams may be open
42888 is 0. The maximum allowed value is 2<sup>32</sup>-1. <strong>Default:</strong>
42890 <li><code>maxHeaderListSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
42892 maximum allowed value is 22<sup>32</sup>-1. <strong>Default:</strong> <code>65535</code>.</li>
42893 <li><code>maxHeaderSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
42894 <li><code>enableConnectProtocol</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
42920 <p>Header field names are <em>case-insensitive</em> and are transmitted over the wire
42921 strictly as lower-case strings. The API provided by Node.js allows header
42922 names to be set as mixed-case strings (e.g. <code>Content-Type</code>) but will convert
42923 those to lower-case (e.g. <code>content-type</code>) upon transmission.</p>
42924 <p>Header field-names <em>must only</em> contain one or more of the following ASCII
42925 characters: <code>a</code>-<code>z</code>, <code>A</code>-<code>Z</code>, <code>0</code>-<code>9</c…
42926 <code>-</code>, <code>.</code>, <code>^</code>, <code>_</code>, <code>`</code> (backtick), <code>|<…
42930 new-line or carriage return characters and <em>should</em> be limited to US-ASCII
42935 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42937 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
42939 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42940 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'push'</span>, <span class…
42941 <span class="hljs-comment">// Process response headers</span>
42943-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(…
42946 …js-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span cl…
42951 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
42953 …s="hljs-keyword">const</span> server = net.<span class="hljs-title function_">createServer</span>(…
42954 <span class="hljs-keyword">let</span> name = <span class="hljs-string">''</span>;
42955 …socket.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</spa…
42956 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
42957-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">()…
42960 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</co…
42962 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42963 <span class="hljs-keyword">const</span> { NGHTTP2_REFUSED_STREAM } = http2.<span class="hljs-proper…
42964 <span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span clas…
42966 <span class="hljs-keyword">const</span> proxy = http2.<span class="hljs-title function_">createServ…
42967 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42968 …<span class="hljs-keyword">if</span> (headers[<span class="hljs-string">':method'</span>] !== <spa…
42969 <span class="hljs-comment">// Only accept CONNECT requests</span>
42970 stream.<span class="hljs-title function_">close</span>(NGHTTP2_REFUSED_STREAM);
42971 <span class="hljs-keyword">return</span>;
42973-keyword">const</span> auth = <span class="hljs-keyword">new</span> <span class="hljs-title functi…
42974 <span class="hljs-comment">// It's a very good idea to verify that hostname and port are</span>
42975 <span class="hljs-comment">// things this proxy should be connecting to.</span>
42976-keyword">const</span> socket = net.<span class="hljs-title function_">connect</span>(auth.<span c…
42977 stream.<span class="hljs-title function_">respond</span>();
42978 socket.<span class="hljs-title function_">pipe</span>(stream);
42979 stream.<span class="hljs-title function_">pipe</span>(socket);
42981 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
42982 …stream.<span class="hljs-title function_">close</span>(http2.<span class="hljs-property">constants…
42986 proxy.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8001</span>);</cod…
42988 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42990 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
42992 <span class="hljs-comment">// Must not specify the ':path' and ':scheme' headers</span>
42993 <span class="hljs-comment">// for CONNECT requests or an error will be thrown.</span>
42994 <span class="hljs-keyword">const</span> req = client.<span class="hljs-title function_">request</sp…
42995 <span class="hljs-string">':method'</span>: <span class="hljs-string">'CONNECT'</span>,
42996 …<span class="hljs-string">':authority'</span>: <span class="hljs-string">`localhost:<span class="h…
42999 …span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span cla…
43000-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[http2.<s…
43002 <span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
43003 req.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
43004 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
43005 req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span clas…
43006 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class=…
43007 client.<span class="hljs-title function_">close</span>();
43009 req.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Jane'</span>);</code><…
43016 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
43017 <span class="hljs-keyword">const</span> settings = { <span class="hljs-attr">enableConnectProtocol<…
43018 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
43021 <code>':protocol'</code> HTTP/2 pseudo-header:</p>
43022 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
43023 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
43024 …n class="hljs-title function_">on</span>(<span class="hljs-string">'remoteSettings'</span>, <span …
43025 …<span class="hljs-keyword">if</span> (settings.<span class="hljs-property">enableConnectProtocol</…
43026-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span clas…
43027 <span class="hljs-comment">// ...</span>
43039 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
43040 …="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>…
43041 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</…
43042 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, …
43043-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-strin…
43044 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
43048 Upgrading from non-tls HTTP/1 servers is not supported.</p>
43060 …s="language-js"><span class="hljs-keyword">const</span> { createSecureServer } = <span class="hljs
43061 <span class="hljs-keyword">const</span> { readFileSync } = <span class="hljs-built_in">require</spa…
43063 <span class="hljs-keyword">const</span> cert = <span class="hljs-title function_">readFileSync</spa…
43064 <span class="hljs-keyword">const</span> key = <span class="hljs-title function_">readFileSync</span…
43066 <span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createSecureSer…
43067 { cert, key, <span class="hljs-attr">allowHTTP1</span>: <span class="hljs-literal">true</span> },
43069 ).<span class="hljs-title function_">listen</span>(<span class="hljs-number">4443</span>);
43071 <span class="hljs-keyword">function</span> <span class="hljs-title function_">onRequest</span>(<spa…
43072 <span class="hljs-comment">// Detects if it is a HTTPS request or HTTP/2</span>
43073-keyword">const</span> { <span class="hljs-attr">socket</span>: { alpnProtocol } } = req.<span cla…
43074 req.<span class="hljs-property">stream</span>.<span class="hljs-property">session</span> : req;
43075 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
43076 …res.<span class="hljs-title function_">end</span>(<span class="hljs-variable constant_">JSON</span…
43078 <span class="hljs-attr">httpVersion</span>: req.<span class="hljs-property">httpVersion</span>
43099 abnormally aborted in mid-communication.</p>
43113 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
43122 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43131 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
43139 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
43149 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
43160 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
43163 <p>Key-value pairs of header names and values. Header names are lower-cased.</p>
43164 <pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
43165 <span class="hljs-comment">//</span>
43166 <span class="hljs-comment">// { 'user-agent': 'curl/7.22.0',</span>
43167 <span class="hljs-comment">// host: '127.0.0.1:8000',</span>
43168 <span class="hljs-comment">// accept: '*/*' }</span>
43169 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
43176 <pre><code class="language-js"><span class="hljs-title function_">removeAllHeaders</span>(request.<…
43177 <span class="hljs-title function_">assert</span>(request.<span class="hljs-property">url</span>); …
43183 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43186 client response, the HTTP version of the connected-to server. Returns
43195 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43197 <p>The request method as a string. Read-only. Examples: <code>'GET'</code>, <code>'DELETE'</code>.<…
43203 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43207 list of tuples. So, the even-numbered offsets are key values, and the
43208 odd-numbered offsets are the associated values.</p>
43210 <pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
43211 <span class="hljs-comment">//</span>
43212 <span class="hljs-comment">// [ 'user-agent',</span>
43213 <span class="hljs-comment">// 'this is invalid because there can be only one',</span>
43214 <span class="hljs-comment">// 'User-Agent',</span>
43215 <span class="hljs-comment">// 'curl/7.22.0',</span>
43216 <span class="hljs-comment">// 'Host',</span>
43217 <span class="hljs-comment">// '127.0.0.1:8000',</span>
43218 <span class="hljs-comment">// 'ACCEPT',</span>
43219 <span class="hljs-comment">// '*/*' ]</span>
43220 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
43226 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43235 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43244 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
43245 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43288 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
43296 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43300 …ode class="language-http"><span class="hljs-keyword">GET</span> <span class="hljs-string">/status?…
43301 <span class="hljs-attribute">Accept</span><span class="hljs-punctuation">: </span>text/plain</code>…
43303 <!-- eslint-disable semi -->
43304 <pre><code class="language-js"><span class="hljs-string">'/status?name=ryan'</span></code></pre>
43306 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
43307 …an class="hljs-meta">> </span><span class="language-bash">new URL(<span class="hljs-string">'/stat…
43351 <li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
43361 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
43372 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43377 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
43378 …2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a> The newly-created <code>Http2Se…
43400-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
43401 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
43402 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43403 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
43416 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
43418 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
43427 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
43428 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
43431 The name is case-insensitive.</p>
43432-js"><span class="hljs-keyword">const</span> contentType = response.<span class="hljs-title functi…
43438 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
43442 …lass="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-
43443 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class…
43445 <span class="hljs-keyword">const</span> headerNames = response.<span class="hljs-title function_">g…
43446 <span class="hljs-comment">// headerNames === ['foo', 'set-cookie']</span></code></pre>
43452 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
43456 header-related http module methods. The keys of the returned object are the
43463 …lass="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-
43464 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class…
43466 <span class="hljs-keyword">const</span> headers = response.<span class="hljs-title function_">getHe…
43467 <span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</spa…
43473 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
43474 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
43477 outgoing headers. The header name matching is case-insensitive.</p>
43478-js"><span class="hljs-keyword">const</span> hasContentType = response.<span class="hljs-title fun…
43484 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
43486 <p>True if headers were sent, false otherwise (read-only).</p>
43492 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
43495 …re><code class="language-js">response.<span class="hljs-title function_">removeHeader</span>(<span…
43501 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
43512 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
43513 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
43516 in the to-be-sent headers, its value will be replaced. Use an array of strings
43518-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Conte…
43520-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-C…
43526 <pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</spa…
43527 …="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>…
43528 …ass="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span…
43529 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, …
43530-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-strin…
43531 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
43538 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
43539 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43567 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
43568 …="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>…
43569 …<span class="hljs-keyword">const</span> ip = req.<span class="hljs-property">socket</span>.<span c…
43570 …<span class="hljs-keyword">const</span> port = req.<span class="hljs-property">socket</span>.<span…
43571-title function_">end</span>(<span class="hljs-string">`Your IP address is <span class="hljs-subst…
43572 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code><…
43578 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
43583 <pre><code class="language-js">response.<span class="hljs-property">statusCode</span> = <span class…
43591 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43608 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
43618-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
43619 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
43620 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43621 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
43634 <p>This is the raw HTTP body and has nothing to do with higher-level multi-part
43664 <li><code>statusCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
43665 <li><code>statusMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
43666 <li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
43669 <p>Sends a response header to the request. The status code is a 3-digit HTTP
43672 <p>For compatibility with <a href="http.html">HTTP/1</a>, a human-readable <code>statusMessage</cod…
43676 <pre><code class="language-js"><span class="hljs-keyword">const</span> body = <span class="hljs-str…
43677 response.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, {
43678 …<span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<…
43679 …<span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain; charset=ut…
43681 <p><code>Content-Length</code> is given in bytes not characters. The
43683 given encoding. On outbound messages, Node.js does not check if Content-Length
43686 <code>Content-Length</code> does not match the actual payload size.</p>
43694 <pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</spa…
43695 …="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>…
43696 …ass="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span…
43697 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, …
43698-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-strin…
43699 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
43706-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">PerformanceObserver…
43708-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
43709 …<span class="hljs-keyword">const</span> entry = items.<span class="hljs-title function_">getEntrie…
43710-variable language_">console</span>.<span class="hljs-title function_">log</span>(entry.<span clas…
43711 …<span class="hljs-keyword">if</span> (entry.<span class="hljs-property">name</span> === <span clas…
43712 <span class="hljs-comment">// Entry contains statistics about the Http2Session</span>
43713 … class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (entry.<span class="hljs-pr…
43714 <span class="hljs-comment">// Entry contains statistics about the Http2Stream</span>
43717 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: …
43724 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
43726 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
43728 <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
43729 <li><code>timeToFirstByte</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
43731 <li><code>timeToFirstByteSent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
43733 <li><code>timeToFirstHeader</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
43739 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
43740 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
43741 <li><code>framesReceived</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
43743 <li><code>framesSent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
43744 <li><code>maxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
43746 <li><code>pingRTT</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
43749 <li><code>streamAverageDuration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
43751 <li><code>streamCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
43753 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
43759 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
43790 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
43794 <p><code>maxCachedSessions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
43798 <p><code>servername</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
43825 <pre><code class="language-js"><span class="hljs-comment">// ...</span>
43826-property">globalAgent</span>.<span class="hljs-title function_">on</span>(<span class="hljs-strin…
43827-title function_">appendFileSync</span>(<span class="hljs-string">'/tmp/ssl-keys.log'</span>, line…
43842 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43851 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
43859 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
43867 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
43875 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
43876 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43893 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
43901 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
43909 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
43911 <li><code>requestListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
43914 <pre><code class="language-js"><span class="hljs-comment">// curl -k https://localhost:8000/</span>
43915 <span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span cl…
43916 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
43918 <span class="hljs-keyword">const</span> options = {
43919 …class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span clas…
43920 …class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span cla…
43923 https.<span class="hljs-title function_">createServer</span>(options, <span class="hljs-function">(…
43924 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>);
43925 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>…
43926 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code><…
43928 …ode class="language-js"><span class="hljs-keyword">const</span> https = <span class="hljs-built_in…
43929 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
43931 <span class="hljs-keyword">const</span> options = {
43932 …<span class="hljs-attr">pfx</span>: fs.<span class="hljs-title function_">readFileSync</span>(<spa…
43933 <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'sample'</span>
43936 https.<span class="hljs-title function_">createServer</span>(options, <span class="hljs-function">(…
43937 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>);
43938 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>…
43939 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code><…
43956 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
43957 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
43959 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43965 …ode class="language-js"><span class="hljs-keyword">const</span> https = <span class="hljs-built_in…
43967 …"hljs-title function_">get</span>(<span class="hljs-string">'https://encrypted.google.com/'</span>…
43968-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
43969 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
43971 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
43972 …process.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">write</span>(…
43975 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
43976 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
44005 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
44006 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
44014 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
44029 …ode class="language-js"><span class="hljs-keyword">const</span> https = <span class="hljs-built_in…
44031 <span class="hljs-keyword">const</span> options = {
44032 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'encrypted.google.com'</span>,
44033 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>,
44034 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
44035 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>
44038 …="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options…
44039-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44040 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
44042 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
44043 …process.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">write</span>(…
44047 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
44048 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
44050 req.<span class="hljs-title function_">end</span>();</code></pre>
44052 <pre><code class="language-js"><span class="hljs-keyword">const</span> options = {
44053 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'encrypted.google.com'</span>,
44054 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>,
44055 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
44056 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>,
44057 …class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span clas…
44058 …class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span cla…
44060 options.<span class="hljs-property">agent</span> = <span class="hljs-keyword">new</span> https.<spa…
44062 …="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options…
44063 <span class="hljs-comment">// ...</span>
44066 <pre><code class="language-js"><span class="hljs-keyword">const</span> options = {
44067 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'encrypted.google.com'</span>,
44068 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>,
44069 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
44070 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>,
44071 …class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span clas…
44072 …class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span cla…
44073 <span class="hljs-attr">agent</span>: <span class="hljs-literal">false</span>
44076 …="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options…
44077 <span class="hljs-comment">// ...</span>
44080-js"><span class="hljs-keyword">const</span> options = <span class="hljs-keyword">new</span> <span…
44082 …="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options…
44083 <span class="hljs-comment">// ...</span>
44086 <code>pin-sha256</code>):</p>
44087 …code class="language-js"><span class="hljs-keyword">const</span> tls = <span class="hljs-built_in"…
44088 <span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span cl…
44089 <span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span c…
44091 <span class="hljs-keyword">function</span> <span class="hljs-title function_">sha256</span>(<span c…
44092-keyword">return</span> crypto.<span class="hljs-title function_">createHash</span>(<span class="h…
44094 <span class="hljs-keyword">const</span> options = {
44095 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'github.com'</span>,
44096 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>,
44097 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
44098 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>,
44099 …<span class="hljs-attr">checkServerIdentity</span>: <span class="hljs-keyword">function</span>(<sp…
44100 …<span class="hljs-comment">// Make sure the certificate is issued to the host we are connected to<…
44101 …<span class="hljs-keyword">const</span> err = tls.<span class="hljs-title function_">checkServerId…
44102 <span class="hljs-keyword">if</span> (err) {
44103 <span class="hljs-keyword">return</span> err;
44106 <span class="hljs-comment">// Pin the public key, similar to HPKP pin-sha25 pinning</span>
44107 …<span class="hljs-keyword">const</span> pubkey256 = <span class="hljs-string">'pL1+qb9HTMRZJmuC/bB…
44108 …<span class="hljs-keyword">if</span> (<span class="hljs-title function_">sha256</span>(cert.<span …
44109 …<span class="hljs-keyword">const</span> msg = <span class="hljs-string">'Certificate verification …
44110 …<span class="hljs-string">`The public key of '<span class="hljs-subst">${cert.subject.CN}</span>' …
44111 <span class="hljs-string">'does not match our pinned fingerprint'</span>;
44112 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-t…
44115 <span class="hljs-comment">// Pin the exact certificate, rather than the pub key</span>
44116 …<span class="hljs-keyword">const</span> cert256 = <span class="hljs-string">'25:FE:39:32:D9:63:8C:…
44117 <span class="hljs-string">'D8:3E:4C:1D:98:DB:71:E4:1A:48:03:98:EA:22:6A:BD:8B:93:16'</span>;
44118 …<span class="hljs-keyword">if</span> (cert.<span class="hljs-property">fingerprint256</span> !== c…
44119 …<span class="hljs-keyword">const</span> msg = <span class="hljs-string">'Certificate verification …
44120 …<span class="hljs-string">`The certificate of '<span class="hljs-subst">${cert.subject.CN}</span>'…
44121 <span class="hljs-string">'does not match our pinned fingerprint'</span>;
44122 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-t…
44125 <span class="hljs-comment">// This loop is informational only.</span>
44126 …<span class="hljs-comment">// Print the certificate and public key fingerprints of all certs in th…
44127 …<span class="hljs-comment">// chain. Its common to pin the public key of the issuer on the public<…
44128 …<span class="hljs-comment">// internet, while pinning the public key of the service in sensitive</…
44129 <span class="hljs-comment">// environments.</span>
44130 <span class="hljs-keyword">do</span> {
44131-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44132-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44134 …hash = crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha2…
44135-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44137 lastprint256 = cert.<span class="hljs-property">fingerprint256</span>;
44138 cert = cert.<span class="hljs-property">issuerCertificate</span>;
44139 …} <span class="hljs-keyword">while</span> (cert.<span class="hljs-property">fingerprint256</span> …
44144 options.<span class="hljs-property">agent</span> = <span class="hljs-keyword">new</span> https.<spa…
44145 …="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options…
44146 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
44147-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44148 <span class="hljs-comment">// Print the HPKP values</span>
44149-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44151 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
44154 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
44155 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
44157 req.<span class="hljs-title function_">end</span>();</code></pre>
44159 <pre><code class="language-text">Subject Common Name: github.com
44161 Public key ping-sha256: pL1+qb9HTMRZJmuC/bB/ZI9d302BYrrqiVuRyW+DGrU=
44164 Public key ping-sha256: RRM1dGqnDFsCJXBTHky16vi1obOlCgFFn/yOhI/y+ho=
44167 Public key ping-sha256: WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18=
44170-age=0; pin-sha256="WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18="; pin-sha256="RRM1dGqnDFsCJXBTHky…
44174 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
44178 …e class="language-js"><span class="hljs-keyword">const</span> inspector = <span class="hljs-built_…
44183 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
44185-js"><span class="hljs-built_in">require</span>(<span class="hljs-string">'inspector'</span>).<spa…
44190 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
44192 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
44194 <li><code>wait</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
44197 <p>Activate inspector on host and port. Equivalent to <code>node --inspect=[[host:]port]</code>, bu…
44205 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
44208 …e><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node…
44209 Debugger listening on ws://127.0.0.1:9229/166e272e-7a30-4d09-97ce-f1c012b43c34
44211 ws://127.0.0.1:9229/166e272e-7a30-4d09-97ce-f1c012b43c34
44212 <span class="hljs-meta">
44213 $ </span><span class="language-bash">node --inspect=localhost:3000 -p <span class="hljs-string">'in…
44214 Debugger listening on ws://localhost:3000/51cf8d0e-3c36-4c59-8efd-54519839e56a
44216 ws://localhost:3000/51cf8d0e-3c36-4c59-8efd-54519839e56a
44217 <span class="hljs-meta">
44218 $ </span><span class="language-bash">node -p <span class="hljs-string">'inspector.url()'</span></sp…
44232 back-end and receiving message responses and notifications.</p>
44245 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
44248-js">session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'inspectorNoti…
44249 <span class="hljs-comment">// Debugger.paused</span>
44250 <span class="hljs-comment">// Debugger.resumed</span></code></pre>
44252 <h4>Event: <code>&#x3C;inspector-protocol-method></code>;<span><a class="mark" href="#inspector_eve…
44257 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
44260 to the <code>&#x3C;inspector-protocol-method></code> value.</p>
44261 …ener on the <a href="https://chromedevtools.github.io/devtools-protocol/v8/Debugger#event-paused">…
44264-js">session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'Debugger.paus…
44265 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
44267 <span class="hljs-comment">// [ '/the/file/that/has/the/breakpoint.js:11:0' ]</span></code></pre>
44272 <p>Connects a session to the inspector back-end.</p>
44277 <p>Connects a session to the main thread inspector back-end. An exception will
44292 <li><code>method</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
44293 <li><code>params</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
44294 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
44296 <p>Posts a message to the inspector back-end. <code>callback</code> will be notified when
44298 arguments: error and message-specific result.</p>
44299-js">session.<span class="hljs-title function_">post</span>(<span class="hljs-string">'Runtime.eva…
44300 …hljs-function">(<span class="hljs-params">error, { result }</span>) =></span> <span class="hljs-va…
44301 <span class="hljs-comment">// Output: { type: 'number', value: 4, description: '4' }</span></code><…
44303 <a href="https://chromedevtools.github.io/devtools-protocol/v8/">Chrome DevTools Protocol Viewer</a…
44307 to the run-time events.</p>
44312 <p>Here's an example showing how to use the <a href="https://chromedevtools.github.io/devtools-prot…
44313 …e class="language-js"><span class="hljs-keyword">const</span> inspector = <span class="hljs-built_…
44314 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
44315 <span class="hljs-keyword">const</span> session = <span class="hljs-keyword">new</span> inspector.<…
44316 session.<span class="hljs-title function_">connect</span>();
44318 session.<span class="hljs-title function_">post</span>(<span class="hljs-string">'Profiler.enable'<…
44319 …session.<span class="hljs-title function_">post</span>(<span class="hljs-string">'Profiler.start'<…
44320 <span class="hljs-comment">// Invoke business logic under measurement here...</span>
44322 <span class="hljs-comment">// some time later...</span>
44323 …n class="hljs-title function_">post</span>(<span class="hljs-string">'Profiler.stop'</span>, <span…
44324 <span class="hljs-comment">// Write profile to disk, upload, etc.</span>
44325 <span class="hljs-keyword">if</span> (!err) {
44326-title function_">writeFileSync</span>(<span class="hljs-string">'./profile.cpuprofile'</span>, <s…
44332 <p>Here's an example showing how to use the <a href="https://chromedevtools.github.io/devtools-prot…
44333 …e class="language-js"><span class="hljs-keyword">const</span> inspector = <span class="hljs-built_…
44334 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
44335 <span class="hljs-keyword">const</span> session = <span class="hljs-keyword">new</span> inspector.<…
44337-keyword">const</span> fd = fs.<span class="hljs-title function_">openSync</span>(<span class="hlj…
44339 session.<span class="hljs-title function_">connect</span>();
44341 …hljs-title function_">on</span>(<span class="hljs-string">'HeapProfiler.addHeapSnapshotChunk'</spa…
44342 …fs.<span class="hljs-title function_">writeSync</span>(fd, m.<span class="hljs-property">params</s…
44345-title function_">post</span>(<span class="hljs-string">'HeapProfiler.takeHeapSnapshot'</span>, <s…
44346 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
44347 session.<span class="hljs-title function_">disconnect</span>();
44348 fs.<span class="hljs-title function_">closeSync</span>(fd);
44357 <li>Locale-sensitive or Unicode-aware functions in the <a href="https://tc39.github.io/ecma262/">EC…
44360 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Strin…
44361 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Strin…
44362 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Strin…
44366 Specification</a> (aka ECMA-402):
44368 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl"…
44369 <li>Locale-sensitive methods like <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
44370 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLo…
44377 <li><a href="https://github.com/tc39/proposal-regexp-unicode-property-escapes"><code>RegExp</code> …
44380 <a href="http://site.icu-project.org/">International Components for Unicode (ICU)</a> to implement …
44390 <li><code>--with-intl=none</code>/<code>--without-intl</code></li>
44391 <li><code>--with-intl=system-icu</code></li>
44392 <li><code>--with-intl=small-icu</code></li>
44393 <li><code>--with-intl=full-icu</code> (default)</li>
44493-icu</code></th><th><code>small-icu</code></th><th><code>full-icu</code></th></tr></thead><tbody><…
44494 <p>The "(not locale-aware)" designation denotes that the function carries out its
44495 operation just like the non-<code>Locale</code> version of the function, if one
44501 <h4>Build with a pre-installed ICU (<code>system-icu</code>)<span><a class="mark" href="#intl_build…
44507 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/no…
44508 supported under <code>system-icu</code>. Features that require ICU locale data in
44509 addition, such as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global…
44512 <h4>Embed a limited set of ICU data (<code>small-icu</code>)<span><a class="mark" href="#intl_embed…
44517 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/no…
44518 supported under <code>small-icu</code>. Features that require ICU locale data in addition,
44519 such as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/D…
44520-js"><span class="hljs-keyword">const</span> january = <span class="hljs-keyword">new</span> <span…
44521-keyword">const</span> english = <span class="hljs-keyword">new</span> <span class="hljs-title cla…
44522-keyword">const</span> spanish = <span class="hljs-keyword">new</span> <span class="hljs-title cla…
44524 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
44525 <span class="hljs-comment">// Prints "January"</span>
44526 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
44527 <span class="hljs-comment">// Prints "M01" on small-icu</span>
44528 <span class="hljs-comment">// Should print "enero"</span></code></pre>
44531 <p>If the <code>small-icu</code> option is used, one can still provide additional locale data
44538 <pre><code class="language-bash">env NODE_ICU_DATA=/some/directory node</code></pre>
44541 <p>The <a href="#cli_icu_data_dir_file"><code>--icu-data-dir</code></a> CLI parameter:</p>
44542 <pre><code class="language-bash">node --icu-data-dir=/some/directory</code></pre>
44545 <p>(If both are specified, the <code>--icu-data-dir</code> CLI parameter takes precedence.)</p>
44550 Check <a href="http://userguide.icu-project.org/icudata">"ICU Data"</a> article in the ICU User Gui…
44552 <p>The <a href="https://www.npmjs.com/package/full-icu">full-icu</a> npm module can greatly simplif…
44554 appropriate data file. After installing the module through <code>npm i full-icu</code>,
44555 the data file will be available at <code>./node_modules/full-icu</code>. This path can be
44556 then passed either to <code>NODE_ICU_DATA</code> or <code>--icu-data-dir</code> as shown above to
44558 <h4>Embed the entire ICU (<code>full-icu</code>)<span><a class="mark" href="#intl_embed_the_entire_…
44562 the default behavior if no <code>--with-intl</code> flag is passed. The official binaries
44565 <p>To verify that ICU is enabled at all (<code>system-icu</code>, <code>small-icu</code>, or
44566 <code>full-icu</code>), simply checking the existence of <code>Intl</code> should suffice:</p>
44567-js"><span class="hljs-keyword">const</span> hasICU = <span class="hljs-keyword">typeof</span> <sp…
44570-js"><span class="hljs-keyword">const</span> hasICU = <span class="hljs-keyword">typeof</span> pro…
44571 <p>To check for support for a non-English locale (i.e. <code>full-icu</code> or
44572 <code>system-icu</code>), <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
44573 <pre><code class="language-js"><span class="hljs-keyword">const</span> hasFullICU = (<span class="h…
44574 <span class="hljs-keyword">try</span> {
44575 …lass="hljs-keyword">const</span> january = <span class="hljs-keyword">new</span> <span class="hljs
44576-keyword">const</span> spanish = <span class="hljs-keyword">new</span> <span class="hljs-title cla…
44577 …<span class="hljs-keyword">return</span> spanish.<span class="hljs-title function_">format</span>(…
44578 } <span class="hljs-keyword">catch</span> (err) {
44579 <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
44588 dedicated to ECMA-402.</li>
44593 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
44597 …ode class="language-js"><span class="hljs-keyword">const</span> circle = <span class="hljs-built_i…
44598-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44602 …de class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-variable consta…
44604-built_in">exports</span>.<span class="hljs-property">area</span> = <span class="hljs-function">(<…
44606-built_in">exports</span>.<span class="hljs-property">circumference</span> = <span class="hljs-fun…
44616-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">Square</span> = <span…
44617 …ss="hljs-keyword">const</span> mySquare = <span class="hljs-keyword">new</span> <span class="hljs-
44618 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hl…
44620 <pre><code class="language-js"><span class="hljs-comment">// Assigning to exports will not modify m…
44621 …s="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class…
44622 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">width</span>) {
44623 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">width</span> = width;
44626 <span class="hljs-title function_">area</span>(<span class="hljs-params"></span>) {
44627 …ss="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hlj…
44649 <code>/usr/lib/node/&#x3C;some-package>/&#x3C;some-version></code> hold the contents of a
44693 <p>Putting together all of the above, here is the high-level algorithm
44742 2. let I = count of PARTS - 1
44748 d. let I = I - 1
44756 …["node", "require"]) <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver…
44767 …`package.json` "exports", ["node", "require"]) <a href="esm.md#resolver-algorithm-specification">d…
44777 <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver</a>.
44808 <p>Additionally, on case-insensitive file systems or operating systems, different
44841 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
44842 <span class="hljs-built_in">exports</span>.<span class="hljs-property">done</span> = <span class="h…
44843 <span class="hljs-keyword">const</span> b = <span class="hljs-built_in">require</span>(<span class=…
44844-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44845 <span class="hljs-built_in">exports</span>.<span class="hljs-property">done</span> = <span class="h…
44846 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
44848 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
44849 <span class="hljs-built_in">exports</span>.<span class="hljs-property">done</span> = <span class="h…
44850 <span class="hljs-keyword">const</span> a = <span class="hljs-built_in">require</span>(<span class=…
44851-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44852 <span class="hljs-built_in">exports</span>.<span class="hljs-property">done</span> = <span class="h…
44853 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
44855 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
44856 <span class="hljs-keyword">const</span> a = <span class="hljs-built_in">require</span>(<span class=…
44857 <span class="hljs-keyword">const</span> b = <span class="hljs-built_in">require</span>(<span class=…
44858-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44866 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
44897 <p>It is convenient to organize programs and libraries into self-contained
44904-json"><span class="hljs-punctuation">{</span> <span class="hljs-attr">"name"</span> <span class="…
44905 …ss="hljs-attr">"main"</span> <span class="hljs-punctuation">:</span> <span class="hljs-string">"./…
44906 <p>If this was in a folder at <code>./some-library</code>, then
44907 <code>require('./some-library')</code> would attempt to load
44908 <code>./some-library/lib/some-library.js</code>.</p>
44914 example, then <code>require('./some-library')</code> would attempt to load:</p>
44916 <li><code>./some-library/index.js</code></li>
44917 <li><code>./some-library/index.node</code></li>
44921 <pre><code class="language-console">Error: Cannot find module 'some-library'</code></pre>
44945 <code>require('example-module/path/to/file')</code> would resolve <code>path/to/file</code>
44946 relative to where <code>example-module</code> is located. The suffixed path follows the
44950 <p>If the <code>NODE_PATH</code> environment variable is set to a colon-delimited list
44977-js">(<span class="hljs-keyword">function</span>(<span class="hljs-params"><span class="hljs-built…
44978 <span class="hljs-comment">// Module code actually lives in here</span>
44982 <li>It keeps top-level variables (defined with <code>var</code>, <code>const</code> or <code>let</c…
44984 <li>It helps to provide some global-looking variables that are actually specific
45001 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
45006 <pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hl…
45007 <span class="hljs-comment">// Prints: /Users/mjr</span>
45008 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45009 <span class="hljs-comment">// Prints: /Users/mjr</span></code></pre>
45016 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
45025 <pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hl…
45026 <span class="hljs-comment">// Prints: /Users/mjr/example.js</span>
45027 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45028 <span class="hljs-comment">// Prints: /Users/mjr</span></code></pre>
45044 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
45066 <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
45067 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
45076 <pre><code class="language-js"><span class="hljs-comment">// Importing a local module with a path r…
45077 <span class="hljs-comment">// working directory. (On Windows, this would resolve to .\path\myLocalM…
45078 <span class="hljs-keyword">const</span> myLocalModule = <span class="hljs-built_in">require</span>(…
45080 <span class="hljs-comment">// Importing a JSON file:</span>
45081 <span class="hljs-keyword">const</span> jsonData = <span class="hljs-built_in">require</span>(<span…
45083 <span class="hljs-comment">// Importing a module from node_modules or Node.js built-in module:</spa…
45084 <span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span c…
45090 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
45098 only <code>node:</code>-prefixed require calls are going to receive the native module.
45100 <!-- eslint-disable node-core/no-duplicate-requires -->
45101 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
45102 <span class="hljs-keyword">const</span> realFs = <span class="hljs-built_in">require</span>(<span c…
45104 <span class="hljs-keyword">const</span> fakeFs = {};
45105 …n class="hljs-built_in">require</span>.<span class="hljs-property">cache</span>.<span class="hljs-
45107 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-built_in">require</s…
45108 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-built_in">require</s…
45113 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
45115 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
45119-js"><span class="hljs-built_in">require</span>.<span class="hljs-property">extensions</span>[<spa…
45120 <p><strong>Deprecated.</strong> In the past, this list has been used to load non-JavaScript
45121 modules into Node.js by compiling them on-demand. However, in practice, there
45137-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
45138 <pre><code class="language-bash">node entry.js</code></pre>
45139 <!-- eslint-skip -->
45140 <pre><code class="language-js"><span class="hljs-title class_">Module</span> {
45141 <span class="hljs-attr">id</span>: <span class="hljs-string">'.'</span>,
45142 <span class="hljs-attr">path</span>: <span class="hljs-string">'/absolute/path/to'</span>,
45143 <span class="hljs-attr">exports</span>: {},
45144 <span class="hljs-attr">parent</span>: <span class="hljs-literal">null</span>,
45145 …<span class="hljs-attr">filename</span>: <span class="hljs-string">'/absolute/path/to/entry.js'</s…
45146 <span class="hljs-attr">loaded</span>: <span class="hljs-literal">false</span>,
45147 <span class="hljs-attr">children</span>: [],
45148 <span class="hljs-attr">paths</span>:
45149 [ <span class="hljs-string">'/absolute/path/to/node_modules'</span>,
45150 <span class="hljs-string">'/absolute/path/node_modules'</span>,
45151 <span class="hljs-string">'/absolute/node_modules'</span>,
45152 <span class="hljs-string">'/node_modules'</span> ] }</code></pre>
45166 <li><code>request</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
45167 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
45169 <li><code>paths</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
45177 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
45187 <li><code>request</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
45188 …illa.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> |…
45200 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
45204 also accessible via the <code>exports</code> module-global. <code>module</code> is not actually
45219 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
45227-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
45229 …ss="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span clas…
45231 <span class="hljs-comment">// Do some work, and after some time emit</span>
45232 <span class="hljs-comment">// the 'ready' event from the module itself.</span>
45233 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
45234 …"hljs-variable language_">module</span>.<span class="hljs-property">exports</span>.<span class="hl…
45235 }, <span class="hljs-number">1000</span>);</code></pre>
45237 …<code class="language-js"><span class="hljs-keyword">const</span> a = <span class="hljs-built_in">…
45238 a.<span class="hljs-title function_">on</span>(<span class="hljs-string">'ready'</span>, <span clas…
45239 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
45244 <pre><code class="language-js"><span class="hljs-built_in">setTimeout</span>(<span class="hljs-func…
45245 …ass="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = { <span c…
45246 }, <span class="hljs-number">0</span>);</code></pre>
45248 …<code class="language-js"><span class="hljs-keyword">const</span> x = <span class="hljs-built_in">…
45249 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45254 <p>The <code>exports</code> variable is available within a module's file-level scope, and is
45259-js"><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span…
45260 …class="hljs-built_in">exports</span> = { <span class="hljs-attr">hello</span>: <span class="hljs-l…
45263 <!-- eslint-disable func-name-matching -->
45264-js"><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span…
45265 <span class="hljs-comment">// ... etc.</span>
45269 …guage-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">require</s…
45270 …<span class="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = { <s…
45271 …(<span class="hljs-function">(<span class="hljs-params"><span class="hljs-variable language_">modu…
45272 <span class="hljs-comment">// Module code here. In this example, define a function.</span>
45273 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">someFunc</span>(<spa…
45274 <span class="hljs-built_in">exports</span> = someFunc;
45275 …<span class="hljs-comment">// At this point, exports is no longer a shortcut to module.exports, an…
45276 <span class="hljs-comment">// this module will still export an empty default object.</span>
45277 …<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = s…
45278 …<span class="hljs-comment">// At this point, the module will now export someFunc, instead of the</…
45279 <span class="hljs-comment">// default object.</span>
45280 …})(<span class="hljs-variable language_">module</span>, <span class="hljs-variable language_">modu…
45281 …<span class="hljs-keyword">return</span> <span class="hljs-variable language_">module</span>.<span…
45288 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
45296 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
45305 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
45313 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
45321 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Please u…
45324 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> | <a…
45334 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
45343 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
45351 <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
45352 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
45363 <!-- Anchors to make sure old links find a target -->
45373 <!-- Anchors to make sure old links find a target -->
45397 <td><p>Unflag Top-Level Await.</p></td></tr>
45401 <td><p>Loading ECMAScript modules no longer requires a command-line flag.</p></td></tr>
45409 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
45412 <p>ECMAScript modules are <a href="https://tc39.github.io/ecma262/#sec-modules">the official standa…
45413 code for reuse. Modules are defined using a variety of <a href="https://developer.mozilla.org/en-US…
45414 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export"><code…
45416 <pre><code class="language-js"><span class="hljs-comment">// addTwo.mjs</span>
45417 <span class="hljs-keyword">function</span> <span class="hljs-title function_">addTwo</span>(<span c…
45418 <span class="hljs-keyword">return</span> num + <span class="hljs-number">2</span>;
45421 <span class="hljs-keyword">export</span> { addTwo };</code></pre>
45423 <pre><code class="language-js"><span class="hljs-comment">// app.mjs</span>
45424 <span class="hljs-keyword">import</span> { addTwo } <span class="hljs-keyword">from</span> <span cl…
45426 <span class="hljs-comment">// Prints: 6</span>
45427 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
45431 <!-- Anchors to make sure old links find a target -->
45440 <code>--input-type</code> flag. See
45443 <!-- Anchors to make sure old links find a target -->
45473 <p><em>Bare specifiers</em> like <code>'some-package'</code> or <code>'some-package/shuffle'</code>…
45508 …e class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-string">'./foo.mj…
45509 <span class="hljs-keyword">import</span> <span class="hljs-string">'./foo.mjs?query=2'</span>; <spa…
45517 <p><a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs"><code>data:…
45520 …for JSON (requires <a href="#cli_experimental_json_modules"><code>--experimental-json-modules</cod…
45521 …for Wasm (requires <a href="#cli_experimental_wasm_modules"><code>--experimental-wasm-modules</cod…
45526 <a href="https://url.spec.whatwg.org/#special-scheme">special scheme</a>. For example, attempting t…
45530 <pre><code class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-string">'…
45531 <span class="hljs-keyword">import</span> _ <span class="hljs-keyword">from</span> <span class="hljs
45547 …e><code class="language-js"><span class="hljs-keyword">import</span> fs <span class="hljs-keyword"…
45554-js"><span class="hljs-keyword">import</span> <span class="hljs-title class_">EventEmitter</span> …
45555 <span class="hljs-keyword">const</span> e = <span class="hljs-keyword">new</span> <span class="hljs
45556 …de class="language-js"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-key…
45557 …n class="hljs-title function_">readFile</span>(<span class="hljs-string">'./foo.txt'</span>, <span…
45558 <span class="hljs-keyword">if</span> (err) {
45559 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
45560 } <span class="hljs-keyword">else</span> {
45561 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
45564 …lass="language-js"><span class="hljs-keyword">import</span> fs, { readFileSync } <span class="hljs
45565 <span class="hljs-keyword">import</span> { syncBuiltinESMExports } <span class="hljs-keyword">from<…
45567-property">readFileSync</span> = <span class="hljs-function">() =></span> <span class="hljs-title …
45568 <span class="hljs-title function_">syncBuiltinESMExports</span>();
45570 fs.<span class="hljs-property">readFileSync</span> === readFileSync;</code></pre>
45572 <p><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/impor…
45576 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
45582 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
45587 … class="language-js"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-k…
45588-keyword">const</span> buffer = <span class="hljs-title function_">readFileSync</span>(<span class…
45590 <!--
45592 - v13.9.0
45593 - v12.16.2
45595 - version: v14.18.0
45596 pr-url: https://github.com/nodejs/node/pull/38587
45598 -->
45599 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
45600 <p>This feature is only available with the <code>--experimental-import-meta-resolve</code>
45603 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
45604 <li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
45606 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
45608 <p>Provides a module-relative resolution function scoped to each module, returning
45610 <!-- eslint-skip -->
45611-js"><span class="hljs-keyword">const</span> dependencyAsset = <span class="hljs-keyword">await</s…
45614 <!-- eslint-skip -->
45615-js"><span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>.<span class=…
45636 <!-- eslint-disable no-duplicate-imports -->
45637-js"><span class="hljs-keyword">import</span> { <span class="hljs-keyword">default</span> <span cl…
45639 <span class="hljs-comment">// The following import statement is "syntax sugar" (equivalent but swee…
45640 <span class="hljs-comment">// for `{ default as cjsSugar }` in the above import statement:</span>
45641 <span class="hljs-keyword">import</span> cjsSugar <span class="hljs-keyword">from</span> <span clas…
45643 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45644 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45645 <span class="hljs-comment">// Prints:</span>
45646 <span class="hljs-comment">// &#x3C;module.exports></span>
45647 <span class="hljs-comment">// true</span></code></pre>
45653 <!-- eslint-skip -->
45654 …uage-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> m <span c…
45655 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45656-variable language_">console</span>.<span class="hljs-title function_">log</span>(m === <span clas…
45657 <span class="hljs-comment">// Prints:</span>
45658 <span class="hljs-comment">// [Module] { default: &#x3C;module.exports> }</span>
45659 <span class="hljs-comment">// true</span></code></pre>
45665 <pre><code class="language-js cjs"><span class="hljs-comment">// cjs.cjs</span>
45666 <span class="hljs-built_in">exports</span>.<span class="hljs-property">name</span> = <span class="h…
45668 <!-- eslint-disable no-duplicate-imports -->
45669 …code class="language-js"><span class="hljs-keyword">import</span> { name } <span class="hljs-keywo…
45670 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45671 <span class="hljs-comment">// Prints: 'exported'</span>
45673 <span class="hljs-keyword">import</span> cjs <span class="hljs-keyword">from</span> <span class="hl…
45674 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45675 <span class="hljs-comment">// Prints: { name: 'exported' }</span>
45677 …pan class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> m <span class="hljs-
45678 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45679 <span class="hljs-comment">// Prints: [Module] { default: { name: 'exported' }, name: 'exported' }<…
45689 …er outputs. See <a href="https://github.com/guybedford/cjs-module-lexer/tree/1.2.2">cjs-module-lex…
45702 <code>--experimental-json-modules</code> flag.</p>
45704 <!-- eslint-skip -->
45705 …de class="language-js"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-key…
45706-keyword">const</span> json = <span class="hljs-variable constant_">JSON</span>.<span class="hljs-
45728 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
45730 and are loaded using the CJS loader. <a href="https://html.spec.whatwg.org/#creating-a-json-module-
45732 additional flag <code>--experimental-json-modules</code> when running Node.js.</p>
45733 <p>When the <code>--experimental-json-modules</code> flag is included, both the
45741 <!-- eslint-skip -->
45742 …de class="language-js"><span class="hljs-keyword">import</span> packageConfig <span class="hljs-ke…
45743 <p>The <code>--experimental-json-modules</code> flag is needed for the module
45745 <pre><code class="language-bash">node index.mjs <span class="hljs-comment"># fails</span>
45746 node --experimental-json-modules index.mjs <span class="hljs-comment"># works</span></code></pre>
45749 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
45751 <code>--experimental-wasm-modules</code> flag, allowing any <code>.wasm</code> files to be
45754 <a href="https://github.com/webassembly/esm-integration">ES Module Integration Proposal for Web Ass…
45756 …uage-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> M <span c…
45757 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45759 <pre><code class="language-bash">node --experimental-wasm-modules index.mjs</code></pre>
45762 </section><section><h3>Top-level <code>await</code><span><a class="mark" href="#esm_top_level_await…
45766 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
45769-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> five = <span…
45771 …code class="language-js"><span class="hljs-keyword">import</span> { five } <span class="hljs-keywo…
45773 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45774 <pre><code class="language-bash">node b.mjs <span class="hljs-comment"># works</span></code></pre>
45777 …code class="language-js"><span class="hljs-keyword">import</span> { spawn } <span class="hljs-keyw…
45778 <span class="hljs-keyword">import</span> { execPath } <span class="hljs-keyword">from</span> <span …
45780 <span class="hljs-title function_">spawn</span>(execPath, [
45781 <span class="hljs-string">'--input-type=module'</span>,
45782 <span class="hljs-string">'--eval'</span>,
45783 <span class="hljs-comment">// Never-resolving Promise:</span>
45784 <span class="hljs-string">'await new Promise(() => {})'</span>,
45785 …<span class="hljs-title function_">once</span>(<span class="hljs-string">'exit'</span>, <span clas…
45786 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
45790 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
45794 provided via a <code>--experimental-loader ./loader-name.mjs</code> argument to Node.js.</p>
45804 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
45805 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
45807 <li><code>conditions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
45808 <li><code>parentURL</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
45811 <li><code>defaultResolve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
45812 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
45814 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
45831 <pre><code class="language-js"><span class="hljs-comment">/**
45832 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">string</span>} <span class="hlj…
45833 * <span class="hljs-doctag">@param</span> {<span class="hljs-type">{
45837 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">Function</span>} <span class="h…
45838 …* <span class="hljs-doctag">@returns</span> {<span class="hljs-type">Promise&#x3C;{ url: string </…
45840-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">functio…
45841 …<span class="hljs-keyword">const</span> { parentURL = <span class="hljs-literal">null</span> } = c…
45842-keyword">if</span> (<span class="hljs-title class_">Math</span>.<span class="hljs-title function_…
45843 …<span class="hljs-comment">// For some or all specifiers, do some custom logic for resolving.</spa…
45844 <span class="hljs-comment">// Always return an object of the form {url: &#x3C;string>}.</span>
45845 <span class="hljs-keyword">return</span> {
45846 <span class="hljs-attr">url</span>: parentURL ?
45847 …<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(specifier, par…
45848 …<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(specifier).<sp…
45851-keyword">if</span> (<span class="hljs-title class_">Math</span>.<span class="hljs-title function_…
45852 …<span class="hljs-comment">// When calling `defaultResolve`, the arguments can be modified. In thi…
45853 …<span class="hljs-comment">// case it's adding another value for matching conditional exports.</sp…
45854 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultResolve</span>(…
45856 …span class="hljs-attr">conditions</span>: [...context.<span class="hljs-property">conditions</span…
45859 <span class="hljs-comment">// Defer to Node.js for all other specifiers.</span>
45860 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultResolve</span>(…
45868 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
45869 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
45870 <li><code>defaultGetFormat</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
45871 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
45873 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
45916-US/docs/Web/JavaScript/Reference/Global_Objects/String"><code>string</code></a>, <a href="https:/…
45919 …g/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> obj…
45920 …rg/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> obje…
45922 <p>Note: If the source value of a text-based format (i.e., <code>'json'</code>, <code>'module'</cod…
45924 <pre><code class="language-js"><span class="hljs-comment">/**
45925 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">string</span>} <span class="hlj…
45926 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">Object</span>} context (current…
45927 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">Function</span>} <span class="h…
45928 …* <span class="hljs-doctag">@returns</span> {<span class="hljs-type">Promise&#x3C;{ format: string…
45930-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">functio…
45931-keyword">if</span> (<span class="hljs-title class_">Math</span>.<span class="hljs-title function_…
45932 …<span class="hljs-comment">// For some or all URLs, do some custom logic for determining format.</…
45933 …<span class="hljs-comment">// Always return an object of the form {format: &#x3C;string>}, where t…
45934 <span class="hljs-comment">// format is one of the strings in the preceding table.</span>
45935 <span class="hljs-keyword">return</span> {
45936 <span class="hljs-attr">format</span>: <span class="hljs-string">'module'</span>,
45939 <span class="hljs-comment">// Defer to Node.js for all other URLs.</span>
45940 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultGetFormat</span…
45948 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
45949 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
45951 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
45954 <li><code>defaultGetSource</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
45955 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
45957-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
45964 <pre><code class="language-js"><span class="hljs-comment">/**
45965 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">string</span>} <span class="hlj…
45966 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">{ format: string </span>}} cont…
45967 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">Function</span>} <span class="h…
45968 …* <span class="hljs-doctag">@returns</span> {<span class="hljs-type">Promise&#x3C;{ source: !(stri…
45970-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">functio…
45971 <span class="hljs-keyword">const</span> { format } = context;
45972-keyword">if</span> (<span class="hljs-title class_">Math</span>.<span class="hljs-title function_…
45973 …<span class="hljs-comment">// For some or all URLs, do some custom logic for retrieving the source…
45974 …<span class="hljs-comment">// Always return an object of the form {source: &#x3C;string|buffer>}.<…
45975 <span class="hljs-keyword">return</span> {
45976 <span class="hljs-attr">source</span>: <span class="hljs-string">'...'</span>,
45979 <span class="hljs-comment">// Defer to Node.js for all other URLs.</span>
45980 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultGetSource</span…
45988-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
45989 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
45991 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
45992 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
45995 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
45997-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
46004 <p>If this hook is used to convert unknown-to-Node.js file types into executable
46006 unknown-to-Node.js file extensions. See the <a href="#esm_transpiler_loader">transpiler loader exam…
46007 <pre><code class="language-js"><span class="hljs-comment">/**
46008 … <span class="hljs-doctag">@param</span> {<span class="hljs-type">!(string | SharedArrayBuffer | U…
46009 * <span class="hljs-doctag">@param</span> {<span class="hljs-type">{
46013 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">Function</span>} <span class="h…
46014 …* <span class="hljs-doctag">@returns</span> {<span class="hljs-type">Promise&#x3C;{ source: !(stri…
46016-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">functio…
46017 <span class="hljs-keyword">const</span> { url, format } = context;
46018-keyword">if</span> (<span class="hljs-title class_">Math</span>.<span class="hljs-title function_…
46019 …<span class="hljs-comment">// For some or all URLs, do some custom logic for modifying the source.…
46020 …<span class="hljs-comment">// Always return an object of the form {source: &#x3C;string|buffer>}.<…
46021 <span class="hljs-keyword">return</span> {
46022 <span class="hljs-attr">source</span>: <span class="hljs-string">'...'</span>,
46025 <span class="hljs-comment">// Defer to Node.js for all other sources.</span>
46026 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultTransformSource…
46034 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
46038 run as sloppy-mode script on startup.</p>
46040 scope. The only argument is a <code>require</code>-like function that can be used to load
46044 <pre><code class="language-js"><span class="hljs-comment">/**
46045 …* <span class="hljs-doctag">@returns</span> {<span class="hljs-type">string</span>} Code to run be…
46047 …js-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title funct…
46048 <span class="hljs-keyword">return</span> <span class="hljs-string">`\
46059 <p>The various loader hooks can be used together to accomplish wide-ranging
46068 <pre><code class="language-js"><span class="hljs-comment">// https-loader.mjs</span>
46069 <span class="hljs-keyword">import</span> { get } <span class="hljs-keyword">from</span> <span class…
46071 …ass="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-titl…
46072 …<span class="hljs-keyword">const</span> { parentURL = <span class="hljs-literal">null</span> } = c…
46074 …<span class="hljs-comment">// Normally Node.js would error on specifiers starting with 'https://',…
46075 …<span class="hljs-comment">// this hook intercepts them and converts them into absolute URLs to be…
46076 <span class="hljs-comment">// passed along to the later hooks below.</span>
46077 …<span class="hljs-keyword">if</span> (specifier.<span class="hljs-title function_">startsWith</spa…
46078 <span class="hljs-keyword">return</span> {
46079 <span class="hljs-attr">url</span>: specifier
46081-keyword">else</span> <span class="hljs-keyword">if</span> (parentURL &#x26;&#x26; parentURL.<span…
46082 <span class="hljs-keyword">return</span> {
46083 …"hljs-attr">url</span>: <span class="hljs-keyword">new</span> <span class="hljs-title function_">U…
46087 <span class="hljs-comment">// Let Node.js handle all other specifiers.</span>
46088 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultResolve</span>(…
46091 …ss="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title…
46092 …<span class="hljs-comment">// This loader assumes all network-provided JavaScript is ES module cod…
46093 …<span class="hljs-keyword">if</span> (url.<span class="hljs-title function_">startsWith</span>(<sp…
46094 <span class="hljs-keyword">return</span> {
46095 <span class="hljs-attr">format</span>: <span class="hljs-string">'module'</span>
46099 <span class="hljs-comment">// Let Node.js handle all other URLs.</span>
46100 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultGetFormat</span…
46103 …ss="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title…
46104 …<span class="hljs-comment">// For JavaScript to be loaded over the network, we need to fetch and</…
46105 <span class="hljs-comment">// return it.</span>
46106 …<span class="hljs-keyword">if</span> (url.<span class="hljs-title function_">startsWith</span>(<sp…
46107-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Prom…
46108 …<span class="hljs-title function_">get</span>(url, <span class="hljs-function">(<span class="hljs-
46109 <span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
46110 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
46111-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">()…
46112-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">…
46116 <span class="hljs-comment">// Let Node.js handle all other URLs.</span>
46117 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultGetSource</span…
46119 <pre><code class="language-js"><span class="hljs-comment">// main.mjs</span>
46120-keyword">import</span> { <span class="hljs-variable constant_">VERSION</span> } <span class="hljs
46122 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
46124 <code>node --experimental-loader ./https-loader.mjs ./main.mjs</code>
46135 <pre><code class="language-js"><span class="hljs-comment">// coffeescript-loader.mjs</span>
46136 …js-keyword">import</span> { <span class="hljs-variable constant_">URL</span>, pathToFileURL } <spa…
46137 …lass="hljs-keyword">import</span> <span class="hljs-title class_">CoffeeScript</span> <span class=…
46139-keyword">const</span> baseURL = <span class="hljs-title function_">pathToFileURL</span>(<span cla…
46141 <span class="hljs-comment">// CoffeeScript files end in .coffee, .litcoffee or .coffee.md.</span>
46142 <span class="hljs-keyword">const</span> extensionsRegex = <span class="hljs-regexp">/\.coffee$|\.li…
46144 …ass="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-titl…
46145 <span class="hljs-keyword">const</span> { parentURL = baseURL } = context;
46147 …<span class="hljs-comment">// Node.js normally errors on unknown file extensions, so return a URL …
46148 <span class="hljs-comment">// specifiers ending in the CoffeeScript file extensions.</span>
46149 …<span class="hljs-keyword">if</span> (extensionsRegex.<span class="hljs-title function_">test</spa…
46150 <span class="hljs-keyword">return</span> {
46151 …"hljs-attr">url</span>: <span class="hljs-keyword">new</span> <span class="hljs-title function_">U…
46155 <span class="hljs-comment">// Let Node.js handle all other specifiers.</span>
46156 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultResolve</span>(…
46159 …ss="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title…
46160 …<span class="hljs-comment">// Now that we patched resolve to let CoffeeScript URLs through, we nee…
46161 …<span class="hljs-comment">// tell Node.js what format such URLs should be interpreted as. For the…
46162 …<span class="hljs-comment">// purposes of this loader, all CoffeeScript URLs are ES modules.</span>
46163 …<span class="hljs-keyword">if</span> (extensionsRegex.<span class="hljs-title function_">test</spa…
46164 <span class="hljs-keyword">return</span> {
46165 <span class="hljs-attr">format</span>: <span class="hljs-string">'module'</span>
46169 <span class="hljs-comment">// Let Node.js handle all other URLs.</span>
46170 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultGetFormat</span…
46173 …"hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title fu…
46174 <span class="hljs-keyword">const</span> { url, format } = context;
46176 …<span class="hljs-keyword">if</span> (extensionsRegex.<span class="hljs-title function_">test</spa…
46177 <span class="hljs-keyword">return</span> {
46178-attr">source</span>: <span class="hljs-title class_">CoffeeScript</span>.<span class="hljs-title …
46182 <span class="hljs-comment">// Let Node.js handle all other sources.</span>
46183 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultTransformSource…
46185 <pre><code class="language-coffee"><span class="hljs-comment"># main.coffee</span>
46186 <span class="hljs-keyword">import</span> { scream } <span class="hljs-keyword">from</span> <span cl…
46187 console.log scream <span class="hljs-string">'hello, world'</span>
46189 <span class="hljs-keyword">import</span> { version } <span class="hljs-keyword">from</span> <span c…
46190 console.log <span class="hljs-string">"Brought to you by Node.js version <span class="hljs-subst">#…
46191 <pre><code class="language-coffee"><span class="hljs-comment"># scream.coffee</span>
46192 <span class="hljs-keyword">export</span> scream = <span class="hljs-function"><span class="hljs-par…
46194 <code>node --experimental-loader ./coffeescript-loader.mjs main.coffee</code>
46203 <li>FileURL-based resolution as is used by ES modules</li>
46221 of these top-level routines unless stated otherwise.</p>
46460 <li>If <em>pjson.imports</em> is a non-null Object, then
46545 <li>If <em>baseLengthA</em> is greater than <em>baseLengthB</em>, return -1.</li>
46548 <li>If <em>keyB</em> does not contain <em>"*"</em>, return -1.</li>
46549 <li>If the length of <em>keyA</em> is greater than the length of <em>keyB</em>, return -1.</li>
46560 <li>If <em>pattern</em> is <strong>false</strong>, <em>subpath</em> has non-zero length and <em>tar…
46602 <li>Otherwise, if <em>target</em> is a non-null Object, then
46604 <li>If <em>exports</em> contains any index property keys, as defined in ECMA-262
46605 <a href="https://tc39.es/ecma262/#integer-index">6.1.7 Array Index</a>, throw an <em>Invalid Packag…
46713 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
46718 <p>The <code>--experimental-specifier-resolution=[mode]</code> flag can be used to customize
46723 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
46725 <span class="hljs-meta">$ </span><span class="language-bash">node index <span class="hljs-comment">…
46727 <span class="hljs-meta">$ </span><span class="language-bash">node --experimental-specifier-resoluti…
46729 <!-- Note: The cjs-module-lexer link should be kept in-sync with the deps version --></section>
46738 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
46748 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
46755 … class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><co…
46756 <span class="hljs-comment">// In an ECMAScript module</span>
46757-keyword">import</span> { builtinModules <span class="hljs-keyword">as</span> builtin } <span clas…
46758 <span class="hljs-comment">// In a CommonJS module</span>
46759 …ss="hljs-keyword">const</span> builtin = <span class="hljs-built_in">require</span>(<span class="h…
46765 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
46770 …lass="language-js mjs"><span class="hljs-keyword">import</span> { createRequire } <span class="hlj…
46771-keyword">const</span> <span class="hljs-built_in">require</span> = <span class="hljs-title functi…
46773 <span class="hljs-comment">// sibling-module.js is a CommonJS module.</span>
46774 <span class="hljs-keyword">const</span> siblingModule = <span class="hljs-built_in">require</span>(…
46779 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Please u…
46781 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
46785 …"language-js"><span class="hljs-keyword">const</span> { createRequireFromPath } = <span class="hlj…
46786 <span class="hljs-keyword">const</span> requireUtil = <span class="hljs-title function_">createRequ…
46788 <span class="hljs-comment">// Require `../src/utils/some-tool`</span>
46789 requireUtil(<span class="hljs-string">'./some-tool'</span>);</code></pre>
46797 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
46798 <span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span c…
46799 <span class="hljs-keyword">const</span> { syncBuiltinESMExports } = <span class="hljs-built_in">req…
46801 fs.<span class="hljs-property">readFile</span> = newAPI;
46803 <span class="hljs-keyword">delete</span> fs.<span class="hljs-property">readFileSync</span>;
46805 <span class="hljs-keyword">function</span> <span class="hljs-title function_">newAPI</span>(<span c…
46806 <span class="hljs-comment">// ...</span>
46809 fs.<span class="hljs-property">newAPI</span> = newAPI;
46811 <span class="hljs-title function_">syncBuiltinESMExports</span>();
46813-title function_">import</span>(<span class="hljs-string">'fs'</span>).<span class="hljs-title fun…
46814 <span class="hljs-comment">// It syncs the existing readFile property with the new value</span>
46815 …assert.<span class="hljs-title function_">strictEqual</span>(esmFS.<span class="hljs-property">rea…
46816 <span class="hljs-comment">// readFileSync has been deleted from the required fs</span>
46817 …hljs-title function_">strictEqual</span>(<span class="hljs-string">'readFileSync'</span> <span cla…
46818 …<span class="hljs-comment">// syncBuiltinESMExports() does not remove readFileSync from esmFS</spa…
46819 …ljs-title function_">strictEqual</span>(<span class="hljs-string">'readFileSync'</span> <span clas…
46820 <span class="hljs-comment">// syncBuiltinESMExports() does not add names</span>
46821 …assert.<span class="hljs-title function_">strictEqual</span>(esmFS.<span class="hljs-property">new…
46827 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
46832 <a href="#cli_enable_source_maps"><code>--enable-source-maps</code></a>, or with code coverage enab…
46835 … class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><co…
46836 <span class="hljs-comment">// In an ECMAScript module</span>
46837-keyword">import</span> { findSourceMap, <span class="hljs-title class_">SourceMap</span> } <span …
46838 <span class="hljs-comment">// In a CommonJS module</span>
46839-keyword">const</span> { findSourceMap, <span class="hljs-title class_">SourceMap</span> } = <span…
46840 <!-- Anchors to make sure old links find a target -->
46847 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
46858 <li><code>payload</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
46863 <li><code>file</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
46864 <li><code>version</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
46865 <li><code>sources</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
46866 <li><code>sourcesContent</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
46867 <li><code>names</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
46868 <li><code>mappings</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
46869 <li><code>sourceRoot</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
46873 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
46878 <li><code>lineNumber</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
46879 <li><code>columnNumber</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
46880 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
46886 <li>generatedLine: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
46887 <li>generatedColumn: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
46888 <li>originalSource: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
46889 <li>originalLine: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#…
46890 <li>originalColumn: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
46891 <li>name: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
46908 <td><p>Unflag self-referencing a package using its name.</p></td></tr>
46932 top-level <a href="#packages_type"><code>"type"</code></a> field with a value of <code>"module"</co…
46935 <p>Strings passed in as an argument to <code>--eval</code>, or piped to <code>node</code> via <code…
46936 with the flag <code>--input-type=module</code>.</p>
46940 where the nearest parent <code>package.json</code> file contains no top-level <code>"type"</code>
46941 field, or string input without the flag <code>--input-type</code>. This behavior is to
46952 top-level field <a href="#packages_type"><code>"type"</code></a> with a value of <code>"commonjs"</…
46955 <p>Strings passed in as an argument to <code>--eval</code> or <code>--print</code>, or piped to <co…
46956 via <code>STDIN</code>, with the flag <code>--input-type=commonjs</code>.</p>
46961 future-proof the package in case the default type of Node.js ever changes, and
46970 <p>The <code>"type"</code> field applies not only to initial entry points (<code>node my-app.js</co…
46972 <pre><code class="language-js"><span class="hljs-comment">// my-app.js, treated as an ES module bec…
46973 <span class="hljs-comment">// file in the same folder with "type": "module".</span>
46975 <span class="hljs-keyword">import</span> <span class="hljs-string">'./startup/init.js'</span>;
46976 <span class="hljs-comment">// Loaded as ES module since ./startup contains no package.json file,</s…
46977 <span class="hljs-comment">// and therefore inherits the "type" value from one level up.</span>
46979 <span class="hljs-keyword">import</span> <span class="hljs-string">'commonjs-package'</span>;
46980 <span class="hljs-comment">// Loaded as CommonJS since ./node_modules/commonjs-package/package.json…
46981 <span class="hljs-comment">// lacks a "type" field or contains "type": "commonjs".</span>
46983 <span class="hljs-keyword">import</span> <span class="hljs-string">'./node_modules/commonjs-package…
46984 <span class="hljs-comment">// Loaded as CommonJS since ./node_modules/commonjs-package/package.json…
46985 <span class="hljs-comment">// lacks a "type" field or contains "type": "commonjs".</span></code></p…
46990 <pre><code class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-string">'…
46991 <span class="hljs-comment">// Loaded as CommonJS since .cjs is always loaded as CommonJS.</span>
46993 <span class="hljs-keyword">import</span> <span class="hljs-string">'commonjs-package/src/index.mjs'…
46994 <span class="hljs-comment">// Loaded as ES module since .mjs is always loaded as ES module.</span><…
47011 <h4><code>--input-type</code> flag<span><a class="mark" href="#packages_input_type_flag" id="packag…
47015 <p>Strings passed in as an argument to <code>--eval</code> (or <code>-e</code>), or piped to <code>…
47016 <code>STDIN</code>, are treated as <a href="esm.html">ES modules</a> when the <code>--input-type=mo…
47018 <pre><code class="language-bash">node --input-type=module --<span class="hljs-built_in">eval</span>…
47020 <span class="hljs-built_in">echo</span> <span class="hljs-string">"import { sep } from 'path'; cons…
47021 <p>For completeness there is also <code>--input-type=commonjs</code>, for explicitly running
47022 string input as CommonJS. This is the default behavior if <code>--input-type</code> is
47025 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
47030 available in your environment - provided you have Node.js installed.</p>
47058 …n_field_definitions"><code>package.json</code></a> (e.g. <code>require('your-package/package.json'…
47060 <p>To make the introduction of <a href="#packages_exports"><code>"exports"</code></a> non-breaking,…
47062 entry points so that the package’s public API is well-defined. For example,
47065 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47066 …<span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47067 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47068 …an class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
47069 … class="hljs-attr">"./lib"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47070 …ass="hljs-attr">"./lib/index"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
47071 …ss="hljs-attr">"./lib/index.js"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
47072 …ss="hljs-attr">"./feature"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47073 …hljs-attr">"./feature/index.js"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
47074 …<span class="hljs-attr">"./package.json"</span><span class="hljs-punctuation">:</span> <span class…
47075 <span class="hljs-punctuation">}</span>
47076 <span class="hljs-punctuation">}</span></code></pre>
47078 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47079 …<span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47080 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47081 …an class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
47082 … class="hljs-attr">"./lib"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47083 …n class="hljs-attr">"./lib/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
47084 …ss="hljs-attr">"./feature"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47085 …ass="hljs-attr">"./feature/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
47086 …<span class="hljs-attr">"./package.json"</span><span class="hljs-punctuation">:</span> <span class…
47087 <span class="hljs-punctuation">}</span>
47088 <span class="hljs-punctuation">}</span></code></pre>
47096 <code>import feature from 'my-mod/feature'</code> as they need to provide the full
47097 path <code>import feature from 'my-mod/feature/index.js</code>.</p>
47101 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47102 …pan class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47103 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47104 <span class="hljs-punctuation">}</span></code></pre>
47121 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47122 …pan class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47123 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47124 …<span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47125 …<span class="hljs-attr">"./submodule"</span><span class="hljs-punctuation">:</span> <span class="h…
47126 <span class="hljs-punctuation">}</span>
47127 <span class="hljs-punctuation">}</span></code></pre>
47129 …="language-js"><span class="hljs-keyword">import</span> submodule <span class="hljs-keyword">from<…
47130 <span class="hljs-comment">// Loads ./node_modules/es-module-package/src/submodule.js</span></code>…
47132 …ge-js"><span class="hljs-keyword">import</span> submodule <span class="hljs-keyword">from</span> <…
47133 <span class="hljs-comment">// Throws ERR_PACKAGE_PATH_NOT_EXPORTED</span></code></pre>
47145 <pre><code class="language-json"><span class="hljs-comment">// package.json</span>
47146 <span class="hljs-punctuation">{</span>
47147 …<span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47148 …<span class="hljs-attr">"#dep"</span><span class="hljs-punctuation">:</span> <span class="hljs-pun…
47149 … class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
47150 …<span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47151 <span class="hljs-punctuation">}</span>
47152 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
47153 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
47154 …<span class="hljs-attr">"dep-node-native"</span><span class="hljs-punctuation">:</span> <span clas…
47155 <span class="hljs-punctuation">}</span>
47156 <span class="hljs-punctuation">}</span></code></pre>
47158 <code>dep-node-native</code> (including its exports in turn), and instead gets the local
47159 file <code>./dep-polyfill.js</code> relative to the package in other environments.</p>
47173 <pre><code class="language-json"><span class="hljs-comment">// ./node_modules/es-module-package/pac…
47174 <span class="hljs-punctuation">{</span>
47175 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47176 …<span class="hljs-attr">"./features/*"</span><span class="hljs-punctuation">:</span> <span class="…
47177 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
47178 …<span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47179 …<span class="hljs-attr">"#internal/*"</span><span class="hljs-punctuation">:</span> <span class="h…
47180 <span class="hljs-punctuation">}</span>
47181 <span class="hljs-punctuation">}</span></code></pre>
47186 …s="language-js"><span class="hljs-keyword">import</span> featureX <span class="hljs-keyword">from<…
47187 <span class="hljs-comment">// Loads ./node_modules/es-module-package/src/features/x.js</span>
47189 <span class="hljs-keyword">import</span> featureY <span class="hljs-keyword">from</span> <span clas…
47190 <span class="hljs-comment">// Loads ./node_modules/es-module-package/src/features/y/y.js</span>
47192 <span class="hljs-keyword">import</span> internalZ <span class="hljs-keyword">from</span> <span cla…
47193 <span class="hljs-comment">// Loads ./node_modules/es-module-package/src/internal/z.js</span></code…
47203 <pre><code class="language-json"><span class="hljs-comment">// ./node_modules/es-module-package/pac…
47204 <span class="hljs-punctuation">{</span>
47205 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47206 …="hljs-attr">"./features/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47207 …<span class="hljs-attr">"./features/private-internal/*"</span><span class="hljs-punctuation">:</sp…
47208 <span class="hljs-punctuation">}</span>
47209 <span class="hljs-punctuation">}</span></code></pre>
47210-js"><span class="hljs-keyword">import</span> featureInternal <span class="hljs-keyword">from</spa…
47211 <span class="hljs-comment">// Throws: ERR_PACKAGE_PATH_NOT_EXPORTED</span>
47213 <span class="hljs-keyword">import</span> featureX <span class="hljs-keyword">from</span> <span clas…
47214 <span class="hljs-comment">// Loads ./node_modules/es-module-package/src/features/x.js</span></code…
47223 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47224 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47225 …<span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47226 <span class="hljs-punctuation">}</span>
47227 <span class="hljs-punctuation">}</span></code></pre>
47229 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47230 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47231 <span class="hljs-punctuation">}</span></code></pre>
47248 <pre><code class="language-json"><span class="hljs-comment">// package.json</span>
47249 <span class="hljs-punctuation">{</span>
47250 …class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
47251 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47252 …class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47253 …<span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47254 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
47255 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47256 <span class="hljs-punctuation">}</span></code></pre>
47259 <li><code>"import"</code> - matches when the package is loaded via <code>import</code> or
47260 <code>import()</code>, or via any top-level import or resolve operation by the
47263 <li><code>"require"</code> - matches when the package is loaded via <code>require()</code>. The
47269 <li><code>"node"</code> - matches for any Node.js environment. Can be a CommonJS or ES
47272 <li><code>"node-addons"</code> - similar to <code>"node"</code> and matches for any Node.js environ…
47276 <a href="#cli_no_addons"><code>--no-addons</code> flag</a>.</li>
47277 <li><code>"default"</code> - the generic fallback that always matches. Can be a CommonJS
47287 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47288 …pan class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47289 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47290 …<span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47291 …<span class="hljs-attr">"./feature"</span><span class="hljs-punctuation">:</span> <span class="hlj…
47292 …class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
47293 …<span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47294 <span class="hljs-punctuation">}</span>
47295 <span class="hljs-punctuation">}</span>
47296 <span class="hljs-punctuation">}</span></code></pre>
47311 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47312 …pan class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47313 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47314 …<span class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-pun…
47315 …lass="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
47316 …<span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47317 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
47318 … class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47319 <span class="hljs-punctuation">}</span>
47320 <span class="hljs-punctuation">}</span></code></pre>
47330 <code>--conditions</code> flag:</p>
47331 <pre><code class="language-bash">node --conditions=development main.js</code></pre>
47333 exports, while resolving the existing <code>"node"</code>, <code>"node-addons"</code>, <code>"defau…
47337 <p>The <code>"import"</code>, <code>"require"</code>, <code>"node"</code>, <code>"node-addons"</cod…
47340 <p>The <code>"node-addons"</code> condition can be used to provide an entry point which
47342 <a href="#cli_no_addons"><code>--no-addons</code> flag</a>. When using <code>"node-addons"</code>, …
47347 … enabled in Node.js via the <a href="#packages_resolving_user_conditions"><code>--conditions</code>
47351 <li><code>"browser"</code> - any environment which implements a standard subset of global
47354 <li><code>"development"</code> - can be used to define a development-only environment
47356 <li><code>"production"</code> - can be used to define a production environment entry
47359 … enabled in Node.js via the <a href="#packages_resolving_user_conditions"><code>--conditions</code>
47361 …fic conditions such as <code>"deno"</code>, <code>"electron"</code>, or <code>"react-native"</code>
47365 to the <a href="https://github.com/nodejs/node/blob/HEAD/doc/api/packages.md#conditions-definitions…
47375 this would not necessarily be the case for company-specific or
47376 application-specific conditions.</li>
47380 <h4>Self-referencing a package using its name<span><a class="mark" href="#packages_self_referencing…
47388 <td><p>Unflag self-referencing a package using its name.</p></td></tr>
47395 <pre><code class="language-json"><span class="hljs-comment">// package.json</span>
47396 <span class="hljs-punctuation">{</span>
47397 …pan class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47398 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47399 …span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47400 …<span class="hljs-attr">"./foo"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
47401 <span class="hljs-punctuation">}</span>
47402 <span class="hljs-punctuation">}</span></code></pre>
47404 <pre><code class="language-js"><span class="hljs-comment">// ./a-module.mjs</span>
47405 …="hljs-keyword">import</span> { something } <span class="hljs-keyword">from</span> <span class="hl…
47406 <p>Self-referencing is available only if <code>package.json</code> has <a href="#packages_exports">…
47410 <pre><code class="language-js"><span class="hljs-comment">// ./another-module.mjs</span>
47412 <span class="hljs-comment">// Imports "another" from ./m.mjs. Fails because</span>
47413 <span class="hljs-comment">// the "package.json" "exports" field</span>
47414 <span class="hljs-comment">// does not provide an export named "./m.mjs".</span>
47415 <span class="hljs-keyword">import</span> { another } <span class="hljs-keyword">from</span> <span c…
47416 <p>Self-referencing is also available when using <code>require</code>, both in an ES module,
47418 <pre><code class="language-js cjs"><span class="hljs-comment">// ./a-module.js</span>
47419 …s-keyword">const</span> { something } = <span class="hljs-built_in">require</span>(<span class="hl…
47420 <p>Finally, self-referencing also works with scoped packages. For example, this
47422 <pre><code class="language-json"><span class="hljs-comment">// package.json</span>
47423 <span class="hljs-punctuation">{</span>
47424 …an class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47425 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47426 <span class="hljs-punctuation">}</span></code></pre>
47428 …ass="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><pre><…
47429-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-
47430 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
47431 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
47441 ignores) the top-level <code>"module"</code> field.</p>
47444 <code>'pkg'</code> and <code>'pkg/es-module'</code>, or both at the same specifier via <a href="#pa…
47466 all-CommonJS or all-ES module environments, respectively, and therefore is
47468 …href="https://babeljs.io/">Babel</a> or <a href="https://github.com/standard-things/esm#readme"><c…
47480 newest version of the package would only be usable in ES module-supporting
47502 <pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</spa…
47503 <span class="hljs-punctuation">{</span>
47504 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47505 …an class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47506 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47507 … class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47508 …<span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47509 <span class="hljs-punctuation">}</span>
47510 <span class="hljs-punctuation">}</span></code></pre>
47516 <pre><code class="language-js cjs"><span class="hljs-comment">// ./node_modules/pkg/index.cjs</span>
47517 <span class="hljs-built_in">exports</span>.<span class="hljs-property">name</span> = <span class="h…
47518 <pre><code class="language-js"><span class="hljs-comment">// ./node_modules/pkg/wrapper.mjs</span>
47519 <span class="hljs-keyword">import</span> cjsModule <span class="hljs-keyword">from</span> <span cla…
47520 <span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> name = cjsModule.<…
47530 …code class="language-js"><span class="hljs-keyword">import</span> cjsModule <span class="hljs-keyw…
47531 <span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> name = cjsModule.<…
47532 <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> cjsModule;</code…
47540 package is used directly in an application, and a <code>utilities-plus</code> package
47542 underlying CommonJS files, it doesn’t matter if <code>utilities-plus</code> is written in
47548 be to add an export, e.g. <code>"./module"</code>, to point to an all-ES module-syntax
47554 <pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</spa…
47555 <span class="hljs-punctuation">{</span>
47556 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47557 …an class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47558 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47559 …span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47560 …<span class="hljs-attr">"./module"</span><span class="hljs-punctuation">:</span> <span class="hljs
47561 <span class="hljs-punctuation">}</span>
47562 <span class="hljs-punctuation">}</span></code></pre>
47566 <pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</spa…
47567 <span class="hljs-punctuation">{</span>
47568 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47569 …an class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47570 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47571 …n class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47572 …<span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47573 <span class="hljs-punctuation">}</span>
47574 <span class="hljs-punctuation">}</span></code></pre>
47584 present. This would likely cause hard-to-troubleshoot bugs.</p>
47594 …age-js"><span class="hljs-keyword">import</span> <span class="hljs-title class_">Date</span> <span…
47595 <span class="hljs-keyword">const</span> someDate = <span class="hljs-keyword">new</span> <span clas…
47596 <span class="hljs-comment">// someDate contains state; Date does not</span></code></pre>
47598 object, or modify a passed-in object, to keep the state external to the
47605 <pre><code class="language-js cjs"><span class="hljs-comment">// ./node_modules/pkg/index.cjs</span>
47606 <span class="hljs-keyword">const</span> state = <span class="hljs-built_in">require</span>(<span cl…
47607 <span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span>.<spa…
47608 <pre><code class="language-js"><span class="hljs-comment">// ./node_modules/pkg/index.mjs</span>
47609 <span class="hljs-keyword">import</span> state <span class="hljs-keyword">from</span> <span class="…
47610 <span class="hljs-keyword">export</span> {
47635 <code>"./module"</code>, to point to an all-ES module-syntax version of the package:</p>
47636 <pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</spa…
47637 <span class="hljs-punctuation">{</span>
47638 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47639 …an class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47640 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47641 …span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47642 …<span class="hljs-attr">"./module"</span><span class="hljs-punctuation">:</span> <span class="hljs
47643 <span class="hljs-punctuation">}</span>
47644 <span class="hljs-punctuation">}</span></code></pre>
47647 as <a href="https://docs.npmjs.com/creating-a-package-json-file">npm</a>) use
47651 <li><a href="#packages_name"><code>"name"</code></a> - Relevant when using named imports within a p…
47653 <li><a href="#packages_main"><code>"main"</code></a> - The default module when loading the package,…
47655 <li><a href="#packages_packagemanager"><code>"packageManager"</code></a> - The package manager reco…
47657 <li><a href="#packages_type"><code>"type"</code></a> - The package type determining whether to load…
47659 <li><a href="#packages_exports"><code>"exports"</code></a> - Package exports and conditional export…
47661 <li><a href="#packages_imports"><code>"imports"</code></a> - Package imports, for use by modules wi…
47672 <td><p>Remove the <code>--experimental-resolve-self</code> option.</p></td></tr>
47677 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
47679 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47680 …<span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47681 <span class="hljs-punctuation">}</span></code></pre>
47686 <a href="#packages_self_referencing_a_package_using_its_name">self-reference</a> a package using it…
47692 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
47694 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47695 …<span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47696 <span class="hljs-punctuation">}</span></code></pre>
47700 …ass="language-js cjs"><span class="hljs-built_in">require</span>(<span class="hljs-string">'./path…
47707 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
47709 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
47711 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47712 …<span class="hljs-attr">"packageManager"</span><span class="hljs-punctuation">:</span> <span class…
47713 <span class="hljs-punctuation">}</span></code></pre>
47719 <p>This field is currently experimental and needs to be opted-in; check the
47727 <td><p>Unflag <code>--experimental-modules</code>.</p></td></tr>
47734 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
47739 <code>package.json</code> file contains a top-level field <code>"type"</code> with a value of
47744 <pre><code class="language-json"><span class="hljs-comment">// package.json</span>
47745 <span class="hljs-punctuation">{</span>
47746 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47747 <span class="hljs-punctuation">}</span></code></pre>
47748 <pre><code class="language-bash"><span class="hljs-comment"># In same folder as preceding package.j…
47749 node my-app.js <span class="hljs-comment"># Runs as ES module</span></code></pre>
47756 <pre><code class="language-js"><span class="hljs-comment">// my-app.js, part of the same example as…
47757 <span class="hljs-keyword">import</span> <span class="hljs-string">'./startup.js'</span>; <span cla…
47770 <td><p>Remove the <code>--experimental-conditional-exports</code> option.</p></td></tr>
47779-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href=…
47781 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47782 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47783 <span class="hljs-punctuation">}</span></code></pre>
47786 <a href="#packages_self_referencing_a_package_using_its_name">self-reference</a> to its own name. I…
47799 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects…
47801 <pre><code class="language-json"><span class="hljs-comment">// package.json</span>
47802 <span class="hljs-punctuation">{</span>
47803 …<span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47804 …<span class="hljs-attr">"#dep"</span><span class="hljs-punctuation">:</span> <span class="hljs-pun…
47805 … class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
47806 …<span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47807 <span class="hljs-punctuation">}</span>
47808 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
47809 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
47810 …<span class="hljs-attr">"dep-node-native"</span><span class="hljs-punctuation">:</span> <span clas…
47811 <span class="hljs-punctuation">}</span>
47812 <span class="hljs-punctuation">}</span></code></pre>
47819 <!--lint disable maximum-line-length-->
47820 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
47822 <p>The <code>net</code> module provides an asynchronous network API for creating stream-based
47826 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
47834 filesystem pathname. It gets truncated to an OS-dependent length of
47835 <code>sizeof(sockaddr_un.sun_path) - 1</code>. Typical values are 107 bytes on Linux and
47843 will persist until unlinked.</p>
47848 <em>not persist</em>. They are removed when the last reference to them is closed.
47853 <pre><code class="language-js">net.<span class="hljs-title function_">createServer</span>().<span c…
47854-title function_">join</span>(<span class="hljs-string">'\\\\?\\pipe'</span>, process.<span class=…
47867 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
47868 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
47876 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
47878 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
47879 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
47888 <li><code>net</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
47889 <li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
47892 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
47900 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
47901 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
47902 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
47906 …de class="language-js"><span class="hljs-keyword">const</span> blockList = <span class="hljs-keywo…
47907 blockList.<span class="hljs-title function_">addAddress</span>(<span class="hljs-string">'123.123.1…
47908 blockList.<span class="hljs-title function_">addRange</span>(<span class="hljs-string">'10.0.0.1'</…
47909 …js-title function_">addSubnet</span>(<span class="hljs-string">'8592:757c:efae:4e45::'</span>, <sp…
47911-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span …
47912-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span …
47913-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span …
47915 <span class="hljs-comment">// IPv6 notation for IPv4 addresses works:</span>
47916-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span …
47917-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span …
47923 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
47935 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
47937 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
47940 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
47941 …s://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3…
47942 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
47951 <li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_ty…
47958 <li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_ty…
47965 <li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_ty…
47972 <li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_ty…
47984 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
47986 <li><code>connectionListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
48011 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error…
48027-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href=…
48031 (useful to find which port was assigned when getting an OS-assigned address):
48035-js"><span class="hljs-keyword">const</span> server = net.<span class="hljs-title function_">creat…
48036 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'goodbye\n'</span>);
48037 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
48038 <span class="hljs-comment">// Handle errors here.</span>
48039 <span class="hljs-keyword">throw</span> err;
48042 <span class="hljs-comment">// Grab an arbitrary unused port.</span>
48043 server.<span class="hljs-title function_">listen</span>(<span class="hljs-function">() =></span> {
48044-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
48053 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48066 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
48068 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
48079 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48089 <!--lint disable no-undefined-references-->
48101 <!--lint enable no-undefined-references-->
48109 …re set to <code>SO_REUSEADDR</code> (see <a href="https://man7.org/linux/man-pages/man7/socket.7.h…
48118 …age-js">server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</spa…
48119 …<span class="hljs-keyword">if</span> (e.<span class="hljs-property">code</span> === <span class="h…
48120 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48121 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
48122 server.<span class="hljs-title function_">close</span>();
48123 …server.<span class="hljs-title function_">listen</span>(<span class="hljs-variable constant_">PORT…
48124 }, <span class="hljs-number">1000</span>);
48132 <li><code>handle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
48133 <li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48134 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48156 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
48158 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48159 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48160 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48162 <li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48164 <li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
48165 <li><code>readableAll</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
48167 <li><code>writableAll</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
48169 <li><code>ipv6Only</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48170 disable dual-stack support, i.e., binding to host <code>::</code> won't make
48174 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48178 <!--lint disable no-undefined-references-->
48185 <!--lint enable no-undefined-references-->
48191 <pre><code class="language-js">server.<span class="hljs-title function_">listen</span>({
48192 <span class="hljs-attr">host</span>: <span class="hljs-string">'localhost'</span>,
48193 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
48194 <span class="hljs-attr">exclusive</span>: <span class="hljs-literal">true</span>
48204 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48206 <li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48207 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48216 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48217 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48218 <li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48219 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48237 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
48244 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
48292 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
48294 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
48296 <li><code>allowHalfOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
48300 <li><code>readable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48302 <li><code>writable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48316 <li><code>hadError</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48331 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
48361 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error…
48380 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> |…
48381 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48382 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
48383 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48403 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
48412 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
48414 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
48433 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
48441 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
48481 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
48482 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48491 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48492 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48493 <li><code>localAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
48494 <li><code>localPort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
48495 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
48497 <li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
48498 <li><code>lookup</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
48502 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48503 …paths_for_ipc_connections">Identifying paths for IPC connections</a>. If provided, the TCP-specific
48508 <li><code>onread</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
48515 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array><…
48517 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48526 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
48527 net.<span class="hljs-title function_">connect</span>({
48528 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
48529 <span class="hljs-attr">onread</span>: {
48530 <span class="hljs-comment">// Reuses a 4KiB Buffer for every read from the socket.</span>
48531-attr">buffer</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title functi…
48532 …<span class="hljs-attr">callback</span>: <span class="hljs-keyword">function</span>(<span class="h…
48533 <span class="hljs-comment">// Received data is available in `buf` from 0 to `nread`.</span>
48534-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
48540 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48542 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48555 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48556 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48557 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48570 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
48584 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
48592 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
48601-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
48602 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48603 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48606 <p>Half-closes the socket. i.e., it sends a FIN packet. It is possible the
48614 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
48625 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
48639 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
48659 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
48669 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
48677 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
48690 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48708 <li><code>enable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
48709 <li><code>initialDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
48712 <p>Enable/disable keep-alive functionality, and optionally set the initial
48718 <p>Enabling the keep-alive functionality will set the following socket options:</p>
48730 <li><code>noDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48745 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48746 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48754 <pre><code class="language-js">socket.<span class="hljs-built_in">setTimeout</span>(<span class="hl…
48755 socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'timeout'</span>, <sp…
48756 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48757 socket.<span class="hljs-title function_">end</span>();
48760 <p>The optional <code>callback</code> parameter will be added as a one-time listener for the
48767 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
48786-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
48787 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48788 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48789 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
48805 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
48830 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
48831 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48841 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48842 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48852 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48853 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48854 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48880 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
48884 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48894 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48900 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
48901-keyword">const</span> client = net.<span class="hljs-title function_">createConnection</span>({ <…
48902 <span class="hljs-comment">// 'connect' listener.</span>
48903 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48904 …client.<span class="hljs-title function_">write</span>(<span class="hljs-string">'world!\r\n'</spa…
48906 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
48907 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48908 client.<span class="hljs-title function_">end</span>();
48910 client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span c…
48911 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48914-js"><span class="hljs-keyword">const</span> client = net.<span class="hljs-title function_">creat…
48920 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48923 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48939 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48941 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48944 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48960 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
48962 <li><code>allowHalfOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
48965 <li><code>pauseOnConnect</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
48969 <li><code>connectionListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
48979 connection is half-closed (non-readable but still writable). See <a href="#net_event_end"><code>'en…
48990 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
48991 …s="hljs-keyword">const</span> server = net.<span class="hljs-title function_">createServer</span>(…
48992 <span class="hljs-comment">// 'connection' listener.</span>
48993 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48994 …c.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class…
48995 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48997 c.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello\r\n'</span>);
48998 c.<span class="hljs-title function_">pipe</span>(c);
49000 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
49001 <span class="hljs-keyword">throw</span> err;
49003 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8124</span>, <spa…
49004 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
49007 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">te…
49009 …s="language-js">server.<span class="hljs-title function_">listen</span>(<span class="hljs-string">…
49010 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
49013 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">nc…
49019 <li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
49020 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
49030 <li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
49031 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
49039 <li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
49040 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
49046 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
49048 <p>The <code>os</code> module provides operating system-related utility methods and
49050 …<code class="language-js"><span class="hljs-keyword">const</span> os = <span class="hljs-built_in"…
49056 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
49058 <p>The operating system-specific end-of-line marker.</p>
49068 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49079 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
49081 <p>Contains commonly used operating system-specific constants for error codes,
49089 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
49094 <li><code>model</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
49095 <li><code>speed</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
49096 <li><code>times</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
49098 <li><code>user</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
49099 <li><code>nice</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
49100 <li><code>sys</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
49101 <li><code>idle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
49102 <li><code>irq</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
49106 <!-- eslint-disable semi -->
49107 <pre><code class="language-js">[
49109 …<span class="hljs-attr">model</span>: <span class="hljs-string">'Intel(R) Core(TM) i7 CPU …
49110 <span class="hljs-attr">speed</span>: <span class="hljs-number">2926</span>,
49111 <span class="hljs-attr">times</span>: {
49112 <span class="hljs-attr">user</span>: <span class="hljs-number">252020</span>,
49113 <span class="hljs-attr">nice</span>: <span class="hljs-number">0</span>,
49114 <span class="hljs-attr">sys</span>: <span class="hljs-number">30340</span>,
49115 <span class="hljs-attr">idle</span>: <span class="hljs-number">1070356870</span>,
49116 <span class="hljs-attr">irq</span>: <span class="hljs-number">0</span>
49120 …<span class="hljs-attr">model</span>: <span class="hljs-string">'Intel(R) Core(TM) i7 CPU …
49121 <span class="hljs-attr">speed</span>: <span class="hljs-number">2926</span>,
49122 <span class="hljs-attr">times</span>: {
49123 <span class="hljs-attr">user</span>: <span class="hljs-number">306960</span>,
49124 <span class="hljs-attr">nice</span>: <span class="hljs-number">0</span>,
49125 <span class="hljs-attr">sys</span>: <span class="hljs-number">26980</span>,
49126 <span class="hljs-attr">idle</span>: <span class="hljs-number">1071569080</span>,
49127 <span class="hljs-attr">irq</span>: <span class="hljs-number">0</span>
49131 …<span class="hljs-attr">model</span>: <span class="hljs-string">'Intel(R) Core(TM) i7 CPU …
49132 <span class="hljs-attr">speed</span>: <span class="hljs-number">2926</span>,
49133 <span class="hljs-attr">times</span>: {
49134 <span class="hljs-attr">user</span>: <span class="hljs-number">248450</span>,
49135 <span class="hljs-attr">nice</span>: <span class="hljs-number">0</span>,
49136 <span class="hljs-attr">sys</span>: <span class="hljs-number">21750</span>,
49137 <span class="hljs-attr">idle</span>: <span class="hljs-number">1070919370</span>,
49138 <span class="hljs-attr">irq</span>: <span class="hljs-number">0</span>
49142 …<span class="hljs-attr">model</span>: <span class="hljs-string">'Intel(R) Core(TM) i7 CPU …
49143 <span class="hljs-attr">speed</span>: <span class="hljs-number">2926</span>,
49144 <span class="hljs-attr">times</span>: {
49145 <span class="hljs-attr">user</span>: <span class="hljs-number">256880</span>,
49146 <span class="hljs-attr">nice</span>: <span class="hljs-number">0</span>,
49147 <span class="hljs-attr">sys</span>: <span class="hljs-number">19430</span>,
49148 <span class="hljs-attr">idle</span>: <span class="hljs-number">1070905480</span>,
49149 <span class="hljs-attr">irq</span>: <span class="hljs-number">20</span>
49153 <p><code>nice</code> values are POSIX-only. On Windows, the <code>nice</code> values of all process…
49160 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
49162 <p>The platform-specific file path of the null device.</p>
49172 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49182 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
49190 <li><code>pid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
49192 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
49201 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49213 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49221 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
49226 <p>The load average is a Unix-specific concept. On Windows, the return value is
49233 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
49241 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
49242 <li><code>netmask</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
49243 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
49244 <li><code>mac</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
49245 <li><code>internal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
49247 <li><code>scopeid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
49249 <li><code>cidr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
49253 <!-- eslint-skip -->
49254 <pre><code class="language-js">{
49255 <span class="hljs-attr">lo</span>: [
49257 <span class="hljs-attr">address</span>: <span class="hljs-string">'127.0.0.1'</span>,
49258 <span class="hljs-attr">netmask</span>: <span class="hljs-string">'255.0.0.0'</span>,
49259 <span class="hljs-attr">family</span>: <span class="hljs-string">'IPv4'</span>,
49260 <span class="hljs-attr">mac</span>: <span class="hljs-string">'00:00:00:00:00:00'</span>,
49261 <span class="hljs-attr">internal</span>: <span class="hljs-literal">true</span>,
49262 <span class="hljs-attr">cidr</span>: <span class="hljs-string">'127.0.0.1/8'</span>
49265 <span class="hljs-attr">address</span>: <span class="hljs-string">'::1'</span>,
49266 …<span class="hljs-attr">netmask</span>: <span class="hljs-string">'ffff:ffff:ffff:ffff:ffff:ffff:f…
49267 <span class="hljs-attr">family</span>: <span class="hljs-string">'IPv6'</span>,
49268 <span class="hljs-attr">mac</span>: <span class="hljs-string">'00:00:00:00:00:00'</span>,
49269 <span class="hljs-attr">scopeid</span>: <span class="hljs-number">0</span>,
49270 <span class="hljs-attr">internal</span>: <span class="hljs-literal">true</span>,
49271 <span class="hljs-attr">cidr</span>: <span class="hljs-string">'::1/128'</span>
49274 <span class="hljs-attr">eth0</span>: [
49276 <span class="hljs-attr">address</span>: <span class="hljs-string">'192.168.1.108'</span>,
49277 <span class="hljs-attr">netmask</span>: <span class="hljs-string">'255.255.255.0'</span>,
49278 <span class="hljs-attr">family</span>: <span class="hljs-string">'IPv4'</span>,
49279 <span class="hljs-attr">mac</span>: <span class="hljs-string">'01:02:03:0a:0b:0c'</span>,
49280 <span class="hljs-attr">internal</span>: <span class="hljs-literal">false</span>,
49281 <span class="hljs-attr">cidr</span>: <span class="hljs-string">'192.168.1.108/24'</span>
49284 …<span class="hljs-attr">address</span>: <span class="hljs-string">'fe80::a00:27ff:fe4e:66a1'</span…
49285 … <span class="hljs-attr">netmask</span>: <span class="hljs-string">'ffff:ffff:ffff:ffff::'</span>,
49286 <span class="hljs-attr">family</span>: <span class="hljs-string">'IPv6'</span>,
49287 <span class="hljs-attr">mac</span>: <span class="hljs-string">'01:02:03:0a:0b:0c'</span>,
49288 <span class="hljs-attr">scopeid</span>: <span class="hljs-number">1</span>,
49289 <span class="hljs-attr">internal</span>: <span class="hljs-literal">false</span>,
49290 …<span class="hljs-attr">cidr</span>: <span class="hljs-string">'fe80::a00:27ff:fe4e:66a1/64'</span>
49299 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49306 …s://github.com/nodejs/node/blob/HEAD/BUILDING.md#androidandroid-based-devices-eg-firefox-os">Andro…
49312 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49323 <li><code>pid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
49325 <li><code>priority</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
49329 <p>The <code>priority</code> input must be an integer between <code>-20</code> (high priority) and …
49344 <td><p>This function is now cross-platform consistent and no longer returns a path with a trailing …
49351 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49360 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
49368 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49387 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
49395 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
49397 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
49402 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
49407 <code>gid</code> fields are <code>-1</code>, and <code>shell</code> is <code>null</code>.</p>
49418 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String…
49491 <td>Sent to a process to identify user-defined conditions.</td>
49931 <h5>Windows-specific error constants<span><a class="mark" href="#os_windows_specific_error_constant…
50175 are exported in <code>os.constants.dlopen</code>. See <a href="http://man7.org/linux/man-pages/man3…
50203 <td>Make a self-contained library use its own symbols in preference to
50242 <code>-7</code> on all other platforms.</td>
50249 <code>-14</code> on all other platforms.</td>
50255 <code>-20</code> on all other platforms.</td>
50272 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
50276 …code class="language-js"><span class="hljs-keyword">const</span> path = <span class="hljs-built_in…
50280 Windows operating system, the <code>path</code> module will assume that Windows-style
50284 <pre><code class="language-js">path.<span class="hljs-title function_">basename</span>(<span class=…
50285 <span class="hljs-comment">// Returns: 'C:\\temp\\myfile.html'</span></code></pre>
50287 <pre><code class="language-js">path.<span class="hljs-title function_">basename</span>(<span class=…
50288 <span class="hljs-comment">// Returns: 'myfile.html'</span></code></pre>
50292 … class="language-js">path.<span class="hljs-property">win32</span>.<span class="hljs-title functio…
50293 <span class="hljs-comment">// Returns: 'myfile.html'</span></code></pre>
50297 … class="language-js">path.<span class="hljs-property">posix</span>.<span class="hljs-title functio…
50298 <span class="hljs-comment">// Returns: 'myfile.html'</span></code></pre>
50299 <p>On Windows Node.js follows the concept of per-drive working directory.
50303 <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-r…
50310 <td><p>Passing a non-string as the <code>path</code> argument will throw now.</p></td></tr>
50317 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50318 <li><code>ext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
50319 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50324 <pre><code class="language-js">path.<span class="hljs-title function_">basename</span>(<span class=…
50325 <span class="hljs-comment">// Returns: 'quux.html'</span>
50327 ….<span class="hljs-title function_">basename</span>(<span class="hljs-string">'/foo/bar/baz/asdf/q…
50328 <span class="hljs-comment">// Returns: 'quux'</span></code></pre>
50330 case-insensitive manner, this function does not. For example, <code>C:\\foo.html</code> and
50332 case-sensitive string:</p>
50333-js">path.<span class="hljs-property">win32</span>.<span class="hljs-title function_">basename</sp…
50334 <span class="hljs-comment">// Returns: 'foo'</span>
50336 …"hljs-property">win32</span>.<span class="hljs-title function_">basename</span>(<span class="hljs-
50337 <span class="hljs-comment">// Returns: 'foo.HTML'</span></code></pre>
50345 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
50347 <p>Provides the platform-specific path delimiter:</p>
50353-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
50354 <span class="hljs-comment">// Prints: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'</span>
50356 …lass="hljs-property">env</span>.<span class="hljs-property">PATH</span>.<span class="hljs-title fu…
50357 <span class="hljs-comment">// Returns: ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']…
50359-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
50360 <span class="hljs-comment">// Prints: 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'</span>
50362 …lass="hljs-property">env</span>.<span class="hljs-property">PATH</span>.<span class="hljs-title fu…
50363 <span class="hljs-comment">// Returns ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\…
50370 <td><p>Passing a non-string as the <code>path</code> argument will throw now.</p></td></tr>
50377 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50378 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50383 <pre><code class="language-js">path.<span class="hljs-title function_">dirname</span>(<span class="…
50384 <span class="hljs-comment">// Returns: '/foo/bar/baz/asdf'</span></code></pre>
50392 <td><p>Passing a non-string as the <code>path</code> argument will throw now.</p></td></tr>
50399 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50400 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50407 <pre><code class="language-js">path.<span class="hljs-title function_">extname</span>(<span class="…
50408 <span class="hljs-comment">// Returns: '.html'</span>
50410 path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index.coffee.md'<…
50411 <span class="hljs-comment">// Returns: '.md'</span>
50413 path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index.'</span>);
50414 <span class="hljs-comment">// Returns: '.'</span>
50416 path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index'</span>);
50417 <span class="hljs-comment">// Returns: ''</span>
50419 path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'.index'</span>);
50420 <span class="hljs-comment">// Returns: ''</span>
50422 path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'.index.md'</span>…
50423 <span class="hljs-comment">// Returns: '.md'</span></code></pre>
50430 <li><code>pathObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere…
50432 <li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
50433 <li><code>root</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50434 <li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50435 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50436 <li><code>ext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
50439 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50450 <pre><code class="language-js"><span class="hljs-comment">// If `dir`, `root` and `base` are provid…
50451 <span class="hljs-comment">// `${dir}${path.sep}${base}`</span>
50452 <span class="hljs-comment">// will be returned. `root` is ignored.</span>
50453 path.<span class="hljs-title function_">format</span>({
50454 <span class="hljs-attr">root</span>: <span class="hljs-string">'/ignored'</span>,
50455 <span class="hljs-attr">dir</span>: <span class="hljs-string">'/home/user/dir'</span>,
50456 <span class="hljs-attr">base</span>: <span class="hljs-string">'file.txt'</span>
50458 <span class="hljs-comment">// Returns: '/home/user/dir/file.txt'</span>
50460 <span class="hljs-comment">// `root` will be used if `dir` is not specified.</span>
50461 <span class="hljs-comment">// If only `root` is provided or `dir` is equal to `root` then the</span>
50462 <span class="hljs-comment">// platform separator will not be included. `ext` will be ignored.</span>
50463 path.<span class="hljs-title function_">format</span>({
50464 <span class="hljs-attr">root</span>: <span class="hljs-string">'/'</span>,
50465 <span class="hljs-attr">base</span>: <span class="hljs-string">'file.txt'</span>,
50466 <span class="hljs-attr">ext</span>: <span class="hljs-string">'ignored'</span>
50468 <span class="hljs-comment">// Returns: '/file.txt'</span>
50470 <span class="hljs-comment">// `name` + `ext` will be used if `base` is not specified.</span>
50471 path.<span class="hljs-title function_">format</span>({
50472 <span class="hljs-attr">root</span>: <span class="hljs-string">'/'</span>,
50473 <span class="hljs-attr">name</span>: <span class="hljs-string">'file'</span>,
50474 <span class="hljs-attr">ext</span>: <span class="hljs-string">'.txt'</span>
50476 <span class="hljs-comment">// Returns: '/file.txt'</span></code></pre>
50478 <pre><code class="language-js">path.<span class="hljs-title function_">format</span>({
50479 <span class="hljs-attr">dir</span>: <span class="hljs-string">'C:\\path\\dir'</span>,
50480 <span class="hljs-attr">base</span>: <span class="hljs-string">'file.txt'</span>
50482 <span class="hljs-comment">// Returns: 'C:\\path\\dir\\file.txt'</span></code></pre>
50488 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50489 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
50492 <p>If the given <code>path</code> is a zero-length string, <code>false</code> will be returned.</p>
50494 …ass="language-js">path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-stri…
50495 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'/baz/..'</span…
50496 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'qux/'</span>);…
50497 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'.'</span>); …
50499 …s="language-js">path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string…
50500 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'\\\\server'</s…
50501 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'C:/foo/..'</sp…
50502 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'C:\\foo\\..'</…
50503 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'bar\\baz'</spa…
50504 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'bar/baz'</span…
50505 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'.'</span>); …
50512 <li><code>...paths</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
50513 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50516 platform-specific separator as a delimiter, then normalizes the resulting path.</p>
50517 <p>Zero-length <code>path</code> segments are ignored. If the joined path string is a
50518 zero-length string then <code>'.'</code> will be returned, representing the current
50520-js">path.<span class="hljs-title function_">join</span>(<span class="hljs-string">'/foo'</span>, …
50521 <span class="hljs-comment">// Returns: '/foo/bar/baz/asdf'</span>
50523 path.<span class="hljs-title function_">join</span>(<span class="hljs-string">'foo'</span>, {}, <sp…
50524 <span class="hljs-comment">// Throws 'TypeError: Path must be a string. Received {}'</span></code><…
50531 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50532 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50538 instance of the platform-specific path segment separator (<code>/</code> on POSIX and
50540 <p>If the <code>path</code> is a zero-length string, <code>'.'</code> is returned, representing the
50543 <pre><code class="language-js">path.<span class="hljs-title function_">normalize</span>(<span class…
50544 <span class="hljs-comment">// Returns: '/foo/bar/baz/asdf'</span></code></pre>
50546 <pre><code class="language-js">path.<span class="hljs-title function_">normalize</span>(<span class…
50547 <span class="hljs-comment">// Returns: 'C:\\temp\\foo\\'</span></code></pre>
50550 … class="language-js">path.<span class="hljs-property">win32</span>.<span class="hljs-title functio…
50551 <span class="hljs-comment">// Returns: 'C:\\temp\\foo\\bar'</span></code></pre>
50558 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50559 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
50566 <li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
50567 <li><code>root</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50568 <li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50569 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50570 <li><code>ext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
50573 <pre><code class="language-js">path.<span class="hljs-title function_">parse</span>(<span class="hl…
50574 <span class="hljs-comment">// Returns:</span>
50575 <span class="hljs-comment">// { root: '/',</span>
50576 <span class="hljs-comment">// dir: '/home/user/dir',</span>
50577 <span class="hljs-comment">// base: 'file.txt',</span>
50578 <span class="hljs-comment">// ext: '.txt',</span>
50579 <span class="hljs-comment">// name: 'file' }</span></code></pre>
50580 <pre><code class="language-text">┌─────────────────────┬────────────┐
50588 <pre><code class="language-js">path.<span class="hljs-title function_">parse</span>(<span class="hl…
50589 <span class="hljs-comment">// Returns:</span>
50590 <span class="hljs-comment">// { root: 'C:\\',</span>
50591 <span class="hljs-comment">// dir: 'C:\\path\\dir',</span>
50592 <span class="hljs-comment">// base: 'file.txt',</span>
50593 <span class="hljs-comment">// ext: '.txt',</span>
50594 <span class="hljs-comment">// name: 'file' }</span></code></pre>
50595 <pre><code class="language-text">┌─────────────────────┬────────────┐
50608 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
50625 <li><code>from</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50626 <li><code>to</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
50627 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50631 path (after calling <code>path.resolve()</code> on each), a zero-length string is returned.</p>
50632 <p>If a zero-length string is passed as <code>from</code> or <code>to</code>, the current working
50633 directory will be used instead of the zero-length strings.</p>
50635 …anguage-js">path.<span class="hljs-title function_">relative</span>(<span class="hljs-string">'/da…
50636 <span class="hljs-comment">// Returns: '../../impl/bbb'</span></code></pre>
50638 …anguage-js">path.<span class="hljs-title function_">relative</span>(<span class="hljs-string">'C:\…
50639 <span class="hljs-comment">// Returns: '..\\..\\impl\\bbb'</span></code></pre>
50646 <li><code>...paths</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
50647 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50660 <p>Zero-length <code>path</code> segments are ignored.</p>
50663 …class="language-js">path.<span class="hljs-title function_">resolve</span>(<span class="hljs-strin…
50664 <span class="hljs-comment">// Returns: '/foo/bar/baz'</span>
50666 path.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'/foo/bar'</span>,…
50667 <span class="hljs-comment">// Returns: '/tmp/file'</span>
50669 …js-title function_">resolve</span>(<span class="hljs-string">'wwwroot'</span>, <span class="hljs-s…
50670 <span class="hljs-comment">// If the current working directory is /home/myself/node,</span>
50671 <span class="hljs-comment">// this returns '/home/myself/node/wwwroot/static_files/gif/image.gif'</…
50678 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
50680 <p>Provides the platform-specific path segment separator:</p>
50686 …class="language-js"><span class="hljs-string">'foo/bar/baz'</span>.<span class="hljs-title functio…
50687 <span class="hljs-comment">// Returns: ['foo', 'bar', 'baz']</span></code></pre>
50689 …lass="language-js"><span class="hljs-string">'foo\\bar\\baz'</span>.<span class="hljs-title functi…
50690 <span class="hljs-comment">// Returns: ['foo', 'bar', 'baz']</span></code></pre>
50699 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50700 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50702 …valent <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#namespaces">…
50706 method is non-operational and always returns <code>path</code> without modifications.</p>
50712 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
50714 <p>The <code>path.win32</code> property provides access to Windows-specific implementations
50719 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
50722 <a href="https://w3c.github.io/perf-timing-primer/">Web Performance APIs</a> as well as additional …
50723 Node.js-specific performance measurements.</p>
50724 <p>Node.js supports the following <a href="https://w3c.github.io/perf-timing-primer/">Web Performan…
50726 <li><a href="https://www.w3.org/TR/hr-time-2">High Resolution Time</a></li>
50727 <li><a href="https://w3c.github.io/performance-timeline/">Performance Timeline</a></li>
50728 <li><a href="https://www.w3.org/TR/user-timing/">User Timing</a></li>
50730-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">PerformanceObserver…
50732-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
50733-variable language_">console</span>.<span class="hljs-title function_">log</span>(items.<span clas…
50734 performance.<span class="hljs-title function_">clearMarks</span>();
50736 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: …
50737 performance.<span class="hljs-title function_">measure</span>(<span class="hljs-string">'Start to N…
50739 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'A'</span>);
50740 <span class="hljs-title function_">doSomeLongRunningProcess</span>(<span class="hljs-function">() =…
50741 …erformance.<span class="hljs-title function_">measure</span>(<span class="hljs-string">'A to Now'<…
50743 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'B'</span>);
50744 …lass="hljs-title function_">measure</span>(<span class="hljs-string">'A to B'</span>, <span class=…
50751 Node.js instance. It is similar to <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window…
50757 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50766 <li><code>utilization1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
50768 <li><code>utilization2</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
50770 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objec…
50772 <li><code>idle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50773 <li><code>active</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
50774 <li><code>utilization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
50798 <pre><code class="language-js"><span class="hljs-meta">'use strict'</span>;
50799-keyword">const</span> { eventLoopUtilization } = <span class="hljs-built_in">require</span>(<span…
50800 <span class="hljs-keyword">const</span> { spawnSync } = <span class="hljs-built_in">require</span>(…
50802 <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> {
50803 …<span class="hljs-keyword">const</span> elu = <span class="hljs-title function_">eventLoopUtilizat…
50804 …<span class="hljs-title function_">spawnSync</span>(<span class="hljs-string">'sleep'</span>, [<sp…
50805-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
50810 <p>Passing in a user-defined object instead of the result of a previous call to
50818 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50838 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50839 <li><code>startMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
50840 <li><code>endMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
50850 …code>startMark</code> is set to <a href="https://w3c.github.io/hr-time/#dom-performance-timeorigin…
50871 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
50880 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
50882 <p>The <a href="https://w3c.github.io/hr-time/#dom-performance-timeorigin"><code>timeOrigin</code><…
50889 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
50895 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
50897 <span class="hljs-title class_">PerformanceObserver</span>
50898 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
50900 <span class="hljs-keyword">function</span> <span class="hljs-title function_">someFunction</span>(<…
50901 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
50904 <span class="hljs-keyword">const</span> wrapped = performance.<span class="hljs-title function_">ti…
50906-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
50907-variable language_">console</span>.<span class="hljs-title function_">log</span>(list.<span class…
50908 obs.<span class="hljs-title function_">disconnect</span>();
50910 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: …
50912 <span class="hljs-comment">// A performance timeline entry will be created</span>
50913 <span class="hljs-title function_">wrapped</span>();</code></pre>
50923 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
50932 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
50949 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
50969 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
50977 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
50994 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51013 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51017 has the value of -1.</p>
51023 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51032 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51044 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51047 exited. If the event loop has not yet exited, the property has the value of -1.
51048 It can only have a value of not -1 in a handler of the <a href="#process_event_exit"><code>'exit'</…
51054 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51058 main script), the property has the value of -1.</p>
51064 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51073 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51083 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
51092 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
51094 <span class="hljs-title class_">PerformanceObserver</span>
51095 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51097-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51098 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
51099 observer.<span class="hljs-title function_">disconnect</span>();
51101-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51103 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'test'</span>)…
51122 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
51124 <li><code>entryTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
51127 <li><code>buffered</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
51138 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
51140 <span class="hljs-title class_">PerformanceObserver</span>
51141 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51143-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51144 <span class="hljs-comment">// Called three times synchronously. `list` contains one item.</span>
51146 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: …
51148 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-num…
51149 …performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">`test<span cl…
51150 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
51152 <span class="hljs-title class_">PerformanceObserver</span>
51153 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51155-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51156 <span class="hljs-comment">// Called once. `list` contains three items.</span>
51158-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51160 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-num…
51161 …performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">`test<span cl…
51178 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
51180 <span class="hljs-title class_">PerformanceObserver</span>
51181 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51183-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51184 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
51185 <span class="hljs-comment">/**
51201 observer.<span class="hljs-title function_">disconnect</span>();
51203-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51205 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'test'</span>);
51206 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'meow'</span>)…
51212 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
51213 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
51220 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
51222 <span class="hljs-title class_">PerformanceObserver</span>
51223 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51225-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51226-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList…
51227 <span class="hljs-comment">/**
51237-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList…
51239-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList…
51240 <span class="hljs-comment">/**
51250-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList…
51251 observer.<span class="hljs-title function_">disconnect</span>();
51253-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51255 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'test'</span>);
51256 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'meow'</span>)…
51262 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
51268 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
51270 <span class="hljs-title class_">PerformanceObserver</span>
51271 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51273-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51274-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList…
51275 <span class="hljs-comment">/**
51291 observer.<span class="hljs-title function_">disconnect</span>();
51293-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51295 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'test'</span>);
51296 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'meow'</span>)…
51302 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
51304 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
51306 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
51308 <li><code>figures</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
51320 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
51322 <li><code>resolution</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
51336 …"language-js"><span class="hljs-keyword">const</span> { monitorEventLoopDelay } = <span class="hlj…
51337-keyword">const</span> h = <span class="hljs-title function_">monitorEventLoopDelay</span>({ <span…
51338 h.<span class="hljs-title function_">enable</span>();
51339 <span class="hljs-comment">// Do something.</span>
51340 h.<span class="hljs-title function_">disable</span>();
51341 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
51342 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
51343 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
51344 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
51345 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
51346-variable language_">console</span>.<span class="hljs-title function_">log</span>(h.<span class="h…
51347-variable language_">console</span>.<span class="hljs-title function_">log</span>(h.<span class="h…
51357 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51366 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51374 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51382 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51390 <li><code>percentile</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
51391 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
51399 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" …
51412 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51422 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
51431 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
51448 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
51461 <pre><code class="language-js"><span class="hljs-meta">'use strict'</span>;
51462 <span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built_in">require</span>(<s…
51463 <span class="hljs-keyword">const</span> {
51465 <span class="hljs-title class_">PerformanceObserver</span>
51466 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51468 <span class="hljs-keyword">const</span> set = <span class="hljs-keyword">new</span> <span class="hl…
51469 <span class="hljs-keyword">const</span> hook = async_hooks.<span class="hljs-title function_">creat…
51470 <span class="hljs-title function_">init</span>(<span class="hljs-params">id, type</span>) {
51471 <span class="hljs-keyword">if</span> (type === <span class="hljs-string">'Timeout'</span>) {
51472 …ormance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">`Timeout-<span cl…
51473 set.<span class="hljs-title function_">add</span>(id);
51476 <span class="hljs-title function_">destroy</span>(<span class="hljs-params">id</span>) {
51477 <span class="hljs-keyword">if</span> (set.<span class="hljs-title function_">has</span>(id)) {
51478 set.<span class="hljs-title function_">delete</span>(id);
51479 …ormance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">`Timeout-<span cl…
51480 …performance.<span class="hljs-title function_">measure</span>(<span class="hljs-string">`Timeout-<…
51481 … <span class="hljs-string">`Timeout-<span class="hljs-subst">${id}</span>-Init`</span>,
51482 … <span class="hljs-string">`Timeout-<span class="hljs-subst">${id}</span>-Destroy`</span>);
51486 hook.<span class="hljs-title function_">enable</span>();
51488-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51489-variable language_">console</span>.<span class="hljs-title function_">log</span>(list.<span class…
51490 performance.<span class="hljs-title function_">clearMarks</span>();
51491 observer.<span class="hljs-title function_">disconnect</span>();
51493-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51495 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {}, <span cl…
51499 <!-- eslint-disable no-global-assign -->
51500 <pre><code class="language-js"><span class="hljs-meta">'use strict'</span>;
51501 <span class="hljs-keyword">const</span> {
51503 <span class="hljs-title class_">PerformanceObserver</span>
51504 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51505 <span class="hljs-keyword">const</span> mod = <span class="hljs-built_in">require</span>(<span clas…
51507 <span class="hljs-comment">// Monkey patch the require function</span>
51508 …pan class="hljs-property">Module</span>.<span class="hljs-property"><span class="hljs-keyword">pro…
51509-title function_">timerify</span>(mod.<span class="hljs-property">Module</span>.<span class="hljs-
51510 <span class="hljs-built_in">require</span> = performance.<span class="hljs-title function_">timerif…
51512 <span class="hljs-comment">// Activate the observer</span>
51513-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51514 …<span class="hljs-keyword">const</span> entries = list.<span class="hljs-title function_">getEntri…
51515 …entries.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class…
51516-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
51518 obs.<span class="hljs-title function_">disconnect</span>();
51520-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51522 <span class="hljs-built_in">require</span>(<span class="hljs-string">'some-module'</span>);</code><…
51527 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
51535 <p>A best practice would be to ensure that the policy manifest is read-only for
51542 <p>The <code>--experimental-policy</code> flag can be used to enable features for policies
51546 <pre><code class="language-bash">node --experimental-policy=policy.json app.js</code></pre>
51550 the policy file itself may be provided via <code>--policy-integrity</code>.
51553 <pre><code class="language-bash">node --experimental-policy=policy.json --policy-integrity=<span cl…
51567 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51568 …<span class="hljs-attr">"onerror"</span><span class="hljs-punctuation">:</span> <span class="hljs-
51569 …<span class="hljs-attr">"resources"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51570 …<span class="hljs-attr">"./app/checked.js"</span><span class="hljs-punctuation">:</span> <span cla…
51571 …<span class="hljs-attr">"integrity"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51572 <span class="hljs-punctuation">}</span>
51573 <span class="hljs-punctuation">}</span>
51574 <span class="hljs-punctuation">}</span></code></pre>
51578 <a href="https://www.w3.org/TR/SRI/#the-integrity-attribute">integrity attribute</a>
51584 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51585 …<span class="hljs-attr">"resources"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51586 …<span class="hljs-attr">"./app/checked.js"</span><span class="hljs-punctuation">:</span> <span cla…
51587 …<span class="hljs-attr">"integrity"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51588 <span class="hljs-punctuation">}</span>
51589 <span class="hljs-punctuation">}</span>
51590 <span class="hljs-punctuation">}</span></code></pre>
51594 <li>A <a href="https://url.spec.whatwg.org/#relative-url-with-fragment-string">relative-URL string<…
51601 <code>node -e 'process.stdout.write("sha256-");process.stdin.pipe(crypto.createHash("sha256").setEn…
51612 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51613 …<span class="hljs-attr">"resources"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51614 …<span class="hljs-attr">"./app/checked.js"</span><span class="hljs-punctuation">:</span> <span cla…
51615 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
51616 …<span class="hljs-attr">"fs"</span><span class="hljs-punctuation">:</span> <span class="hljs-keywo…
51617 …ss="hljs-attr">"os"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./app…
51618-attr">"http"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span…
51619 <span class="hljs-punctuation">}</span>
51620 <span class="hljs-punctuation">}</span>
51621 <span class="hljs-punctuation">}</span>
51622 <span class="hljs-punctuation">}</span></code></pre>
51634 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51635 …<span class="hljs-attr">"resources"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51636 …<span class="hljs-attr">"file:///C:/app/utils.js"</span><span class="hljs-punctuation">:</span> <s…
51637 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
51638 …<span class="hljs-attr">"./utils.js"</span><span class="hljs-punctuation">:</span> <span class="hl…
51639 <span class="hljs-punctuation">}</span>
51640 <span class="hljs-punctuation">}</span>
51641 <span class="hljs-punctuation">}</span>
51642 <span class="hljs-punctuation">}</span></code></pre>
51644 and redirected to <code>file:///C:/app/utils-v2.js</code> instead regardless of using an
51676 …de class="language-js"><span class="hljs-keyword">const</span> original = <span class="hljs-built_…
51677-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-
51678 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">time</span…
51679 <span class="hljs-keyword">try</span> {
51680 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span>.<span class="hljs-p…
51681 …<span class="hljs-title class_">Reflect</span>.<span class="hljs-title function_">construct</span>…
51682 …<span class="hljs-title class_">Reflect</span>.<span class="hljs-title function_">apply</span>(ori…
51683 } <span class="hljs-keyword">finally</span> {
51684 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">timeEnd</s…
51693 <a href="https://url.spec.whatwg.org/#special-scheme">special schemes</a>, keeping trailing <code>"…
51695 If the URL is non-special the scope will be located by the URL's origin. If no
51706 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51707 …<span class="hljs-attr">"scopes"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51708-attr">"file:///C:/app/"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuati…
51709-attr">"file:"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</spa…
51710 …<span class="hljs-attr">""</span><span class="hljs-punctuation">:</span> <span class="hljs-punctua…
51711 <span class="hljs-punctuation">}</span>
51712 <span class="hljs-punctuation">}</span></code></pre>
51764 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51765 …<span class="hljs-attr">"scopes"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51766 …<span class="hljs-attr">"file:"</span><span class="hljs-punctuation">:</span> <span class="hljs-pu…
51767 …<span class="hljs-attr">"integrity"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51768 <span class="hljs-punctuation">}</span>
51769 <span class="hljs-punctuation">}</span>
51770 <span class="hljs-punctuation">}</span></code></pre>
51774 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51775 …<span class="hljs-attr">"resources"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51776 …<span class="hljs-attr">"./app/checked.js"</span><span class="hljs-punctuation">:</span> <span cla…
51777 …<span class="hljs-attr">"cascade"</span><span class="hljs-punctuation">:</span> <span class="hljs-
51778 …<span class="hljs-attr">"integrity"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51779 <span class="hljs-punctuation">}</span>
51780 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
51781 …<span class="hljs-attr">"scopes"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51782 …<span class="hljs-attr">"./app/"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51783 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
51784 …<span class="hljs-attr">"fs"</span><span class="hljs-punctuation">:</span> <span class="hljs-keywo…
51785 <span class="hljs-punctuation">}</span>
51786 <span class="hljs-punctuation">}</span>
51787 <span class="hljs-punctuation">}</span>
51788 <span class="hljs-punctuation">}</span></code></pre>
51790 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51791 …<span class="hljs-attr">"resources"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51792 …<span class="hljs-attr">"data:text/javascript,import('fs');"</span><span class="hljs-punctuation">…
51793 …<span class="hljs-attr">"cascade"</span><span class="hljs-punctuation">:</span> <span class="hljs-
51794 …<span class="hljs-attr">"integrity"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51795 <span class="hljs-punctuation">}</span>
51796 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
51797 …<span class="hljs-attr">"scopes"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51798 …<span class="hljs-attr">"data:"</span><span class="hljs-punctuation">:</span> <span class="hljs-pu…
51799 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
51800 …<span class="hljs-attr">"fs"</span><span class="hljs-punctuation">:</span> <span class="hljs-keywo…
51801 <span class="hljs-punctuation">}</span>
51802 <span class="hljs-punctuation">}</span>
51803 <span class="hljs-punctuation">}</span>
51804 <span class="hljs-punctuation">}</span></code></pre>
51805 <h5>Example: <a href="https://url.spec.whatwg.org/#relative-url-with-fragment-string">import maps</…
51807 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51808 …<span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
51809 …<span class="hljs-attr">"react"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
51810 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
51811 …<span class="hljs-attr">"scopes"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51812 …<span class="hljs-attr">"./ssr/"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51813 …<span class="hljs-attr">"react"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
51814 <span class="hljs-punctuation">}</span>
51815 <span class="hljs-punctuation">}</span>
51816 <span class="hljs-punctuation">}</span></code></pre>
51817 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51818 …an class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51819 …<span class="hljs-attr">"scopes"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51820 …<span class="hljs-attr">""</span><span class="hljs-punctuation">:</span> <span class="hljs-punctua…
51821 …<span class="hljs-attr">"cascade"</span><span class="hljs-punctuation">:</span> <span class="hljs-
51822 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
51823 …<span class="hljs-attr">"react"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
51824 <span class="hljs-punctuation">}</span>
51825 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
51826 …<span class="hljs-attr">"./ssr/"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51827 …<span class="hljs-attr">"cascade"</span><span class="hljs-punctuation">:</span> <span class="hljs-
51828 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
51829 …<span class="hljs-attr">"react"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
51830 <span class="hljs-punctuation">}</span>
51831 <span class="hljs-punctuation">}</span>
51832 <span class="hljs-punctuation">}</span>
51833 <span class="hljs-punctuation">}</span></code></pre>
51836 Policies require this to be opt-in since it enables all resources of the
51860 …de class="language-js"><span class="hljs-keyword">const</span> process = <span class="hljs-built_i…
51878-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'beforeExit'</…
51879 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
51882 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
51883 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
51886 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
51888 <span class="hljs-comment">// Prints:</span>
51889 <span class="hljs-comment">// This message is displayed first.</span>
51890 <span class="hljs-comment">// Process beforeExit event with code: 0</span>
51891 <span class="hljs-comment">// Process exit event with code: 0</span></code></pre>
51904 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
51917 …age-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</spa…
51918-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
51924 …age-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</spa…
51925 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
51926 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
51927 }, <span class="hljs-number">0</span>);
51934-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href=…
51954 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
51955 <li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
51956 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
51969 example, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/…
51970-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'multipleResol…
51971 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
51972-title function_">setImmediate</span>(<span class="hljs-function">() =></span> process.<span class…
51975 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
51976 <span class="hljs-keyword">try</span> {
51977-keyword">return</span> <span class="hljs-keyword">await</span> <span class="hljs-keyword">new</sp…
51978 … <span class="hljs-title function_">resolve</span>(<span class="hljs-string">'First call'</span>);
51979 …<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'Swallowed resolve'</s…
51980 …lass="hljs-title function_">reject</span>(<span class="hljs-keyword">new</span> <span class="hljs-
51982 } <span class="hljs-keyword">catch</span> {
51983 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
51987 …s-title function_">main</span>().<span class="hljs-title function_">then</span>(<span class="hljs-
51988 <span class="hljs-comment">// resolve: Promise { 'First call' } 'Swallowed resolve'</span>
51989 <span class="hljs-comment">// reject: Promise { 'First call' } Error: Swallowed reject</span>
51990 <span class="hljs-comment">// at Promise (*)</span>
51991 <span class="hljs-comment">// at new Promise (&#x3C;anonymous>)</span>
51992 <span class="hljs-comment">// at main (*)</span>
51993 <span class="hljs-comment">// First call</span></code></pre>
51999 <li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
52002 and an error handler was attached to it (using <a href="https://developer.mozilla.org/en-US/docs/We…
52012 an ever-growing list of unhandled exceptions, with Promises there can be a
52013 growing-and-shrinking list of unhandled rejections.</p>
52019 …class="language-js"><span class="hljs-keyword">const</span> unhandledRejections = <span class="hlj…
52020 …class="hljs-title function_">on</span>(<span class="hljs-string">'unhandledRejection'</span>, <spa…
52021 unhandledRejections.<span class="hljs-title function_">set</span>(promise, reason);
52023 … class="hljs-title function_">on</span>(<span class="hljs-string">'rejectionHandled'</span>, <span…
52024 unhandledRejections.<span class="hljs-title function_">delete</span>(promise);
52029 likely best for long-running application) or upon process exit (which is likely
52044 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
52045 <li><code>origin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
52048 <a href="#cli_unhandled_rejections_mode"><code>--unhandled-rejections</code></a> flag set to <code>…
52060-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'uncaughtExcep…
52061 fs.<span class="hljs-title function_">writeSync</span>(
52062 process.<span class="hljs-property">stderr</span>.<span class="hljs-property">fd</span>,
52063 <span class="hljs-string">`Caught exception: <span class="hljs-subst">${err}</span>\n`</span> +
52064 <span class="hljs-string">`Exception origin: <span class="hljs-subst">${origin}</span>`</span>
52068 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
52069 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
52070 }, <span class="hljs-number">500</span>);
52072 <span class="hljs-comment">// Intentionally cause an exception, but don't catch it.</span>
52073 <span class="hljs-title function_">nonexistentFunc</span>();
52074 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
52086 process will exit with a non-zero exit code and the stack trace will be printed.
52104 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
52105 <li><code>origin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
52108 <a href="#cli_unhandled_rejections_mode"><code>--unhandled-rejections</code></a> flag set to <code>…
52117-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'uncaughtExcep…
52118 …<span class="hljs-title class_">MyMonitoringTool</span>.<span class="hljs-title function_">logSync…
52121 <span class="hljs-comment">// Intentionally cause an exception, but don't catch it.</span>
52122 <span class="hljs-title function_">nonexistentFunc</span>();
52123 <span class="hljs-comment">// Still crashes Node.js</span></code></pre>
52139 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> |…
52141 <li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
52146 promises". Rejections can be caught and handled using <a href="https://developer.mozilla.org/en-US/…
52150-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'unhandledReje…
52151-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
52152 …<span class="hljs-comment">// Application specific logging, throwing an error, or other logic here…
52155 somePromise.<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class…
52156-keyword">return</span> <span class="hljs-title function_">reportToUser</span>(<span class="hljs-v…
52157 }); <span class="hljs-comment">// No `.catch()` or `.then()`</span></code></pre>
52160 … class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_"…
52161 <span class="hljs-comment">// Initially set the loaded status to a rejected promise</span>
52162-variable language_">this</span>.<span class="hljs-property">loaded</span> = <span class="hljs-tit…
52165 <span class="hljs-keyword">const</span> resource = <span class="hljs-keyword">new</span> <span clas…
52166 <span class="hljs-comment">// no .catch or .then on resource.loaded for at least a turn</span></cod…
52169 address such failures, a non-operational
52170 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/c…
52178 <li><code>warning</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
52180 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52181 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#…
52182 <li><code>stack</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
52192 lead to sub-optimal application performance, bugs, or security vulnerabilities.</p>
52193 …ge-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'warning'</s…
52194-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52195-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52196-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52198 <p>By default, Node.js will print process warnings to <code>stderr</code>. The <code>--no-warnings<…
52199 command-line option can be used to suppress the default console output but the
52203 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
52204 <span class="hljs-meta">> </span><span class="language-bash">events.defaultMaxListeners = 1;</span>
52205 <span class="hljs-meta">> </span><span class="language-bash">process.on(<span class="hljs-string">'…
52206 <span class="hljs-meta">> </span><span class="language-bash">process.on(<span class="hljs-string">'…
52207 <span class="hljs-meta">> </span><span class="language-bash">(node:38638) MaxListenersExceededWarni…
52211 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
52212-meta">> </span><span class="language-bash">const p = process.on(<span class="hljs-string">'warnin…
52213 <span class="hljs-meta">> </span><span class="language-bash">events.defaultMaxListeners = 1;</span>
52214 <span class="hljs-meta">> </span><span class="language-bash">process.on(<span class="hljs-string">'…
52215 <span class="hljs-meta">> </span><span class="language-bash">process.on(<span class="hljs-string">'…
52216 <span class="hljs-meta">> </span><span class="language-bash">Do not <span class="hljs-keyword">do</…
52217 <p>The <code>--trace-warnings</code> command-line option can be used to have the default
52219 <p>Launching Node.js using the <code>--throw-deprecation</code> command-line flag will
52221 <p>Using the <code>--trace-deprecation</code> command-line flag will cause the custom
52223 <p>Using the <code>--no-deprecation</code> command-line flag will suppress all reporting
52225 <p>The <code>*-deprecation</code> command-line flags only affect warnings that use the name
52237 custom or application-specific warnings.</p>
52243 <li><code>'DeprecationWarning'</code> - Indicates use of a deprecated Node.js API or feature.
52246 <li><code>'ExperimentalWarning'</code> - Indicates use of an experimental Node.js API or
52248 time and are not subject to the same strict semantic-versioning and long-term
52250 <li><code>'MaxListenersExceededWarning'</code> - Indicates that too many listeners for a
52253 <li><code>'TimeoutOverflowWarning'</code> - Indicates that a numeric value that cannot fit
52254 within a 32-bit signed integer has been provided to either the <code>setTimeout()</code>
52256 <li><code>'UnsupportedWarning'</code> - Indicates use of an unsupported option or feature
52264 refer to <a href="http://man7.org/linux/man-pages/man7/signal.7.html"><code>signal(7)</code></a> fo…
52271 <pre><code class="language-js"><span class="hljs-comment">// Begin reading from stdin so the proces…
52272 process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">resume</span>();
52274 process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGINT'</span>, <sp…
52275 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
52278 <span class="hljs-comment">// Using a single function to handle multiple signals</span>
52279 <span class="hljs-keyword">function</span> <span class="hljs-title function_">handle</span>(<span c…
52280 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
52283 process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGINT'</span>, han…
52284 process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGTERM'</span>, ha…
52288 <li><code>'SIGTERM'</code> and <code>'SIGINT'</code> have default handlers on non-Windows platforms…
52294 other platforms under various similar conditions. See <a href="http://man7.org/linux/man-pages/man7…
52296 Windows about 10 seconds later. On non-Windows platforms, the default
52305 pressed. On non-Windows platforms, it can be listened on, but there is no way
52314 artificially using <a href="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code><…
52342 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set" …
52345 read-only <code>Set</code> of flags allowable within the <a href="#cli_node_options_options"><code>…
52352 <li>Flags may omit leading single (<code>-</code>) or double (<code>--</code>) dashes; e.g.,
52353 <code>inspect-brk</code> for <code>--inspect-brk</code>, or <code>r</code> for <code>-r</code>.</li>
52354 <li>Flags passed through to V8 (as listed in <code>--v8-options</code>) may replace
52355 one or more <em>non-leading</em> dashes for an underscore, or vice-versa;
52356 e.g., <code>--perf_basic_prof</code>, <code>--perf-basic-prof</code>, <code>--perf_basic-prof</code…
52360 e.g., <code>--stack-trace-limit=100</code>.</li>
52365 passed through to V8 will contain underscores instead of non-leading
52367-js">process.<span class="hljs-property">allowedNodeEnvironmentFlags</span>.<span class="hljs-titl…
52368 <span class="hljs-comment">// -r</span>
52369 <span class="hljs-comment">// --inspect-brk</span>
52370 <span class="hljs-comment">// --abort_on_uncaught_exception</span>
52371 <span class="hljs-comment">// ...</span>
52384 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
52389-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
52395 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
52397 <p>The <code>process.argv</code> property returns an array containing the command-line
52401 file being executed. The remaining elements will be any additional command-line
52403 <p>For example, assuming the following script for <code>process-args.js</code>:</p>
52404 <pre><code class="language-js"><span class="hljs-comment">// print process.argv</span>
52405 …span class="hljs-property">argv</span>.<span class="hljs-title function_">forEach</span>(<span cla…
52406-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
52409 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
52411 <pre><code class="language-text">0: /usr/local/bin/node
52412 1: /Users/mjr/work/node/process-args.js
52421 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
52423 <p>The <code>process.argv0</code> property stores a read-only copy of the original value of
52425 …code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">bash -c…
52426 <span class="hljs-meta">> </span><span class="language-bash">process.argv[0]</span>
52428 <span class="hljs-meta">> </span><span class="language-bash">process.argv0</span>
52443 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
52472 <li><code>directory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
52477-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
52478 <span class="hljs-keyword">try</span> {
52479 process.<span class="hljs-title function_">chdir</span>(<span class="hljs-string">'/tmp'</span>);
52480 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
52481 } <span class="hljs-keyword">catch</span> (err) {
52482 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span …
52490 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
52497 <!-- eslint-skip -->
52498 <pre><code class="language-js">{
52499 <span class="hljs-attr">target_defaults</span>:
52500 { <span class="hljs-attr">cflags</span>: [],
52501 … <span class="hljs-attr">default_configuration</span>: <span class="hljs-string">'Release'</span>,
52502 <span class="hljs-attr">defines</span>: [],
52503 <span class="hljs-attr">include_dirs</span>: [],
52504 <span class="hljs-attr">libraries</span>: [] },
52505 <span class="hljs-attr">variables</span>:
52507 <span class="hljs-attr">host_arch</span>: <span class="hljs-string">'x64'</span>,
52508 <span class="hljs-attr">napi_build_version</span>: <span class="hljs-number">5</span>,
52509 <span class="hljs-attr">node_install_npm</span>: <span class="hljs-string">'true'</span>,
52510 <span class="hljs-attr">node_prefix</span>: <span class="hljs-string">''</span>,
52511 <span class="hljs-attr">node_shared_cares</span>: <span class="hljs-string">'false'</span>,
52512 … <span class="hljs-attr">node_shared_http_parser</span>: <span class="hljs-string">'false'</span>,
52513 <span class="hljs-attr">node_shared_libuv</span>: <span class="hljs-string">'false'</span>,
52514 <span class="hljs-attr">node_shared_zlib</span>: <span class="hljs-string">'false'</span>,
52515 <span class="hljs-attr">node_use_dtrace</span>: <span class="hljs-string">'false'</span>,
52516 <span class="hljs-attr">node_use_openssl</span>: <span class="hljs-string">'true'</span>,
52517 <span class="hljs-attr">node_shared_openssl</span>: <span class="hljs-string">'false'</span>,
52518 <span class="hljs-attr">strict_aliasing</span>: <span class="hljs-string">'true'</span>,
52519 <span class="hljs-attr">target_arch</span>: <span class="hljs-string">'x64'</span>,
52520 <span class="hljs-attr">v8_use_snapshot</span>: <span class="hljs-number">1</span>
52523 <p>The <code>process.config</code> property is <strong>not</strong> read-only and there are existing
52531 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
52544 <li><code>previousValue</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
52546 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
52548 <li><code>user</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52549 <li><code>system</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
52560 <pre><code class="language-js"><span class="hljs-keyword">const</span> startUsage = process.<span c…
52561 <span class="hljs-comment">// { user: 38579, system: 6986 }</span>
52563 <span class="hljs-comment">// spin the CPU for 500 milliseconds</span>
52564 <span class="hljs-keyword">const</span> now = <span class="hljs-title class_">Date</span>.<span cla…
52565 …ljs-keyword">while</span> (<span class="hljs-title class_">Date</span>.<span class="hljs-title fun…
52567 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
52568 <span class="hljs-comment">// { user: 514883, system: 11226 }</span></code></pre>
52574 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
52578-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
52584 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
52587 <pre><code class="language-js">process.<span class="hljs-property">debugPort</span> = <span class="…
52613 <li><code>module</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
52614 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
52631 …<code class="language-js"><span class="hljs-keyword">const</span> os = <span class="hljs-built_in"…
52632 <span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span cla…
52633 <span class="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = { <sp…
52634-title function_">dlopen</span>(<span class="hljs-variable language_">module</span>, path.<span cl…
52635 …os.<span class="hljs-property">constants</span>.<span class="hljs-property">dlopen</span>.<span cl…
52636 <span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span>.<spa…
52642 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
52643 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
52645 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52647 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52648 <li><code>ctor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
52651 <li><code>detail</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
52658 <pre><code class="language-js"><span class="hljs-comment">// Emit a warning with a code and additio…
52659 process.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Something …
52660 <span class="hljs-attr">code</span>: <span class="hljs-string">'MY_WARNING'</span>,
52661 …<span class="hljs-attr">detail</span>: <span class="hljs-string">'This is some additional informat…
52663 <span class="hljs-comment">// Emits:</span>
52664 <span class="hljs-comment">// (node:56338) [MY_WARNING] Warning: Something happened!</span>
52665 <span class="hljs-comment">// This is some additional information</span></code></pre>
52669 …ge-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'warning'</s…
52670-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52671-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52672-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52673-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52674-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52682 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
52683 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52685 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52686 <li><code>ctor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
52693 <pre><code class="language-js"><span class="hljs-comment">// Emit a warning using a string.</span>
52694 process.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Something …
52695 <span class="hljs-comment">// Emits: (node: 56338) Warning: Something happened!</span></code></pre>
52696 <pre><code class="language-js"><span class="hljs-comment">// Emit a warning using a string and a ty…
52697 …ess.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Something Hap…
52698 <span class="hljs-comment">// Emits: (node:56338) CustomWarning: Something Happened!</span></code><…
52699-js">process.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Some…
52700 <span class="hljs-comment">// Emits: (node:56338) [WARN001] CustomWarning: Something happened!</spa…
52704 …ge-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'warning'</s…
52705 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span…
52706 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span…
52707 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span…
52708 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span…
52713 <pre><code class="language-js"><span class="hljs-comment">// Emit a warning using an Error object.<…
52714 …ss="hljs-keyword">const</span> myWarning = <span class="hljs-keyword">new</span> <span class="hljs
52715 <span class="hljs-comment">// Use the Error name property to specify the type name</span>
52716 myWarning.<span class="hljs-property">name</span> = <span class="hljs-string">'CustomWarning'</span…
52717 myWarning.<span class="hljs-property">code</span> = <span class="hljs-string">'WARN001'</span>;
52719 process.<span class="hljs-title function_">emitWarning</span>(myWarning);
52720 <span class="hljs-comment">// Emits: (node:56338) [WARN001] CustomWarning: Something happened!</spa…
52728 <li>If the <code>--throw-deprecation</code> command-line flag is used, the deprecation
52730 <li>If the <code>--no-deprecation</code> command-line flag is used, the deprecation
52732 <li>If the <code>--trace-deprecation</code> command-line flag is used, the deprecation
52739 …class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">…
52740 <span class="hljs-keyword">if</span> (!emitMyWarning.<span class="hljs-property">warned</span>) {
52741 … emitMyWarning.<span class="hljs-property">warned</span> = <span class="hljs-literal">true</span>;
52742 …process.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Only warn…
52745 <span class="hljs-title function_">emitMyWarning</span>();
52746 <span class="hljs-comment">// Emits: (node: 56339) Warning: Only warn once!</span>
52747 <span class="hljs-title function_">emitMyWarning</span>();
52748 <span class="hljs-comment">// Emits nothing</span></code></pre>
52764 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
52767 See <a href="http://man7.org/linux/man-pages/man7/environ.7.html"><code>environ(7)</code></a>.</p>
52769 <!-- eslint-skip -->
52770 <pre><code class="language-js">{
52771 <span class="hljs-attr">TERM</span>: <span class="hljs-string">'xterm-256color'</span>,
52772 <span class="hljs-attr">SHELL</span>: <span class="hljs-string">'/usr/local/bin/bash'</span>,
52773 <span class="hljs-attr">USER</span>: <span class="hljs-string">'maciej'</span>,
52774 …<span class="hljs-attr">PATH</span>: <span class="hljs-string">'~/.bin/:/usr/bin:/bin:/usr/sbin:/s…
52775 <span class="hljs-attr">PWD</span>: <span class="hljs-string">'/Users/maciej'</span>,
52776 <span class="hljs-attr">EDITOR</span>: <span class="hljs-string">'vim'</span>,
52777 <span class="hljs-attr">SHLVL</span>: <span class="hljs-string">'1'</span>,
52778 <span class="hljs-attr">HOME</span>: <span class="hljs-string">'/Users/maciej'</span>,
52779 <span class="hljs-attr">LOGNAME</span>: <span class="hljs-string">'maciej'</span>,
52780 <span class="hljs-attr">_</span>: <span class="hljs-string">'/usr/local/bin/node'</span>
52786-console"><span class="hljs-meta">$ </span><span class="language-bash">node -e <span class="hljs-s…
52788 …<code class="language-js">process.<span class="hljs-property">env</span>.<span class="hljs-propert…
52789 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span…
52793 …code class="language-js">process.<span class="hljs-property">env</span>.<span class="hljs-property…
52794 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span…
52795 <span class="hljs-comment">// => 'null'</span>
52796 process.<span class="hljs-property">env</span>.<span class="hljs-property">test</span> = <span clas…
52797 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span…
52798 <span class="hljs-comment">// => 'undefined'</span></code></pre>
52800 …code class="language-js">process.<span class="hljs-property">env</span>.<span class="hljs-property…
52801 <span class="hljs-keyword">delete</span> process.<span class="hljs-property">env</span>.<span class…
52802 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span…
52803 <span class="hljs-comment">// => undefined</span></code></pre>
52804 <p>On Windows operating systems, environment variables are case-insensitive.</p>
52805 …code class="language-js">process.<span class="hljs-property">env</span>.<span class="hljs-property…
52806 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span…
52807 <span class="hljs-comment">// => 1</span></code></pre>
52813 are visible to the operating system or to native add-ons.</p>
52819 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
52821 <p>The <code>process.execArgv</code> property returns the set of Node.js-specific command-line
52827 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
52829 <!-- eslint-disable semi -->
52830 <pre><code class="language-js">[<span class="hljs-string">'--harmony'</span>]</code></pre>
52832 <!-- eslint-disable semi -->
52833 …language-js">[<span class="hljs-string">'/usr/local/bin/node'</span>, <span class="hljs-string">'s…
52841 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
52845 <!-- eslint-disable semi -->
52846 <pre><code class="language-js"><span class="hljs-string">'/usr/local/bin/node'</span></code></pre>
52852 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52860 <pre><code class="language-js">process.<span class="hljs-title function_">exit</span>(<span class="…
52873 <pre><code class="language-js"><span class="hljs-comment">// This is an example of what *not* to do…
52874 <span class="hljs-keyword">if</span> (<span class="hljs-title function_">someConditionNotMet</span>…
52875 <span class="hljs-title function_">printUsageToStdout</span>();
52876 process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">1</span>);
52885 <pre><code class="language-js"><span class="hljs-comment">// How to properly set the exit code whil…
52886 <span class="hljs-comment">// the process exit gracefully.</span>
52887 <span class="hljs-keyword">if</span> (<span class="hljs-title function_">someConditionNotMet</span>…
52888 <span class="hljs-title function_">printUsageToStdout</span>();
52889 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
52901 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
52913 of the Node.js process. (See <a href="http://man7.org/linux/man-pages/man2/getegid.2.html"><code>ge…
52914 <pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-prop…
52915 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
52924 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
52927 the process. (See <a href="http://man7.org/linux/man-pages/man2/geteuid.2.html"><code>geteuid(2)</c…
52928 <pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-prop…
52929 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
52938 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
52941 process. (See <a href="http://man7.org/linux/man-pages/man2/getgid.2.html"><code>getgid(2)</code></…
52942 <pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-prop…
52943 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
52952 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
52957 <pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-prop…
52958-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span cl…
52967 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
52970 (See <a href="http://man7.org/linux/man-pages/man2/getuid.2.html"><code>getuid(2)</code></a>.)</p>
52971 <pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-prop…
52972 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
52981 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
52989 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy. Use <a href=…
52991 <li><code>time</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52992 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
52996 <p>The <code>process.hrtime()</code> method returns the current high-resolution real time
53002 user-defined array instead of the result of a previous call to
53007 … class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-variable constant_"…
53008 <span class="hljs-keyword">const</span> time = process.<span class="hljs-title function_">hrtime</s…
53009 <span class="hljs-comment">// [ 1800216, 25 ]</span>
53011 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
53012 …<span class="hljs-keyword">const</span> diff = process.<span class="hljs-title function_">hrtime</…
53013 <span class="hljs-comment">// [ 1, 552 ]</span>
53015-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
53016 <span class="hljs-comment">// Benchmark took 1000000552 nanoseconds</span>
53017 }, <span class="hljs-number">1000</span>);</code></pre>
53023 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
53026 current high-resolution real time in nanoseconds as a <code>bigint</code>.</p>
53030 … class="language-js"><span class="hljs-keyword">const</span> start = process.<span class="hljs-pro…
53031 <span class="hljs-comment">// 191051479007711n</span>
53033 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
53034 …<span class="hljs-keyword">const</span> end = process.<span class="hljs-property">hrtime</span>.<s…
53035 <span class="hljs-comment">// 191052633396993n</span>
53037-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
53038 <span class="hljs-comment">// Benchmark took 1154389282 nanoseconds</span>
53039 }, <span class="hljs-number">1000</span>);</code></pre>
53045 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53046 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53053-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53054 …"hljs-title function_">initgroups</span>(<span class="hljs-string">'nodeuser'</span>, <span class=…
53055-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span cl…
53056 …ess.<span class="hljs-title function_">setgid</span>(<span class="hljs-number">1000</span>); …
53057-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span cl…
53066 <li><code>pid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
53067 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53073 and <a href="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code></a> for more in…
53081 …e class="language-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-strin…
53082 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
53085 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
53086 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
53087 process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>);
53088 }, <span class="hljs-number">100</span>);
53090 process.<span class="hljs-title function_">kill</span>(process.<span class="hljs-property">pid</spa…
53097 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
53099 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
53123 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
53125 <li><code>rss</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
53126 <li><code>heapTotal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
53127 <li><code>heapUsed</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
53128 <li><code>external</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
53129 <li><code>arrayBuffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
53135 …s="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title funct…
53136 <span class="hljs-comment">// Prints:</span>
53137 <span class="hljs-comment">// {</span>
53138 <span class="hljs-comment">// rss: 4935680,</span>
53139 <span class="hljs-comment">// heapTotal: 1826816,</span>
53140 <span class="hljs-comment">// heapUsed: 650472,</span>
53141 <span class="hljs-comment">// external: 49879,</span>
53142 <span class="hljs-comment">// arrayBuffers: 9386</span>
53143 <span class="hljs-comment">// }</span></code></pre>
53167 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
53176-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53177 <span class="hljs-comment">// 35655680</span></code></pre>
53191 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
53192 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
53198 See the <a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#process-nexttick…
53199 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
53200 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span>…
53201 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
53203 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
53204 <span class="hljs-comment">// Output:</span>
53205 <span class="hljs-comment">// start</span>
53206 <span class="hljs-comment">// scheduled</span>
53207 <span class="hljs-comment">// nextTick callback</span></code></pre>
53211 …de class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function…
53212 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setupOptions<…
53214 …process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span…
53215 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">startDoingStu…
53219 <span class="hljs-keyword">const</span> thing = <span class="hljs-keyword">new</span> <span class="…
53220 thing.<span class="hljs-title function_">getReadyForStuff</span>();
53222 <span class="hljs-comment">// thing.startDoingStuff() gets called now, not before.</span></code></p…
53225 <pre><code class="language-js"><span class="hljs-comment">// WARNING! DO NOT USE! BAD UNSAFE HAZA…
53226 <span class="hljs-keyword">function</span> <span class="hljs-title function_">maybeSync</span>(<spa…
53227 <span class="hljs-keyword">if</span> (arg) {
53228 <span class="hljs-title function_">cb</span>();
53229 <span class="hljs-keyword">return</span>;
53232 fs.<span class="hljs-title function_">stat</span>(<span class="hljs-string">'file'</span>, cb);
53235-js"><span class="hljs-keyword">const</span> maybeTrue = <span class="hljs-title class_">Math</spa…
53237 <span class="hljs-title function_">maybeSync</span>(maybeTrue, <span class="hljs-function">() =></s…
53238 <span class="hljs-title function_">foo</span>();
53241 <span class="hljs-title function_">bar</span>();</code></pre>
53244 …lass="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">d…
53245 <span class="hljs-keyword">if</span> (arg) {
53246 process.<span class="hljs-title function_">nextTick</span>(cb);
53247 <span class="hljs-keyword">return</span>;
53250 fs.<span class="hljs-title function_">stat</span>(<span class="hljs-string">'file'</span>, cb);
53258-js"><span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</sp…
53259-title function_">queueMicrotask</span>(<span class="hljs-function">() =></span> <span class="hljs
53260-title function_">nextTick</span>(<span class="hljs-function">() =></span> <span class="hljs-varia…
53261 <span class="hljs-comment">// Output:</span>
53262 <span class="hljs-comment">// 1</span>
53263 <span class="hljs-comment">// 2</span>
53264 <span class="hljs-comment">// 3</span></code></pre>
53268 In simple scenarios, <code>queueMicrotask()</code> can be a drop-in replacement for
53270 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
53271 <span class="hljs-title function_">queueMicrotask</span>(<span class="hljs-function">() =></span> {
53272 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
53274 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
53275 <span class="hljs-comment">// Output:</span>
53276 <span class="hljs-comment">// start</span>
53277 <span class="hljs-comment">// scheduled</span>
53278 <span class="hljs-comment">// microtask callback</span></code></pre>
53279 <p>One note-worthy difference between the two APIs is that <code>process.nextTick()</code>
53283 …de class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function…
53284 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
53287 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
53288-title function_">queueMicrotask</span>(deferred.<span class="hljs-title function_">bind</span>(<s…
53289 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
53290 <span class="hljs-comment">// Output:</span>
53291 <span class="hljs-comment">// start</span>
53292 <span class="hljs-comment">// scheduled</span>
53293 <span class="hljs-comment">// microtask 3</span></code></pre>
53306 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53308 <p>The <code>process.noDeprecation</code> property indicates whether the <code>--no-deprecation</co…
53318 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
53321-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53327 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
53341-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53344 …s://github.com/nodejs/node/blob/HEAD/BUILDING.md#androidandroid-based-devices-eg-firefox-os">is ex…
53350 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
53354-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53368 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
53371 to the current release, including URLs for the source tarball and headers-only
53375 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
53376 <li><code>sourceUrl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
53378 <li><code>headersUrl</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
53381 Node.js native add-ons.</li>
53382 <li><code>libUrl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
53384 compiling Node.js native add-ons. <em>This property is only present on Windows
53386 <li><code>lts</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
53397 <!-- eslint-skip -->
53398 <pre><code class="language-js">{
53399 <span class="hljs-attr">name</span>: <span class="hljs-string">'node'</span>,
53400 <span class="hljs-attr">lts</span>: <span class="hljs-string">'Erbium'</span>,
53401 …<span class="hljs-attr">sourceUrl</span>: <span class="hljs-string">'https://nodejs.org/download/r…
53402 …<span class="hljs-attr">headersUrl</span>: <span class="hljs-string">'https://nodejs.org/download/…
53403 …<span class="hljs-attr">libUrl</span>: <span class="hljs-string">'https://nodejs.org/download/rele…
53405 <p>In custom builds from non-release versions of the source tree, only the
53421 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
53431 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53433 <p>Write reports in a compact format, single-line JSON, more easily consumable
53434 by log processing systems than the default multi-line format designed for
53436-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53450 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
53455-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53469 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
53474-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53488 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
53489 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
53494 … class="language-js"><span class="hljs-keyword">const</span> data = process.<span class="hljs-prop…
53495 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span cl…
53497 <span class="hljs-comment">// Similar to process.report.writeReport()</span>
53498 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
53499-title function_">writeFileSync</span>(<span class="hljs-string">'my-report.log'</span>, util.<spa…
53514 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53518-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53532 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53536-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53550 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53553-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53567 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
53571-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53586 <p><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
53592 <p><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
53595 <p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String…
53601 <pre><code class="language-js">process.<span class="hljs-property">report</span>.<span class="hljs-
53608 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
53612 <li><code>userCPUTime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
53614 <li><code>systemCPUTime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
53616 <li><code>maxRSS</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
53618 <li><code>sharedMemorySize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
53620 <li><code>unsharedDataSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
53622 <li><code>unsharedStackSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
53624 <li><code>minorPageFault</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
53627 <li><code>majorPageFault</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
53631 <li><code>swappedOut</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
53633 <li><code>fsRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
53635 <li><code>fsWrite</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
53637 <li><code>ipcSent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
53639 <li><code>ipcReceived</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
53641 <li><code>signalsCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
53643 <li><code>voluntaryContextSwitches</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
53647 <li><code>involuntaryContextSwitches</code> <a href="https://developer.mozilla.org/en-US/docs/Web/J…
53654 …s="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title funct…
53655 <span class="hljs-comment">/*
53681 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
53683 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
53686 <li><code>keepOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
53691 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
53692 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
53706 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53709 (See <a href="http://man7.org/linux/man-pages/man2/setegid.2.html"><code>setegid(2)</code></a>.) Th…
53712 …="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getegid</…
53713 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
53714 <span class="hljs-keyword">try</span> {
53715 process.<span class="hljs-title function_">setegid</span>(<span class="hljs-number">501</span>);
53716 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
53717 } <span class="hljs-keyword">catch</span> (err) {
53718 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
53729 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53732 (See <a href="http://man7.org/linux/man-pages/man2/seteuid.2.html"><code>seteuid(2)</code></a>.) Th…
53735 …="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">geteuid</…
53736 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
53737 <span class="hljs-keyword">try</span> {
53738 process.<span class="hljs-title function_">seteuid</span>(<span class="hljs-number">501</span>);
53739 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
53740 } <span class="hljs-keyword">catch</span> (err) {
53741 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
53752 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53755 <a href="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>.) The <code…
53758 …="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getgid</s…
53759 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
53760 <span class="hljs-keyword">try</span> {
53761 process.<span class="hljs-title function_">setgid</span>(<span class="hljs-number">501</span>);
53762 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
53763 } <span class="hljs-keyword">catch</span> (err) {
53764 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
53775 <li><code>groups</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
53781 …"language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getgroups<…
53782 <span class="hljs-keyword">try</span> {
53783 …process.<span class="hljs-title function_">setgroups</span>([<span class="hljs-number">501</span>]…
53784-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span cl…
53785 } <span class="hljs-keyword">catch</span> (err) {
53786 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
53797 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
53800 <a href="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>.) The <code…
53803 …="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getuid</s…
53804 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
53805 <span class="hljs-keyword">try</span> {
53806 process.<span class="hljs-title function_">setuid</span>(<span class="hljs-number">501</span>);
53807 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
53808 } <span class="hljs-keyword">catch</span> (err) {
53809 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
53819 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
53821 <li><code>val</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
53826 <code>--enable-source-maps</code>.</p>
53834 …a.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a…
53840 not be emitted. If <code>--abort-on-uncaught-exception</code> was passed from the
53846 method with a non-<code>null</code> argument while another capture function is set will
53849 <a href="domain.html"><code>domain</code></a> built-in module.</p>
53862 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
53884 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
53898 …ass="language-js">process.<span class="hljs-property">stdin</span>.<span class="hljs-title functio…
53903 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
53940 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
53942 …s-meta">$ </span><span class="language-bash"><span class="hljs-built_in">echo</span> <span class="…
53944 <span class="hljs-meta">$ </span><span class="language-bash">node -p <span class="hljs-string">"Boo…
53946 <span class="hljs-meta">$ </span><span class="language-bash">node -p <span class="hljs-string">"Boo…
53954 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53957 <code>--throw-deprecation</code> flag is set on the current Node.js process.
53962 …de class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node --th…
53964 <span class="hljs-meta">$ </span><span class="language-bash">node -p <span class="hljs-string">"pro…
53966 <span class="hljs-meta">$ </span><span class="language-bash">node</span>
53967 …pan class="hljs-meta">> </span><span class="language-bash">process.emitWarning(<span class="hljs-s…
53969 <span class="hljs-meta">> </span><span class="language-bash">(node:26598) DeprecationWarning: <span…
53970 <span class="hljs-meta">> </span><span class="language-bash">process.throwDeprecation = <span class…
53972 …pan class="hljs-meta">> </span><span class="language-bash">process.emitWarning(<span class="hljs-s…
53980 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
53988 binary name plus the length of the command-line arguments because setting the
54001 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
54004 <code>--trace-deprecation</code> flag is set on the current Node.js process. See the
54020 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Calling …
54021 the process-wide umask to be written twice. This introduces a race condition
54023 cross-platform alternative API.</div><p></p>
54031 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
54035 <pre><code class="language-js"><span class="hljs-keyword">const</span> newmask = <span class="hljs-
54036 <span class="hljs-keyword">const</span> oldmask = process.<span class="hljs-title function_">umask<…
54037 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
54038-string">`Changed umask from <span class="hljs-subst">${oldmask.toString(<span class="hljs-number"…
54046 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
54057 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
54060-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
54061 <span class="hljs-comment">// Version: v14.8.0</span></code></pre>
54079 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
54085 …s="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title funct…
54087 <pre><code class="language-console">{ node: '11.13.0',
54088 v8: '7.0.276.38-node.18',
54121 <li><code>6</code> <strong>Non-function Internal Exception Handler</strong>: There was an
54123 function was somehow set to a non-function, and could not be called.</li>
54124 <li><code>7</code> <strong>Internal Exception Handler Run-Time Failure</strong>: There was an
54133 <li><code>10</code> <strong>Internal JavaScript Run-Time Failure</strong>: The JavaScript
54137 …ode> <strong>Invalid Debug Argument</strong>: The <code>--inspect</code> and/or <code>--inspect-br…
54139 <li><code>13</code> <strong>Unfinished Top-Level Await</strong>: <code>await</code> was used outsid…
54140 in the top-level code, but the passed <code>Promise</code> never resolved.</li>
54144 exit codes are defined to be 7-bit integers, and signal exits set
54145 the high-order bit, and then contain the value of the signal code.
54155 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
54160 userland-provided <a href="https://github.com/bestiejs/punycode.js">Punycode.js</a> module instead.…
54165 …de class="language-js"><span class="hljs-keyword">const</span> punycode = <span class="hljs-built_…
54169 non-ASCII characters must be converted into ASCII using the Punycode scheme.
54173 <code>'xn--fsq.com'</code>.</p>
54175 <p>The <code>punycode</code> module is a third-party dependency used by Node.js and
54183 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54185 …> method converts a <a href="https://tools.ietf.org/html/rfc3492">Punycode</a> string of ASCII-only
54187 …ss="language-js">punycode.<span class="hljs-title function_">decode</span>(<span class="hljs-strin…
54188 …ode.<span class="hljs-title function_">decode</span>(<span class="hljs-string">'--dqo34k'</span>);…
54194 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54197 <a href="https://tools.ietf.org/html/rfc3492">Punycode</a> string of ASCII-only characters.</p>
54198 …uage-js">punycode.<span class="hljs-title function_">encode</span>(<span class="hljs-string">'maña…
54199 …ycode.<span class="hljs-title function_">encode</span>(<span class="hljs-string">'☃-⌘'</span>); <s…
54205 <li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54208 …Name to <a href="https://tools.ietf.org/html/rfc3492">Punycode</a>. Only the non-ASCII parts of the
54211 <pre><code class="language-js"><span class="hljs-comment">// encode domain names</span>
54212 …pan class="hljs-title function_">toASCII</span>(<span class="hljs-string">'mañana.com'</span>); <…
54213 ….<span class="hljs-title function_">toASCII</span>(<span class="hljs-string">'☃-⌘.com'</span>); …
54214 punycode.<span class="hljs-title function_">toASCII</span>(<span class="hljs-string">'example.com'<…
54220 <li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54225 <pre><code class="language-js"><span class="hljs-comment">// decode domain names</span>
54226 …code.<span class="hljs-title function_">toUnicode</span>(<span class="hljs-string">'xn--maana-pta.…
54227 …pan class="hljs-title function_">toUnicode</span>(<span class="hljs-string">'xn----dqo34k.com'</sp…
54228 …code.<span class="hljs-title function_">toUnicode</span>(<span class="hljs-string">'example.com'</…
54238 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54242-js">punycode.<span class="hljs-property">ucs2</span>.<span class="hljs-title function_">decode</s…
54243 <span class="hljs-comment">// surrogate pair for U+1D306 tetragram for centre:</span>
54244 …="hljs-property">ucs2</span>.<span class="hljs-title function_">decode</span>(<span class="hljs-st…
54250 <li><code>codePoints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
54254-js">punycode.<span class="hljs-property">ucs2</span>.<span class="hljs-title function_">encode</s…
54255 …ass="hljs-property">ucs2</span>.<span class="hljs-title function_">encode</span>([<span class="hlj…
54261 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
54267 …ty api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy</div><p></p>
54272 … class="language-js"><span class="hljs-keyword">const</span> querystring = <span class="hljs-built…
54290 <li><code>str</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
54292 <p>The <code>querystring.escape()</code> method performs URL percent-encoding on the given
54297 application code to provide a replacement percent-encoding implementation if
54316 <li><code>str</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
54317 <li><code>sep</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
54319 <li><code>eq</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
54321 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
54323 <li><code>decodeURIComponent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
54324 percent-encoded characters in the query string. <strong>Default:</strong>
54326 <li><code>maxKeys</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
54334 <!-- eslint-skip -->
54335 <pre><code class="language-js">{
54336 <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span>,
54337 …<span class="hljs-attr">abc</span>: [<span class="hljs-string">'xyz'</span>, <span class="hljs-str…
54343 <p>By default, percent-encoded characters within the query string will be assumed
54344 to use UTF-8 encoding. If an alternative character encoding is used, then an
54346 <pre><code class="language-js"><span class="hljs-comment">// Assuming gbkDecodeURIComponent functio…
54348-title function_">parse</span>(<span class="hljs-string">'w=%D6%D0%CE%C4&#x26;foo=bar'</span>, <sp…
54349 … { <span class="hljs-attr">decodeURIComponent</span>: gbkDecodeURIComponent });</code></pre>
54355 <li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
54356 <li><code>sep</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
54358 <li><code>eq</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
54362 <li><code>encodeURIComponent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
54363 URL-unsafe characters to percent-encoding in the query string. <strong>Default:</strong>
54371-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
54374-js">querystring.<span class="hljs-title function_">stringify</span>({ <span class="hljs-attr">foo…
54375 <span class="hljs-comment">// Returns 'foo=bar&#x26;baz=qux&#x26;baz=quux&#x26;corge='</span>
54377-title function_">stringify</span>({ <span class="hljs-attr">foo</span>: <span class="hljs-string"…
54378 <span class="hljs-comment">// Returns 'foo:bar;baz:qux'</span></code></pre>
54379 <p>By default, characters requiring percent-encoding within the query string will
54380 be encoded as UTF-8. If an alternative encoding is required, then an alternative
54382 <pre><code class="language-js"><span class="hljs-comment">// Assuming gbkEncodeURIComponent functio…
54384-title function_">stringify</span>({ <span class="hljs-attr">w</span>: <span class="hljs-string">'…
54385 … { <span class="hljs-attr">encodeURIComponent</span>: gbkEncodeURIComponent });</code></pre>
54391 <li><code>str</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
54393 <p>The <code>querystring.unescape()</code> method performs decoding of URL percent-encoded
54400 JavaScript built-in <code>decodeURIComponent()</code> method to decode. If that fails,
54405 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
54410 …de class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_…
54412 …de class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_…
54414 <span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInte…
54415 <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>,
54416 <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>
54419 …js-title function_">question</span>(<span class="hljs-string">'What do you think of Node.js? '</sp…
54420 …<span class="hljs-comment">// <span class="hljs-doctag">TODO:</span> Log the answer in a database<…
54421-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
54423 rl.<span class="hljs-title function_">close</span>();
54450 end-of-transmission (EOT);</li>
54463 end-of-line input (<code>\n</code>, <code>\r</code>, or <code>\r\n</code>). This usually occurs whe…
54467 …nguage-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>…
54468 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
54478 <p>The primary purpose is to allow a listener to persist the history.
54482 …uage-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'history'</span…
54483 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
54496 …code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string"…
54497 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
54505 …ode class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">…
54506 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
54514 brought back to the foreground using <a href="http://man7.org/linux/man-pages/man1/fg.1p.html"><cod…
54518 …ode class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">…
54519 <span class="hljs-comment">// `prompt` will automatically resume the stream</span>
54520 rl.<span class="hljs-title function_">prompt</span>();
54532 …ode class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">…
54533 …js-title function_">question</span>(<span class="hljs-string">'Are you sure you want to exit? '</s…
54534 …hljs-keyword">if</span> (answer.<span class="hljs-title function_">match</span>(<span class="hljs-
54545 <p>When the program is resumed using <a href="http://man7.org/linux/man-pages/man1/fg.1p.html"><cod…
54550 …ode class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">…
54551 …<span class="hljs-comment">// This will override SIGTSTP and prevent the program from going to the…
54552 <span class="hljs-comment">// background.</span>
54553 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
54578 <li><code>preserveCursor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
54593 <li><code>query</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
54595 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
54601 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
54615-js">rl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What is your…
54616-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
54619 …e><code class="language-js"><span class="hljs-keyword">const</span> ac = <span class="hljs-keyword…
54620 <span class="hljs-keyword">const</span> signal = ac.<span class="hljs-property">signal</span>;
54622-title function_">question</span>(<span class="hljs-string">'What is your favorite food? '</span>,…
54623-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
54626 signal.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'abort'…
54627 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
54628 }, { <span class="hljs-attr">once</span>: <span class="hljs-literal">true</span> });
54630 …"hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> ac.<span class="hljs-ti…
54634 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
54635-keyword">const</span> question = util.<span class="hljs-title function_">promisify</span>(rl.<spa…
54637 …"hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title fun…
54638 <span class="hljs-keyword">try</span> {
54639 …="hljs-keyword">const</span> answer = <span class="hljs-keyword">await</span> <span class="hljs-ti…
54640-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
54641 } <span class="hljs-keyword">catch</span> (err) {
54642 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
54645 <span class="hljs-title function_">questionExample</span>();</code></pre>
54656 <li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54665 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
54673 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
54674 <li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
54676 <li><code>ctrl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
54677 <li><code>meta</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
54678 <li><code>shift</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
54679 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
54692 <pre><code class="language-js">rl.<span class="hljs-title function_">write</span>(<span class="hljs
54693 <span class="hljs-comment">// Simulate Ctrl+U to delete the line written previously</span>
54694-title function_">write</span>(<span class="hljs-literal">null</span>, { <span class="hljs-attr">c…
54710 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x…
54720 instead for performance-sensitive applications.</p>
54721-js"><span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span clas…
54722 …<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInt…
54723 <span class="hljs-comment">// ...</span>
54726 …an class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyw…
54727 …<span class="hljs-comment">// Each line in the readline input will be successively available here …
54728 <span class="hljs-comment">// `line`.</span>
54747 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
54758 … class="language-js"><span class="hljs-keyword">const</span> values = [<span class="hljs-string">'…
54759 …span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInte…
54760 <span class="hljs-keyword">const</span> showResults = <span class="hljs-title function_">debounce</…
54761 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
54762 <span class="hljs-string">'\n'</span>,
54763-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">val</span>)…
54765 }, <span class="hljs-number">300</span>);
54766-property">stdin</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'ke…
54767 <span class="hljs-title function_">showResults</span>();
54774 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
54786 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
54788 <li><code>rows</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
54789 <li><code>cols</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
54810 <li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
54812 <li><code>-1</code>: to the left from cursor</li>
54817 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
54818 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
54838 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
54839 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
54870 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
54876 <li><code>completer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
54877 <li><code>terminal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
54880 <li><code>history</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
54884 <li><code>historySize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
54889 <li><code>removeHistoryDuplicates</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
54892 <li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54893 <li><code>crlfDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
54895 end-of-line input. <code>crlfDelay</code> will be coerced to a number no less than
54899 <li><code>escapeCodeTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
54904 <li><code>tabSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
54914 …de class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_…
54915 <span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInte…
54916 <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>,
54917 <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>
54921 …nguage-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>…
54922 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
54934 <pre><code class="language-js">process.<span class="hljs-property">stdin</span>.<span class="hljs-t…
54943 …e class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_…
54944-keyword">const</span> completions = <span class="hljs-string">'.help .error .exit .quit .q'</span…
54945-keyword">const</span> hits = completions.<span class="hljs-title function_">filter</span>(<span c…
54946 <span class="hljs-comment">// Show all completions if none found</span>
54947 …<span class="hljs-keyword">return</span> [hits.<span class="hljs-property">length</span> ? hits : …
54951 …e class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_…
54952 …<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, [[<span…
54968 <li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
54969 <li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
54970 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
54971 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
54988 autocompletion is disabled when copy-pasted input is detected.</p>
54993 <pre><code class="language-js">readline.<span class="hljs-title function_">emitKeypressEvents</span…
54994 <span class="hljs-keyword">if</span> (process.<span class="hljs-property">stdin</span>.<span class=…
54995 …process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">setRawMode</sp…
55010 <li><code>dx</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
55011 <li><code>dy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
55012 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
55013 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
55021 implement a small command-line interface:</p>
55022 …de class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_…
55023 <span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInte…
55024 <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>,
55025 <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>,
55026 <span class="hljs-attr">prompt</span>: <span class="hljs-string">'OHAI> '</span>
55029 rl.<span class="hljs-title function_">prompt</span>();
55031 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class…
55032 <span class="hljs-keyword">switch</span> (line.<span class="hljs-title function_">trim</span>()) {
55033 <span class="hljs-keyword">case</span> <span class="hljs-string">'hello'</span>:
55034 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
55035 <span class="hljs-keyword">break</span>;
55036 <span class="hljs-attr">default</span>:
55037-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
55038 <span class="hljs-keyword">break</span>;
55040 rl.<span class="hljs-title function_">prompt</span>();
55041 }).<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span cla…
55042 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
55043 process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>);
55045 </section><section><h3>Example: Read file stream line-by-Line<span><a class="mark" href="#readline_…
55049 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
55050 <span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span…
55052 …hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title func…
55053 …<span class="hljs-keyword">const</span> fileStream = fs.<span class="hljs-title function_">createR…
55055 …<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInt…
55056 <span class="hljs-attr">input</span>: fileStream,
55057 <span class="hljs-attr">crlfDelay</span>: <span class="hljs-title class_">Infinity</span>
55059 …<span class="hljs-comment">// Note: we use the crlfDelay option to recognize all instances of CR L…
55060 <span class="hljs-comment">// ('\r\n') in input.txt as a single line break.</span>
55062 …an class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyw…
55063 …<span class="hljs-comment">// Each line in input.txt will be successively available here as `line`…
55064 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
55068 <span class="hljs-title function_">processLineByLine</span>();</code></pre>
55070 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
55071 <span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span…
55073 <span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInte…
55074 …<span class="hljs-attr">input</span>: fs.<span class="hljs-title function_">createReadStream</span…
55075 <span class="hljs-attr">crlfDelay</span>: <span class="hljs-title class_">Infinity</span>
55078 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class…
55079 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
55083 …de class="language-js"><span class="hljs-keyword">const</span> { once } = <span class="hljs-built_…
55084 <span class="hljs-keyword">const</span> { createReadStream } = <span class="hljs-built_in">require<…
55085 <span class="hljs-keyword">const</span> { createInterface } = <span class="hljs-built_in">require</…
55087 …hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title func…
55088 <span class="hljs-keyword">try</span> {
55089 …<span class="hljs-keyword">const</span> rl = <span class="hljs-title function_">createInterface</s…
55090 …<span class="hljs-attr">input</span>: <span class="hljs-title function_">createReadStream</span>(<…
55091 <span class="hljs-attr">crlfDelay</span>: <span class="hljs-title class_">Infinity</span>
55094 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class…
55095 <span class="hljs-comment">// Process the line.</span>
55098 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(rl, <span c…
55100 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
55101 } <span class="hljs-keyword">catch</span> (err) {
55102 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
55231 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
55233 <p>The <code>repl</code> module provides a Read-Eval-Print-Loop (REPL) implementation that
55236 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55240 evaluate those according to a user-defined evaluation function, then output the
55244 completion preview, simplistic Emacs-style line editing, multi-line inputs,
55245 …dia.org/wiki/Z_shell">ZSH</a>-like reverse-i-search, <a href="https://en.wikipedia.org/wiki/Z_shel…
55246 ANSI-styled output, saving and restoring current REPL session state, error
55248 ANSI styles and Emacs-style line editing automatically fall back to a limited
55253 <li><code>.break</code>: When in the process of inputting a multi-line expression, enter
55257 multi-line expression being input.</li>
55267 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">.e…
55277 <span class="hljs-meta">></span></code></pre>
55289 <p>For key bindings related to the reverse-i-search, see <a href="#repl_reverse_i_search"><code>rev…
55293 that evaluates JavaScript expressions and provides access to Node.js built-in
55298 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">1 …
55300 <span class="hljs-meta">> </span><span class="language-bash">const m = 2</span>
55302 <span class="hljs-meta">> </span><span class="language-bash">m + 1</span>
55311 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55312 <span class="hljs-keyword">const</span> msg = <span class="hljs-string">'message'</span>;
55314 …class="hljs-title function_">start</span>(<span class="hljs-string">'> '</span>).<span class="hljs
55316 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
55317 <span class="hljs-meta">> </span><span class="language-bash">m</span>
55319 <p>Context properties are not read-only by default. To specify read-only globals,
55321 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55322 <span class="hljs-keyword">const</span> msg = <span class="hljs-string">'message'</span>;
55324 <span class="hljs-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>(<s…
55325 …s-title class_">Object</span>.<span class="hljs-title function_">defineProperty</span>(r.<span cla…
55326 <span class="hljs-attr">configurable</span>: <span class="hljs-literal">false</span>,
55327 <span class="hljs-attr">enumerable</span>: <span class="hljs-literal">true</span>,
55328 <span class="hljs-attr">value</span>: msg
55333 global or scoped variable, the input <code>fs</code> will be evaluated on-demand as
55335 …><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">fs.cr…
55354 <pre><code class="language-js"><span class="hljs-keyword">const</span> r = repl.<span class="hljs-t…
55356 r.<span class="hljs-title function_">write</span>(<span class="hljs-string">'process.on("uncaughtEx…
55357 <span class="hljs-comment">// Output stream includes:</span>
55358 <span class="hljs-comment">// TypeError [ERR_INVALID_REPL_INPUT]: Listeners for `uncaughtExceptio…
55359 <span class="hljs-comment">// cannot be used in the REPL</span>
55361 r.<span class="hljs-title function_">close</span>();</code></pre>
55381-console"><span class="hljs-meta">> </span><span class="language-bash">[ <span class="hljs-string"…
55383 <span class="hljs-meta">> </span><span class="language-bash">_.length</span>
55385 <span class="hljs-meta">> </span><span class="language-bash">_ += 1</span>
55388 <span class="hljs-meta">> </span><span class="language-bash">1 + 1</span>
55390 <span class="hljs-meta">> </span><span class="language-bash">_</span>
55394 …re><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">thr…
55396 <span class="hljs-meta">> </span><span class="language-bash">_error.message</span>
55399 …With the <a href="#cli_experimental_repl_await"><code>--experimental-repl-await</code></a> command
55401 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">aw…
55403 <span class="hljs-meta">> </span><span class="language-bash">await Promise.reject(new Error(<span c…
55406 <span class="hljs-meta">> </span><span class="language-bash">const timeout = util.promisify(setTime…
55408 <span class="hljs-meta">> </span><span class="language-bash">const old = Date.now(); await timeout(…
55415 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">co…
55417 <span class="hljs-meta">> </span><span class="language-bash">m</span>
55419 <span class="hljs-meta">> </span><span class="language-bash">const m = await Promise.resolve(234)</…
55421 <span class="hljs-meta">> </span><span class="language-bash">m</span>
55423 <h4>Reverse-i-search<span><a class="mark" href="#repl_reverse_i_search" id="repl_reverse_i_search">…
55427 <p>The REPL supports bi-directional reverse-i-search similar to <a href="https://en.wikipedia.org/w…
55443 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55444 …="hljs-keyword">const</span> { <span class="hljs-title class_">Translator</span> } = <span class="…
55446-keyword">const</span> myTranslator = <span class="hljs-keyword">new</span> <span class="hljs-titl…
55448 <span class="hljs-keyword">function</span> <span class="hljs-title function_">myEval</span>(<span c…
55449 …<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, myTrans…
55452 … class="hljs-title function_">start</span>({ <span class="hljs-attr">prompt</span>: <span class="h…
55455 the <code>eval</code> function. In order to support multi-line input, the <code>eval</code> function
55457 …ode class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title functio…
55458 <span class="hljs-keyword">let</span> result;
55459 <span class="hljs-keyword">try</span> {
55460 result = vm.<span class="hljs-title function_">runInThisContext</span>(cmd);
55461 } <span class="hljs-keyword">catch</span> (e) {
55462 …<span class="hljs-keyword">if</span> (<span class="hljs-title function_">isRecoverableError</span>…
55463 …ss="hljs-keyword">return</span> <span class="hljs-title function_">callback</span>(<span class="hl…
55466 …<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, result);
55469 <span class="hljs-keyword">function</span> <span class="hljs-title function_">isRecoverableError</s…
55470 …<span class="hljs-keyword">if</span> (error.<span class="hljs-property">name</span> === <span clas…
55471-keyword">return</span> <span class="hljs-regexp">/^(Unexpected end of input|Unexpected token)/</s…
55473 <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
55488 …lass="language-console"><span class="hljs-meta">> </span><span class="language-bash">util.inspect.…
55490 <span class="hljs-meta">> </span><span class="language-bash">[1]</span>
55494 <span class="hljs-meta">></span></code></pre>
55498 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55500-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>({ <span class="hlj…
55502 <span class="hljs-keyword">function</span> <span class="hljs-title function_">myEval</span>(<span c…
55503 <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, cmd);
55506 <span class="hljs-keyword">function</span> <span class="hljs-title function_">myWriter</span>(<span…
55507 …<span class="hljs-keyword">return</span> output.<span class="hljs-title function_">toUpperCase</sp…
55514 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
55519 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55521 <span class="hljs-keyword">const</span> options = { <span class="hljs-attr">useColors</span>: <span…
55523 <span class="hljs-keyword">const</span> firstInstance = repl.<span class="hljs-title function_">sta…
55524 <span class="hljs-keyword">const</span> secondInstance = <span class="hljs-keyword">new</span> repl…
55535 … class="language-js">replServer.<span class="hljs-title function_">on</span>(<span class="hljs-str…
55536 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
55537 process.<span class="hljs-title function_">exit</span>();
55548 <p>This can be used primarily to re-initialize REPL context to some pre-defined
55550 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55552 <span class="hljs-keyword">function</span> <span class="hljs-title function_">initializeContext</sp…
55553 context.<span class="hljs-property">m</span> = <span class="hljs-string">'test'</span>;
55556 …s="hljs-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>({ <span cla…
55557 <span class="hljs-title function_">initializeContext</span>(r.<span class="hljs-property">context</…
55559 r.<span class="hljs-title function_">on</span>(<span class="hljs-string">'reset'</span>, initialize…
55562 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">./…
55563 <span class="hljs-meta">> </span><span class="language-bash">m</span>
55565 <span class="hljs-meta">> </span><span class="language-bash">m = 1</span>
55567 <span class="hljs-meta">> </span><span class="language-bash">m</span>
55569 <span class="hljs-meta">> </span><span class="language-bash">.clear</span>
55571 <span class="hljs-meta">> </span><span class="language-bash">m</span>
55573 <span class="hljs-meta">></span></code></pre>
55579 <li><code>keyword</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
55580 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
55582 <p>The <code>replServer.defineCommand()</code> method is used to add new <code>.</code>-prefixed co…
55587 <li><code>help</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
55588 <li><code>action</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
55592 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55594 …js-keyword">const</span> replServer = repl.<span class="hljs-title function_">start</span>({ <span…
55595 replServer.<span class="hljs-title function_">defineCommand</span>(<span class="hljs-string">'sayhe…
55596 <span class="hljs-attr">help</span>: <span class="hljs-string">'Say hello'</span>,
55597 <span class="hljs-title function_">action</span>(<span class="hljs-params">name</span>) {
55598 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">clearBuffered…
55599 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
55600 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">displayPrompt…
55603-title function_">defineCommand</span>(<span class="hljs-string">'saybye'</span>, <span class="hlj…
55604 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
55605 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">close</span>(…
55608 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">.s…
55610 <span class="hljs-meta">> </span><span class="language-bash">.saybye</span>
55617 <li><code>preserveCursor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
55622 <p>When multi-line input is being entered, an ellipsis is printed rather than the
55640 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated.</div><p>…
55642 <li><code>keyword</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
55643 <li><code>rest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
55644 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
55653 <li><code>historyPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
55654 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
55656 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
55662 Node.js binary and using the command-line REPL, a history file is initialized
55671 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
55695 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
55697 <li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
55703 <li><code>terminal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
55707 <li><code>eval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
55711 <li><code>useColors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
55716 <li><code>useGlobal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
55720 <li><code>ignoreUndefined</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
55723 <li><code>writer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
55725 <li><code>completer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
55727 <li><code>replMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
55736 <li><code>breakEvalOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
55740 <li><code>preview</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
55750 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55752 <span class="hljs-comment">// a Unix style prompt</span>
55753 repl.<span class="hljs-title function_">start</span>(<span class="hljs-string">'$ '</span>);</code>…
55757 without passing any arguments (or by passing the <code>-i</code> argument):</p>
55758 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
55759 <span class="hljs-meta">> </span><span class="language-bash">const a = [1, 2, 3];</span>
55761 <span class="hljs-meta">> </span><span class="language-bash">a</span>
55763 <span class="hljs-meta">> </span><span class="language-bash">a.forEach((v) => {</span>
55783 <code>'sloppy'</code>, which will allow non-strict mode code to be run.</li>
55786 <p>By default, the Node.js REPL will persist history between <code>node</code> REPL sessions
55790 <h4>Using the Node.js REPL with advanced line-editors<span><a class="mark" href="#repl_using_the_no…
55791 <p>For advanced line-editors, start Node.js with the environment variable
55795 <pre><code class="language-text">alias node="env NODE_NO_READLINE=1 rlwrap node"</code></pre>
55802 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
55803 <span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span cla…
55804 <span class="hljs-keyword">let</span> connections = <span class="hljs-number">0</span>;
55806 repl.<span class="hljs-title function_">start</span>({
55807 <span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via stdin> '</span>,
55808 <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>,
55809 <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>
55812 net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class…
55813 connections += <span class="hljs-number">1</span>;
55814 repl.<span class="hljs-title function_">start</span>({
55815 …<span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via Unix socket> '</span…
55816 <span class="hljs-attr">input</span>: socket,
55817 <span class="hljs-attr">output</span>: socket
55818 …}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span cla…
55819 socket.<span class="hljs-title function_">end</span>();
55821 }).<span class="hljs-title function_">listen</span>(<span class="hljs-string">'/tmp/node-repl-sock'…
55823 net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class…
55824 connections += <span class="hljs-number">1</span>;
55825 repl.<span class="hljs-title function_">start</span>({
55826 …<span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via TCP socket> '</span>,
55827 <span class="hljs-attr">input</span>: socket,
55828 <span class="hljs-attr">output</span>: socket
55829 …}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span cla…
55830 socket.<span class="hljs-title function_">end</span>();
55832 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">5001</span>);</code><…
55837 <p>By starting a REPL from a Unix socket-based server instead of stdin, it is
55838 possible to connect to a long-running Node.js process without restarting it.</p>
55839 <p>For an example of running a "full-featured" (<code>terminal</code>) REPL over
55848 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
55850 <p>Delivers a JSON-formatted diagnostic summary, written to a file.</p>
55859 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
55860 …<span class="hljs-attr">"header"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
55861 …span class="hljs-attr">"reportVersion"</span><span class="hljs-punctuation">:</span> <span class="…
55862 …pan class="hljs-attr">"event"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
55863 …an class="hljs-attr">"trigger"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55864-attr">"filename"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"report.…
55865 …"hljs-attr">"dumpEventTime"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
55866 …="hljs-attr">"dumpEventTimeStamp"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55867 …span class="hljs-attr">"processId"</span><span class="hljs-punctuation">:</span> <span class="hljs
55868 …s="hljs-attr">"cwd"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/home…
55869 …<span class="hljs-attr">"commandLine"</span><span class="hljs-punctuation">:</span> <span class="h…
55870 …<span class="hljs-string">"/home/nodeuser/project/node/out/Release/node"</span><span class="hljs-p…
55871 …<span class="hljs-string">"--report-uncaught-exception"</span><span class="hljs-punctuation">,</sp…
55872 …<span class="hljs-string">"/home/nodeuser/project/node/test/report/test-exception.js"</span><span …
55873 <span class="hljs-string">"child"</span>
55874 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
55875 …lass="hljs-attr">"nodejsVersion"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55876 …lass="hljs-attr">"glibcVersionRuntime"</span><span class="hljs-punctuation">:</span> <span class="…
55877 …lass="hljs-attr">"glibcVersionCompiler"</span><span class="hljs-punctuation">:</span> <span class=…
55878 …pan class="hljs-attr">"wordSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55879 …<span class="hljs-attr">"arch"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55880 …pan class="hljs-attr">"platform"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55881 …<span class="hljs-attr">"componentVersions"</span><span class="hljs-punctuation">:</span> <span cl…
55882 …pan class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
55883 … class="hljs-attr">"v8"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"7…
55884 …<span class="hljs-attr">"uv"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
55885 …<span class="hljs-attr">"zlib"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55886 …<span class="hljs-attr">"ares"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55887 …<span class="hljs-attr">"modules"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55888 …pan class="hljs-attr">"nghttp2"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
55889 …<span class="hljs-attr">"napi"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55890 …span class="hljs-attr">"llhttp"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
55891 …<span class="hljs-attr">"openssl"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55892 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55893 …<span class="hljs-attr">"release"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55894 …<span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55895 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55896 …span class="hljs-attr">"osName"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
55897 …s="hljs-attr">"osRelease"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
55898-attr">"osVersion"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"#1 SMP…
55899 …an class="hljs-attr">"osMachine"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55900 …<span class="hljs-attr">"cpus"</span><span class="hljs-punctuation">:</span> <span class="hljs-pun…
55901 <span class="hljs-punctuation">{</span>
55902-attr">"model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Intel(R) C…
55903 …<span class="hljs-attr">"speed"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
55904 …<span class="hljs-attr">"user"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55905 …<span class="hljs-attr">"nice"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55906 …<span class="hljs-attr">"sys"</span><span class="hljs-punctuation">:</span> <span class="hljs-numb…
55907 …span class="hljs-attr">"idle"</span><span class="hljs-punctuation">:</span> <span class="hljs-numb…
55908 …<span class="hljs-attr">"irq"</span><span class="hljs-punctuation">:</span> <span class="hljs-numb…
55909 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55910 <span class="hljs-punctuation">{</span>
55911-attr">"model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Intel(R) C…
55912 …<span class="hljs-attr">"speed"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
55913 …<span class="hljs-attr">"user"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55914 …<span class="hljs-attr">"nice"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55915 …<span class="hljs-attr">"sys"</span><span class="hljs-punctuation">:</span> <span class="hljs-numb…
55916 …span class="hljs-attr">"idle"</span><span class="hljs-punctuation">:</span> <span class="hljs-numb…
55917 …<span class="hljs-attr">"irq"</span><span class="hljs-punctuation">:</span> <span class="hljs-numb…
55918 <span class="hljs-punctuation">}</span>
55919 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
55920 …<span class="hljs-attr">"networkInterfaces"</span><span class="hljs-punctuation">:</span> <span cl…
55921 <span class="hljs-punctuation">{</span>
55922 …<span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55923 …span class="hljs-attr">"internal"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55924 … class="hljs-attr">"mac"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
55925 …an class="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55926 … class="hljs-attr">"netmask"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
55927 …<span class="hljs-attr">"family"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55928 <span class="hljs-punctuation">}</span>
55929 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
55930 …<span class="hljs-attr">"host"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55931 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55932 …<span class="hljs-attr">"javascriptStack"</span><span class="hljs-punctuation">:</span> <span clas…
55933-attr">"message"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Error: *…
55934 …<span class="hljs-attr">"stack"</span><span class="hljs-punctuation">:</span> <span class="hljs-pu…
55935 …<span class="hljs-string">"at myException (/home/nodeuser/project/node/test/report/test-exception.…
55936 …n class="hljs-string">"at Object.&#x3C;anonymous> (/home/nodeuser/project/node/test/report/test-ex…
55937 …<span class="hljs-string">"at Module._compile (internal/modules/cjs/loader.js:718:30)"</span><span…
55938 …<span class="hljs-string">"at Object.Module._extensions..js (internal/modules/cjs/loader.js:729:10…
55939 …<span class="hljs-string">"at Module.load (internal/modules/cjs/loader.js:617:32)"</span><span cla…
55940 …<span class="hljs-string">"at tryModuleLoad (internal/modules/cjs/loader.js:560:12)"</span><span c…
55941 …<span class="hljs-string">"at Function.Module._load (internal/modules/cjs/loader.js:552:3)"</span>…
55942 …<span class="hljs-string">"at Function.Module.runMain (internal/modules/cjs/loader.js:771:12)"</sp…
55943 <span class="hljs-string">"at executeUserCode (internal/bootstrap/node.js:332:15)"</span>
55944 <span class="hljs-punctuation">]</span>
55945 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55946 …<span class="hljs-attr">"nativeStack"</span><span class="hljs-punctuation">:</span> <span class="h…
55947 <span class="hljs-punctuation">{</span>
55948 … class="hljs-attr">"pc"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"0…
55949 …<span class="hljs-attr">"symbol"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55950 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55951 <span class="hljs-punctuation">{</span>
55952 … class="hljs-attr">"pc"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"0…
55953 …<span class="hljs-attr">"symbol"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55954 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55955 <span class="hljs-punctuation">{</span>
55956 … class="hljs-attr">"pc"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"0…
55957 …<span class="hljs-attr">"symbol"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55958 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55959 <span class="hljs-punctuation">{</span>
55960 … class="hljs-attr">"pc"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"0…
55961 …<span class="hljs-attr">"symbol"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55962 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55963 <span class="hljs-punctuation">{</span>
55964 … class="hljs-attr">"pc"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"0…
55965 …<span class="hljs-attr">"symbol"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55966 <span class="hljs-punctuation">}</span>
55967 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
55968 …<span class="hljs-attr">"javascriptHeap"</span><span class="hljs-punctuation">:</span> <span class…
55969 …an class="hljs-attr">"totalMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs
55970 …ass="hljs-attr">"totalCommittedMemory"</span><span class="hljs-punctuation">:</span> <span class="…
55971 …an class="hljs-attr">"usedMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55972 …lass="hljs-attr">"availableMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs
55973 … class="hljs-attr">"memoryLimit"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
55974 …<span class="hljs-attr">"heapSpaces"</span><span class="hljs-punctuation">:</span> <span class="hl…
55975 …<span class="hljs-attr">"read_only_space"</span><span class="hljs-punctuation">:</span> <span clas…
55976 …pan class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hljs
55977 …n class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class="h…
55978 …span class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55979 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55980 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55981 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55982 …<span class="hljs-attr">"new_space"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55983 …an class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55984 … class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class="hl…
55985 …pan class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
55986 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55987 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55988 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55989 …<span class="hljs-attr">"old_space"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55990 …an class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55991 … class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class="hl…
55992 …pan class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
55993 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55994 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55995 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55996 …<span class="hljs-attr">"code_space"</span><span class="hljs-punctuation">:</span> <span class="hl…
55997 …pan class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hljs
55998 … class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class="hl…
55999 …span class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56000 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
56001 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56002 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56003 …<span class="hljs-attr">"map_space"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56004 …pan class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hljs
56005 … class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class="hl…
56006 …span class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56007 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
56008 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56009 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56010 …<span class="hljs-attr">"large_object_space"</span><span class="hljs-punctuation">:</span> <span c…
56011 …<span class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hl…
56012 …pan class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class=…
56013 …an class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
56014 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
56015 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56016 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56017 …<span class="hljs-attr">"new_large_object_space"</span><span class="hljs-punctuation">:</span> <sp…
56018 …<span class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hl…
56019 …pan class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class=…
56020 …<span class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs
56021 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
56022 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56023 <span class="hljs-punctuation">}</span>
56024 <span class="hljs-punctuation">}</span>
56025 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56026 …<span class="hljs-attr">"resourceUsage"</span><span class="hljs-punctuation">:</span> <span class=…
56027 … class="hljs-attr">"userCpuSeconds"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56028 …class="hljs-attr">"kernelCpuSeconds"</span><span class="hljs-punctuation">:</span> <span class="hl…
56029 …ss="hljs-attr">"cpuConsumptionPercent"</span><span class="hljs-punctuation">:</span> <span class="…
56030 …span class="hljs-attr">"maxRss"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
56031 …<span class="hljs-attr">"pageFaults"</span><span class="hljs-punctuation">:</span> <span class="hl…
56032 …<span class="hljs-attr">"IORequired"</span><span class="hljs-punctuation">:</span> <span class="hl…
56033 …<span class="hljs-attr">"IONotRequired"</span><span class="hljs-punctuation">:</span> <span class=…
56034 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56035 …<span class="hljs-attr">"fsActivity"</span><span class="hljs-punctuation">:</span> <span class="hl…
56036 …<span class="hljs-attr">"reads"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
56037 …<span class="hljs-attr">"writes"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
56038 <span class="hljs-punctuation">}</span>
56039 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56040 …<span class="hljs-attr">"uvthreadResourceUsage"</span><span class="hljs-punctuation">:</span> <spa…
56041 … class="hljs-attr">"userCpuSeconds"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56042 …class="hljs-attr">"kernelCpuSeconds"</span><span class="hljs-punctuation">:</span> <span class="hl…
56043 …ss="hljs-attr">"cpuConsumptionPercent"</span><span class="hljs-punctuation">:</span> <span class="…
56044 …<span class="hljs-attr">"fsActivity"</span><span class="hljs-punctuation">:</span> <span class="hl…
56045 …<span class="hljs-attr">"reads"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
56046 …<span class="hljs-attr">"writes"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
56047 <span class="hljs-punctuation">}</span>
56048 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56049 …<span class="hljs-attr">"libuv"</span><span class="hljs-punctuation">:</span> <span class="hljs-pu…
56050 <span class="hljs-punctuation">{</span>
56051 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56052 …span class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs
56053 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56054 …ass="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
56055 …<span class="hljs-attr">"details"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56056 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56057 <span class="hljs-punctuation">{</span>
56058 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56059 …pan class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56060 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56061 …ass="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
56062 …<span class="hljs-attr">"repeat"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
56063 …ss="hljs-attr">"firesInMsFromNow"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56064 …<span class="hljs-attr">"expired"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56065 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56066 <span class="hljs-punctuation">{</span>
56067 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56068 …span class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs
56069 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56070 …<span class="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56071 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56072 <span class="hljs-punctuation">{</span>
56073 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56074 …pan class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56075 …an class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hl…
56076 …<span class="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56077 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56078 <span class="hljs-punctuation">{</span>
56079 …span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
56080 …pan class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56081 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56082 …<span class="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56083 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56084 <span class="hljs-punctuation">{</span>
56085 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56086 …pan class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56087 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56088 …<span class="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56089 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56090 <span class="hljs-punctuation">{</span>
56091 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56092 …span class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs
56093 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56094 …<span class="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56095 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56096 <span class="hljs-punctuation">{</span>
56097 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56098 …pan class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56099 …an class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hl…
56100 …ass="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
56101 …<span class="hljs-attr">"width"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
56102 …<span class="hljs-attr">"height"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
56103 …<span class="hljs-attr">"fd"</span><span class="hljs-punctuation">:</span> <span class="hljs-numbe…
56104 …pan class="hljs-attr">"writeQueueSize"</span><span class="hljs-punctuation">:</span> <span class="…
56105 …span class="hljs-attr">"readable"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56106 …<span class="hljs-attr">"writable"</span><span class="hljs-punctuation">:</span> <span class="hljs
56107 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56108 <span class="hljs-punctuation">{</span>
56109 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56110 …span class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs
56111 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56112 …ass="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
56113 …<span class="hljs-attr">"signum"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
56114 …<span class="hljs-attr">"signal"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
56115 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56116 <span class="hljs-punctuation">{</span>
56117 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56118 …span class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs
56119 …an class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hl…
56120 …ass="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
56121 …<span class="hljs-attr">"width"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
56122 …<span class="hljs-attr">"height"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
56123 …<span class="hljs-attr">"fd"</span><span class="hljs-punctuation">:</span> <span class="hljs-numbe…
56124 …pan class="hljs-attr">"writeQueueSize"</span><span class="hljs-punctuation">:</span> <span class="…
56125 …span class="hljs-attr">"readable"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56126 …<span class="hljs-attr">"writable"</span><span class="hljs-punctuation">:</span> <span class="hljs
56127 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56128 <span class="hljs-punctuation">{</span>
56129 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56130 …span class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs
56131 …ass="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
56132 …<span class="hljs-attr">"loopIdleTimeSeconds"</span><span class="hljs-punctuation">:</span> <span …
56133 <span class="hljs-punctuation">}</span>
56134 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
56135-attr">"workers"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</s…
56136 …<span class="hljs-attr">"environmentVariables"</span><span class="hljs-punctuation">:</span> <span…
56137 …n class="hljs-attr">"REMOTEHOST"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
56138-attr">"MANPATH"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/opt/rh/…
56139 … class="hljs-attr">"XDG_SESSION_ID"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56140 … class="hljs-attr">"HOSTNAME"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
56141 …an class="hljs-attr">"HOST"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
56142 …n class="hljs-attr">"TERM"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
56143 …pan class="hljs-attr">"SHELL"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
56144 …n class="hljs-attr">"SSH_CLIENT"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
56145-attr">"PERL5LIB"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/opt/rh…
56146 …ljs-attr">"OLDPWD"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/home/…
56147-attr">"JAVACONFDIRS"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/op…
56148 …n class="hljs-attr">"SSH_TTY"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
56149 …="hljs-attr">"PCP_DIR"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/o…
56150 …an class="hljs-attr">"GROUP"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56151 …span class="hljs-attr">"USER"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
56152-attr">"LD_LIBRARY_PATH"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
56153 … class="hljs-attr">"HOSTTYPE"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
56154-attr">"XDG_CONFIG_DIRS"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
56155 …ss="hljs-attr">"MAIL"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/va…
56156-attr">"PATH"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/home/nodeu…
56157 …s="hljs-attr">"PWD"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/home…
56158 …an class="hljs-attr">"LANG"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
56159-attr">"PS1"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"\\u@\\h : \\…
56160 …<span class="hljs-attr">"SHLVL"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
56161 …n class="hljs-attr">"HOME"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
56162 …span class="hljs-attr">"OSTYPE"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
56163 …pan class="hljs-attr">"VENDOR"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56164-attr">"PYTHONPATH"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/opt/…
56165 …pan class="hljs-attr">"MACHTYPE"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
56166 …an class="hljs-attr">"LOGNAME"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56167-attr">"XDG_DATA_DIRS"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/o…
56168 …"hljs-attr">"LESSOPEN"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"||…
56169-attr">"INFOPATH"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/opt/rh…
56170 …s="hljs-attr">"XDG_RUNTIME_DIR"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
56171 …<span class="hljs-attr">"_"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
56172 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56173 …<span class="hljs-attr">"userLimits"</span><span class="hljs-punctuation">:</span> <span class="hl…
56174 …<span class="hljs-attr">"core_file_size_blocks"</span><span class="hljs-punctuation">:</span> <spa…
56175 …<span class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56176 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56177 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56178 …<span class="hljs-attr">"data_seg_size_kbytes"</span><span class="hljs-punctuation">:</span> <span…
56179 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56180 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56181 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56182 …<span class="hljs-attr">"file_size_blocks"</span><span class="hljs-punctuation">:</span> <span cla…
56183 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56184 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56185 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56186 …<span class="hljs-attr">"max_locked_memory_bytes"</span><span class="hljs-punctuation">:</span> <s…
56187 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56188 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
56189 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56190 …<span class="hljs-attr">"max_memory_size_kbytes"</span><span class="hljs-punctuation">:</span> <sp…
56191 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56192 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56193 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56194 …<span class="hljs-attr">"open_files"</span><span class="hljs-punctuation">:</span> <span class="hl…
56195 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56196 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
56197 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56198 …<span class="hljs-attr">"stack_size_bytes"</span><span class="hljs-punctuation">:</span> <span cla…
56199 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56200 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56201 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56202 …<span class="hljs-attr">"cpu_time_seconds"</span><span class="hljs-punctuation">:</span> <span cla…
56203 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56204 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56205 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56206 …<span class="hljs-attr">"max_user_processes"</span><span class="hljs-punctuation">:</span> <span c…
56207 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56208 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
56209 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56210 …<span class="hljs-attr">"virtual_memory_kbytes"</span><span class="hljs-punctuation">:</span> <spa…
56211 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56212 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56213 <span class="hljs-punctuation">}</span>
56214 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56215 …<span class="hljs-attr">"sharedObjects"</span><span class="hljs-punctuation">:</span> <span class=…
56216 <span class="hljs-string">"/lib64/libdl.so.2"</span><span class="hljs-punctuation">,</span>
56217 <span class="hljs-string">"/lib64/librt.so.1"</span><span class="hljs-punctuation">,</span>
56218 <span class="hljs-string">"/lib64/libstdc++.so.6"</span><span class="hljs-punctuation">,</span>
56219 <span class="hljs-string">"/lib64/libm.so.6"</span><span class="hljs-punctuation">,</span>
56220 <span class="hljs-string">"/lib64/libgcc_s.so.1"</span><span class="hljs-punctuation">,</span>
56221 <span class="hljs-string">"/lib64/libpthread.so.0"</span><span class="hljs-punctuation">,</span>
56222 <span class="hljs-string">"/lib64/libc.so.6"</span><span class="hljs-punctuation">,</span>
56223 <span class="hljs-string">"/lib64/ld-linux-x86-64.so.2"</span>
56224 <span class="hljs-punctuation">]</span>
56225 <span class="hljs-punctuation">}</span></code></pre>
56227 <pre><code class="language-bash">node --report-uncaught-exception --report-on-signal \
56228 --report-on-fatalerror app.js</code></pre>
56231 <p><code>--report-uncaught-exception</code> Enables report to be generated on
56232 un-caught exceptions. Useful when inspecting JavaScript stack in conjunction
56236 <p><code>--report-on-signal</code> Enables report to be generated upon receiving
56251 <p><code>--report-on-fatalerror</code> Enables the report to be triggered on fatal errors
56258 <p><code>--report-compact</code> Write reports in a compact format, single-line JSON, more
56259 easily consumable by log processing systems than the default multi-line format
56263 <p><code>--report-directory</code> Location at which the report will be
56267 <p><code>--report-filename</code> Name of the file to which the report will be
56271 <p><code>--report-signal</code> Sets or resets the signal for report generation
56276 <pre><code class="language-js">process.<span class="hljs-property">report</span>.<span class="hljs-
56279 …ass="language-js">process.<span class="hljs-property">report</span>.<span class="hljs-title functi…
56285 <pre><code class="language-js"><span class="hljs-keyword">try</span> {
56286 …process.<span class="hljs-title function_">chdir</span>(<span class="hljs-string">'/non-existent-p…
56287 } <span class="hljs-keyword">catch</span> (err) {
56288 …process.<span class="hljs-property">report</span>.<span class="hljs-title function_">writeReport</…
56290 <span class="hljs-comment">// Any other code</span></code></pre>
56293 <pre><code class="language-js"><span class="hljs-keyword">try</span> {
56294 …process.<span class="hljs-title function_">chdir</span>(<span class="hljs-string">'/non-existent-p…
56295 } <span class="hljs-keyword">catch</span> (err) {
56296 …process.<span class="hljs-property">report</span>.<span class="hljs-title function_">writeReport</…
56298 <span class="hljs-comment">// Any other code</span></code></pre>
56301 …class="language-js"><span class="hljs-keyword">const</span> report = process.<span class="hljs-pro…
56302-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
56304 <span class="hljs-comment">// Similar to process.report.writeReport() output</span>
56305-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
56309-js"><span class="hljs-keyword">const</span> report = process.<span class="hljs-property">report</…
56310-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
56312 the application, in expectation of self-adjusting the resource consumption,
56320 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
56321 <span class="hljs-meta">> </span><span class="language-bash">process.report.writeReport();</span>
56324 <span class="hljs-meta">></span></code></pre>
56351 <pre><code class="language-js"><span class="hljs-comment">// Trigger report only on uncaught except…
56352 process.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnFatalError</s…
56353 process.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnSignal</span>…
56354 …ocess.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnUncaughtExcept…
56356 <span class="hljs-comment">// Trigger report for both internal errors as well as external signal.</…
56357 process.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnFatalError</s…
56358 process.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnSignal</span>…
56359 …ocess.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnUncaughtExcept…
56361 <span class="hljs-comment">// Change the default signal to 'SIGQUIT' and enable it.</span>
56362 process.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnFatalError</s…
56363 …ocess.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnUncaughtExcept…
56364 process.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnSignal</span>…
56365 process.<span class="hljs-property">report</span>.<span class="hljs-property">signal</span> = <span…
56368 <pre><code class="language-bash">NODE_OPTIONS=<span class="hljs-string">"--report-uncaught-exceptio…
56369 --report-on-fatalerror --report-on-signal \
56370 --report-signal=SIGUSR2 --report-filename=./report.json \
56371 --report-directory=/home/nodeuser"</span></code></pre>
56395 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
56405 …ode class="language-js"><span class="hljs-keyword">const</span> stream = <span class="hljs-built_i…
56484 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
56486 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
56487 <span class="hljs-comment">// `req` is an http.IncomingMessage, which is a readable stream.</span>
56488 <span class="hljs-comment">// `res` is an http.ServerResponse, which is a writable stream.</span>
56490 <span class="hljs-keyword">let</span> body = <span class="hljs-string">''</span>;
56491 <span class="hljs-comment">// Get the data as utf8 strings.</span>
56492 <span class="hljs-comment">// If an encoding is not set, Buffer objects will be received.</span>
56493 …req.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
56495 <span class="hljs-comment">// Readable streams emit 'data' events once a listener is added.</span>
56496 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
56500 …<span class="hljs-comment">// The 'end' event indicates that the entire body has been received.</s…
56501 …req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span cla…
56502 <span class="hljs-keyword">try</span> {
56503 …<span class="hljs-keyword">const</span> data = <span class="hljs-variable constant_">JSON</span>.<…
56504 <span class="hljs-comment">// Write back something interesting to the user:</span>
56505 …res.<span class="hljs-title function_">write</span>(<span class="hljs-keyword">typeof</span> data);
56506 res.<span class="hljs-title function_">end</span>();
56507 } <span class="hljs-keyword">catch</span> (er) {
56508 <span class="hljs-comment">// uh oh! bad json!</span>
56509 res.<span class="hljs-property">statusCode</span> = <span class="hljs-number">400</span>;
56510 …n class="hljs-keyword">return</span> res.<span class="hljs-title function_">end</span>(<span class…
56515 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);
56517 <span class="hljs-comment">// $ curl localhost:1337 -d "{}"</span>
56518 <span class="hljs-comment">// object</span>
56519 <span class="hljs-comment">// $ curl localhost:1337 -d "\"foo\""</span>
56520 <span class="hljs-comment">// string</span>
56521 <span class="hljs-comment">// $ curl localhost:1337 -d "not json"</span>
56522 <span class="hljs-comment">// error: Unexpected token o in JSON at position 1</span></code></pre>
56558 <pre><code class="language-js"><span class="hljs-keyword">const</span> myStream = <span class="hljs
56559 myStream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data'</spa…
56560 myStream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some more data'…
56561 myStream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'done writing data…
56591 <pre><code class="language-js"><span class="hljs-comment">// Write the data to the supplied writabl…
56592 <span class="hljs-comment">// Be attentive to back-pressure.</span>
56593 <span class="hljs-keyword">function</span> <span class="hljs-title function_">writeOneMillionTimes<…
56594 <span class="hljs-keyword">let</span> i = <span class="hljs-number">1000000</span>;
56595 <span class="hljs-title function_">write</span>();
56596 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">write</span>(<span c…
56597 <span class="hljs-keyword">let</span> ok = <span class="hljs-literal">true</span>;
56598 <span class="hljs-keyword">do</span> {
56599 i--;
56600 <span class="hljs-keyword">if</span> (i === <span class="hljs-number">0</span>) {
56601 <span class="hljs-comment">// Last time!</span>
56602 writer.<span class="hljs-title function_">write</span>(data, encoding, callback);
56603 } <span class="hljs-keyword">else</span> {
56604 <span class="hljs-comment">// See if we should continue, or wait.</span>
56605 <span class="hljs-comment">// Don't pass the callback, because we're not done yet.</span>
56606 ok = writer.<span class="hljs-title function_">write</span>(data, encoding);
56608 …} <span class="hljs-keyword">while</span> (i > <span class="hljs-number">0</span> &#x26;&#x26; ok);
56609 <span class="hljs-keyword">if</span> (i > <span class="hljs-number">0</span>) {
56610 <span class="hljs-comment">// Had to stop early!</span>
56611 <span class="hljs-comment">// Write some more once it drains.</span>
56612 …writer.<span class="hljs-title function_">once</span>(<span class="hljs-string">'drain'</span>, wr…
56621 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error…
56636 <pre><code class="language-js"><span class="hljs-keyword">const</span> writer = <span class="hljs-t…
56637 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
56638 …writer.<span class="hljs-title function_">write</span>(<span class="hljs-string">`hello, #<span cl…
56640 writer.<span class="hljs-title function_">on</span>(<span class="hljs-string">'finish'</span>, <spa…
56641 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
56643 writer.<span class="hljs-title function_">end</span>(<span class="hljs-string">'This is the end\n'<…
56653 <pre><code class="language-js"><span class="hljs-keyword">const</span> writer = <span class="hljs-t…
56654 <span class="hljs-keyword">const</span> reader = <span class="hljs-title function_">getReadableStre…
56655 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'pipe'</span>, <span class…
56656 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
56657 assert.<span class="hljs-title function_">equal</span>(src, reader);
56659 reader.<span class="hljs-title function_">pipe</span>(writer);</code></pre>
56673 <pre><code class="language-js"><span class="hljs-keyword">const</span> writer = <span class="hljs-t…
56674 <span class="hljs-keyword">const</span> reader = <span class="hljs-title function_">getReadableStre…
56675 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'unpipe'</span>, <span clas…
56676 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
56677 assert.<span class="hljs-title function_">equal</span>(src, reader);
56679 reader.<span class="hljs-title function_">pipe</span>(writer);
56680 reader.<span class="hljs-title function_">unpipe</span>(writer);</code></pre>
56692 all to <code>writable._writev()</code>, if present. This prevents a head-of-line blocking
56703 <td><p>Work as a no-op on a stream that has already been destroyed.</p></td></tr>
56710 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
56711 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
56722-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><pre><code cl…
56724 <span class="hljs-keyword">const</span> myStream = <span class="hljs-keyword">new</span> <span clas…
56726 …lass="hljs-keyword">const</span> fooErr = <span class="hljs-keyword">new</span> <span class="hljs-
56727 myStream.<span class="hljs-title function_">destroy</span>(fooErr);
56728-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">…
56730 <span class="hljs-keyword">const</span> myStream = <span class="hljs-keyword">new</span> <span clas…
56732 myStream.<span class="hljs-title function_">destroy</span>();
56733-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword">f…
56734-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span>…
56736 <span class="hljs-keyword">const</span> myStream = <span class="hljs-keyword">new</span> <span clas…
56737 myStream.<span class="hljs-title function_">destroy</span>();
56739-title function_">write</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function"…
56740 <span class="hljs-comment">// ERR_STREAM_DESTROYED</span></code></pre>
56741 <p>Once <code>destroy()</code> has been called any further calls will be a no-op and no
56750 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
56753-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span>…
56755 <span class="hljs-keyword">const</span> myStream = <span class="hljs-keyword">new</span> <span clas…
56757-variable language_">console</span>.<span class="hljs-title function_">log</span>(myStream.<span c…
56758 myStream.<span class="hljs-title function_">destroy</span>();
56759-variable language_">console</span>.<span class="hljs-title function_">log</span>(myStream.<span c…
56777-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
56781 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
56782 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
56784 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
56793 <pre><code class="language-js"><span class="hljs-comment">// Write 'hello, ' and then end with 'wor…
56794 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
56795 <span class="hljs-keyword">const</span> file = fs.<span class="hljs-title function_">createWriteStr…
56796 file.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello, '</span>);
56797 file.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world!'</span>);
56798 <span class="hljs-comment">// Writing more now is not allowed!</span></code></pre>
56812 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
56813 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
56827 <pre><code class="language-js">stream.<span class="hljs-title function_">cork</span>();
56828 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some '</span>);
56829 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'data '</span>);
56830 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span>…
56834 <pre><code class="language-js">stream.<span class="hljs-title function_">cork</span>();
56835 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some '</span>);
56836 stream.<span class="hljs-title function_">cork</span>();
56837 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'data '</span>);
56838 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span>…
56839 stream.<span class="hljs-title function_">uncork</span>();
56840 …<span class="hljs-comment">// The data will not be flushed until uncork() is called a second time.…
56841 stream.<span class="hljs-title function_">uncork</span>();
56849 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
56858 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
56868 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
56877 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
56885 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
56894 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
56904 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
56912 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
56930-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
56934 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
56935 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
56936 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
56971 …ode class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title functio…
56972 …<span class="hljs-keyword">if</span> (!stream.<span class="hljs-title function_">write</span>(data…
56973 …stream.<span class="hljs-title function_">once</span>(<span class="hljs-string">'drain'</span>, cb…
56974 } <span class="hljs-keyword">else</span> {
56975 process.<span class="hljs-title function_">nextTick</span>(cb);
56979 <span class="hljs-comment">// Wait for cb to be called before doing any other write.</span>
56980 <span class="hljs-title function_">write</span>(<span class="hljs-string">'hello'</span>, <span cla…
56981 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57071-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">PassThrough</span>,…
57072 <span class="hljs-keyword">const</span> pass = <span class="hljs-keyword">new</span> <span class="h…
57073 <span class="hljs-keyword">const</span> writable = <span class="hljs-keyword">new</span> <span clas…
57075 pass.<span class="hljs-title function_">pipe</span>(writable);
57076 pass.<span class="hljs-title function_">unpipe</span>(writable);
57077 <span class="hljs-comment">// readableFlowing is now false.</span>
57079-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(…
57080 pass.<span class="hljs-title function_">write</span>(<span class="hljs-string">'ok'</span>); <span…
57081 pass.<span class="hljs-title function_">resume</span>(); <span class="hljs-comment">// Must be …
57093 require more fine-grained control over the transfer and generation of data can
57123 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
57141 <pre><code class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs
57142 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
57143 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
57155 <pre><code class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs
57156 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
57157 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
57159 readable.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span…
57160 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57167 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error…
57197 <pre><code class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs
57198 …ass="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hlj…
57199 <span class="hljs-comment">// There is some data to read now.</span>
57200 <span class="hljs-keyword">let</span> data;
57202 …<span class="hljs-keyword">while</span> (data = <span class="hljs-variable language_">this</span>.…
57203 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57213 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
57214 <span class="hljs-keyword">const</span> rr = fs.<span class="hljs-title function_">createReadStream…
57215 rr.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span …
57216 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
57218 rr.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class…
57219 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57222 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
57247 <td><p>Work as a no-op on a stream that has already been destroyed.</p></td></tr>
57254 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
57255 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57261 <p>Once <code>destroy()</code> has been called any further calls will be a no-op and no
57270 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
57278 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
57284 …e class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs-keyword…
57286 readable.<span class="hljs-title function_">isPaused</span>(); <span class="hljs-comment">// === fa…
57287 readable.<span class="hljs-title function_">pause</span>();
57288 readable.<span class="hljs-title function_">isPaused</span>(); <span class="hljs-comment">// === tr…
57289 readable.<span class="hljs-title function_">resume</span>();
57290 readable.<span class="hljs-title function_">isPaused</span>(); <span class="hljs-comment">// === fa…
57296 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57301 <pre><code class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs
57302 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
57303 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
57304 readable.<span class="hljs-title function_">pause</span>();
57305 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57306 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
57307 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57308 readable.<span class="hljs-title function_">resume</span>();
57309 }, <span class="hljs-number">1000</span>);
57319 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
57321 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
57334 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
57335 <span class="hljs-keyword">const</span> readable = <span class="hljs-title function_">getReadableSt…
57336 <span class="hljs-keyword">const</span> writable = fs.<span class="hljs-title function_">createWrit…
57337 <span class="hljs-comment">// All the data from readable goes into 'file.txt'.</span>
57338 readable.<span class="hljs-title function_">pipe</span>(writable);</code></pre>
57343 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
57344 <span class="hljs-keyword">const</span> r = fs.<span class="hljs-title function_">createReadStream<…
57345 <span class="hljs-keyword">const</span> z = zlib.<span class="hljs-title function_">createGzip</spa…
57346 <span class="hljs-keyword">const</span> w = fs.<span class="hljs-title function_">createWriteStream…
57347 r.<span class="hljs-title function_">pipe</span>(z).<span class="hljs-title function_">pipe</span>(…
57352 …class="language-js">reader.<span class="hljs-title function_">pipe</span>(writer, { <span class="h…
57353 reader.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span c…
57354 …writer.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Goodbye\n'</span>);
57367 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
57368-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
57385 <pre><code class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs
57387 <span class="hljs-comment">// 'readable' may be triggered multiple times as data is buffered in</sp…
57388 readable.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, …
57389 <span class="hljs-keyword">let</span> chunk;
57390 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57391 <span class="hljs-comment">// Use a loop to make sure we read all currently available data</span>
57392 …<span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = reada…
57393 …lass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span …
57397 <span class="hljs-comment">// 'end' will be triggered once when there is no more data available</sp…
57398 readable.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span…
57399 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57410 <pre><code class="language-js"><span class="hljs-keyword">const</span> chunks = [];
57412 readable.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, …
57413 <span class="hljs-keyword">let</span> chunk;
57414 …<span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = reada…
57415 chunks.<span class="hljs-title function_">push</span>(chunk);
57419 readable.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span…
57420 …<span class="hljs-keyword">const</span> content = chunks.<span class="hljs-title function_">join</…
57434 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
57443 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
57453 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> | <a…
57462 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
57470 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
57479 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
57488 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
57498 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
57514 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57520 <pre><code class="language-js"><span class="hljs-title function_">getReadableStreamSomehow</span>()
57521 .<span class="hljs-title function_">resume</span>()
57522 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class=…
57523 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57532 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
57533 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57541 output data to be interpreted as UTF-8 data, and passed as strings. Calling
57544 <p>The <code>Readable</code> stream will properly handle multi-byte characters delivered
57547 <pre><code class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs
57548 readable.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</sp…
57549 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
57550 …assert.<span class="hljs-title function_">equal</span>(<span class="hljs-keyword">typeof</span> ch…
57551-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
57559 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57566 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
57567 <span class="hljs-keyword">const</span> readable = <span class="hljs-title function_">getReadableSt…
57568 <span class="hljs-keyword">const</span> writable = fs.<span class="hljs-title function_">createWrit…
57569 <span class="hljs-comment">// All the data from readable goes into 'file.txt',</span>
57570 <span class="hljs-comment">// but only for the first second.</span>
57571 readable.<span class="hljs-title function_">pipe</span>(writable);
57572 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
57573 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57574 readable.<span class="hljs-title function_">unpipe</span>(writable);
57575 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57576 writable.<span class="hljs-title function_">end</span>();
57577 }, <span class="hljs-number">1000</span>);</code></pre>
57591-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> | …
57595 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
57604 code that needs to "un-consume" some amount of data that it has optimistically
57611 <pre><code class="language-js"><span class="hljs-comment">// Pull off a header delimited by \n\n.</…
57612 <span class="hljs-comment">// Use unshift() if we get too much.</span>
57613 <span class="hljs-comment">// Call the callback with (error, header, stream).</span>
57614 …"hljs-keyword">const</span> { <span class="hljs-title class_">StringDecoder</span> } = <span class…
57615 <span class="hljs-keyword">function</span> <span class="hljs-title function_">parseHeader</span>(<s…
57616 …stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, call…
57617 …stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, o…
57618 …"hljs-keyword">const</span> decoder = <span class="hljs-keyword">new</span> <span class="hljs-titl…
57619 <span class="hljs-keyword">let</span> header = <span class="hljs-string">''</span>;
57620 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">onReadable</span>(<s…
57621 <span class="hljs-keyword">let</span> chunk;
57622 …<span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = strea…
57623 …<span class="hljs-keyword">const</span> str = decoder.<span class="hljs-title function_">write</sp…
57624 …<span class="hljs-keyword">if</span> (str.<span class="hljs-title function_">match</span>(<span cl…
57625 <span class="hljs-comment">// Found the header boundary.</span>
57626 …<span class="hljs-keyword">const</span> split = str.<span class="hljs-title function_">split</span…
57627 header += split.<span class="hljs-title function_">shift</span>();
57628 …<span class="hljs-keyword">const</span> remaining = split.<span class="hljs-title function_">join<…
57629 …s-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
57630 …stream.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'error'<…
57631 <span class="hljs-comment">// Remove the 'readable' listener before unshifting.</span>
57632 …stream.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'readabl…
57633 <span class="hljs-keyword">if</span> (buf.<span class="hljs-property">length</span>)
57634 stream.<span class="hljs-title function_">unshift</span>(buf);
57635 … <span class="hljs-comment">// Now the body of the message can be read from the stream.</span>
57636 …<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, header,…
57637 } <span class="hljs-keyword">else</span> {
57638 <span class="hljs-comment">// Still reading the header.</span>
57658 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57669-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">OldReader</span> } …
57670 …s="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } = <span class="h…
57671 <span class="hljs-keyword">const</span> oreader = <span class="hljs-keyword">new</span> <span class…
57672 …="hljs-keyword">const</span> myReader = <span class="hljs-keyword">new</span> <span class="hljs-ti…
57674 myReader.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, …
57675 …myReader.<span class="hljs-title function_">read</span>(); <span class="hljs-comment">// etc.</spa…
57690 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x…
57692 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
57694 …lass="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-titl…
57695 …readable.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</s…
57696 <span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
57697 …n class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keywo…
57700 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57703-title function_">print</span>(fs.<span class="hljs-title function_">createReadStream</span>(<span…
57751 <td><p>Work as a no-op on a stream that has already been destroyed.</p></td></tr>
57758 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
57759 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57767 <p>Once <code>destroy()</code> has been called, any further calls will be a no-op and no
57787 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
57789 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
57791 <li><code>readable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
57794 <li><code>writable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
57799 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
57801 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
57806 … class="language-js"><span class="hljs-keyword">const</span> { finished } = <span class="hljs-buil…
57808 <span class="hljs-keyword">const</span> rs = fs.<span class="hljs-title function_">createReadStream…
57810 <span class="hljs-title function_">finished</span>(rs, <span class="hljs-function">(<span class="hl…
57811 <span class="hljs-keyword">if</span> (err) {
57812 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
57813 } <span class="hljs-keyword">else</span> {
57814 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57818 rs.<span class="hljs-title function_">resume</span>(); <span class="hljs-comment">// Drain the stre…
57822 <p>The <code>finished</code> API is promisify-able as well;</p>
57823 …anguage-js"><span class="hljs-keyword">const</span> finished = util.<span class="hljs-title functi…
57825 <span class="hljs-keyword">const</span> rs = fs.<span class="hljs-title function_">createReadStream…
57827 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
57828 <span class="hljs-keyword">await</span> <span class="hljs-title function_">finished</span>(rs);
57829 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57832 …s-title function_">run</span>().<span class="hljs-title function_">catch</span>(<span class="hljs-
57833 rs.<span class="hljs-title function_">resume</span>(); <span class="hljs-comment">// Drain the stre…
57840-js"><span class="hljs-keyword">const</span> cleanup = <span class="hljs-title function_">finished…
57841 <span class="hljs-title function_">cleanup</span>();
57842 <span class="hljs-comment">// ...</span>
57860-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type">&#x3C;It…
57861-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type">&#x3C;It…
57863-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type">&#x3C;It…
57866 …" class="type">&#x3C;Stream></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
57868 <li><code>source</code> <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class…
57869 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type">&#x…
57872 …" class="type">&#x3C;Stream></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
57874 <li><code>source</code> <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class…
57875 …github.io/ecma262/#sec-asynciterable-interface" class="type">&#x3C;AsyncIterable></a> | <a href="h…
57878 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
57880 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
57888 … class="language-js"><span class="hljs-keyword">const</span> { pipeline } = <span class="hljs-buil…
57889 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
57890 <span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in">require</span>(<span cla…
57892 <span class="hljs-comment">// Use the pipeline API to easily pipe a series of streams</span>
57893 <span class="hljs-comment">// together and get notified when the pipeline is fully done.</span>
57895 <span class="hljs-comment">// A pipeline to gzip a potentially huge tar file efficiently:</span>
57897 <span class="hljs-title function_">pipeline</span>(
57898 …fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'archive.t…
57899 zlib.<span class="hljs-title function_">createGzip</span>(),
57900 …fs.<span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'archive.…
57901 <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
57902 <span class="hljs-keyword">if</span> (err) {
57903 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
57904 } <span class="hljs-keyword">else</span> {
57905 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57909 <p>The <code>pipeline</code> API is promisify-able as well:</p>
57910 …anguage-js"><span class="hljs-keyword">const</span> pipeline = util.<span class="hljs-title functi…
57912 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
57913 <span class="hljs-keyword">await</span> <span class="hljs-title function_">pipeline</span>(
57914 …fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'archive.t…
57915 zlib.<span class="hljs-title function_">createGzip</span>(),
57916 …fs.<span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'archive.…
57918 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57921 …s-title function_">run</span>().<span class="hljs-title function_">catch</span>(<span class="hljs-
57923 …anguage-js"><span class="hljs-keyword">const</span> pipeline = util.<span class="hljs-title functi…
57924 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
57926 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
57927 <span class="hljs-keyword">await</span> <span class="hljs-title function_">pipeline</span>(
57928 …fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'lowercase…
57929 <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>* (source) {
57930 …source.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</spa…
57931 …n class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keywo…
57932 …<span class="hljs-keyword">yield</span> chunk.<span class="hljs-title function_">toUpperCase</span…
57935 …fs.<span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'uppercas…
57937 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57940 …s-title function_">run</span>().<span class="hljs-title function_">catch</span>(<span class="hljs-
57954 <li><code>iterable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
57957 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
57963-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
57965 …ss="hljs-keyword">async</span> <span class="hljs-keyword">function</span> * <span class="hljs-titl…
57966 <span class="hljs-keyword">yield</span> <span class="hljs-string">'hello'</span>;
57967 <span class="hljs-keyword">yield</span> <span class="hljs-string">'streams'</span>;
57970 …ljs-keyword">const</span> readable = <span class="hljs-title class_">Readable</span>.<span class="…
57972 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
57973 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57986 <!-- eslint-disable no-useless-constructor -->
57987-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
57989 …lass="hljs-keyword">class</span> <span class="hljs-title class_">MyWritable</span> <span class="hl…
57990 …<span class="hljs-title function_">constructor</span>(<span class="hljs-params">{ highWaterMark, .…
57991 <span class="hljs-variable language_">super</span>({ highWaterMark });
57992 <span class="hljs-comment">// ...</span>
58033 <table><thead><tr><th>Use-case</th><th>Class</th><th>Method(s) to implement</th></tr></thead><tbody…
58052-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58054 <span class="hljs-keyword">const</span> myWritable = <span class="hljs-keyword">new</span> <span cl…
58055 …<span class="hljs-title function_">write</span>(<span class="hljs-params">chunk, encoding, callbac…
58056 <span class="hljs-comment">// ...</span>
58079 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
58081 <li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
58084 <li><code>decodeStrings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
58090 <li><code>defaultEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
58093 <li><code>objectMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
58098 <li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
58100 <li><code>write</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
58102 <li><code>writev</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
58104 <li><code>destroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
58106 <li><code>final</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
58108 <li><code>autoDestroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
58113 <!-- eslint-disable no-useless-constructor -->
58114-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58116 …lass="hljs-keyword">class</span> <span class="hljs-title class_">MyWritable</span> <span class="hl…
58117 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">options</span>) {
58118 <span class="hljs-comment">// Calls the stream.Writable() constructor.</span>
58119 <span class="hljs-variable language_">super</span>(options);
58120 <span class="hljs-comment">// ...</span>
58123 <p>Or, when using pre-ES6 style constructors:</p>
58124-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58125 <span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span cla…
58127 <span class="hljs-keyword">function</span> <span class="hljs-title function_">MyWritable</span>(<sp…
58128 …ss="hljs-keyword">if</span> (!(<span class="hljs-variable language_">this</span> <span class="hljs
58129 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-t…
58130 …<span class="hljs-title class_">Writable</span>.<span class="hljs-title function_">call</span>(<sp…
58132 util.<span class="hljs-title function_">inherits</span>(<span class="hljs-title class_">MyWritable<…
58134-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58136 <span class="hljs-keyword">const</span> myWritable = <span class="hljs-keyword">new</span> <span cl…
58137 …<span class="hljs-title function_">write</span>(<span class="hljs-params">chunk, encoding, callbac…
58138 <span class="hljs-comment">// ...</span>
58140 …<span class="hljs-title function_">writev</span>(<span class="hljs-params">chunks, callback</span>…
58141 <span class="hljs-comment">// ...</span>
58155 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
58160 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58163 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58196-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object[]></a> The data…
58200 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
58203 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58207 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58225 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
58226 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58236 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58253-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58255 <span class="hljs-keyword">const</span> myWritable = <span class="hljs-keyword">new</span> <span cl…
58256 …<span class="hljs-title function_">write</span>(<span class="hljs-params">chunk, encoding, callbac…
58257-keyword">if</span> (chunk.<span class="hljs-title function_">toString</span>().<span class="hljs-
58258 …ass="hljs-title function_">callback</span>(<span class="hljs-keyword">new</span> <span class="hljs
58259 } <span class="hljs-keyword">else</span> {
58260 <span class="hljs-title function_">callback</span>();
58269-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58271 …lass="hljs-keyword">class</span> <span class="hljs-title class_">MyWritable</span> <span class="hl…
58272 …<span class="hljs-title function_">_write</span>(<span class="hljs-params">chunk, encoding, callba…
58273-keyword">if</span> (chunk.<span class="hljs-title function_">toString</span>().<span class="hljs-
58274 …ass="hljs-title function_">callback</span>(<span class="hljs-keyword">new</span> <span class="hljs
58275 } <span class="hljs-keyword">else</span> {
58276 <span class="hljs-title function_">callback</span>();
58282 input is a string. This is not a trivial process when using multi-byte
58283 characters encoding, such as UTF-8. The following example shows how to decode
58284 multi-byte strings using <code>StringDecoder</code> and <a href="#stream_class_stream_writable"><co…
58285-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58286 …"hljs-keyword">const</span> { <span class="hljs-title class_">StringDecoder</span> } = <span class…
58288 …ss="hljs-keyword">class</span> <span class="hljs-title class_">StringWritable</span> <span class="…
58289 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">options</span>) {
58290 <span class="hljs-variable language_">super</span>(options);
58291-variable language_">this</span>.<span class="hljs-property">_decoder</span> = <span class="hljs-k…
58292 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">data</span> = <span …
58294 …<span class="hljs-title function_">_write</span>(<span class="hljs-params">chunk, encoding, callba…
58295 <span class="hljs-keyword">if</span> (encoding === <span class="hljs-string">'buffer'</span>) {
58296 …chunk = <span class="hljs-variable language_">this</span>.<span class="hljs-property">_decoder</sp…
58298 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">data</span> += chunk;
58299 <span class="hljs-title function_">callback</span>();
58301 <span class="hljs-title function_">_final</span>(<span class="hljs-params">callback</span>) {
58302-variable language_">this</span>.<span class="hljs-property">data</span> += <span class="hljs-vari…
58303 <span class="hljs-title function_">callback</span>();
58307-keyword">const</span> euro = [[<span class="hljs-number">0xE2</span>, <span class="hljs-number">0…
58308 <span class="hljs-keyword">const</span> w = <span class="hljs-keyword">new</span> <span class="hljs
58310 w.<span class="hljs-title function_">write</span>(<span class="hljs-string">'currency: '</span>);
58311 w.<span class="hljs-title function_">write</span>(euro[<span class="hljs-number">0</span>]);
58312 w.<span class="hljs-title function_">end</span>(euro[<span class="hljs-number">1</span>]);
58314 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(w.<span clas…
58332 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
58334 <li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
58337 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58339 <li><code>objectMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
58342 <li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
58344 <li><code>read</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
58346 <li><code>destroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
58348 <li><code>autoDestroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
58353 <!-- eslint-disable no-useless-constructor -->
58354-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
58356 …lass="hljs-keyword">class</span> <span class="hljs-title class_">MyReadable</span> <span class="hl…
58357 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">options</span>) {
58358 <span class="hljs-comment">// Calls the stream.Readable(options) constructor.</span>
58359 <span class="hljs-variable language_">super</span>(options);
58360 <span class="hljs-comment">// ...</span>
58363 <p>Or, when using pre-ES6 style constructors:</p>
58364-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
58365 <span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span cla…
58367 <span class="hljs-keyword">function</span> <span class="hljs-title function_">MyReadable</span>(<sp…
58368 …ss="hljs-keyword">if</span> (!(<span class="hljs-variable language_">this</span> <span class="hljs
58369 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-t…
58370 …<span class="hljs-title class_">Readable</span>.<span class="hljs-title function_">call</span>(<sp…
58372 util.<span class="hljs-title function_">inherits</span>(<span class="hljs-title class_">MyReadable<…
58374-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
58376 <span class="hljs-keyword">const</span> myReadable = <span class="hljs-keyword">new</span> <span cl…
58377 <span class="hljs-title function_">read</span>(<span class="hljs-params">size</span>) {
58378 <span class="hljs-comment">// ...</span>
58386 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
58418 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
58419 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58435-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> | …
58439 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58441 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
58455 example, when wrapping a lower-level source that provides some form of
58456 pause/resume mechanism, and a data callback, the low-level source can be wrapped
58458 <pre><code class="language-js"><span class="hljs-comment">// `_source` is an object with readStop()…
58459 <span class="hljs-comment">// and an `ondata` member that gets called when it has data, and</span>
58460 <span class="hljs-comment">// an `onend` member that gets called when the data is over.</span>
58462 …ss="hljs-keyword">class</span> <span class="hljs-title class_">SourceWrapper</span> <span class="h…
58463 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">options</span>) {
58464 <span class="hljs-variable language_">super</span>(options);
58466 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">_source</span> = <sp…
58468 <span class="hljs-comment">// Every time there's data, push it into the internal buffer.</span>
58469-variable language_">this</span>.<span class="hljs-property">_source</span>.<span class="hljs-prop…
58470 <span class="hljs-comment">// If push() returns false, then stop reading from source.</span>
58471 …<span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span cla…
58472 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">_source</span>.<span…
58475 <span class="hljs-comment">// When the source ends, push the EOF-signaling `null` chunk.</span>
58476 …ss="hljs-variable language_">this</span>.<span class="hljs-property">_source</span>.<span class="h…
58477 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">push</span>(<…
58480 …<span class="hljs-comment">// _read() will be called when the stream wants to pull more data in.</…
58481 <span class="hljs-comment">// The advisory size argument is ignored in this case.</span>
58482 <span class="hljs-title function_">_read</span>(<span class="hljs-params">size</span>) {
58483 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">_source</span>.<span…
58496-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
58498 <span class="hljs-keyword">const</span> myReadable = <span class="hljs-keyword">new</span> <span cl…
58499 <span class="hljs-title function_">read</span>(<span class="hljs-params">size</span>) {
58500 …<span class="hljs-keyword">const</span> err = <span class="hljs-title function_">checkSomeErrorCon…
58501 <span class="hljs-keyword">if</span> (err) {
58502 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">destroy</span…
58503 } <span class="hljs-keyword">else</span> {
58504 <span class="hljs-comment">// Do some work.</span>
58512-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
58514 …class="hljs-keyword">class</span> <span class="hljs-title class_">Counter</span> <span class="hljs
58515 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">opt</span>) {
58516 <span class="hljs-variable language_">super</span>(opt);
58517 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">_max</span> = <span …
58518 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">_index</span> = <spa…
58521 <span class="hljs-title function_">_read</span>(<span class="hljs-params"></span>) {
58522 …<span class="hljs-keyword">const</span> i = <span class="hljs-variable language_">this</span>.<spa…
58523 …<span class="hljs-keyword">if</span> (i > <span class="hljs-variable language_">this</span>.<span …
58524 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">push</span>(<…
58525 <span class="hljs-keyword">else</span> {
58526 … <span class="hljs-keyword">const</span> str = <span class="hljs-title class_">String</span>(i);
58527 …hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-t…
58528 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">push</span>(b…
58540 both base classes due to overriding <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
58556 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
58559 <li><code>allowHalfOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
58562 <li><code>readable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58564 <li><code>writable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58566 <li><code>readableObjectMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
58568 <li><code>writableObjectMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
58570 <li><code>readableHighWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
58572 <li><code>writableHighWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
58577 <!-- eslint-disable no-useless-constructor -->
58578-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Duplex</span> } = <…
58580 …class="hljs-keyword">class</span> <span class="hljs-title class_">MyDuplex</span> <span class="hlj…
58581 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">options</span>) {
58582 <span class="hljs-variable language_">super</span>(options);
58583 <span class="hljs-comment">// ...</span>
58586 <p>Or, when using pre-ES6 style constructors:</p>
58587-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Duplex</span> } = <…
58588 <span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span cla…
58590 <span class="hljs-keyword">function</span> <span class="hljs-title function_">MyDuplex</span>(<span…
58591 …ss="hljs-keyword">if</span> (!(<span class="hljs-variable language_">this</span> <span class="hljs
58592 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-t…
58593 …<span class="hljs-title class_">Duplex</span>.<span class="hljs-title function_">call</span>(<span…
58595 util.<span class="hljs-title function_">inherits</span>(<span class="hljs-title class_">MyDuplex</s…
58597-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Duplex</span> } = <…
58599 <span class="hljs-keyword">const</span> myDuplex = <span class="hljs-keyword">new</span> <span clas…
58600 <span class="hljs-title function_">read</span>(<span class="hljs-params">size</span>) {
58601 <span class="hljs-comment">// ...</span>
58603 …<span class="hljs-title function_">write</span>(<span class="hljs-params">chunk, encoding, callbac…
58604 <span class="hljs-comment">// ...</span>
58609 hypothetical lower-level source object to which data can be written, and
58615-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Duplex</span> } = <…
58616 <span class="hljs-keyword">const</span> kSource = <span class="hljs-title class_">Symbol</span>(<sp…
58618 …class="hljs-keyword">class</span> <span class="hljs-title class_">MyDuplex</span> <span class="hlj…
58619 …<span class="hljs-title function_">constructor</span>(<span class="hljs-params">source, options</s…
58620 <span class="hljs-variable language_">super</span>(options);
58621 <span class="hljs-variable language_">this</span>[kSource] = source;
58624 …<span class="hljs-title function_">_write</span>(<span class="hljs-params">chunk, encoding, callba…
58625 <span class="hljs-comment">// The underlying source only deals with strings.</span>
58626 …<span class="hljs-keyword">if</span> (<span class="hljs-title class_">Buffer</span>.<span class="h…
58627 chunk = chunk.<span class="hljs-title function_">toString</span>();
58628 …<span class="hljs-variable language_">this</span>[kSource].<span class="hljs-title function_">writ…
58629 <span class="hljs-title function_">callback</span>();
58632 <span class="hljs-title function_">_read</span>(<span class="hljs-params">size</span>) {
58633 …s-variable language_">this</span>[kSource].<span class="hljs-title function_">fetchSomeData</span>…
58634 …hljs-variable language_">this</span>.<span class="hljs-title function_">push</span>(<span class="h…
58639 <code>Writable</code> sides operate independently of one another despite co-existing within
58649-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Transform</span> } …
58651 <span class="hljs-comment">// All Transform streams are also Duplex Streams.</span>
58652 <span class="hljs-keyword">const</span> myTransform = <span class="hljs-keyword">new</span> <span c…
58653 <span class="hljs-attr">writableObjectMode</span>: <span class="hljs-literal">true</span>,
58655 …<span class="hljs-title function_">transform</span>(<span class="hljs-params">chunk, encoding, cal…
58656 <span class="hljs-comment">// Coerce the chunk to a number if necessary.</span>
58657 chunk |= <span class="hljs-number">0</span>;
58659 <span class="hljs-comment">// Transform the chunk into something else.</span>
58660 …<span class="hljs-keyword">const</span> data = chunk.<span class="hljs-title function_">toString</…
58662 <span class="hljs-comment">// Push the data onto the readable queue.</span>
58663-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string…
58667 myTransform.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'ascii'…
58668-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(…
58670 myTransform.<span class="hljs-title function_">write</span>(<span class="hljs-number">1</span>);
58671 <span class="hljs-comment">// Prints: 01</span>
58672 myTransform.<span class="hljs-title function_">write</span>(<span class="hljs-number">10</span>);
58673 <span class="hljs-comment">// Prints: 0a</span>
58674 myTransform.<span class="hljs-title function_">write</span>(<span class="hljs-number">100</span>);
58675 <span class="hljs-comment">// Prints: 64</span></code></pre>
58696 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
58699 <li><code>transform</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
58701 <li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
58706 <!-- eslint-disable no-useless-constructor -->
58707-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Transform</span> } …
58709 …ass="hljs-keyword">class</span> <span class="hljs-title class_">MyTransform</span> <span class="hl…
58710 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">options</span>) {
58711 <span class="hljs-variable language_">super</span>(options);
58712 <span class="hljs-comment">// ...</span>
58715 <p>Or, when using pre-ES6 style constructors:</p>
58716-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Transform</span> } …
58717 <span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span cla…
58719 <span class="hljs-keyword">function</span> <span class="hljs-title function_">MyTransform</span>(<s…
58720 …ss="hljs-keyword">if</span> (!(<span class="hljs-variable language_">this</span> <span class="hljs
58721 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-t…
58722 …<span class="hljs-title class_">Transform</span>.<span class="hljs-title function_">call</span>(<s…
58724 util.<span class="hljs-title function_">inherits</span>(<span class="hljs-title class_">MyTransform…
58726-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Transform</span> } …
58728 <span class="hljs-keyword">const</span> myTransform = <span class="hljs-keyword">new</span> <span c…
58729 …<span class="hljs-title function_">transform</span>(<span class="hljs-params">chunk, encoding, cal…
58730 <span class="hljs-comment">// ...</span>
58745 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58768 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
58773 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58776 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58796-js">transform.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<spa…
58797 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">push</span>(d…
58798 <span class="hljs-title function_">callback</span>();
58801-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">_transfo…
58802 <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, data);
58819 generators are effectively a first-class language-level stream construct at
58824 …e><code class="language-js">(<span class="hljs-keyword">async</span> <span class="hljs-keyword">fu…
58825 …n class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keywo…
58826 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
58830 unhandled post-destroy errors.</p>
58834-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
58836 …ss="hljs-keyword">async</span> <span class="hljs-keyword">function</span> * <span class="hljs-titl…
58837 <span class="hljs-keyword">yield</span> <span class="hljs-string">'a'</span>;
58838 <span class="hljs-keyword">yield</span> <span class="hljs-string">'b'</span>;
58839 <span class="hljs-keyword">yield</span> <span class="hljs-string">'c'</span>;
58842 …ljs-keyword">const</span> readable = <span class="hljs-title class_">Readable</span>.<span class="…
58844 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
58845 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
58850 the handling of backpressure and backpressure-related errors:</p>
58851 … class="language-js"><span class="hljs-keyword">const</span> { pipeline } = <span class="hljs-buil…
58852 <span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span cla…
58853 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
58855 <span class="hljs-keyword">const</span> writable = fs.<span class="hljs-title function_">createWrit…
58857 <span class="hljs-comment">// Callback Pattern</span>
58858 <span class="hljs-title function_">pipeline</span>(iterator, writable, <span class="hljs-function">…
58859 <span class="hljs-keyword">if</span> (err) {
58860 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
58861 } <span class="hljs-keyword">else</span> {
58862 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
58866 <span class="hljs-comment">// Promise Pattern</span>
58867 <span class="hljs-keyword">const</span> pipelinePromise = util.<span class="hljs-title function_">p…
58868 <span class="hljs-title function_">pipelinePromise</span>(iterator, writable)
58869 ….<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-par…
58870 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
58872 ….<span class="hljs-title function_">catch</span>(<span class="hljs-variable language_">console</sp…
58902 <pre><code class="language-js"><span class="hljs-comment">// WARNING! BROKEN!</span>
58903 net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class…
58905 <span class="hljs-comment">// We add an 'end' listener, but never consume the data.</span>
58906 …socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span …
58907 <span class="hljs-comment">// It will never get here.</span>
58908 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'The message was re…
58911 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);</code><…
58916 <pre><code class="language-js"><span class="hljs-comment">// Workaround.</span>
58917 net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class…
58918 …socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span …
58919 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'The message was re…
58922 <span class="hljs-comment">// Start the flow of data, discarding it.</span>
58923 socket.<span class="hljs-title function_">resume</span>();
58924 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);</code><…
58926 pre-0.10 style streams can be wrapped in a <code>Readable</code> class using the
58935 a low-level <a href="#stream_readable_read_size_1"><code>stream._read()</code></a> call.</p>
58941 <p>Pushing a zero-byte string, <code>Buffer</code> or <code>Uint8Array</code> to a stream that is n…
58948 <code>highWaterMark</code> operates in non-object mode.</p>
58954 contain multi-byte characters.</p></section>
58958 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
58961 strings in a manner that preserves encoded multi-byte UTF-8 and UTF-16
58963-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">StringDecoder</span…
58965-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">StringDecoder</span…
58966 …"hljs-keyword">const</span> decoder = <span class="hljs-keyword">new</span> <span class="hljs-titl…
58968-keyword">const</span> cent = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
58969 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
58971-keyword">const</span> euro = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
58972 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
58977 <p>In the following example, the three UTF-8 encoded bytes of the European Euro
58979-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">StringDecoder</span…
58980 …"hljs-keyword">const</span> decoder = <span class="hljs-keyword">new</span> <span class="hljs-titl…
58982 …"hljs-title function_">write</span>(<span class="hljs-title class_">Buffer</span>.<span class="hlj…
58983 …"hljs-title function_">write</span>(<span class="hljs-title class_">Buffer</span>.<span class="hlj…
58984-variable language_">console</span>.<span class="hljs-title function_">log</span>(decoder.<span cl…
58991 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
59000 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
59002 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
59005 representing incomplete UTF-8 and UTF-16 characters will be replaced with
59023 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
59025 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
59034 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
59041 …ode.js <a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-
59055 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
59095 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
59141 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
59159 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59160 …ode.js <a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-
59161 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
59174 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
59175 <span class="hljs-keyword">const</span> setImmediatePromise = util.<span class="hljs-title function…
59177-title function_">setImmediatePromise</span>(<span class="hljs-string">'foobar'</span>).<span clas…
59178 <span class="hljs-comment">// value === 'foobar' (passing values is optional)</span>
59179 <span class="hljs-comment">// This is executed after all I/O callbacks.</span>
59182 <span class="hljs-comment">// Or with async function</span>
59183 …s="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title f…
59184 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
59185 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">setImmediatePromise</sp…
59186 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
59188 <span class="hljs-title function_">timerExample</span>();</code></pre>
59194 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59195 <li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
59197 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
59202 set to <code>1</code>. Non-integer delays are truncated to an integer.</p>
59209 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59210 <li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
59212 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
59215 <p>Schedules execution of a one-time <code>callback</code> after <code>delay</code> milliseconds.</…
59221 will be set to <code>1</code>. Non-integer delays are truncated to an integer.</p>
59225 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
59226 <span class="hljs-keyword">const</span> setTimeoutPromise = util.<span class="hljs-title function_"…
59228-number">40</span>, <span class="hljs-string">'foobar'</span>).<span class="hljs-title function_">…
59229 <span class="hljs-comment">// value === 'foobar' (passing values is optional)</span>
59230 <span class="hljs-comment">// This is executed after about 40 milliseconds.</span>
59240 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
59241 <span class="hljs-keyword">const</span> setImmediatePromise = util.<span class="hljs-title function…
59243 <span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hlj…
59244 <span class="hljs-keyword">const</span> signal = ac.<span class="hljs-property">signal</span>;
59246 <span class="hljs-title function_">setImmediatePromise</span>(<span class="hljs-string">'foobar'</s…
59247 ….<span class="hljs-title function_">then</span>(<span class="hljs-variable language_">console</spa…
59248 ….<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-pa…
59249 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">name</span> === <span class=…
59250 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
59253 ac.<span class="hljs-title function_">abort</span>();</code></pre>
59255 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
59256 <span class="hljs-keyword">const</span> setTimeoutPromise = util.<span class="hljs-title function_"…
59258 <span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hlj…
59259 <span class="hljs-keyword">const</span> signal = ac.<span class="hljs-property">signal</span>;
59261 setTimeoutPromise(<span class="hljs-number">1000</span>, <span class="hljs-string">'foobar'</span>,…
59262 ….<span class="hljs-title function_">then</span>(<span class="hljs-variable language_">console</spa…
59263 ….<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-pa…
59264 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">name</span> === <span class=…
59265 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
59268 ac.<span class="hljs-title function_">abort</span>();</code></pre>
59283 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
59292 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
59299 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
59304 …code class="language-js"><span class="hljs-keyword">const</span> tls = <span class="hljs-built_in"…
59309 use of the OpenSSL command-line interface to generate a 2048-bit RSA private
59311 <pre><code class="language-bash">openssl genrsa -out ryans-key.pem 2048</code></pre>
59315 private key (such certificates are referred to as "self-signed"). The first
59318 <p>The OpenSSL command-line interface can be used to generate a CSR for a private
59320 <pre><code class="language-bash">openssl req -new -sha256 -key ryans-key.pem -out ryans-csr.pem</co…
59322 Authority for signing or used to generate a self-signed certificate.</p>
59323 <p>Creating a self-signed certificate using the OpenSSL command-line interface
59325 …<code class="language-bash">openssl x509 -req -<span class="hljs-keyword">in</span> ryans-csr.pem
59328 …s="language-bash">openssl pkcs12 -<span class="hljs-built_in">export</span> -<span class="hljs-key…
59329 -certfile ca-cert.pem -out ryans.pfx</code></pre>
59335 a single file, e.g. <code>cat ca1-cert.pem ca2-cert.pem > ca-cert.pem</code></li>
59340 of key-agreement (i.e., key-exchange) methods. That is, the server and client
59344 by eavesdroppers if the attacker manages to obtain the key-pair specifically
59347 key-agreement on every TLS/SSL handshake (in contrast to using the same key for
59352 …80%93Hellman_key_exchange">DHE</a>: An ephemeral version of the Diffie-Hellman key-agreement proto…
59353 …_curve_Diffie%E2%80%93Hellman">ECDHE</a>: An ephemeral version of the Elliptic Curve Diffie-Hellman
59354 key-agreement protocol.</li>
59359 to generate Diffie-Hellman parameters and specify them with the <code>dhparam</code>
59361 the OpenSSL command-line interface to generate such parameters:</p>
59362 <pre><code class="language-bash">openssl dhparam -outform PEM -out dhparam.pem 2048</code></pre>
59363 <p>If using perfect forward secrecy using <code>ECDHE</code>, Diffie-Hellman parameters are
59371 <p>ALPN (Application-Layer Protocol Negotiation Extension) and
59378 <h4>Pre-shared keys<span><a class="mark" href="#tls_pre_shared_keys" id="tls_pre_shared_keys">#</a>…
59380 <p>TLS-PSK support is available as an alternative to normal certificate-based
59381 authentication. It uses a pre-shared key instead of certificates to
59383 TLS-PSK and public key infrastructure are not mutually exclusive. Clients and
59386 <p>TLS-PSK is only a good choice where means exist to securely share a
59389 The TLS-PSK implementation in OpenSSL has seen many security flaws in
59394 low-entropy sources is not secure.</p>
59395 <p>PSK ciphers are disabled by default, and using TLS-PSK thus requires explicitly
59397 ciphers can be retrieved via <code>openssl ciphers -v 'PSK'</code>. All TLS 1.3
59399 supported they can be retrieved via <code>openssl ciphers -v -s -tls1_3 -psk</code>.</p>
59405 <h4>Client-initiated renegotiation attack mitigation<span><a class="mark" href="#tls_client_initiat…
59409 of server-side resources, making it a potential vector for denial-of-service
59415 <li><code>tls.CLIENT_RENEG_LIMIT</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
59417 <li><code>tls.CLIENT_RENEG_WINDOW</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
59447 in the initial connection. This mechanism avoids the need for server-side
59456 ticket, otherwise it contains client-side session state.</p>
59462 all have the same ticket keys. There are three 16-byte keys internally, but the
59463 tls API exposes them as a single 48-byte buffer for convenience.</p>
59484 <code>-reconnect</code> option to <code>openssl s_client</code>, for example:</p>
59485 …code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">openssl…
59488 <pre><code class="language-text">New, TLSv1.2, Cipher is ECDHE-RSA-AES128-GCM-SHA256</code></pre>
59490 <pre><code class="language-text">Reused, TLSv1.2, Cipher is ECDHE-RSA-AES128-GCM-SHA256</code></pre>
59496 <pre><code class="language-console">node -p crypto.constants.defaultCoreCipherList | tr ':' '\n'
59500 ECDHE-RSA-AES128-GCM-SHA256
59501 ECDHE-ECDSA-AES128-GCM-SHA256
59502 ECDHE-RSA-AES256-GCM-SHA384
59503 ECDHE-ECDSA-AES256-GCM-SHA384
59504 DHE-RSA-AES128-GCM-SHA256
59505 ECDHE-RSA-AES128-SHA256
59506 DHE-RSA-AES128-SHA256
59507 ECDHE-RSA-AES256-SHA384
59508 DHE-RSA-AES256-SHA384
59509 ECDHE-RSA-AES256-SHA256
59510 DHE-RSA-AES256-SHA256
59521 … replaced entirely using the <a href="#cli_tls_cipher_list_list"><code>--tls-cipher-list</code></a>
59522 command-line switch (directly, or via the <a href="#cli_node_options_options"><code>NODE_OPTIONS</c…
59523 variable). For instance, the following makes <code>ECDHE-RSA-AES128-GCM-SHA256:!RC4</code>
59525 <pre><code class="language-bash">node --tls-cipher-list=<span class="hljs-string">'ECDHE-RSA-AES128
59527 <span class="hljs-built_in">export</span> NODE_OPTIONS=--tls-cipher-list=<span class="hljs-string">…
59536 the OpenSSL <a href="https://www.openssl.org/docs/man1.1.1/man1/ciphers.html#CIPHER-LIST-FORMAT">ci…
59548 of an application. The <code>--tls-cipher-list</code> switch and <code>ciphers</code> option should…
59550 …ciphers for <a href="https://www.chromium.org/Home/chromium-security/education/tls#TOC-Cipher-Suit…
59555 <p>Old clients that rely on insecure and deprecated RC4 or DES-based ciphers
59559 …L <a href="https://www.openssl.org/docs/man1.1.1/man1/ciphers.html#CIPHER-LIST-FORMAT">cipher list…
59568 <p>The first 3 are enabled by default. The last 2 <code>CCM</code>-based suites are supported
59575 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
59589 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
59637-js"><span class="hljs-keyword">const</span> logFile = fs.<span class="hljs-title function_">creat…
59638 <span class="hljs-comment">// ...</span>
59639 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'keylog'</span>, <span clas…
59640 …<span class="hljs-keyword">if</span> (tlsSocket.<span class="hljs-property">remoteAddress</span> !…
59641 …<span class="hljs-keyword">return</span>; <span class="hljs-comment">// Only log keys for a partic…
59642 logFile.<span class="hljs-title function_">write</span>(line);
59663 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59677 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59683 Both <code>certificate</code> and <code>issuer</code> are <code>Buffer</code> DER-representations o…
59702 <p>The <code>issuer</code> can be <code>null</code> if the certificate is either self-signed or the
59717 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59720 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
59735 <pre><code class="language-js"><span class="hljs-keyword">const</span> tlsSessionStore = {};
59736 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'newSession'</span>, <span cl…
59737 …tlsSessionStore[id.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'h…
59738 <span class="hljs-title function_">cb</span>();
59740 …an class="hljs-title function_">on</span>(<span class="hljs-string">'resumeSession'</span>, <span …
59741-title function_">cb</span>(<span class="hljs-literal">null</span>, tlsSessionStore[id.<span class…
59771 <li><code>exception</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
59780 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
59781 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
59792 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
59802 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59813 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
59815 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
59823 <li>Returns: <a href="#buffer_class_buffer" class="type">&#x3C;Buffer></a> A 48-byte buffer contain…
59835 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
59846-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
59885 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
59900 <li><code>requestOCSP</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
59926-js"><span class="hljs-keyword">const</span> logFile = fs.<span class="hljs-title function_">creat…
59927 <span class="hljs-comment">// ...</span>
59928-title function_">on</span>(<span class="hljs-string">'keylog'</span>, <span class="hljs-function"…
59974 the handshake is complete. For TLSv1.3, only ticket-based resumption is allowed
59981-js">tlsSocket.<span class="hljs-title function_">once</span>(<span class="hljs-string">'session'<…
59982 <span class="hljs-comment">// The session can be used immediately or later.</span>
59983 tls.<span class="hljs-title function_">connect</span>({
59984 <span class="hljs-attr">session</span>: session,
59985 <span class="hljs-comment">// Other connect options...</span>
59993 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60009 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
60025 …ut is identical to the output of <code>openssl s_client -trace</code> or <code>openssl s_server -t…
60039 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60062 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60064 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60065 <li><code>standardName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
60066 <li><code>version</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
60073 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
60074 …n class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
60075-attr">"standardName"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"TLS…
60076 …<span class="hljs-attr">"version"</span><span class="hljs-punctuation">:</span> <span class="hljs-
60077 <span class="hljs-punctuation">}</span></code></pre>
60086 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60100 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60109 to implement the <code>tls-unique</code> channel binding from <a href="https://tools.ietf.org/html/…
60115 <li><code>detailed</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
60117 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60139 <li><code>subject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60144 <li><code>issuer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
60146 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#…
60147 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#…
60148 <li><code>serialNumber</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
60150 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#…
60152 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#…
60155 <li><code>ext_key_usage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
60156 <li><code>subjectaltname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
60158 <li><code>infoAccess</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere…
60160 <li><code>issuerCertificate</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
60161 self-signed certificates, this may be a circular reference.</li>
60167 <li><code>bits</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60168 <li><code>exponent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
60170 <li><code>modulus</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
60177 <li><code>bits</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60178 <li><code>asn1Curve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
60179 curve. Well-known curves are identified by an OID. While it is unusual, it is
60182 <li><code>nistCurve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
60183 has one (not all well-known curves have been assigned names by NIST). Example:
60184 <code>'P-256'</code>.</li>
60187 <!-- eslint-skip -->
60188 <pre><code class="language-js">{ <span class="hljs-attr">subject</span>:
60189 …{ <span class="hljs-attr">OU</span>: [ <span class="hljs-string">'Domain Control Validated'</span>…
60190 <span class="hljs-attr">CN</span>: <span class="hljs-string">'*.nodejs.org'</span> },
60191 <span class="hljs-attr">issuer</span>:
60192 { <span class="hljs-attr">C</span>: <span class="hljs-string">'GB'</span>,
60193 <span class="hljs-attr">ST</span>: <span class="hljs-string">'Greater Manchester'</span>,
60194 <span class="hljs-attr">L</span>: <span class="hljs-string">'Salford'</span>,
60195 <span class="hljs-attr">O</span>: <span class="hljs-string">'COMODO CA Limited'</span>,
60196 …<span class="hljs-attr">CN</span>: <span class="hljs-string">'COMODO RSA Domain Validation Secure …
60197 …<span class="hljs-attr">subjectaltname</span>: <span class="hljs-string">'DNS:*.nodejs.org, DNS:no…
60198 <span class="hljs-attr">infoAccess</span>:
60199 { <span class="hljs-string">'CA Issuers - URI'</span>:
60200 …[ <span class="hljs-string">'http://crt.comodoca.com/COMODORSADomainValidationSecureServerCA.crt'<…
60201 …<span class="hljs-string">'OCSP - URI'</span>: [ <span class="hljs-string">'http://ocsp.comodoca.c…
60202 …<span class="hljs-attr">modulus</span>: <span class="hljs-string">'B56CE45CB740B09A13F64AC543B712F…
60203 <span class="hljs-attr">exponent</span>: <span class="hljs-string">'0x10001'</span>,
60204 <span class="hljs-attr">pubkey</span>: &#x3C;Buffer ... >,
60217 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60226 to implement the <code>tls-unique</code> channel binding from <a href="https://tools.ietf.org/html/…
60232 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
60266 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60278 <p><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
60281 <p><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60283 <a href="https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#exporter-labels">IANA…
60295 <pre><code class="language-js"><span class="hljs-keyword">const</span> keyingMaterial = tlsSocket.<…
60296 <span class="hljs-number">128</span>,
60297 <span class="hljs-string">'client finished'</span>);
60299 <span class="hljs-comment">/**
60323 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
60331 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
60339 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
60347 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
60356 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
60364 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
60373 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
60375 <li><code>rejectUnauthorized</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60383 <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60390 <p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolea…
60407 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60409 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
60425 …ier</code> subject alternative names has been disabled in response to CVE-2021-44531.</p></td></tr>
60432 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
60434 <li><code>cert</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
60435 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> |…
60438 <p>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Object…
60439 failure. On success, returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
60448 was present (see <a href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-44531">CVE-2021-4…
60484 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60487 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60489 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60490 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60501 <li><code>allowHalfOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
60505 <li><code>rejectUnauthorized</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60509 <li><code>pskCallback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer…
60511 <li>hint: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
60514 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60518 <code>identity</code> must use UTF-8 encoding.</li>
60520 When negotiating TLS-PSK (pre-shared keys), this function is called
60528-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="#…
60536 <li><code>servername</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
60538 name, and not an IP address. It can be used by a multi-homed server to
60541 <li><code>checkServerIdentity(servername, cert)</code> <a href="https://developer.mozilla.org/en-US…
60544 …certificate. This should return an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60548 <li><code>minDHSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
60552 <li><code>highWaterMark</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
60558 <li><code>onread</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
60567 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
60580 <pre><code class="language-js"><span class="hljs-comment">// Assumes an echo server that is listeni…
60581 <span class="hljs-keyword">const</span> tls = <span class="hljs-built_in">require</span>(<span clas…
60582 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
60584 <span class="hljs-keyword">const</span> options = {
60585 …<span class="hljs-comment">// Necessary only if the server requires client certificate authenticat…
60586 …<span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<spa…
60587 …<span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<sp…
60589 <span class="hljs-comment">// Necessary only if the server uses a self-signed certificate.</span>
60590 …<span class="hljs-attr">ca</span>: [ fs.<span class="hljs-title function_">readFileSync</span>(<sp…
60592 <span class="hljs-comment">// Necessary only if the server's cert isn't for "localhost".</span>
60593 …ss="hljs-attr">checkServerIdentity</span>: <span class="hljs-function">() =></span> { <span class=…
60596-keyword">const</span> socket = tls.<span class="hljs-title function_">connect</span>(<span class=…
60597 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
60598 …socket.<span class="hljs-property">authorized</span> ? <span class="hljs-string">'authorized'</spa…
60599 …process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">pipe</span>(so…
60600 …process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">resume</span>(…
60602 socket.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span…
60603 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
60604 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
60606 socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span c…
60607 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
60614 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60615 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60616 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
60627 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60628 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60629 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60630 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
60668 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60670 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
60671 certificates. Default is to trust the well-known CAs curated by Mozilla.
60677 certificates that are not chainable to a well-known CA, the certificate's CA
60683 For self-signed certificates, the certificate is its own CA, and must be
60688 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
60692 including the root CA (the root CA must be pre-known to the peer, see <code>ca</code>).
60697 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&…
60699 algorithms (<code>RSA-PSS</code>, <code>ECDSA</code> etc.), combination of both (e.g
60703 <li><code>ciphers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
60707 <li><code>clientCertEngine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
60709 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
60711-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
60717 <li><code>ecdhCurve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
60718 list of curve NIDs or names, for example <code>P-521:P-384:P-256</code>, to use for
60721 available curve names. On recent releases, <code>openssl ecparam -list_curves</code>
60724 <li><code>honorCipherOrder</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
60728-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
60737 <li><code>privateKeyEngine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
60739 <li><code>privateKeyIdentifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
60743 <li><code>maxVersion</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
60747 <li><code>minVersion</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
60753 <li><code>passphrase</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
60755-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
60764 <li><code>secureOptions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
60768 <li><code>secureProtocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
60772 <a href="https://www.openssl.org/docs/man1.1.1/man7/ssl.html#Dealing-with-Protocol-Methods">SSL_MET…
60777 <li><code>sessionIdContext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
60779 …#buffer_class_buffer" class="type">&#x3C;Buffer></a> 48-bytes of cryptographically strong pseudo-r…
60781 <li><code>sessionTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
60798 <a href="https://hg.mozilla.org/mozilla-central/raw-file/tip/security/nss/lib/ckfw/builtins/certdat…
60813 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
60815 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60817 <li><code>isServer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
60819 <li><code>requestCert</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
60821 <li><code>rejectUnauthorized</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60827 <li><code>isServer</code>: If <code>true</code> the TLS socket will be instantiated in server-mode.
60835 <li><code>requestOCSP</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
60851 <pre><code class="language-js">pair = tls.<span class="hljs-title function_">createSecurePair</span…
60852 pair.<span class="hljs-property">encrypted</span>.<span class="hljs-title function_">pipe</span>(so…
60853 socket.<span class="hljs-title function_">pipe</span>(pair.<span class="hljs-property">encrypted</s…
60855 <pre><code class="language-js">secureSocket = tls.<span class="hljs-title function_">TLSSocket</spa…
60876 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60878-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="#…
60885 <li><code>clientCertEngine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
60887 <li><code>enableTrace</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
60891 <li><code>handshakeTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
60895 <li><code>rejectUnauthorized</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60898 <li><code>requestCert</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
60901 <li><code>sessionTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
60904 <li><code>SNICallback(servername, callback)</code> <a href="https://developer.mozilla.org/en-US/doc…
60907 error-first callback that takes two optional arguments: <code>error</code> and <code>ctx</code>.
60912 default callback with high-level API will be used (see below).</li>
60913 …#buffer_class_buffer" class="type">&#x3C;Buffer></a> 48-bytes of cryptographically strong pseudo-r…
60915 <li><code>pskCallback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer…
60919 <li>identity: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Stri…
60920-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
60924 When negotiating TLS-PSK (pre-shared keys), this function is called
60931 PSK ciphers are disabled by default, and using TLS-PSK thus
60934 <li><code>pskIdentityHint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
60935 with selecting the identity during TLS-PSK negotiation. Will be ignored
60944 <li><code>secureConnectionListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
60952 …code class="language-js"><span class="hljs-keyword">const</span> tls = <span class="hljs-built_in"…
60953 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
60955 <span class="hljs-keyword">const</span> options = {
60956 …<span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<spa…
60957 …<span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<sp…
60959 …<span class="hljs-comment">// This is necessary only if using client certificate authentication.</…
60960 <span class="hljs-attr">requestCert</span>: <span class="hljs-literal">true</span>,
60962 …<span class="hljs-comment">// This is necessary only if the client uses a self-signed certificate.…
60963 …<span class="hljs-attr">ca</span>: [ fs.<span class="hljs-title function_">readFileSync</span>(<sp…
60966 …ljs-keyword">const</span> server = tls.<span class="hljs-title function_">createServer</span>(opti…
60967 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
60968 …socket.<span class="hljs-property">authorized</span> ? <span class="hljs-string">'authorized'</spa…
60969 …socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'welcome!\n'</spa…
60970 …socket.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</spa…
60971 socket.<span class="hljs-title function_">pipe</span>(socket);
60973 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>, <spa…
60974 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
60983 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
60986 lower-case for historical reasons, but must be uppercased to be used in
60990-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
60996 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
61022 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
61026 <code>--tls-max-v1.2</code> sets the default to <code>'TLSv1.2'</code>. Using <code>--tls-max-v1.3<…
61035 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
61039 <code>--tls-min-v1.0</code> sets the default to <code>'TLSv1'</code>. Using <code>--tls-min-v1.1</c…
61040 the default to <code>'TLSv1.1'</code>. Using <code>--tls-min-v1.3</code> sets the default to
61047 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
61051 <p>Tracing can be enabled with the <code>--trace-event-categories</code> command-line flag
61052 or by using the <code>trace_events</code> module. The <code>--trace-event-categories</code> flag
61053 accepts a list of comma-separated category names.</p>
61075 of unhandled Promise rejections and handled-after-rejections.</li>
61081 <pre><code class="language-bash">node --trace-event-categories v8,node,node.async_hooks server.js</…
61082 <p>Prior versions of Node.js required the use of the <code>--trace-events-enabled</code>
61084 <code>--trace-events-enabled</code> flag <em>may</em> still be used and will enable the
61086 <pre><code class="language-bash">node --trace-events-enabled
61088 <span class="hljs-comment"># is equivalent to</span>
61090 node --trace-event-categories v8,node,node.async_hooks</code></pre>
61092 … class="language-js"><span class="hljs-keyword">const</span> trace_events = <span class="hljs-buil…
61093-keyword">const</span> tracing = trace_events.<span class="hljs-title function_">createTracing</sp…
61094 tracing.<span class="hljs-title function_">enable</span>(); <span class="hljs-comment">// Enable t…
61096 <span class="hljs-comment">// do work</span>
61098 tracing.<span class="hljs-title function_">disable</span>(); <span class="hljs-comment">// Disable…
61100 in the <a href="https://www.chromium.org/developers/how-tos/trace-event-profiling-tool"><code>chrom…
61103 <code>${rotation}</code> is an incrementing log-rotation id. The filepath pattern can
61104 be specified with <code>--trace-event-file-pattern</code> that accepts a template
61106 <pre><code class="language-bash">node --trace-event-categories v8 --trace-event-file-pattern <span …
61109 However the trace-event timestamps are expressed in microseconds,
61132 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
61134 <p>A comma-separated list of the trace event categories covered by this
61142 and <em>not</em> specified by the <code>--trace-event-categories</code> flag will be disabled.</p>
61143 … class="language-js"><span class="hljs-keyword">const</span> trace_events = <span class="hljs-buil…
61144-keyword">const</span> t1 = trace_events.<span class="hljs-title function_">createTracing</span>({…
61145-keyword">const</span> t2 = trace_events.<span class="hljs-title function_">createTracing</span>({…
61146 t1.<span class="hljs-title function_">enable</span>();
61147 t2.<span class="hljs-title function_">enable</span>();
61149 <span class="hljs-comment">// Prints 'node,node.perf,v8'</span>
61150 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
61152 t2.<span class="hljs-title function_">disable</span>(); <span class="hljs-comment">// Will only dis…
61154 <span class="hljs-comment">// Prints 'node,v8'</span>
61155 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
61167 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
61174 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
61176 <li><code>categories</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
61184 … class="language-js"><span class="hljs-keyword">const</span> trace_events = <span class="hljs-buil…
61185 <span class="hljs-keyword">const</span> categories = [<span class="hljs-string">'node.perf'</span>,…
61186 <span class="hljs-keyword">const</span> tracing = trace_events.<span class="hljs-title function_">c…
61187 tracing.<span class="hljs-title function_">enable</span>();
61188 <span class="hljs-comment">// do stuff</span>
61189 tracing.<span class="hljs-title function_">disable</span>();</code></pre>
61195 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
61197 <p>Returns a comma-separated list of all currently-enabled trace event
61199 by the <em>union</em> of all currently-enabled <code>Tracing</code> objects and any categories
61200 enabled using the <code>--trace-event-categories</code> flag.</p>
61202 <code>node --trace-event-categories node.perf test.js</code> will print
61204 … class="language-js"><span class="hljs-keyword">const</span> trace_events = <span class="hljs-buil…
61205-keyword">const</span> t1 = trace_events.<span class="hljs-title function_">createTracing</span>({…
61206-keyword">const</span> t2 = trace_events.<span class="hljs-title function_">createTracing</span>({…
61207-keyword">const</span> t3 = trace_events.<span class="hljs-title function_">createTracing</span>({…
61209 t1.<span class="hljs-title function_">enable</span>();
61210 t2.<span class="hljs-title function_">enable</span>();
61212 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
61216 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
61221 …code class="language-js"><span class="hljs-keyword">const</span> tty = <span class="hljs-built_in"…
61228 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
61230 <span class="hljs-meta">$ </span><span class="language-bash">node -p -e <span class="hljs-string">"…
61261 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61265 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
61268 <p>When in raw mode, input is always available character-by-character, not
61290-js">process.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">on</span…
61291 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
61292-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
61307 <li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
61309 <li><code>-1</code>: to the left from cursor</li>
61314 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61315 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
61334 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61335 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
61360 <li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
61361 <li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
61362 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61363 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
61374 <li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
61377 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
61407 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
61418 <li><code>count</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
61420 <li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
61423 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
61429 <pre><code class="language-js">process.<span class="hljs-property">stdout</span>.<span class="hljs-
61430 <span class="hljs-comment">// Returns true or false depending on if `stdout` supports at least 16 c…
61431 process.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">hasColors</spa…
61432 <span class="hljs-comment">// Returns true or false depending on if `stdout` supports at least 256 …
61433 …ass="hljs-property">stdout</span>.<span class="hljs-title function_">hasColors</span>({ <span clas…
61434 <span class="hljs-comment">// Returns true.</span>
61435-property">stdout</span>.<span class="hljs-title function_">hasColors</span>(<span class="hljs-num…
61436 <span class="hljs-comment">// Returns false (the environment setting pretends to support 2 ** 8 col…
61455 <li><code>dx</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
61456 <li><code>dy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
61457 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61458 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
61475 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
61476 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
61479 a TTY and <code>false</code> if it is not, including whenever <code>fd</code> is not a non-negative
61484 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
61488 …ode class="language-js"><span class="hljs-keyword">const</span> dgram = <span class="hljs-built_in…
61489 <span class="hljs-keyword">const</span> server = dgram.<span class="hljs-title function_">createSoc…
61491 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
61492 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
61493 server.<span class="hljs-title function_">close</span>();
61496 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
61497-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
61500 server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</span>, <…
61501 …<span class="hljs-keyword">const</span> address = server.<span class="hljs-title function_">addres…
61502-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
61505 server.<span class="hljs-title function_">bind</span>(<span class="hljs-number">41234</span>);
61506 <span class="hljs-comment">// Prints: server listening 0.0.0.0:41234</span></code></pre>
61534 <li><code>exception</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
61555 <li><code>rinfo</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
61557 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
61558 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
61559 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61560 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61564 <p>If the source address of the incoming packet is an IPv6 link-local
61574 <li><code>multicastAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
61575 <li><code>multicastInterface</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
61587 …de class="language-js"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_i…
61588 <span class="hljs-keyword">const</span> dgram = <span class="hljs-built_in">require</span>(<span cl…
61589 <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isMaster</span>) {
61590 …cluster.<span class="hljs-title function_">fork</span>(); <span class="hljs-comment">// Works ok.<…
61591 …cluster.<span class="hljs-title function_">fork</span>(); <span class="hljs-comment">// Fails with…
61592 } <span class="hljs-keyword">else</span> {
61593 …<span class="hljs-keyword">const</span> s = dgram.<span class="hljs-title function_">createSocket<…
61594 …s.<span class="hljs-title function_">bind</span>(<span class="hljs-number">1234</span>, <span clas…
61595 …s.<span class="hljs-title function_">addMembership</span>(<span class="hljs-string">'224.0.0.114'<…
61603 <li><code>sourceAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
61604 <li><code>groupAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
61605 <li><code>multicastInterface</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
61607 <p>Tells the kernel to join a source-specific multicast channel at the given
61620 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
61639 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61640 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
61641 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61658 …ode class="language-js"><span class="hljs-keyword">const</span> dgram = <span class="hljs-built_in…
61659 <span class="hljs-keyword">const</span> server = dgram.<span class="hljs-title function_">createSoc…
61661 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
61662 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
61663 server.<span class="hljs-title function_">close</span>();
61666 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
61667-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
61670 server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</span>, <…
61671 …<span class="hljs-keyword">const</span> address = server.<span class="hljs-title function_">addres…
61672-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
61675 server.<span class="hljs-title function_">bind</span>(<span class="hljs-number">41234</span>);
61676 <span class="hljs-comment">// Prints: server listening 0.0.0.0:41234</span></code></pre>
61682 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
61684 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61685 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
61686 <li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
61687 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
61690 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61718 <pre><code class="language-js">socket.<span class="hljs-title function_">bind</span>({
61719 <span class="hljs-attr">address</span>: <span class="hljs-string">'localhost'</span>,
61720 <span class="hljs-attr">port</span>: <span class="hljs-number">8000</span>,
61721 <span class="hljs-attr">exclusive</span>: <span class="hljs-literal">true</span>
61728 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61737 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61738 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
61739 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61763 <li><code>multicastAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
61764 <li><code>multicastInterface</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
61777 <li><code>sourceAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
61778 <li><code>groupAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
61779 <li><code>multicastInterface</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
61781 <p>Instructs the kernel to leave a source-specific multicast channel at the given
61793 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
61801 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
61824 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
61852-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
61853 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
61854 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
61855 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61856 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
61857 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61870 contain multi-byte characters, <code>offset</code> and <code>length</code> will be calculated with
61893 …ode class="language-js"><span class="hljs-keyword">const</span> dgram = <span class="hljs-built_in…
61894 …hljs-keyword">const</span> message = <span class="hljs-title class_">Buffer</span>.<span class="hl…
61895 <span class="hljs-keyword">const</span> client = dgram.<span class="hljs-title function_">createSoc…
61896-title function_">send</span>(message, <span class="hljs-number">41234</span>, <span class="hljs-s…
61897 client.<span class="hljs-title function_">close</span>();
61901 …ode class="language-js"><span class="hljs-keyword">const</span> dgram = <span class="hljs-built_in…
61902 …="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
61903 …="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
61904 <span class="hljs-keyword">const</span> client = dgram.<span class="hljs-title function_">createSoc…
61905 …class="hljs-title function_">send</span>([buf1, buf2], <span class="hljs-number">41234</span>, <sp…
61906 client.<span class="hljs-title function_">close</span>();
61910 determine the optimal strategy on a case-by-case basis. Generally speaking,
61914 …ode class="language-js"><span class="hljs-keyword">const</span> dgram = <span class="hljs-built_in…
61915 …hljs-keyword">const</span> message = <span class="hljs-title class_">Buffer</span>.<span class="hl…
61916 <span class="hljs-keyword">const</span> client = dgram.<span class="hljs-title function_">createSoc…
61917-title function_">connect</span>(<span class="hljs-number">41234</span>, <span class="hljs-string"…
61918 …client.<span class="hljs-title function_">send</span>(message, <span class="hljs-function">(<span …
61919 client.<span class="hljs-title function_">close</span>();
61937 as the <code>MTU</code> for dial-up type applications), whether they arrive whole or in
61954 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61964 <li><code>multicastInterface</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
61984 …="language-js"><span class="hljs-keyword">const</span> socket = dgram.<span class="hljs-title func…
61986 socket.<span class="hljs-title function_">bind</span>(<span class="hljs-number">1234</span>, <span …
61987 …socket.<span class="hljs-title function_">setMulticastInterface</span>(<span class="hljs-string">'…
61990 …="language-js"><span class="hljs-keyword">const</span> socket = dgram.<span class="hljs-title func…
61992 socket.<span class="hljs-title function_">bind</span>(<span class="hljs-number">1234</span>, <span …
61993 …socket.<span class="hljs-title function_">setMulticastInterface</span>(<span class="hljs-string">'…
61997 …="language-js"><span class="hljs-keyword">const</span> socket = dgram.<span class="hljs-title func…
61999 socket.<span class="hljs-title function_">bind</span>(<span class="hljs-number">1234</span>, <span …
62000 …socket.<span class="hljs-title function_">setMulticastInterface</span>(<span class="hljs-string">'…
62020 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62030 <li><code>ttl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
62044 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62054 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62064 <li><code>ttl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
62109 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
62111 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62113 <li><code>reuseAddr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
62116 <li><code>ipv6Only</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
62117 disable dual-stack support, i.e., binding to address <code>::</code> won't make
62119 <li><code>recvBufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
62120 <li><code>sendBufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
62121 <li><code>lookup</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
62125 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
62137 …ode class="language-js"><span class="hljs-keyword">const</span> controller = <span class="hljs-key…
62138 <span class="hljs-keyword">const</span> { signal } = controller;
62139 …s-keyword">const</span> server = dgram.<span class="hljs-title function_">createSocket</span>({ <s…
62140 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
62141-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
62143 <span class="hljs-comment">// Later, when you want to close the server.</span>
62144 controller.<span class="hljs-title function_">abort</span>();</code></pre>
62150 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62151 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
62164 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
62168 …code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in"…
62182 <pre><code class="language-text">┌─────────────────────────────────────────────────────────────────…
62201 <pre><code class="language-js"><span class="hljs-keyword">const</span> myURL =
62202 …<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="h…
62204 …code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in"…
62205 <span class="hljs-keyword">const</span> myURL =
62206 …url.<span class="hljs-title function_">parse</span>(<span class="hljs-string">'https://user:pass@s…
62210-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62211 myURL.<span class="hljs-property">pathname</span> = <span class="hljs-string">'/a/b/c'</span>;
62212 myURL.<span class="hljs-property">search</span> = <span class="hljs-string">'?d=e'</span>;
62213 myURL.<span class="hljs-property">hash</span> = <span class="hljs-string">'#fgh'</span>;</code></pr…
62214 <pre><code class="language-js"><span class="hljs-keyword">const</span> pathname = <span class="hljs
62215 <span class="hljs-keyword">const</span> search = <span class="hljs-string">'?d=e'</span>;
62216 <span class="hljs-keyword">const</span> hash = <span class="hljs-string">'#fgh'</span>;
62217-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title funct…
62219 …ss="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title func…
62233 <p>Browser-compatible <code>URL</code> class, implemented by following the WHATWG URL
62234 Standard. <a href="https://url.spec.whatwg.org/#example-url-parsing">Examples of parsed URLs</a> ma…
62243 <li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
62246 <li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62251-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62252 <span class="hljs-comment">// https://example.org/foo</span></code></pre>
62254 It can also be imported from the built-in url module:</p>
62255-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
62259-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62260 <span class="hljs-comment">// https://example.org/</span></code></pre>
62262 automatically converted to ASCII using the <a href="https://tools.ietf.org/html/rfc5891#section-4.4…
62263-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62264 <span class="hljs-comment">// https://xn--g6w251d/</span></code></pre>
62270-js"><span class="hljs-keyword">let</span> myURL = <span class="hljs-keyword">new</span> <span cla…
62271 <span class="hljs-comment">// http://example.com/</span>
62273 …"hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">…
62274 <span class="hljs-comment">// https://example.com/</span>
62276 …="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string"…
62277 <span class="hljs-comment">// foo://Example.com/</span>
62279 …s="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string…
62280 <span class="hljs-comment">// http://example.com/</span>
62282 …="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string"…
62283 <span class="hljs-comment">// https://example.org/Example.com/</span>
62285 …s="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string…
62286 <span class="hljs-comment">// foo:Example.com/</span></code></pre>
62289 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62292-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62293 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62294 <span class="hljs-comment">// Prints #bar</span>
62296 myURL.<span class="hljs-property">hash</span> = <span class="hljs-string">'baz'</span>;
62297 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62298 <span class="hljs-comment">// Prints https://example.org/foo#baz</span></code></pre>
62300 are <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which characters to
62301 percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystring_s…
62305 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62308-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62309 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62310 <span class="hljs-comment">// Prints example.org:81</span>
62312 myURL.<span class="hljs-property">host</span> = <span class="hljs-string">'example.com:82'</span>;
62313 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62314 <span class="hljs-comment">// Prints https://example.com:82/foo</span></code></pre>
62318 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62323-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62324 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62325 <span class="hljs-comment">// Prints example.org</span>
62327 <span class="hljs-comment">// Setting the hostname does not change the port</span>
62328 myURL.<span class="hljs-property">hostname</span> = <span class="hljs-string">'example.com:82'</spa…
62329 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62330 <span class="hljs-comment">// Prints https://example.com:81/foo</span>
62332 <span class="hljs-comment">// Use myURL.host to change the hostname and port</span>
62333 myURL.<span class="hljs-property">host</span> = <span class="hljs-string">'example.org:82'</span>;
62334 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62335 <span class="hljs-comment">// Prints https://example.org:82/foo</span></code></pre>
62339 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62342-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62343 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62344 <span class="hljs-comment">// Prints https://example.org/foo</span>
62346 myURL.<span class="hljs-property">href</span> = <span class="hljs-string">'https://example.com/bar'…
62347 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62348 <span class="hljs-comment">// Prints https://example.com/bar</span></code></pre>
62358 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62360 <p>Gets the read-only serialization of the URL's origin.</p>
62361-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62362 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62363 <span class="hljs-comment">// Prints https://example.org</span></code></pre>
62364-js"><span class="hljs-keyword">const</span> idnURL = <span class="hljs-keyword">new</span> <span …
62365 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62366 <span class="hljs-comment">// Prints https://xn--g6w251d</span>
62368 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62369 <span class="hljs-comment">// Prints xn--g6w251d</span></code></pre>
62372 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62375-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62376 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62377 <span class="hljs-comment">// Prints xyz</span>
62379 myURL.<span class="hljs-property">password</span> = <span class="hljs-string">'123'</span>;
62380 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62381 <span class="hljs-comment">// Prints https://abc:123@example.com</span></code></pre>
62383 are <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which characters to
62384 percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystring_s…
62388 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62391-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62392 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62393 <span class="hljs-comment">// Prints /abc/xyz</span>
62395 myURL.<span class="hljs-property">pathname</span> = <span class="hljs-string">'/abcdef'</span>;
62396 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62397 <span class="hljs-comment">// Prints https://example.org/abcdef?123</span></code></pre>
62399 property are <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which charact…
62400 to percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystrin…
62404 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62456-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62457 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62458 <span class="hljs-comment">// Prints 8888</span>
62460 <span class="hljs-comment">// Default ports are automatically transformed to the empty string</span>
62461 <span class="hljs-comment">// (HTTPS protocol's default port is 443)</span>
62462 myURL.<span class="hljs-property">port</span> = <span class="hljs-string">'443'</span>;
62463 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62464 <span class="hljs-comment">// Prints the empty string</span>
62465 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62466 <span class="hljs-comment">// Prints https://example.org/</span>
62468 myURL.<span class="hljs-property">port</span> = <span class="hljs-number">1234</span>;
62469 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62470 <span class="hljs-comment">// Prints 1234</span>
62471 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62472 <span class="hljs-comment">// Prints https://example.org:1234/</span>
62474 <span class="hljs-comment">// Completely invalid port strings are ignored</span>
62475 myURL.<span class="hljs-property">port</span> = <span class="hljs-string">'abcd'</span>;
62476 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62477 <span class="hljs-comment">// Prints 1234</span>
62479 <span class="hljs-comment">// Leading numbers are treated as a port number</span>
62480 myURL.<span class="hljs-property">port</span> = <span class="hljs-string">'5678abcd'</span>;
62481 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62482 <span class="hljs-comment">// Prints 5678</span>
62484 <span class="hljs-comment">// Non-integers are truncated</span>
62485 myURL.<span class="hljs-property">port</span> = <span class="hljs-number">1234.5678</span>;
62486 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62487 <span class="hljs-comment">// Prints 1234</span>
62489 <span class="hljs-comment">// Out-of-range numbers which are not represented in scientific notation…
62490 <span class="hljs-comment">// will be ignored.</span>
62491 …span class="hljs-property">port</span> = <span class="hljs-number">1e10</span>; <span class="hljs-
62492 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62493 <span class="hljs-comment">// Prints 1234</span></code></pre>
62495 such as floating-point numbers or numbers in scientific notation,
62499 <pre><code class="language-js">myURL.<span class="hljs-property">port</span> = <span class="hljs-nu…
62500 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62501 <span class="hljs-comment">// Prints 4 (because it is the leading number in the string '4.567e21')<…
62504 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62507-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62508 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62509 <span class="hljs-comment">// Prints https:</span>
62511 myURL.<span class="hljs-property">protocol</span> = <span class="hljs-string">'ftp'</span>;
62512 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62513 <span class="hljs-comment">// Prints ftp://example.org/</span></code></pre>
62520 non-special protocol, and vice versa.</p>
62522 …e-js"><span class="hljs-keyword">const</span> u = <span class="hljs-keyword">new</span> <span clas…
62523 u.<span class="hljs-property">protocol</span> = <span class="hljs-string">'https'</span>;
62524 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62525 <span class="hljs-comment">// https://example.org</span></code></pre>
62528 …e-js"><span class="hljs-keyword">const</span> u = <span class="hljs-keyword">new</span> <span clas…
62529 u.<span class="hljs-property">protocol</span> = <span class="hljs-string">'fish'</span>;
62530 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62531 <span class="hljs-comment">// http://example.org</span></code></pre>
62532 <p>Likewise, changing from a non-special protocol to a special protocol is also
62534 …e-js"><span class="hljs-keyword">const</span> u = <span class="hljs-keyword">new</span> <span clas…
62535 u.<span class="hljs-property">protocol</span> = <span class="hljs-string">'http'</span>;
62536 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62537 <span class="hljs-comment">// fish://example.org</span></code></pre>
62542 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62545-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62546 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62547 <span class="hljs-comment">// Prints ?123</span>
62549 myURL.<span class="hljs-property">search</span> = <span class="hljs-string">'abc=xyz'</span>;
62550 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62551 <span class="hljs-comment">// Prints https://example.org/abc?abc=xyz</span></code></pre>
62553 property will be <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which
62554 characters to percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_pars…
62561 URL. This property is read-only but the <code>URLSearchParams</code> object it provides
62567 different rules to determine which characters to percent-encode. For
62570-js"><span class="hljs-keyword">const</span> myUrl = <span class="hljs-keyword">new</span> <span c…
62572 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(myUrl.<span cla…
62574 <span class="hljs-comment">// Modify the URL via searchParams...</span>
62575 myUrl.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">sort</span…
62577 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(myUrl.<span cla…
62580 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62583-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62584 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62585 <span class="hljs-comment">// Prints abc</span>
62587 myURL.<span class="hljs-property">username</span> = <span class="hljs-string">'123'</span>;
62588 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62589 <span class="hljs-comment">// Prints https://123:xyz@example.com/</span></code></pre>
62591 property will be <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which
62592 characters to percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_pars…
62596 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62605 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62611 with <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON…
62612 <pre><code class="language-js"><span class="hljs-keyword">const</span> myURLs = [
62613 …<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="h…
62614 …<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="h…
62616 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hl…
62617 <span class="hljs-comment">// Prints ["https://www.example.com/","https://test.example.org/"]</span…
62638-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62639-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span clas…
62640 <span class="hljs-comment">// Prints 123</span>
62642 …s="hljs-property">searchParams</span>.<span class="hljs-title function_">append</span>(<span class…
62643 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62644 <span class="hljs-comment">// Prints https://example.org/?abc=123&#x26;abc=xyz</span>
62646 myURL.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">delete</sp…
62647 …lass="hljs-property">searchParams</span>.<span class="hljs-title function_">set</span>(<span class…
62648 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62649 <span class="hljs-comment">// Prints https://example.org/?a=b</span>
62651-keyword">const</span> newSearchParams = <span class="hljs-keyword">new</span> <span class="hljs-t…
62652 <span class="hljs-comment">// The above is equivalent to</span>
62653 <span class="hljs-comment">// const newSearchParams = new URLSearchParams(myURL.search);</span>
62655 …ewSearchParams.<span class="hljs-title function_">append</span>(<span class="hljs-string">'a'</spa…
62656 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62657 <span class="hljs-comment">// Prints https://example.org/?a=b</span>
62658 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62659 <span class="hljs-comment">// Prints a=b&#x26;a=c</span>
62661 <span class="hljs-comment">// newSearchParams.toString() is implicitly called</span>
62662 myURL.<span class="hljs-property">search</span> = newSearchParams;
62663 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62664 <span class="hljs-comment">// Prints https://example.org/?a=b&#x26;a=c</span>
62665 newSearchParams.<span class="hljs-title function_">delete</span>(<span class="hljs-string">'a'</spa…
62666 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62667 <span class="hljs-comment">// Prints https://example.org/?a=b&#x26;a=c</span></code></pre>
62672 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
62676 <pre><code class="language-js"><span class="hljs-keyword">let</span> params;
62678 params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</…
62679-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span cla…
62680 <span class="hljs-comment">// Prints 'abc'</span>
62681 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62682 <span class="hljs-comment">// Prints 'user=abc&#x26;query=xyz'</span>
62684 params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</…
62685 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62686 <span class="hljs-comment">// Prints 'user=abc&#x26;query=xyz'</span></code></pre>
62692 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>…
62697 not allowed. Arrays are stringified using <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
62699 …<code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keywo…
62700 <span class="hljs-attr">user</span>: <span class="hljs-string">'abc'</span>,
62701 …<span class="hljs-attr">query</span>: [<span class="hljs-string">'first'</span>, <span class="hljs
62703-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span cla…
62704 <span class="hljs-comment">// Prints [ 'first,second' ]</span>
62705 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62706 <span class="hljs-comment">// Prints 'user=abc&#x26;query=first%2Csecond'</span></code></pre>
62712-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type">&#x3C;It…
62715 is similar to <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obj…
62718 <code>URLSearchParams</code>. Elements of <code>iterable</code> are key-value pairs, and can
62721 <pre><code class="language-js"><span class="hljs-keyword">let</span> params;
62723 <span class="hljs-comment">// Using an array</span>
62724 params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</…
62725 [<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>],
62726 [<span class="hljs-string">'query'</span>, <span class="hljs-string">'first'</span>],
62727 [<span class="hljs-string">'query'</span>, <span class="hljs-string">'second'</span>],
62729 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62730 <span class="hljs-comment">// Prints 'user=abc&#x26;query=first&#x26;query=second'</span>
62732 <span class="hljs-comment">// Using a Map object</span>
62733 <span class="hljs-keyword">const</span> map = <span class="hljs-keyword">new</span> <span class="hl…
62734 map.<span class="hljs-title function_">set</span>(<span class="hljs-string">'user'</span>, <span cl…
62735 map.<span class="hljs-title function_">set</span>(<span class="hljs-string">'query'</span>, <span c…
62736 params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</…
62737 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62738 <span class="hljs-comment">// Prints 'user=abc&#x26;query=xyz'</span>
62740 <span class="hljs-comment">// Using a generator function</span>
62741 <span class="hljs-keyword">function</span>* <span class="hljs-title function_">getQueryPairs</span>…
62742 …<span class="hljs-keyword">yield</span> [<span class="hljs-string">'user'</span>, <span class="hlj…
62743 …<span class="hljs-keyword">yield</span> [<span class="hljs-string">'query'</span>, <span class="hl…
62744 …<span class="hljs-keyword">yield</span> [<span class="hljs-string">'query'</span>, <span class="hl…
62746 params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</…
62747 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62748 <span class="hljs-comment">// Prints 'user=abc&#x26;query=first&#x26;query=second'</span>
62750 <span class="hljs-comment">// Each key-value pair must have exactly two elements</span>
62751 <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</span>([
62752 …[<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>, <span class="hlj…
62754 <span class="hljs-comment">// Throws TypeError [ERR_INVALID_TUPLE]:</span>
62755 <span class="hljs-comment">// Each query pair must be an iterable [name, value] tuple</span>…
62758 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62759 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
62761 <p>Append a new name-value pair to the query string.</p>
62764 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62766 <p>Remove all name-value pairs whose name is <code>name</code>.</p>
62769 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
62771 <p>Returns an ES6 <code>Iterator</code> over each of the name-value pairs in the query.
62777 …oper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;F…
62778 <li><code>thisArg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
62780 <p>Iterates over each name-value pair in the query and invokes the given function.</p>
62781-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62782 … class="hljs-property">searchParams</span>.<span class="hljs-title function_">forEach</span>(<span…
62783 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
62785 <span class="hljs-comment">// Prints:</span>
62786 <span class="hljs-comment">// a b true</span>
62787 <span class="hljs-comment">// c d true</span></code></pre>
62790 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62791 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> …
62794 <p>Returns the value of the first name-value pair whose name is <code>name</code>. If there
62798 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62799 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62801 <p>Returns the values of all name-value pairs whose name is <code>name</code>. If there are
62805 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62806 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
62808 <p>Returns <code>true</code> if there is at least one name-value pair whose name is <code>name</cod…
62811 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
62813 <p>Returns an ES6 <code>Iterator</code> over the names of each name-value pair.</p>
62814-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span …
62815 … class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="hljs-k…
62816 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
62818 <span class="hljs-comment">// Prints:</span>
62819 <span class="hljs-comment">// foo</span>
62820 <span class="hljs-comment">// foo</span></code></pre>
62823 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62824 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
62827 <code>value</code>. If there are any pre-existing name-value pairs whose names are <code>name</code…
62829 append the name-value pair to the query string.</p>
62830 …<code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keywo…
62831 params.<span class="hljs-title function_">append</span>(<span class="hljs-string">'foo'</span>, <sp…
62832 params.<span class="hljs-title function_">append</span>(<span class="hljs-string">'foo'</span>, <sp…
62833 params.<span class="hljs-title function_">append</span>(<span class="hljs-string">'abc'</span>, <sp…
62834 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62835 <span class="hljs-comment">// Prints foo=bar&#x26;foo=baz&#x26;abc=def</span>
62837 params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'foo'</span>, <span …
62838 params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'xyz'</span>, <span …
62839 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62840 <span class="hljs-comment">// Prints foo=def&#x26;abc=def&#x26;xyz=opq</span></code></pre>
62845 <p>Sort all existing name-value pairs in-place by their names. Sorting is done
62846 …rting_algorithm#Stability">stable sorting algorithm</a>, so relative order between name-value pairs
62849-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span …
62850 params.<span class="hljs-title function_">sort</span>();
62851 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62852 <span class="hljs-comment">// Prints query%5B%5D=abc&#x26;query%5B%5D=123&#x26;type=search</span></…
62855 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62858 percent-encoded where necessary.</p>
62861 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
62863 <p>Returns an ES6 <code>Iterator</code> over the values of each name-value pair.</p>
62866 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
62868 <p>Returns an ES6 <code>Iterator</code> over each of the name-value pairs in the query string.
62872-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span …
62873 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> [name, value] <span …
62874 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
62876 <span class="hljs-comment">// Prints:</span>
62877 <span class="hljs-comment">// foo bar</span>
62878 <span class="hljs-comment">// xyz baz</span></code></pre>
62884 <li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
62885 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62887 <p>Returns the <a href="https://tools.ietf.org/html/rfc5891#section-4.4">Punycode</a> ASCII seriali…
62892 …code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in"…
62893-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62894 <span class="hljs-comment">// Prints xn--espaol-zwa.com</span>
62895-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62896 <span class="hljs-comment">// Prints xn--fiq228c.com</span>
62897-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62898 <span class="hljs-comment">// Prints an empty string</span></code></pre>
62904 <li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
62905 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62912 …code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in"…
62913-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62914 <span class="hljs-comment">// Prints español.com</span>
62915-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62916 <span class="hljs-comment">// Prints 中文.com</span>
62917-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62918 <span class="hljs-comment">// Prints an empty string</span></code></pre>
62924 …api" class="type">&#x3C;URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
62925 …loper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string…
62927 <p>This function ensures the correct decodings of percent-encoded characters as
62928 well as ensuring a cross-platform valid absolute path string.</p>
62930-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
62932-keyword">const</span> __filename = <span class="hljs-title function_">fileURLToPath</span>(<span …
62934-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62935 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///C:/path/…
62937-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62938 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file://nas/foo.t…
62940-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62941 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///你好.txt'<…
62943-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62944-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///hello world'</span>); …
62945-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62946 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///C:/path/…
62948-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62949 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file://nas/foo.t…
62951-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62952 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///你好.txt'<…
62954-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62955 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///hello wo…
62962 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
62964 <li><code>auth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62966 <li><code>fragment</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
62968 <li><code>search</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
62970 <li><code>unicode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
62975 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62984-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
62985 …lass="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-t…
62987 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62988 <span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
62990 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62991 <span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
62993-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62994-comment">// Prints 'https://測試/?abc'</span></code><code class="language-js cjs"><span class="hljs
62995 …lass="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-t…
62997 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62998 <span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
63000 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
63001 <span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
63003-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
63004 <span class="hljs-comment">// Prints 'https://測試/?abc'</span></code></pre>
63010 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63016-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
63018-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/foo…
63019 <span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/foo#1'</span>);…
63021-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/som…
63022-title function_">pathToFileURL</span>(<span class="hljs-string">'/some/path%.c'</span>); <s…
63023 <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(__filename); …
63024 <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(__filename); …
63025 <span class="hljs-title function_">pathToFileURL</span>(__filename); <span class="hljs-c…
63026 <span class="hljs-title function_">pathToFileURL</span>(__filename); <span class="hljs-c…
63028-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/foo…
63029 <span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/foo#1'</span>);…
63031-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/som…
63032 <span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/some/path%.c'</…
63039 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
63041 <li><code>protocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
63042 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
63044 <li><code>hash</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63045 <li><code>search</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
63046 <li><code>pathname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
63047 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63051 <li><code>href</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63052 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63053 <li><code>auth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63060 …ss="language-js"><span class="hljs-keyword">const</span> { urlToHttpOptions } = <span class="hljs-
63061 …lass="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-t…
63063 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
63064 <span class="hljs-comment">/**
63067 hostname: 'xn--g6w251d',
63072 href: 'https://a:b<span class="hljs-doctag">@xn</span>--g6w251d/?abc#foo',
63088 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHAT…
63101 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHAT…
63116 <p>The <code>host</code> property is the full lower-cased host portion of the URL, including
63120 <p>The <code>hostname</code> property is the lower-cased host name portion of the <code>host</code>
63125 <code>protocol</code> and <code>host</code> components converted to lower-case.</p>
63143 <p>The <code>protocol</code> property identifies the URL's lower-cased protocol scheme.</p>
63160 forward-slash characters (<code>/</code>) are required following the colon in the
63178 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHAT…
63180 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
63186 …code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in"…
63187 url.<span class="hljs-title function_">format</span>({
63188 <span class="hljs-attr">protocol</span>: <span class="hljs-string">'https'</span>,
63189 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'example.com'</span>,
63190 <span class="hljs-attr">pathname</span>: <span class="hljs-string">'/some/path'</span>,
63191 <span class="hljs-attr">query</span>: {
63192 <span class="hljs-attr">page</span>: <span class="hljs-number">1</span>,
63193 <span class="hljs-attr">format</span>: <span class="hljs-string">'json'</span>
63197 <span class="hljs-comment">// => 'https://example.com/some/path?page=1&#x26;format=json'</span></co…
63203 <li>If <code>urlObject.protocol</code> is a string, it is appended as-is to <code>result</code>.</l…
63288 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHAT…
63290 <li><code>urlString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
63291 <li><code>parseQueryString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
63295 <li><code>slashesDenoteHost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
63307 lenient, non-standard algorithm for parsing URL strings, security
63330 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHAT…
63332 <li><code>from</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63333 <li><code>to</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
63337 …code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in"…
63338-title function_">resolve</span>(<span class="hljs-string">'/one/two/three'</span>, <span class="h…
63339-title function_">resolve</span>(<span class="hljs-string">'http://example.com/'</span>, <span cla…
63340-title function_">resolve</span>(<span class="hljs-string">'http://example.com/one'</span>, <span …
63342 …guage-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">resolve</s…
63343-keyword">const</span> resolvedUrl = <span class="hljs-keyword">new</span> <span class="hljs-title…
63344 …<span class="hljs-keyword">if</span> (resolvedUrl.<span class="hljs-property">protocol</span> === …
63345 <span class="hljs-comment">// `from` is a relative URL.</span>
63346 <span class="hljs-keyword">const</span> { pathname, search, hash } = resolvedUrl;
63347 <span class="hljs-keyword">return</span> pathname + search + hash;
63349 …<span class="hljs-keyword">return</span> resolvedUrl.<span class="hljs-title function_">toString</…
63352-title function_">resolve</span>(<span class="hljs-string">'/one/two/three'</span>, <span class="h…
63353-title function_">resolve</span>(<span class="hljs-string">'http://example.com/'</span>, <span cla…
63354-title function_">resolve</span>(<span class="hljs-string">'http://example.com/one'</span>, <span …
63355 <p><a id="whatwg-percent-encoding"></a></p>
63356 </section><section><h3>Percent-encoding in URLs<span><a class="mark" href="#url_percent_encoding_in…
63364 <pre><code class="language-text">&#x3C; > " ` \r \n \t { } | \ ^ '</code></pre>
63370 <p>The WHATWG algorithm defines four "percent-encode sets" that describe ranges
63371 of characters that must be percent-encoded:</p>
63374 <p>The <em>C0 control percent-encode set</em> includes code points in range U+0000 to
63378 <p>The <em>fragment percent-encode set</em> includes the <em>C0 control percent-encode set</em>
63382 <p>The <em>path percent-encode set</em> includes the <em>C0 control percent-encode set</em>
63387 <p>The <em>userinfo encode set</em> includes the <em>path percent-encode set</em> and code
63392 <p>The <em>userinfo percent-encode set</em> is used exclusively for username and
63393 passwords encoded within the URL. The <em>path percent-encode set</em> is used for the
63394 path of most URLs. The <em>fragment percent-encode set</em> is used for URL fragments.
63395 The <em>C0 control percent-encode set</em> is used for host and path under certain
63397 <p>When non-ASCII characters appear within a host name, the host name is encoded
63398 using the <a href="https://tools.ietf.org/html/rfc5891#section-4.4">Punycode</a> algorithm. Note, h…
63399 <em>both</em> Punycode encoded and percent-encoded characters:</p>
63400-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
63401 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
63402 <span class="hljs-comment">// Prints https://xn--1xa.example.com/foo</span>
63403 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
63404 <span class="hljs-comment">// Prints https://xn--1xa.example.com</span></code></pre></section>
63408 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
63413 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63419 <li><code>original</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
63420 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
63423 function following the error-first callback style, i.e. taking
63427 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63429 … class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-ti…
63430 <span class="hljs-keyword">return</span> <span class="hljs-string">'hello world'</span>;
63432 <span class="hljs-keyword">const</span> callbackFunction = util.<span class="hljs-title function_">…
63434 <span class="hljs-title function_">callbackFunction</span>(<span class="hljs-function">(<span class…
63435 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
63436 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
63439 <pre><code class="language-text">hello world</code></pre>
63447 …<code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title funct…
63448 …s="hljs-keyword">return</span> <span class="hljs-title class_">Promise</span>.<span class="hljs-ti…
63450 <span class="hljs-keyword">const</span> callbackFunction = util.<span class="hljs-title function_">…
63452 <span class="hljs-title function_">callbackFunction</span>(<span class="hljs-function">(<span class…
63453 …<span class="hljs-comment">// When the Promise was rejected with `null` it is wrapped with an Erro…
63454 <span class="hljs-comment">// the original value is stored in `reason`.</span>
63455-title function_">hasOwnProperty</span>(<span class="hljs-string">'reason'</span>) &#x26;&#x26; er…
63462 <li><code>section</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
63464 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
63466 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
63472 …ror_data_args"><code>console.error()</code></a>. If not, then the returned function is a no-op.</p>
63473 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63474 <span class="hljs-keyword">const</span> debuglog = util.<span class="hljs-title function_">debuglog…
63476 <span class="hljs-title function_">debuglog</span>(<span class="hljs-string">'hello from foo [%d]'<…
63479 <pre><code class="language-console">FOO 3245: hello from foo [123]</code></pre>
63483 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63484 …span class="hljs-keyword">const</span> debuglog = util.<span class="hljs-title function_">debuglog…
63486 <span class="hljs-title function_">debuglog</span>(<span class="hljs-string">'hi there, it\'s foo-b…
63489 <pre><code class="language-console">FOO-BAR 3257: hi there, it's foo-bar [2333]</code></pre>
63490 <p>Multiple comma-separated <code>section</code> names may be specified in the <code>NODE_DEBUG</co…
63495 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63496-keyword">let</span> debuglog = util.<span class="hljs-title function_">debuglog</span>(<span clas…
63497 <span class="hljs-comment">// Replace with a logging function that optimizes out</span>
63498 <span class="hljs-comment">// testing if the section is enabled</span>
63506 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
63513 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63514 …ljs-keyword">const</span> enabled = util.<span class="hljs-title function_">debuglog</span>(<span …
63515 <span class="hljs-keyword">if</span> (enabled) {
63516-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
63520 <pre><code class="language-console">hello from foo [123]</code></pre>
63540 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
63541 <li><code>msg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
63543 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63545 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
63549 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63551-built_in">exports</span>.<span class="hljs-property">obsoleteFunction</span> = util.<span class="…
63552 <span class="hljs-comment">// Do something here.</span>
63553 }, <span class="hljs-string">'obsoleteFunction() is deprecated. Use newShinyFunction() instead.'</s…
63561 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63563 …class="hljs-keyword">const</span> fn1 = util.<span class="hljs-title function_">deprecate</span>(s…
63564 …="hljs-keyword">const</span> fn2 = util.<span class="hljs-title function_">deprecate</span>(someOt…
63565 <span class="hljs-title function_">fn1</span>(); <span class="hljs-comment">// Emits a deprecation …
63566 <span class="hljs-title function_">fn2</span>(); <span class="hljs-comment">// Does not emit a depr…
63567 <p>If either the <code>--no-deprecation</code> or <code>--no-warnings</code> command-line flags are
63570 <p>If the <code>--trace-deprecation</code> or <code>--trace-warnings</code> command-line flags are …
63574 <p>If the <code>--throw-deprecation</code> command-line flag is set, or the
63577 <p>The <code>--throw-deprecation</code> command-line flag and <code>process.throwDeprecation</code>
63578 property take precedence over <code>--trace-deprecation</code> and
63607 …eveloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;str…
63610 as a <code>printf</code>-like format string which can contain zero or more format
63615 and <code>-0</code>. <code>BigInt</code> values will be represented with an <code>n</code> and Obje…
63628 including non-enumerable properties and proxies.</li>
63631 the full object not including non-enumerable properties and proxies.</li>
63634 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
63637 …e class="language-js">util.<span class="hljs-title function_">format</span>(<span class="hljs-stri…
63638 <span class="hljs-comment">// Returns: 'foo:%s'</span></code></pre>
63644-js">util.<span class="hljs-title function_">format</span>(<span class="hljs-string">'%s:%s'</span…
63645 <span class="hljs-comment">// Returns: 'foo:bar baz'</span></code></pre>
63648 …ge-js">util.<span class="hljs-title function_">format</span>(<span class="hljs-number">1</span>, <…
63649 <span class="hljs-comment">// Returns: '1 2 3'</span></code></pre>
63652 <pre><code class="language-js">util.<span class="hljs-title function_">format</span>(<span class="h…
63653 <span class="hljs-comment">// Returns: '%% %s'</span></code></pre>
63662 <li><code>inspectOptions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
63663 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
63668-js">util.<span class="hljs-title function_">formatWithOptions</span>({ <span class="hljs-attr">co…
63669 <span class="hljs-comment">// Returns 'See object { foo: 42 }', where `42` is colored as a number</…
63670 <span class="hljs-comment">// when printed to a terminal.</span></code></pre>
63676 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
63677 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
63680 The mapping between error codes and error names is platform-dependent.
63682-js">fs.<span class="hljs-title function_">access</span>(<span class="hljs-string">'file/that/does…
63683 …<span class="hljs-keyword">const</span> name = util.<span class="hljs-title function_">getSystemEr…
63684 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
63691 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
63694 The mapping between error codes and error names is platform-dependent.
63696-js">fs.<span class="hljs-title function_">access</span>(<span class="hljs-string">'file/that/does…
63697 …<span class="hljs-keyword">const</span> errorMap = util.<span class="hljs-title function_">getSyst…
63698 …<span class="hljs-keyword">const</span> name = errorMap.<span class="hljs-title function_">get</sp…
63699 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
63713 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use ES2015 c…
63715 <li><code>constructor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer…
63716 <li><code>superConstructor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
63721 <p>Inherit the prototype methods from one <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
63728 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63729 …s="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="h…
63731 <span class="hljs-keyword">function</span> <span class="hljs-title function_">MyStream</span>(<span…
63732 …<span class="hljs-title class_">EventEmitter</span>.<span class="hljs-title function_">call</span>…
63735 util.<span class="hljs-title function_">inherits</span>(<span class="hljs-title class_">MyStream</s…
63737-title class_">MyStream</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</s…
63738 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<…
63741 <span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class=…
63743-variable language_">console</span>.<span class="hljs-title function_">log</span>(stream <span cla…
63744-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
63746 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
63747 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
63749 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'It works!'</span>…
63751-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
63753 …class="hljs-keyword">class</span> <span class="hljs-title class_">MyStream</span> <span class="hlj…
63754 <span class="hljs-title function_">write</span>(<span class="hljs-params">data</span>) {
63755 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<…
63759 <span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class=…
63761 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
63762 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
63764 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'With ES6'</span>)…
63774 …text</code> now, a custom inspection function on it will not receive context-specific arguments an…
63817 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
63818 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
63820 …la.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If …
63821 properties are included in the formatted result. <a href="https://developer.mozilla.org/en-US/docs/…
63822 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet">…
63824 <li><code>depth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
63828 <li><code>colors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
63831 <li><code>customInspect</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
63834 <li><code>showProxy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
63835 the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy…
63836 <li><code>maxArrayLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
63837-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>, <a href=…
63840 <li><code>maxStringLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
63843 <li><code>breakLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
63847 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
63854 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
63856 string. If set to <code>true</code> the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
63857 it is used as a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_O…
63858 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
63865 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
63873 <pre><code class="language-js"><span class="hljs-keyword">class</span> <span class="hljs-title clas…
63874 …get [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toStringTag</span>]…
63875 <span class="hljs-keyword">return</span> <span class="hljs-string">'bar'</span>;
63879 <span class="hljs-keyword">class</span> <span class="hljs-title class_">Bar</span> {}
63881-keyword">const</span> baz = <span class="hljs-title class_">Object</span>.<span class="hljs-title…
63883 …ss="hljs-title function_">inspect</span>(<span class="hljs-keyword">new</span> <span class="hljs-t…
63884 …ss="hljs-title function_">inspect</span>(<span class="hljs-keyword">new</span> <span class="hljs-t…
63885 util.<span class="hljs-title function_">inspect</span>(baz); <span class="hljs-comment">// '[…
63887 … class="language-js"><span class="hljs-keyword">const</span> { inspect } = <span class="hljs-built…
63889 <span class="hljs-keyword">const</span> obj = {};
63890 obj.<span class="hljs-property">a</span> = [obj];
63891 obj.<span class="hljs-property">b</span> = {};
63892 obj.<span class="hljs-property">b</span>.<span class="hljs-property">inner</span> = obj.<span class…
63893 obj.<span class="hljs-property">b</span>.<span class="hljs-property">obj</span> = obj;
63895 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
63896 <span class="hljs-comment">// &#x3C;ref *1> {</span>
63897 <span class="hljs-comment">// a: [ [Circular *1] ],</span>
63898 <span class="hljs-comment">// b: &#x3C;ref *2> { inner: [Circular *2], obj: [Circular *1] }</span>
63899 <span class="hljs-comment">// }</span></code></pre>
63901 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63903-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class…
63905 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63907 <span class="hljs-keyword">const</span> o = {
63908 …<span class="hljs-attr">a</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">2…
63909 …<span class="hljs-string">'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do '</span…
63910 … <span class="hljs-string">'eiusmod tempor incididunt ut labore et dolore magna aliqua.'</span>,
63911 <span class="hljs-string">'test'</span>,
63912 <span class="hljs-string">'foo'</span>]], <span class="hljs-number">4</span>],
63913-attr">b</span>: <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>(…
63915-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class…
63917 <span class="hljs-comment">// { a:</span>
63918 <span class="hljs-comment">// [ 1,</span>
63919 <span class="hljs-comment">// 2,</span>
63920 <span class="hljs-comment">// [ [ 'Lorem ipsum dolor sit amet, consectetur [...]', // A long li…
63921 <span class="hljs-comment">// 'test',</span>
63922 <span class="hljs-comment">// 'foo' ] ],</span>
63923 <span class="hljs-comment">// 4 ],</span>
63924 <span class="hljs-comment">// b: Map(2) { 'za' => 1, 'zb' => 'test' } }</span>
63926 <span class="hljs-comment">// Setting `compact` to false changes the output to be more reader frien…
63927-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class…
63929 <span class="hljs-comment">// {</span>
63930 <span class="hljs-comment">// a: [</span>
63931 <span class="hljs-comment">// 1,</span>
63932 <span class="hljs-comment">// 2,</span>
63933 <span class="hljs-comment">// [</span>
63934 <span class="hljs-comment">// [</span>
63935 <span class="hljs-comment">// 'Lorem ipsum dolor sit amet, consectetur ' +</span>
63936 <span class="hljs-comment">// 'adipiscing elit, sed do eiusmod tempor ' +</span>
63937 <span class="hljs-comment">// 'incididunt ut labore et dolore magna ' +</span>
63938 <span class="hljs-comment">// 'aliqua.,</span>
63939 <span class="hljs-comment">// 'test',</span>
63940 <span class="hljs-comment">// 'foo'</span>
63941 <span class="hljs-comment">// ]</span>
63942 <span class="hljs-comment">// ],</span>
63943 <span class="hljs-comment">// 4</span>
63944 <span class="hljs-comment">// ],</span>
63945 <span class="hljs-comment">// b: Map(2) {</span>
63946 <span class="hljs-comment">// 'za' => 1,</span>
63947 <span class="hljs-comment">// 'zb' => 'test'</span>
63948 <span class="hljs-comment">// }</span>
63949 <span class="hljs-comment">// }</span>
63951 <span class="hljs-comment">// Setting `breakLength` to e.g. 150 will print the "Lorem ipsum" text i…
63952 <span class="hljs-comment">// single line.</span>
63953 <span class="hljs-comment">// Reducing the `breakLength` will split the "Lorem ipsum" text in small…
63954 <span class="hljs-comment">// chunks.</span></code></pre>
63955 …zilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and…
63958 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet">…
63960 … class="language-js"><span class="hljs-keyword">const</span> { inspect } = <span class="hljs-built…
63962 <span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">a</span>: <span class="hljs
63963 <span class="hljs-keyword">const</span> obj2 = { <span class="hljs-attr">b</span>: <span class="hlj…
63964 <span class="hljs-keyword">const</span> weakSet = <span class="hljs-keyword">new</span> <span class…
63966-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
63967 <span class="hljs-comment">// WeakSet { { a: 1 }, { b: 2 } }</span></code></pre>
63970 … class="language-js"><span class="hljs-keyword">const</span> { inspect } = <span class="hljs-built…
63971 <span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span c…
63973 <span class="hljs-keyword">const</span> o1 = {
63974 …<span class="hljs-attr">b</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">3…
63975 <span class="hljs-attr">a</span>: <span class="hljs-string">'`a` comes before `b`'</span>,
63976-attr">c</span>: <span class="hljs-keyword">new</span> <span class="hljs-title class_">Set</span>(…
63978-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
63979 <span class="hljs-comment">// { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set(3) { 1, 2, 3 } }<…
63980-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
63981 <span class="hljs-comment">// { c: Set(3) { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' }<…
63983 <span class="hljs-keyword">const</span> o2 = {
63984-attr">c</span>: <span class="hljs-keyword">new</span> <span class="hljs-title class_">Set</span>(…
63985 <span class="hljs-attr">a</span>: <span class="hljs-string">'`a` comes before `b`'</span>,
63986 …<span class="hljs-attr">b</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">3…
63988 assert.<span class="hljs-property">strict</span>.<span class="hljs-title function_">equal</span>(
63989 …<span class="hljs-title function_">inspect</span>(o1, { <span class="hljs-attr">sorted</span>: <sp…
63990 …<span class="hljs-title function_">inspect</span>(o2, { <span class="hljs-attr">sorted</span>: <sp…
64024 <li><code>reset</code> - Resets all (color) modifiers to their defaults</li>
64025 <li><strong>bold</strong> - Make text bold</li>
64026 <li><em>italic</em> - Make text italic</li>
64027 <li><span style="border-bottom: 1px;">underline</span> - Make text underlined</li>
64028 <li><del>strikethrough</del> - Puts a horizontal line through the center of the text
64030 <li><code>hidden</code> - Prints the text, but makes it invisible (Alias: conceal)</li>
64031 <li><span style="opacity: 0.5;">dim</span> - Decreased color intensity (Alias:
64033 <li><span style="border-top: 1px">overlined</span> - Make text overlined</li>
64034 <li>blink - Hides and shows the text in an interval</li>
64035 <li><span style="filter: invert(100%)">inverse</span> - Swap foreground and
64037 <li><span style="border-bottom: 1px double;">doubleunderline</span> - Make text
64039 <li><span style="border: 1px">framed</span> - Draw a frame around the text</li>
64085 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64087 <span class="hljs-keyword">class</span> <span class="hljs-title class_">Box</span> {
64088 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">value</span>) {
64089 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span> = value;
64092 …[util.<span class="hljs-property">inspect</span>.<span class="hljs-property">custom</span>](depth,…
64093 <span class="hljs-keyword">if</span> (depth &#x3C; <span class="hljs-number">0</span>) {
64094 …"hljs-keyword">return</span> options.<span class="hljs-title function_">stylize</span>(<span class…
64097 …<span class="hljs-keyword">const</span> newOptions = <span class="hljs-title class_">Object</span>…
64098-attr">depth</span>: options.<span class="hljs-property">depth</span> === <span class="hljs-litera…
64101 <span class="hljs-comment">// Five space padding because that's the size of "Box&#x3C; ".</span>
64102 …ss="hljs-keyword">const</span> padding = <span class="hljs-string">' '</span>.<span class="hljs-ti…
64103-keyword">const</span> inner = util.<span class="hljs-title function_">inspect</span>(<span class=…
64104 …span class="hljs-title function_">replace</span>(<span class="hljs-regexp">/\n/g</span>, <span cla…
64105-keyword">return</span> <span class="hljs-string">`<span class="hljs-subst">${options.stylize(<spa…
64109 … class="hljs-keyword">const</span> box = <span class="hljs-keyword">new</span> <span class="hljs-t…
64111 util.<span class="hljs-title function_">inspect</span>(box);
64112 <span class="hljs-comment">// Returns: "Box&#x3C; true >"</span></code></pre>
64116 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64118 <span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">foo</span>: <span class="hl…
64119 …<span class="hljs-property">inspect</span>.<span class="hljs-property">custom</span>] = <span clas…
64120 …<span class="hljs-keyword">return</span> { <span class="hljs-attr">bar</span>: <span class="hljs-s…
64123 util.<span class="hljs-title function_">inspect</span>(obj);
64124 <span class="hljs-comment">// Returns: "{ bar: 'baz' }"</span></code></pre>
64138 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" c…
64141 symbol is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects…
64143-js"><span class="hljs-keyword">const</span> inspect = <span class="hljs-title class_">Symbol</spa…
64145 <span class="hljs-keyword">class</span> <span class="hljs-title class_">Password</span> {
64146 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">value</span>) {
64147 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span> = value;
64150 <span class="hljs-title function_">toString</span>(<span class="hljs-params"></span>) {
64151 <span class="hljs-keyword">return</span> <span class="hljs-string">'xxxxxxxx'</span>;
64155 …<span class="hljs-keyword">return</span> <span class="hljs-string">`Password &#x3C;<span class="hl…
64159 …s="hljs-keyword">const</span> password = <span class="hljs-keyword">new</span> <span class="hljs-t…
64160 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
64161 <span class="hljs-comment">// Prints Password &#x3C;xxxxxxxx></span></code></pre>
64172 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64173-keyword">const</span> arr = <span class="hljs-title class_">Array</span>(<span class="hljs-number…
64175 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
64176 …ljs-property">inspect</span>.<span class="hljs-property">defaultOptions</span>.<span class="hljs-p…
64177 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
64183 <li><code>val1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
64184 <li><code>val2</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
64185 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64196 <li><code>original</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
64197 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
64199 <p>Takes a function following the common error-first callback style, i.e. taking
64202 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64203 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
64205 <span class="hljs-keyword">const</span> stat = util.<span class="hljs-title function_">promisify</s…
64206-title function_">stat</span>(<span class="hljs-string">'.'</span>).<span class="hljs-title functi…
64207 <span class="hljs-comment">// Do something with `stats`</span>
64208 }).<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-p…
64209 <span class="hljs-comment">// Handle the error.</span>
64212 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64213 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
64215 <span class="hljs-keyword">const</span> stat = util.<span class="hljs-title function_">promisify</s…
64217 …ass="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title…
64218 …ss="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span class="hljs-t…
64219-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
64226 an error-first callback, it will still be passed an error-first
64230 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64232 <span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> {
64233 <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) {
64234 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">a</span> = <span cla…
64237 <span class="hljs-title function_">bar</span>(<span class="hljs-params">callback</span>) {
64238 …"hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-v…
64242 <span class="hljs-keyword">const</span> foo = <span class="hljs-keyword">new</span> <span class="hl…
64244 <span class="hljs-keyword">const</span> naiveBar = util.<span class="hljs-title function_">promisif…
64245 <span class="hljs-comment">// TypeError: Cannot read property 'a' of undefined</span>
64246 <span class="hljs-comment">// naiveBar().then(a => console.log(a));</span>
64248-title function_">call</span>(foo).<span class="hljs-title function_">then</span>(<span class="hlj…
64250 <span class="hljs-keyword">const</span> bindBar = naiveBar.<span class="hljs-title function_">bind<…
64251-title function_">bindBar</span>().<span class="hljs-title function_">then</span>(<span class="hlj…
64255 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64257 <span class="hljs-keyword">function</span> <span class="hljs-title function_">doSomething</span>(<s…
64258 <span class="hljs-comment">// ...</span>
64261 …span class="hljs-property">promisify</span>.<span class="hljs-property">custom</span>] = <span cla…
64262 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">getPromiseSomehow</spa…
64265 <span class="hljs-keyword">const</span> promisified = util.<span class="hljs-title function_">promi…
64266-variable language_">console</span>.<span class="hljs-title function_">log</span>(promisified === …
64267 <span class="hljs-comment">// prints 'true'</span></code></pre>
64269 standard format of taking an error-first callback as the last argument.</p>
64272-js">doSomething[util.<span class="hljs-property">promisify</span>.<span class="hljs-property">cus…
64273-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Prom…
64274 <span class="hljs-title function_">doSomething</span>(foo, resolve, reject);
64292 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" c…
64296 symbol is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects…
64300-js"><span class="hljs-keyword">const</span> kCustomPromisifiedSymbol = <span class="hljs-title cl…
64302 doSomething[kCustomPromisifiedSymbol] = <span class="hljs-function">(<span class="hljs-params">foo<…
64303-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Prom…
64304 <span class="hljs-title function_">doSomething</span>(foo, resolve, reject);
64312-js"><span class="hljs-keyword">const</span> decoder = <span class="hljs-keyword">new</span> <span…
64313 <span class="hljs-keyword">let</span> string = <span class="hljs-string">''</span>;
64314 <span class="hljs-keyword">let</span> buffer;
64315 <span class="hljs-keyword">while</span> (buffer = <span class="hljs-title function_">getNextChunkSo…
64316 …= decoder.<span class="hljs-title function_">decode</span>(buffer, { <span class="hljs-attr">strea…
64318 string += decoder.<span class="hljs-title function_">decode</span>(); <span class="hljs-comment">//…
64471-8859-2'</code></td><td><code>'csisolatin2'</code>, <code>'iso-ir-101'</code>, <code>'iso8859-2'</…
64472 <h5>Encodings supported when Node.js is built with the <code>small-icu</code> option<span><a class=…
64494-8'</code></td><td><code>'unicode-1-1-utf-8'</code>, <code>'utf8'</code></td></tr><tr><td><code>'u…
64513 …<code>'utf-8'</code></td><td><code>'unicode-1-1-utf-8'</code>, <code>'utf8'</code></td></tr><tr><t…
64514 <p>The <code>'iso-8859-16'</code> encoding listed in the <a href="https://encoding.spec.whatwg.org/…
64529 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
64530 supports. <strong>Default:</strong> <code>'utf-8'</code>.</li>
64531 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
64533 <li><code>fatal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64536 <li><code>ignoreBOM</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
64539 <code>'utf-8'</code>, <code>'utf-16be'</code> or <code>'utf-16le'</code>. <strong>Default:</strong>…
64548-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> …
64550 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
64552 <li><code>stream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
64556 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
64565 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
64570 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
64576 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
64593 instances of <code>TextEncoder</code> only support UTF-8 encoding.</p>
64594 …<code class="language-js"><span class="hljs-keyword">const</span> encoder = <span class="hljs-keyw…
64595 <span class="hljs-keyword">const</span> uint8array = encoder.<span class="hljs-title function_">enc…
64599 <li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64600 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
64602 <p>UTF-8 encodes the <code>input</code> string and returns a <code>Uint8Array</code> containing the
64606 <li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
64607 <li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
64608 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
64610 <li><code>read</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
64611 …//developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;…
64615 <p>UTF-8 encodes the <code>src</code> string to the <code>dest</code> Uint8Array and returns an obj…
64616 containing the read Unicode code units and written UTF-8 bytes.</p>
64617 …<code class="language-js"><span class="hljs-keyword">const</span> encoder = <span class="hljs-keyw…
64618 <span class="hljs-keyword">const</span> src = <span class="hljs-string">'this is some data'</span>;
64619-keyword">const</span> dest = <span class="hljs-keyword">new</span> <span class="hljs-title class_…
64620 <span class="hljs-keyword">const</span> { read, written } = encoder.<span class="hljs-title functio…
64623 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
64625 <p>The encoding supported by the <code>TextEncoder</code> instance. Always set to <code>'utf-8'</co…
64631 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
64640 <p><code>util.types</code> provides type checks for different kinds of built-in objects.
64652 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64653 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64655 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64656 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArr…
64659-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isAnyArrayBu…
64660-property">types</span>.<span class="hljs-title function_">isAnyArrayBuffer</span>(<span class="hl…
64666 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64667 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64669 …value is an instance of one of the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
64670 views, such as typed array objects or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
64671 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuff…
64672-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isArrayBuffe…
64673-property">types</span>.<span class="hljs-title function_">isArrayBufferView</span>(<span class="h…
64674-property">types</span>.<span class="hljs-title function_">isArrayBufferView</span>(<span class="h…
64675-property">types</span>.<span class="hljs-title function_">isArrayBufferView</span>(<span class="h…
64681 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64682 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64685 <!-- eslint-disable prefer-rest-params -->
64686 …code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title functi…
64687-property">types</span>.<span class="hljs-title function_">isArgumentsObject</span>(<span class="h…
64694 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64695 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64697 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64698 This does <em>not</em> include <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
64700-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isArrayBuffe…
64701-property">types</span>.<span class="hljs-title function_">isArrayBuffer</span>(<span class="hljs-
64707 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64708 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64710 <p>Returns <code>true</code> if the value is an <a href="https://developer.mozilla.org/en-US/docs/W…
64714-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isAsyncFunct…
64715-property">types</span>.<span class="hljs-title function_">isAsyncFunction</span>(<span class="hlj…
64721 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64722 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64725-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBigInt64Ar…
64726-property">types</span>.<span class="hljs-title function_">isBigInt64Array</span>(<span class="hlj…
64732 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64733 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64736-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBigUint64A…
64737-property">types</span>.<span class="hljs-title function_">isBigUint64Array</span>(<span class="hl…
64743 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64744 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64748-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBooleanObj…
64749 …"hljs-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class…
64750-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class="hlj…
64751-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class="hlj…
64752-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class="hlj…
64753-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class="hlj…
64759 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64760 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64765-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBoxedPrimi…
64766-property">types</span>.<span class="hljs-title function_">isBoxedPrimitive</span>(<span class="hl…
64767-property">types</span>.<span class="hljs-title function_">isBoxedPrimitive</span>(<span class="hl…
64768-property">types</span>.<span class="hljs-title function_">isBoxedPrimitive</span>(<span class="hl…
64769-property">types</span>.<span class="hljs-title function_">isBoxedPrimitive</span>(<span class="hl…
64775 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64776 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64778 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64779-js"><span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">new</span> <span clas…
64780-property">types</span>.<span class="hljs-title function_">isDataView</span>(<span class="hljs-key…
64781-property">types</span>.<span class="hljs-title function_">isDataView</span>(<span class="hljs-key…
64782 <p>See also <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objec…
64788 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64789 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64791 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64792-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isDate</span…
64798 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64799 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64805 addons. In JavaScript, they are <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
64807 <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <s…
64808 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
64810 <span class="hljs-keyword">static</span> napi_value <span class="hljs-title function_">MyNapi</span…
64811 …pan class="hljs-type">int</span>* raw = (<span class="hljs-type">int</span>*) <span class="hljs-bu…
64812 …e_external(env, (<span class="hljs-type">void</span>*) raw, <span class="hljs-literal">NULL</span>…
64813 <span class="hljs-keyword">if</span> (status != napi_ok) {
64814 …napi_throw_error(env, <span class="hljs-literal">NULL</span>, <span class="hljs-string">"napi_crea…
64815 <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
64817 <span class="hljs-keyword">return</span> result;
64820 DECLARE_NAPI_PROPERTY(<span class="hljs-string">"myNapi"</span>, MyNapi)
64822 …ode class="language-js"><span class="hljs-keyword">const</span> native = <span class="hljs-built_i…
64823 <span class="hljs-keyword">const</span> data = native.<span class="hljs-title function_">myNapi</sp…
64824 util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isExternal</span>(…
64825 …lass="hljs-property">types</span>.<span class="hljs-title function_">isExternal</span>(<span class…
64826-property">types</span>.<span class="hljs-title function_">isExternal</span>(<span class="hljs-key…
64834 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64835 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64837 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64838-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isFloat32Arr…
64839-property">types</span>.<span class="hljs-title function_">isFloat32Array</span>(<span class="hljs
64840-property">types</span>.<span class="hljs-title function_">isFloat32Array</span>(<span class="hljs
64846 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64847 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64849 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64850-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isFloat64Arr…
64851-property">types</span>.<span class="hljs-title function_">isFloat64Array</span>(<span class="hljs
64852-property">types</span>.<span class="hljs-title function_">isFloat64Array</span>(<span class="hljs
64858 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64859 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64865-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isGeneratorF…
64866-property">types</span>.<span class="hljs-title function_">isGeneratorFunction</span>(<span class=…
64872 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64873 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64876 built-in generator function.
64880 …code class="language-js"><span class="hljs-keyword">function</span>* <span class="hljs-title funct…
64881 <span class="hljs-keyword">const</span> generator = <span class="hljs-title function_">foo</span>();
64882 …l.<span class="hljs-property">types</span>.<span class="hljs-title function_">isGeneratorObject</s…
64888 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64889 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64891 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64892-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isInt8Array<…
64893-property">types</span>.<span class="hljs-title function_">isInt8Array</span>(<span class="hljs-ke…
64894-property">types</span>.<span class="hljs-title function_">isInt8Array</span>(<span class="hljs-ke…
64900 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64901 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64903 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64904-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isInt16Array…
64905-property">types</span>.<span class="hljs-title function_">isInt16Array</span>(<span class="hljs-k…
64906-property">types</span>.<span class="hljs-title function_">isInt16Array</span>(<span class="hljs-k…
64912 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64913 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64915 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64916-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isInt32Array…
64917-property">types</span>.<span class="hljs-title function_">isInt32Array</span>(<span class="hljs-k…
64918-property">types</span>.<span class="hljs-title function_">isInt32Array</span>(<span class="hljs-k…
64924 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64925 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64927 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64928-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isMap</span>…
64934 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64935 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64937 <p>Returns <code>true</code> if the value is an iterator returned for a built-in
64938 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><cod…
64939 …e><code class="language-js"><span class="hljs-keyword">const</span> map = <span class="hljs-keywor…
64940-property">types</span>.<span class="hljs-title function_">isMapIterator</span>(map.<span class="h…
64941-property">types</span>.<span class="hljs-title function_">isMapIterator</span>(map.<span class="h…
64942-property">types</span>.<span class="hljs-title function_">isMapIterator</span>(map.<span class="h…
64943-property">types</span>.<span class="hljs-title function_">isMapIterator</span>(map[<span class="h…
64949 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64950 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64952 …is an instance of a <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects">…
64953 <!-- eslint-skip -->
64954 …uage-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> ns <span …
64956 …l.<span class="hljs-property">types</span>.<span class="hljs-title function_">isModuleNamespaceObj…
64962 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64963 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64965 <p>Returns <code>true</code> if the value is an instance of a built-in <a href="#errors_class_error…
64966-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isNativeErro…
64967-property">types</span>.<span class="hljs-title function_">isNativeError</span>(<span class="hljs-
64968-property">types</span>.<span class="hljs-title function_">isNativeError</span>(<span class="hljs-
64974 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64975 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64979-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isNumberObje…
64980-property">types</span>.<span class="hljs-title function_">isNumberObject</span>(<span class="hljs
64986 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64987 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64989 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64990-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isPromise</s…
64996 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64997 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64999 <p>Returns <code>true</code> if the value is a <a href="https://developer.mozilla.org/en-US/docs/We…
65000 <pre><code class="language-js"><span class="hljs-keyword">const</span> target = {};
65001 <span class="hljs-keyword">const</span> proxy = <span class="hljs-keyword">new</span> <span class="…
65002 util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isProxy</span>(tar…
65003 util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isProxy</span>(pro…
65009 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65010 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65013-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isRegExp</sp…
65014-property">types</span>.<span class="hljs-title function_">isRegExp</span>(<span class="hljs-keywo…
65020 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65021 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65023 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65024-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSet</span>…
65030 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65031 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65033 <p>Returns <code>true</code> if the value is an iterator returned for a built-in
65034 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set"><cod…
65035 …e><code class="language-js"><span class="hljs-keyword">const</span> set = <span class="hljs-keywor…
65036-property">types</span>.<span class="hljs-title function_">isSetIterator</span>(set.<span class="h…
65037-property">types</span>.<span class="hljs-title function_">isSetIterator</span>(set.<span class="h…
65038-property">types</span>.<span class="hljs-title function_">isSetIterator</span>(set.<span class="h…
65039-property">types</span>.<span class="hljs-title function_">isSetIterator</span>(set[<span class="h…
65045 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65046 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65048 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65049 This does <em>not</em> include <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
65051-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSharedArra…
65052-property">types</span>.<span class="hljs-title function_">isSharedArrayBuffer</span>(<span class=…
65058 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65059 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65063-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isStringObje…
65064-property">types</span>.<span class="hljs-title function_">isStringObject</span>(<span class="hljs
65070 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65071 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65075 …e class="language-js"><span class="hljs-keyword">const</span> symbol = <span class="hljs-title cla…
65076 util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSymbolObject</sp…
65077-property">types</span>.<span class="hljs-title function_">isSymbolObject</span>(<span class="hljs
65083 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65084 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65086 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65087-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isTypedArray…
65088-property">types</span>.<span class="hljs-title function_">isTypedArray</span>(<span class="hljs-k…
65089-property">types</span>.<span class="hljs-title function_">isTypedArray</span>(<span class="hljs-k…
65090 <p>See also <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objec…
65096 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65097 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65099 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65100-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint8Array…
65101-property">types</span>.<span class="hljs-title function_">isUint8Array</span>(<span class="hljs-k…
65102-property">types</span>.<span class="hljs-title function_">isUint8Array</span>(<span class="hljs-k…
65108 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65109 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65111 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65112-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint8Clamp…
65113-property">types</span>.<span class="hljs-title function_">isUint8ClampedArray</span>(<span class=…
65114-property">types</span>.<span class="hljs-title function_">isUint8ClampedArray</span>(<span class=…
65120 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65121 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65123 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65124-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint16Arra…
65125-property">types</span>.<span class="hljs-title function_">isUint16Array</span>(<span class="hljs-
65126-property">types</span>.<span class="hljs-title function_">isUint16Array</span>(<span class="hljs-
65132 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65133 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65135 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65136-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint32Arra…
65137-property">types</span>.<span class="hljs-title function_">isUint32Array</span>(<span class="hljs-
65138-property">types</span>.<span class="hljs-title function_">isUint32Array</span>(<span class="hljs-
65144 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65145 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65147 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65148-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isWeakMap</s…
65154 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65155 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65157 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65158-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isWeakSet</s…
65163 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65165 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65166 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65168 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65169-js"><span class="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> =…
65170-property">types</span>.<span class="hljs-title function_">isWebAssemblyCompiledModule</span>(<spa…
65178 …cumentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="https://developer.mozilla…
65180 <li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
65181 <li><code>source</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
65186 similar built-in functionality through <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
65191 …cumentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="https://developer.mozilla…
65193 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65194 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65196 <p>Alias for <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
65198 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65200 util.<span class="hljs-title function_">isArray</span>([]);
65201 <span class="hljs-comment">// Returns: true</span>
65202 util.<span class="hljs-title function_">isArray</span>(<span class="hljs-keyword">new</span> <span …
65203 <span class="hljs-comment">// Returns: true</span>
65204 util.<span class="hljs-title function_">isArray</span>({});
65205 <span class="hljs-comment">// Returns: false</span></code></pre>
65210 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65212 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65213 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65216 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65218 util.<span class="hljs-title function_">isBoolean</span>(<span class="hljs-number">1</span>);
65219 <span class="hljs-comment">// Returns: false</span>
65220 util.<span class="hljs-title function_">isBoolean</span>(<span class="hljs-number">0</span>);
65221 <span class="hljs-comment">// Returns: false</span>
65222 util.<span class="hljs-title function_">isBoolean</span>(<span class="hljs-literal">false</span>);
65223 <span class="hljs-comment">// Returns: true</span></code></pre>
65228 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
65230 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65231 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65234 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65236 util.<span class="hljs-title function_">isBuffer</span>({ <span class="hljs-attr">length</span>: <s…
65237 <span class="hljs-comment">// Returns: false</span>
65238 util.<span class="hljs-title function_">isBuffer</span>([]);
65239 <span class="hljs-comment">// Returns: false</span>
65240 …hljs-title function_">isBuffer</span>(<span class="hljs-title class_">Buffer</span>.<span class="h…
65241 <span class="hljs-comment">// Returns: true</span></code></pre>
65246 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
65248 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65249 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65252 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65254 util.<span class="hljs-title function_">isDate</span>(<span class="hljs-keyword">new</span> <span c…
65255 <span class="hljs-comment">// Returns: true</span>
65256 util.<span class="hljs-title function_">isDate</span>(<span class="hljs-title class_">Date</span>()…
65257 <span class="hljs-comment">// false (without 'new' returns a String)</span>
65258 util.<span class="hljs-title function_">isDate</span>({});
65259 <span class="hljs-comment">// Returns: false</span></code></pre>
65264 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
65266 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65267 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65271 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65273 util.<span class="hljs-title function_">isError</span>(<span class="hljs-keyword">new</span> <span …
65274 <span class="hljs-comment">// Returns: true</span>
65275 util.<span class="hljs-title function_">isError</span>(<span class="hljs-keyword">new</span> <span …
65276 <span class="hljs-comment">// Returns: true</span>
65277-title function_">isError</span>({ <span class="hljs-attr">name</span>: <span class="hljs-string">…
65278 <span class="hljs-comment">// Returns: false</span></code></pre>
65282 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65283-keyword">const</span> obj = { <span class="hljs-attr">name</span>: <span class="hljs-string">'Err…
65285 util.<span class="hljs-title function_">isError</span>(obj);
65286 <span class="hljs-comment">// Returns: false</span>
65287 obj[<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toStringTag</span>] =…
65288 util.<span class="hljs-title function_">isError</span>(obj);
65289 <span class="hljs-comment">// Returns: true</span></code></pre>
65294 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65296 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65297 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65301 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65303 <span class="hljs-keyword">function</span> <span class="hljs-title function_">Foo</span>(<span clas…
65304 <span class="hljs-keyword">const</span> <span class="hljs-title function_">Bar</span> = (<span clas…
65306 util.<span class="hljs-title function_">isFunction</span>({});
65307 <span class="hljs-comment">// Returns: false</span>
65308 util.<span class="hljs-title function_">isFunction</span>(<span class="hljs-title class_">Foo</span…
65309 <span class="hljs-comment">// Returns: true</span>
65310 util.<span class="hljs-title function_">isFunction</span>(<span class="hljs-title class_">Bar</span…
65311 <span class="hljs-comment">// Returns: true</span></code></pre>
65316 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65318 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65319 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65323 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65325 util.<span class="hljs-title function_">isNull</span>(<span class="hljs-number">0</span>);
65326 <span class="hljs-comment">// Returns: false</span>
65327 util.<span class="hljs-title function_">isNull</span>(<span class="hljs-literal">undefined</span>);
65328 <span class="hljs-comment">// Returns: false</span>
65329 util.<span class="hljs-title function_">isNull</span>(<span class="hljs-literal">null</span>);
65330 <span class="hljs-comment">// Returns: true</span></code></pre>
65335 …bility api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use
65338 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65339 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65343 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65345 util.<span class="hljs-title function_">isNullOrUndefined</span>(<span class="hljs-number">0</span>…
65346 <span class="hljs-comment">// Returns: false</span>
65347 util.<span class="hljs-title function_">isNullOrUndefined</span>(<span class="hljs-literal">undefin…
65348 <span class="hljs-comment">// Returns: true</span>
65349 util.<span class="hljs-title function_">isNullOrUndefined</span>(<span class="hljs-literal">null</s…
65350 <span class="hljs-comment">// Returns: true</span></code></pre>
65355 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65357 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65358 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65361 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65363 util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-literal">false</span>);
65364 <span class="hljs-comment">// Returns: false</span>
65365 util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-title class_">Infinity</s…
65366 <span class="hljs-comment">// Returns: true</span>
65367 util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-number">0</span>);
65368 <span class="hljs-comment">// Returns: true</span>
65369 util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-title class_">Na</span>N);
65370 <span class="hljs-comment">// Returns: true</span></code></pre>
65375 …_stability api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated:
65378 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65379 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65384 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65386 util.<span class="hljs-title function_">isObject</span>(<span class="hljs-number">5</span>);
65387 <span class="hljs-comment">// Returns: false</span>
65388 util.<span class="hljs-title function_">isObject</span>(<span class="hljs-literal">null</span>);
65389 <span class="hljs-comment">// Returns: false</span>
65390 util.<span class="hljs-title function_">isObject</span>({});
65391 <span class="hljs-comment">// Returns: true</span>
65392 util.<span class="hljs-title function_">isObject</span>(<span class="hljs-function">() =></span> {}…
65393 <span class="hljs-comment">// Returns: false</span></code></pre>
65398 …bility api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use
65402 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65403 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65407 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65409 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-number">5</span>);
65410 <span class="hljs-comment">// Returns: true</span>
65411 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-string">'foo'</span>);
65412 <span class="hljs-comment">// Returns: true</span>
65413 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-literal">false</span>);
65414 <span class="hljs-comment">// Returns: true</span>
65415 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-literal">null</span>);
65416 <span class="hljs-comment">// Returns: true</span>
65417 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-literal">undefined</sp…
65418 <span class="hljs-comment">// Returns: true</span>
65419 util.<span class="hljs-title function_">isPrimitive</span>({});
65420 <span class="hljs-comment">// Returns: false</span>
65421 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-function">() =></span>…
65422 <span class="hljs-comment">// Returns: false</span>
65423 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-regexp">/^$/</span>);
65424 <span class="hljs-comment">// Returns: false</span>
65425 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-keyword">new</span> <s…
65426 <span class="hljs-comment">// Returns: false</span></code></pre>
65431 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
65433 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65434 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65437 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65439 util.<span class="hljs-title function_">isRegExp</span>(<span class="hljs-regexp">/some regexp/</sp…
65440 <span class="hljs-comment">// Returns: true</span>
65441 …ss="hljs-title function_">isRegExp</span>(<span class="hljs-keyword">new</span> <span class="hljs-
65442 <span class="hljs-comment">// Returns: true</span>
65443 util.<span class="hljs-title function_">isRegExp</span>({});
65444 <span class="hljs-comment">// Returns: false</span></code></pre>
65449 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65451 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65452 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65455 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65457 util.<span class="hljs-title function_">isString</span>(<span class="hljs-string">''</span>);
65458 <span class="hljs-comment">// Returns: true</span>
65459 util.<span class="hljs-title function_">isString</span>(<span class="hljs-string">'foo'</span>);
65460 <span class="hljs-comment">// Returns: true</span>
65461 util.<span class="hljs-title function_">isString</span>(<span class="hljs-title class_">String</spa…
65462 <span class="hljs-comment">// Returns: true</span>
65463 util.<span class="hljs-title function_">isString</span>(<span class="hljs-number">5</span>);
65464 <span class="hljs-comment">// Returns: false</span></code></pre>
65469 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65471 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65472 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65475 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65477 util.<span class="hljs-title function_">isSymbol</span>(<span class="hljs-number">5</span>);
65478 <span class="hljs-comment">// Returns: false</span>
65479 util.<span class="hljs-title function_">isSymbol</span>(<span class="hljs-string">'foo'</span>);
65480 <span class="hljs-comment">// Returns: false</span>
65481 util.<span class="hljs-title function_">isSymbol</span>(<span class="hljs-title class_">Symbol</spa…
65482 <span class="hljs-comment">// Returns: true</span></code></pre>
65487 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65489 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65490 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65493 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65495 <span class="hljs-keyword">const</span> foo = <span class="hljs-literal">undefined</span>;
65496 util.<span class="hljs-title function_">isUndefined</span>(<span class="hljs-number">5</span>);
65497 <span class="hljs-comment">// Returns: false</span>
65498 util.<span class="hljs-title function_">isUndefined</span>(foo);
65499 <span class="hljs-comment">// Returns: true</span>
65500 util.<span class="hljs-title function_">isUndefined</span>(<span class="hljs-literal">null</span>);
65501 <span class="hljs-comment">// Returns: false</span></code></pre>
65506 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use a th…
65508 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65512 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65514 util.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Timestamped message.'…
65521 …<code class="language-js"><span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in"…
65527 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
65530 command-line flags, and detected CPU features. This is useful for determining
65533-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
65534 <span class="hljs-comment">// The value returned by v8.cachedDataVersionTag() is derived from the V…
65535 <span class="hljs-comment">// version, command-line flags, and detected CPU features. Test that the…
65536 <span class="hljs-comment">// does indeed update when flags are toggled.</span>
65537 v8.<span class="hljs-title function_">setFlagsFromString</span>(<span class="hljs-string">'--allow_…
65538-variable language_">console</span>.<span class="hljs-title function_">log</span>(v8.<span class="…
65544 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
65548 <li><code>code_and_metadata_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
65549 <li><code>bytecode_and_metadata_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/J…
65550 <li><code>external_script_source_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/…
65552 <!-- eslint-skip -->
65553 <pre><code class="language-js">{
65554 <span class="hljs-attr">code_and_metadata_size</span>: <span class="hljs-number">212208</span>,
65555 …<span class="hljs-attr">bytecode_and_metadata_size</span>: <span class="hljs-number">161368</span>,
65556 …<span class="hljs-attr">external_script_source_size</span>: <span class="hljs-number">1410794</spa…
65570 <pre><code class="language-js"><span class="hljs-comment">// Print heap snapshot to the console</sp…
65571 <span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class…
65572 <span class="hljs-keyword">const</span> stream = v8.<span class="hljs-title function_">getHeapSnaps…
65573 stream.<span class="hljs-title function_">pipe</span>(process.<span class="hljs-property">stdout</s…
65580 <td><p>Support values exceeding the 32-bit unsigned integer range.</p></td></tr>
65587 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
65592 <a href="https://v8docs.nodesource.com/node-13.2/d5/dda/classv8_1_1_isolate.html#ac673576f24fdc7a33…
65596 <li><code>space_name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
65597 <li><code>space_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
65598 <li><code>space_used_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
65599 <li><code>space_available_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
65600 <li><code>physical_space_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
65602 <pre><code class="language-json"><span class="hljs-punctuation">[</span>
65603 <span class="hljs-punctuation">{</span>
65604 … class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
65605 …an class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-
65606 … class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hl…
65607 …lass="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class=…
65608 …<span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span …
65609 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
65610 <span class="hljs-punctuation">{</span>
65611 … class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
65612 …an class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-
65613 … class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hl…
65614 … class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span clas…
65615 …<span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span …
65616 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
65617 <span class="hljs-punctuation">{</span>
65618 … class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
65619 …an class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-
65620 … class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hl…
65621 …class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class…
65622 …<span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span …
65623 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
65624 <span class="hljs-punctuation">{</span>
65625 … class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
65626 …an class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-
65627 … class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hl…
65628 … class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span clas…
65629 …<span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span …
65630 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
65631 <span class="hljs-punctuation">{</span>
65632 …ss="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
65633 …<span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hl…
65634 …pan class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class=…
65635 …ss="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="h…
65636 …<span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span …
65637 <span class="hljs-punctuation">}</span>
65638 <span class="hljs-punctuation">]</span></code></pre>
65645 <td><p>Support values exceeding the 32-bit unsigned integer range.</p></td></tr>
65654 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
65658 <li><code>total_heap_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
65659 <li><code>total_heap_size_executable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/J…
65660 <li><code>total_physical_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
65661 <li><code>total_available_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
65662 <li><code>used_heap_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
65663 <li><code>heap_size_limit</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
65664 <li><code>malloced_memory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
65665 <li><code>peak_malloced_memory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
65666 <li><code>does_zap_garbage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
65667 <li><code>number_of_native_contexts</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Ja…
65668 <li><code>number_of_detached_contexts</code> <a href="https://developer.mozilla.org/en-US/docs/Web/…
65671 <code>--zap_code_space</code> option is enabled or not. This makes V8 overwrite heap
65676 top-level contexts currently active. Increase of this number over time indicates
65680 being non-zero indicates a potential memory leak.</p>
65681 <!-- eslint-skip -->
65682 <pre><code class="language-js">{
65683 <span class="hljs-attr">total_heap_size</span>: <span class="hljs-number">7326976</span>,
65684 …<span class="hljs-attr">total_heap_size_executable</span>: <span class="hljs-number">4194304</span…
65685 <span class="hljs-attr">total_physical_size</span>: <span class="hljs-number">7326976</span>,
65686 <span class="hljs-attr">total_available_size</span>: <span class="hljs-number">1152656</span>,
65687 <span class="hljs-attr">used_heap_size</span>: <span class="hljs-number">3476208</span>,
65688 <span class="hljs-attr">heap_size_limit</span>: <span class="hljs-number">1535115264</span>,
65689 <span class="hljs-attr">malloced_memory</span>: <span class="hljs-number">16384</span>,
65690 <span class="hljs-attr">peak_malloced_memory</span>: <span class="hljs-number">1127496</span>,
65691 <span class="hljs-attr">does_zap_garbage</span>: <span class="hljs-number">0</span>,
65692 <span class="hljs-attr">number_of_native_contexts</span>: <span class="hljs-number">1</span>,
65693 <span class="hljs-attr">number_of_detached_contexts</span>: <span class="hljs-number">0</span>
65700 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65703 V8 command-line flags. This method should be used with care. Changing settings
65707 <code>node --v8-options</code>.</p>
65709 <pre><code class="language-js"><span class="hljs-comment">// Print GC events to stdout for one minu…
65710 <span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class…
65711 v8.<span class="hljs-title function_">setFlagsFromString</span>(<span class="hljs-string">'--trace_…
65712-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { v8.<span class="hljs-title…
65737 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
65739 <code>'Heap-${yyyymmdd}-${hhmmss}-${pid}-${thread_id}.heapsnapshot'</code> will be
65743 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
65752 …s="language-js"><span class="hljs-keyword">const</span> { writeHeapSnapshot } = <span class="hljs-
65753 <span class="hljs-keyword">const</span> {
65754 <span class="hljs-title class_">Worker</span>,
65757 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'worker_threads'</span>);
65759 <span class="hljs-keyword">if</span> (isMainThread) {
65760 …<span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class…
65762 …span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span cl…
65763 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class=…
65764 <span class="hljs-comment">// Now get a heapdump for the main thread.</span>
65765 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
65768 <span class="hljs-comment">// Tell the worker to create a heapdump.</span>
65769 …worker.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'heapdump'<…
65770 } <span class="hljs-keyword">else</span> {
65771 …span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span cl…
65772 <span class="hljs-keyword">if</span> (message === <span class="hljs-string">'heapdump'</span>) {
65773 <span class="hljs-comment">// Generate a heapdump for the worker</span>
65774 <span class="hljs-comment">// and return the filename to the parent.</span>
65775 …parentPort.<span class="hljs-title function_">postMessage</span>(<span class="hljs-title function_…
65781 that is compatible with the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_A…
65782 <p>The format is backward-compatible (i.e. safe to store to disk).
65789 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65798 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
65812 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65826 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&…
65827 <li><code>arrayBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer…
65834 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65836 <p>Write a raw 32-bit unsigned integer.
65840 <li><code>hi</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
65841 <li><code>lo</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
65843 <p>Write a raw 64-bit unsigned integer, split into high and low 32-bit parts.
65847 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65853 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
65860 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
65869 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
65877 <li><code>sharedArrayBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
65880 <code>SharedArrayBuffer</code> object. It must return an unsigned 32-bit integer ID for
65889 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
65899 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
65911 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&…
65912 …rg/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer>…
65920 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
65927 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
65929 <p>Read a raw 32-bit unsigned integer and return it.
65933 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
65935 <p>Read a raw 64-bit unsigned integer and return it as an array <code>[hi, lo]</code>
65936 with two 32-bit unsigned integer entries.
65940 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
65946 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65975 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
65989 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
65991 <span class="hljs-keyword">const</span> x = <span class="hljs-number">1</span>;
65993 <span class="hljs-keyword">const</span> context = { <span class="hljs-attr">x</span>: <span class="…
65994 vm.<span class="hljs-title function_">createContext</span>(context); <span class="hljs-comment">// …
65996 <span class="hljs-keyword">const</span> code = <span class="hljs-string">'x += 40; var y = 17;'</sp…
65997 <span class="hljs-comment">// `x` and `y` are global variables in the context.</span>
65998 <span class="hljs-comment">// Initially, x has the value 2 because that is the value of context.x.<…
65999 vm.<span class="hljs-title function_">runInContext</span>(code, context);
66001 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(context.<span…
66002 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(context.<span…
66004 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
66026 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66027 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
66029 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
66031 <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
66033 …developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;nu…
66035 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
66039 <li><code>produceCachedData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
66047 <li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
66053 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
66055 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="t…
66077-js"><span class="hljs-keyword">const</span> script = <span class="hljs-keyword">new</span> vm.<sp…
66085 <span class="hljs-keyword">const</span> cacheWithoutX = script.<span class="hljs-title function_">c…
66087 script.<span class="hljs-title function_">runInThisContext</span>();
66089 <span class="hljs-keyword">const</span> cacheWithX = script.<span class="hljs-title function_">crea…
66103 <li><code>contextifiedObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
66105 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66107 <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66110 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66113 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66120 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
66128 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
66130 <span class="hljs-keyword">const</span> context = {
66131 <span class="hljs-attr">animal</span>: <span class="hljs-string">'cat'</span>,
66132 <span class="hljs-attr">count</span>: <span class="hljs-number">2</span>
66135 …ss="hljs-keyword">const</span> script = <span class="hljs-keyword">new</span> vm.<span class="hljs
66137 vm.<span class="hljs-title function_">createContext</span>(context);
66138 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
66139 script.<span class="hljs-title function_">runInContext</span>(context);
66142 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
66143 <span class="hljs-comment">// Prints: { animal: 'cat', count: 12, name: 'kitty' }</span></code></pr…
66145 and corresponding threads being started, which have a non-zero performance
66164 <li><code>contextObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
66166 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66168 <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66171 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66174 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66179 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&…
66182 …ozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <…
66188 <li><code>contextCodeGeneration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
66190 <li><code>strings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66193 <li><code>wasm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66197 <li><code>microtaskMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66203 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
66211 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
66213 …ss="hljs-keyword">const</span> script = <span class="hljs-keyword">new</span> vm.<span class="hljs
66215 <span class="hljs-keyword">const</span> contexts = [{}, {}, {}];
66216 contexts.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class…
66217 script.<span class="hljs-title function_">runInNewContext</span>(context);
66220 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
66221 <span class="hljs-comment">// Prints: [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'se…
66235 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66237 <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66240 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66243 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66250 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
66257 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
66259 <span class="hljs-variable language_">global</span>.<span class="hljs-property">globalVar</span> = …
66261-keyword">const</span> script = <span class="hljs-keyword">new</span> vm.<span class="hljs-title c…
66263 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
66264 script.<span class="hljs-title function_">runInThisContext</span>();
66267 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
66269 <span class="hljs-comment">// 1000</span></code></pre>
66274 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
66275 <p>This feature is only available with the <code>--experimental-vm-modules</code> command
66277 <p>The <code>vm.Module</code> class provides a low-level interface for using
66279 class that closely mirrors <a href="https://www.ecma-international.org/ecma-262/#sec-abstract-modul…
66292-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
66294 <span class="hljs-keyword">const</span> contextifiedObject = vm.<span class="hljs-title function_">…
66295 <span class="hljs-attr">secret</span>: <span class="hljs-number">42</span>,
66296 …<span class="hljs-attr">print</span>: <span class="hljs-variable language_">console</span>.<span c…
66299 <span class="hljs-comment">// Step 1</span>
66300 <span class="hljs-comment">//</span>
66301 <span class="hljs-comment">// Create a Module by constructing a new `vm.SourceTextModule` object. T…
66302 <span class="hljs-comment">// parses the provided source text, throwing a `SyntaxError` if anything…
66303 <span class="hljs-comment">// wrong. By default, a Module is created in the top context. But here, …
66304 <span class="hljs-comment">// specify `contextifiedObject` as the context this Module belongs to.</…
66305 <span class="hljs-comment">//</span>
66306 <span class="hljs-comment">// Here, we attempt to obtain the default export from the module "foo", …
66307 <span class="hljs-comment">// put it into local binding "secret".</span>
66309 …hljs-keyword">const</span> bar = <span class="hljs-keyword">new</span> vm.<span class="hljs-title …
66313 `</span>, { <span class="hljs-attr">context</span>: contextifiedObject });
66315 <span class="hljs-comment">// Step 2</span>
66316 <span class="hljs-comment">//</span>
66317 <span class="hljs-comment">// "Link" the imported dependencies of this Module to it.</span>
66318 <span class="hljs-comment">//</span>
66319 <span class="hljs-comment">// The provided linking callback (the "linker") accepts two arguments: t…
66320 <span class="hljs-comment">// parent module (`bar` in this case) and the string that is the specifi…
66321 <span class="hljs-comment">// the imported module. The callback is expected to return a Module that…
66322 <span class="hljs-comment">// corresponds to the provided specifier, with certain requirements docu…
66323 <span class="hljs-comment">// in `module.link()`.</span>
66324 <span class="hljs-comment">//</span>
66325 <span class="hljs-comment">// If linking has not started for the returned Module, the same linker</…
66326 <span class="hljs-comment">// callback will be called on the returned Module.</span>
66327 <span class="hljs-comment">//</span>
66328 <span class="hljs-comment">// Even top-level Modules without dependencies must be explicitly linked…
66329 <span class="hljs-comment">// callback provided would never be called, however.</span>
66330 <span class="hljs-comment">//</span>
66331 <span class="hljs-comment">// The link() method returns a Promise that will be resolved when all th…
66332 <span class="hljs-comment">// Promises returned by the linker resolve.</span>
66333 <span class="hljs-comment">//</span>
66334 <span class="hljs-comment">// Note: This is a contrived example in that the linker function creates…
66335 <span class="hljs-comment">// "foo" module every time it is called. In a full-fledged module system…
66336 <span class="hljs-comment">// cache would probably be used to avoid duplicated modules.</span>
66338 …lass="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-titl…
66339 <span class="hljs-keyword">if</span> (specifier === <span class="hljs-string">'foo'</span>) {
66340 …s="hljs-keyword">return</span> <span class="hljs-keyword">new</span> vm.<span class="hljs-title cl…
66344 …`</span>, { <span class="hljs-attr">context</span>: referencingModule.<span class="hljs-property">…
66346 …<span class="hljs-comment">// Using `contextifiedObject` instead of `referencingModule.context`</s…
66347 <span class="hljs-comment">// here would work as well.</span>
66349-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error…
66351 <span class="hljs-keyword">await</span> bar.<span class="hljs-title function_">link</span>(linker);
66353 <span class="hljs-comment">// Step 3</span>
66354 <span class="hljs-comment">//</span>
66355 <span class="hljs-comment">// Evaluate the Module. The evaluate() method returns a promise which wi…
66356 <span class="hljs-comment">// resolve after the module has finished evaluating.</span>
66358 <span class="hljs-comment">// Prints 42.</span>
66359-keyword">await</span> bar.evaluate();</code><code class="language-js cjs"><span class="hljs-keywo…
66361 <span class="hljs-keyword">const</span> contextifiedObject = vm.<span class="hljs-title function_">…
66362 <span class="hljs-attr">secret</span>: <span class="hljs-number">42</span>,
66363 …<span class="hljs-attr">print</span>: <span class="hljs-variable language_">console</span>.<span c…
66366 (<span class="hljs-keyword">async</span> () => {
66367 <span class="hljs-comment">// Step 1</span>
66368 <span class="hljs-comment">//</span>
66369 …<span class="hljs-comment">// Create a Module by constructing a new `vm.SourceTextModule` object. …
66370 …<span class="hljs-comment">// parses the provided source text, throwing a `SyntaxError` if anythin…
66371 …<span class="hljs-comment">// wrong. By default, a Module is created in the top context. But here,…
66372 …<span class="hljs-comment">// specify `contextifiedObject` as the context this Module belongs to.<…
66373 <span class="hljs-comment">//</span>
66374 …<span class="hljs-comment">// Here, we attempt to obtain the default export from the module "foo",…
66375 <span class="hljs-comment">// put it into local binding "secret".</span>
66377 …hljs-keyword">const</span> bar = <span class="hljs-keyword">new</span> vm.<span class="hljs-title …
66381 `</span>, { <span class="hljs-attr">context</span>: contextifiedObject });
66383 <span class="hljs-comment">// Step 2</span>
66384 <span class="hljs-comment">//</span>
66385 <span class="hljs-comment">// "Link" the imported dependencies of this Module to it.</span>
66386 <span class="hljs-comment">//</span>
66387 …<span class="hljs-comment">// The provided linking callback (the "linker") accepts two arguments: …
66388 …<span class="hljs-comment">// parent module (`bar` in this case) and the string that is the specif…
66389 …<span class="hljs-comment">// the imported module. The callback is expected to return a Module tha…
66390 …<span class="hljs-comment">// corresponds to the provided specifier, with certain requirements doc…
66391 <span class="hljs-comment">// in `module.link()`.</span>
66392 <span class="hljs-comment">//</span>
66393 …<span class="hljs-comment">// If linking has not started for the returned Module, the same linker<…
66394 <span class="hljs-comment">// callback will be called on the returned Module.</span>
66395 <span class="hljs-comment">//</span>
66396 …<span class="hljs-comment">// Even top-level Modules without dependencies must be explicitly linke…
66397 <span class="hljs-comment">// callback provided would never be called, however.</span>
66398 <span class="hljs-comment">//</span>
66399 …<span class="hljs-comment">// The link() method returns a Promise that will be resolved when all t…
66400 <span class="hljs-comment">// Promises returned by the linker resolve.</span>
66401 <span class="hljs-comment">//</span>
66402 …<span class="hljs-comment">// Note: This is a contrived example in that the linker function create…
66403 …<span class="hljs-comment">// "foo" module every time it is called. In a full-fledged module syste…
66404 <span class="hljs-comment">// cache would probably be used to avoid duplicated modules.</span>
66406 …lass="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-titl…
66407 <span class="hljs-keyword">if</span> (specifier === <span class="hljs-string">'foo'</span>) {
66408 …s="hljs-keyword">return</span> <span class="hljs-keyword">new</span> vm.<span class="hljs-title cl…
66412 …`</span>, { <span class="hljs-attr">context</span>: referencingModule.<span class="hljs-property">…
66414 …<span class="hljs-comment">// Using `contextifiedObject` instead of `referencingModule.context`</s…
66415 <span class="hljs-comment">// here would work as well.</span>
66417-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error…
66419 …<span class="hljs-keyword">await</span> bar.<span class="hljs-title function_">link</span>(linker);
66421 <span class="hljs-comment">// Step 3</span>
66422 <span class="hljs-comment">//</span>
66423 …<span class="hljs-comment">// Evaluate the Module. The evaluate() method returns a promise which w…
66424 <span class="hljs-comment">// resolve after the module has finished evaluating.</span>
66426 <span class="hljs-comment">// Prints 42.</span>
66427 <span class="hljs-keyword">await</span> bar.evaluate();
66431 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
66435 …uestedModules]]</code> field of <a href="https://tc39.es/ecma262/#sec-cyclic-module-records">Cycli…
66439 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" cl…
66446 …aluationError]]</code> field of <a href="https://tc39.es/ecma262/#sec-cyclic-module-records">Cycli…
66450 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66452 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66455 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66462 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
66468 (<code>module.status</code> is <code>'evaluated'</code>) or it will re-throw the exception that the
66472 …/tc39.es/ecma262/#sec-moduleevaluation">Evaluate() concrete method</a> field of <a href="https://t…
66476 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
66481 <li><code>linker</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
66484 <p><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
66485 …<code class="language-js mjs"><span class="hljs-keyword">import</span> foo <span class="hljs-keywo…
66486 <span class="hljs-comment">// ^^^^^ the module specifier</span></code></pre>
66492 …lass="type">&#x3C;vm.Module></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
66496 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
66513 <p>The linker function roughly corresponds to the implementation-defined
66514 <a href="https://tc39.es/ecma262/#sec-hostresolveimportedmodule">HostResolveImportedModule</a> abst…
66518 <a href="https://tc39.es/ecma262/#sec-hostresolveimportedmodule">HostResolveImportedModule</a> is s…
66520 <p>The actual <a href="https://tc39.es/ecma262/#sec-hostresolveimportedmodule">HostResolveImportedM…
66523 <a href="https://tc39.es/ecma262/#sec-hostresolveimportedmodule">HostResolveImportedModule</a> impl…
66525 …c39.es/ecma262/#sec-moduledeclarationlinking">Link() concrete method</a> field of <a href="https:/…
66529 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
66533 <p>Corresponds to the <a href="https://tc39.es/ecma262/#sec-getmodulenamespace">GetModuleNamespace<…
66537 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
66564 <a href="https://tc39.es/ecma262/#sec-cyclic-module-records">Cyclic Module Record</a>'s <code>[[Sta…
66571 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
66572 <p>This feature is only available with the <code>--experimental-vm-modules</code> command
66577 …le</code> class provides the <a href="https://tc39.es/ecma262/#sec-source-text-module-records">Sou…
66581 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66584 <li><code>identifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
66585 <strong>Default:</strong> <code>'vm:module(i)'</code> where <code>i</code> is a context-specific as…
66587 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
66591 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66593 <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
66595 …developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;in…
66597 <li><code>initializeImportMeta</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
66604 <li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
66608 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
66610 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="t…
66623-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
66625-keyword">const</span> contextifiedObject = vm.<span class="hljs-title function_">createContext</s…
66627 …ss="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = <span class="…
66628 <span class="hljs-string">'Object.getPrototypeOf(import.meta.prop).secret = secret;'</span>,
66630 …<span class="hljs-title function_">initializeImportMeta</span>(<span class="hljs-params">meta</spa…
66631 <span class="hljs-comment">// Note: this object is created in the top context. As such,</span>
66632 <span class="hljs-comment">// Object.getPrototypeOf(import.meta.prop) points to the</span>
66633 <span class="hljs-comment">// Object.prototype in the top context rather than that in</span>
66634 <span class="hljs-comment">// the contextified object.</span>
66635 meta.<span class="hljs-property">prop</span> = {};
66638 <span class="hljs-comment">// Since module has no dependencies, the linker function will never be c…
66639 …="hljs-keyword">await</span> <span class="hljs-variable language_">module</span>.<span class="hljs
66640 <span class="hljs-keyword">await</span> <span class="hljs-variable language_">module</span>.evaluat…
66642 <span class="hljs-comment">// Now, Object.prototype.secret will be equal to 42.</span>
66643 <span class="hljs-comment">//</span>
66644 <span class="hljs-comment">// To fix this problem, replace</span>
66645 <span class="hljs-comment">// meta.prop = {};</span>
66646 <span class="hljs-comment">// above with</span>
66647-comment">// meta.prop = vm.runInContext('{}', contextifiedObject);</span></code><code class="…
66648-keyword">const</span> contextifiedObject = vm.<span class="hljs-title function_">createContext</s…
66649 (<span class="hljs-keyword">async</span> () => {
66650 …ss="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = <span class="…
66651 <span class="hljs-string">'Object.getPrototypeOf(import.meta.prop).secret = secret;'</span>,
66653 …<span class="hljs-title function_">initializeImportMeta</span>(<span class="hljs-params">meta</spa…
66654 … <span class="hljs-comment">// Note: this object is created in the top context. As such,</span>
66655 <span class="hljs-comment">// Object.getPrototypeOf(import.meta.prop) points to the</span>
66656 <span class="hljs-comment">// Object.prototype in the top context rather than that in</span>
66657 <span class="hljs-comment">// the contextified object.</span>
66658 meta.<span class="hljs-property">prop</span> = {};
66661 …<span class="hljs-comment">// Since module has no dependencies, the linker function will never be …
66662 …="hljs-keyword">await</span> <span class="hljs-variable language_">module</span>.<span class="hljs
66663 …<span class="hljs-keyword">await</span> <span class="hljs-variable language_">module</span>.evalua…
66664 <span class="hljs-comment">// Now, Object.prototype.secret will be equal to 42.</span>
66665 <span class="hljs-comment">//</span>
66666 <span class="hljs-comment">// To fix this problem, replace</span>
66667 <span class="hljs-comment">// meta.prop = {};</span>
66668 <span class="hljs-comment">// above with</span>
66669 <span class="hljs-comment">// meta.prop = vm.runInContext('{}', contextifiedObject);</span>
66681 <pre><code class="language-js"><span class="hljs-comment">// Create an initial module</span>
66682-keyword">const</span> <span class="hljs-variable language_">module</span> = <span class="hljs-key…
66684 <span class="hljs-comment">// Create cached data from this module</span>
66685 <span class="hljs-keyword">const</span> cachedData = <span class="hljs-variable language_">module</…
66687 <span class="hljs-comment">// Create a new module using the cached data. The code must be the same.…
66688 …js-keyword">const</span> module2 = <span class="hljs-keyword">new</span> vm.<span class="hljs-titl…
66693 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
66694 <p>This feature is only available with the <code>--experimental-vm-modules</code> command
66699 …> class provides the <a href="https://heycam.github.io/webidl/#synthetic-module-records">Synthetic…
66701 provide a generic interface for exposing non-JavaScript sources to ECMAScript
66703 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
66705 <span class="hljs-keyword">const</span> source = <span class="hljs-string">'{ "a": 1 }'</span>;
66706-keyword">const</span> <span class="hljs-variable language_">module</span> = <span class="hljs-key…
66707 …<span class="hljs-keyword">const</span> obj = <span class="hljs-variable constant_">JSON</span>.<s…
66708 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setExport</sp…
66711 <span class="hljs-comment">// Use `module` in linking...</span></code></pre>
66717 <li><code>exportNames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
66718 <li><code>evaluateCallback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
66721 <li><code>identifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
66723 <strong>Default:</strong> <code>'vm:module(i)'</code> where <code>i</code> is a context-specific as…
66726 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66740 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66741 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
66747-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
66749-keyword">const</span> m = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_…
66750 …m.<span class="hljs-title function_">setExport</span>(<span class="hljs-string">'x'</span>, <span …
66753 <span class="hljs-keyword">await</span> m.<span class="hljs-title function_">link</span>(<span clas…
66754 <span class="hljs-keyword">await</span> m.evaluate();
66756-title function_">strictEqual</span>(m.<span class="hljs-property">namespace</span>.<span class="h…
66757 (<span class="hljs-keyword">async</span> () => {
66758-keyword">const</span> m = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_…
66759 …m.<span class="hljs-title function_">setExport</span>(<span class="hljs-string">'x'</span>, <span …
66761 …<span class="hljs-keyword">await</span> m.<span class="hljs-title function_">link</span>(<span cla…
66762 <span class="hljs-keyword">await</span> m.evaluate();
66763 …="hljs-title function_">strictEqual</span>(m.<span class="hljs-property">namespace</span>.<span cl…
66780 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66781 <li><code>params</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
66783 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66785 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
66787 <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
66789 …developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;nu…
66791 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
66794 <li><code>produceCachedData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
66796 <li><code>parsingContext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
66798 <li><code>contextExtensions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
66803 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
66825 <li><code>contextObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
66826 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66828 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&…
66831 …ozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <…
66837 <li><code>codeGeneration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
66839 <li><code>strings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66842 <li><code>wasm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66846 <li><code>microtaskMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66852 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
66858 properties but also having the built-in objects and functions any standard
66861 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
66863 <span class="hljs-variable language_">global</span>.<span class="hljs-property">globalVar</span> = …
66865 <span class="hljs-keyword">const</span> context = { <span class="hljs-attr">globalVar</span>: <span…
66866 vm.<span class="hljs-title function_">createContext</span>(context);
66868 vm.<span class="hljs-title function_">runInContext</span>(<span class="hljs-string">'globalVar *= 2…
66870 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
66871 <span class="hljs-comment">// Prints: { globalVar: 2 }</span>
66873-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
66874 <span class="hljs-comment">// Prints: 3</span></code></pre>
66889 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
66890 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
66898 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
66902 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66904 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66909 <li><code>execution</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
66917 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
66927 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
66928 <span class="hljs-comment">// Measure the memory used by the main context.</span>
66929 vm.<span class="hljs-title function_">measureMemory</span>({ <span class="hljs-attr">mode</span>: <…
66930 <span class="hljs-comment">// This is the same as vm.measureMemory()</span>
66931 ….<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-par…
66932 <span class="hljs-comment">// The current format is:</span>
66933 <span class="hljs-comment">// {</span>
66934 <span class="hljs-comment">// total: {</span>
66935 …<span class="hljs-comment">// jsMemoryEstimate: 2418479, jsMemoryRange: [ 2418479, 2745799 ]<…
66936 <span class="hljs-comment">// }</span>
66937 <span class="hljs-comment">// }</span>
66938 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
66941 …ljs-keyword">const</span> context = vm.<span class="hljs-title function_">createContext</span>({ <…
66942-title function_">measureMemory</span>({ <span class="hljs-attr">mode</span>: <span class="hljs-st…
66943 ….<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-par…
66944 <span class="hljs-comment">// Reference the context here so that it won't be GC'ed</span>
66945 <span class="hljs-comment">// until the measurement is complete.</span>
66946 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
66947 <span class="hljs-comment">// {</span>
66948 <span class="hljs-comment">// total: {</span>
66949 <span class="hljs-comment">// jsMemoryEstimate: 2574732,</span>
66950 <span class="hljs-comment">// jsMemoryRange: [ 2574732, 2904372 ]</span>
66951 <span class="hljs-comment">// },</span>
66952 <span class="hljs-comment">// current: {</span>
66953 <span class="hljs-comment">// jsMemoryEstimate: 2438996,</span>
66954 <span class="hljs-comment">// jsMemoryRange: [ 2438996, 2768636 ]</span>
66955 <span class="hljs-comment">// },</span>
66956 <span class="hljs-comment">// other: [</span>
66957 <span class="hljs-comment">// {</span>
66958 <span class="hljs-comment">// jsMemoryEstimate: 135736,</span>
66959 <span class="hljs-comment">// jsMemoryRange: [ 135736, 465376 ]</span>
66960 <span class="hljs-comment">// }</span>
66961 <span class="hljs-comment">// ]</span>
66962 <span class="hljs-comment">// }</span>
66963 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
66978 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66979 <li><code>contextifiedObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
66981 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
66983 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
66985 <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
66987 …developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;nu…
66989 <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66992 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66995 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
67000 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
67004 <li><code>produceCachedData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
67012 <li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
67018 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
67020 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="t…
67027 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
67036 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
67038 <span class="hljs-keyword">const</span> contextObject = { <span class="hljs-attr">globalVar</span>:…
67039 vm.<span class="hljs-title function_">createContext</span>(contextObject);
67041 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
67042 …vm.<span class="hljs-title function_">runInContext</span>(<span class="hljs-string">'globalVar *= …
67044 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67045 <span class="hljs-comment">// Prints: { globalVar: 1024 }</span></code></pre>
67063 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
67064 <li><code>contextObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
67066 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
67068 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
67070 <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
67072 …developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;nu…
67074 <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
67077 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
67080 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
67085 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&…
67088 …ozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <…
67094 <li><code>contextCodeGeneration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
67096 <li><code>strings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
67099 <li><code>wasm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
67103 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
67107 <li><code>produceCachedData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
67115 <li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
67121 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
67123 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="t…
67128 <li><code>microtaskMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
67134 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
67143 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
67145 <span class="hljs-keyword">const</span> contextObject = {
67146 <span class="hljs-attr">animal</span>: <span class="hljs-string">'cat'</span>,
67147 <span class="hljs-attr">count</span>: <span class="hljs-number">2</span>
67150 vm.<span class="hljs-title function_">runInNewContext</span>(<span class="hljs-string">'count += 1;…
67151 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67152 <span class="hljs-comment">// Prints: { animal: 'cat', count: 3, name: 'kitty' }</span></code></pre>
67166 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
67167 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
67169 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
67171 <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
67173 …developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;nu…
67175 <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
67178 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
67181 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
67186 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
67190 <li><code>produceCachedData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
67198 <li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
67204 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
67206 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="t…
67213 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
67220 the JavaScript <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Ob…
67221 <!-- eslint-disable prefer-const -->
67222 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
67223 <span class="hljs-keyword">let</span> localVar = <span class="hljs-string">'initial value'</span>;
67225 <span class="hljs-keyword">const</span> vmResult = vm.<span class="hljs-title function_">runInThisC…
67226-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
67227 <span class="hljs-comment">// Prints: vmResult: 'vm', localVar: 'initial value'</span>
67229 <span class="hljs-keyword">const</span> evalResult = <span class="hljs-built_in">eval</span>(<span …
67230-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
67231 <span class="hljs-comment">// Prints: evalResult: 'eval', localVar: 'eval'</span></code></pre>
67233 <code>localVar</code> is unchanged. In contrast, <a href="https://developer.mozilla.org/en-US/docs/…
67244 <pre><code class="language-js"><span class="hljs-meta">'use strict'</span>;
67245 <span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class…
67247 <span class="hljs-keyword">const</span> code = <span class="hljs-string">`
67252 response.writeHead(200, { 'Content-Type': 'text/plain' });
67259 vm.<span class="hljs-title function_">runInThisContext</span>(code)(<span class="hljs-built_in">req…
67272 (or a newly-created object if <code>contextObject</code> is <code>undefined</code>) is associated
67287 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
67289 <span class="hljs-keyword">function</span> <span class="hljs-title function_">loop</span>(<span cla…
67290 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
67291-keyword">while</span> (<span class="hljs-number">1</span>) <span class="hljs-variable language_">…
67294 vm.<span class="hljs-title function_">runInNewContext</span>(
67295 <span class="hljs-string">'Promise.resolve().then(() => loop());'</span>,
67296 { loop, <span class="hljs-variable language_">console</span> },
67297 { <span class="hljs-attr">timeout</span>: <span class="hljs-number">5</span> }
67299 <span class="hljs-comment">// This is printed *before* 'entering loop' (!)</span>
67300 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67303 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
67305 <span class="hljs-keyword">function</span> <span class="hljs-title function_">loop</span>(<span cla…
67306-keyword">while</span> (<span class="hljs-number">1</span>) <span class="hljs-variable language_">…
67309 vm.<span class="hljs-title function_">runInNewContext</span>(
67310 <span class="hljs-string">'Promise.resolve().then(() => loop());'</span>,
67311 { loop, <span class="hljs-variable language_">console</span> },
67312 …pan class="hljs-attr">timeout</span>: <span class="hljs-number">5</span>, <span class="hljs-attr">…
67331 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
67335 underlying operating system via a collection of POSIX-like functions.</p>
67337-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
67338 …ass="hljs-keyword">import</span> { <span class="hljs-variable constant_">WASI</span> } <span class…
67340 <span class="hljs-keyword">const</span> wasi = <span class="hljs-keyword">new</span> <span class="h…
67341 <span class="hljs-attr">args</span>: process.<span class="hljs-property">argv</span>,
67342 <span class="hljs-attr">env</span>: process.<span class="hljs-property">env</span>,
67343 <span class="hljs-attr">preopens</span>: {
67344 …<span class="hljs-string">'/sandbox'</span>: <span class="hljs-string">'/some/real/path/that/wasm/…
67347 <span class="hljs-keyword">const</span> importObject = { <span class="hljs-attr">wasi_snapshot_prev…
67349-keyword">const</span> wasm = <span class="hljs-keyword">await</span> <span class="hljs-title clas…
67350 …"hljs-keyword">const</span> instance = <span class="hljs-keyword">await</span> <span class="hljs-t…
67352 wasi.<span class="hljs-title function_">start</span>(instance);</code><code class="language-js cjs"…
67353 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
67354 …="hljs-keyword">const</span> { <span class="hljs-variable constant_">WASI</span> } = <span class="…
67355 <span class="hljs-keyword">const</span> wasi = <span class="hljs-keyword">new</span> <span class="h…
67356 <span class="hljs-attr">args</span>: process.<span class="hljs-property">argv</span>,
67357 <span class="hljs-attr">env</span>: process.<span class="hljs-property">env</span>,
67358 <span class="hljs-attr">preopens</span>: {
67359 …<span class="hljs-string">'/sandbox'</span>: <span class="hljs-string">'/some/real/path/that/wasm/…
67362 <span class="hljs-keyword">const</span> importObject = { <span class="hljs-attr">wasi_snapshot_prev…
67364 (<span class="hljs-keyword">async</span> () => {
67365-keyword">const</span> wasm = <span class="hljs-keyword">await</span> <span class="hljs-title clas…
67366 …"hljs-keyword">const</span> instance = <span class="hljs-keyword">await</span> <span class="hljs-t…
67368 wasi.<span class="hljs-title function_">start</span>(instance);
67372 <pre><code class="language-text">(module
67375 ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
67387 …(i32.store (i32.const 0) (i32.const 8)) ;; iov.iov_base - This is a pointer to the start of the '…
67388 …(i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string
67391 (i32.const 1) ;; file_descriptor - 1 for stdout
67392 … (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
67393 (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
67394 (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
67400 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">wa…
67401 <p>The <code>--experimental-wasi-unstable-preview1</code> and <code>--experimental-wasm-bigint</cod…
67408 methods for working with WASI-based applications. Each <code>WASI</code> instance
67410 instance must have its command-line arguments, environment variables, and
67417 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
67419 <li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
67420 see as command-line arguments. The first argument is the virtual path to the
67422 <li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
67424 <li><code>preopens</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
67428 <li><code>returnOnExit</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
67432 <li><code>stdin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
67434 <li><code>stdout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
67436 <li><code>stderr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
67446 <li><code>instance</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
67451 …at <code>instance</code> exports a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
67459 <li><code>instance</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
67464 …at <code>instance</code> exports a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
67472 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
67476 of a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebA…
67480 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
67484 …ode class="language-js"><span class="hljs-keyword">const</span> worker = <span class="hljs-built_i…
67485 <p>Workers (threads) are useful for performing CPU-intensive JavaScript operations.
67486 They will not help much with I/O-intensive work. Node.js’s built-in asynchronous
67491 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
67492 <span class="hljs-title class_">Worker</span>, isMainThread, parentPort, workerData
67493 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'worker_threads'</span>);
67495 <span class="hljs-keyword">if</span> (isMainThread) {
67496-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-
67497-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Prom…
67498 …<span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class…
67499 <span class="hljs-attr">workerData</span>: script
67501 …worker.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, re…
67502 …worker.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, reje…
67503 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
67504 <span class="hljs-keyword">if</span> (code !== <span class="hljs-number">0</span>)
67505-title function_">reject</span>(<span class="hljs-keyword">new</span> <span class="hljs-title clas…
67509 } <span class="hljs-keyword">else</span> {
67510 …span class="hljs-keyword">const</span> { parse } = <span class="hljs-built_in">require</span>(<spa…
67511 <span class="hljs-keyword">const</span> script = workerData;
67512 …parentPort.<span class="hljs-title function_">postMessage</span>(<span class="hljs-title function_…
67520 <a href="#async-resource-worker-pool">"Using <code>AsyncResource</code> for a <code>Worker</code> t…
67522 <p>Worker threads inherit non-process-specific options by default. Refer to
67529 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
67531 <li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
67532 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" clas…
67533 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
67539 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
67540 <span class="hljs-title class_">Worker</span>,
67544 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'worker_threads'</span>);
67546 <span class="hljs-keyword">if</span> (isMainThread) {
67547 …<span class="hljs-title function_">setEnvironmentData</span>(<span class="hljs-string">'Hello'</sp…
67548 …<span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class…
67549 } <span class="hljs-keyword">else</span> {
67550-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
67557 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
67560-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, isMa…
67562 <span class="hljs-keyword">if</span> (isMainThread) {
67563 <span class="hljs-comment">// This re-loads the current file inside a Worker instance.</span>
67564 <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename);
67565 } <span class="hljs-keyword">else</span> {
67566 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
67567 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
67580-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</spa…
67582 …hljs-keyword">const</span> pooledBuffer = <span class="hljs-keyword">new</span> <span class="hljs-
67583 …ss="hljs-keyword">const</span> typedArray1 = <span class="hljs-keyword">new</span> <span class="hl…
67584 …s="hljs-keyword">const</span> typedArray2 = <span class="hljs-keyword">new</span> <span class="hlj…
67586 <span class="hljs-title function_">markAsUntransferable</span>(pooledBuffer);
67588 <span class="hljs-keyword">const</span> { port1 } = <span class="hljs-keyword">new</span> <span cla…
67589 port1.<span class="hljs-title function_">postMessage</span>(typedArray1, [ typedArray1.<span class=…
67591 <span class="hljs-comment">// The following line prints the contents of typedArray1 -- it still own…
67592 <span class="hljs-comment">// its memory and has been cloned, not transferred. Without</span>
67593 <span class="hljs-comment">// `markAsUntransferable()`, this would print an empty Uint8Array.</span>
67594 <span class="hljs-comment">// typedArray2 is intact as well.</span>
67595 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67596 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67607 <p><code>contextifiedSandbox</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
67619 <a href="https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/onmessage"><code>port.onmessa…
67622 …eloper.mozilla.org/en-US/docs/Web/API/EventTarget"><code>EventTarget</code></a>, and only <a href=…
67629 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" cla…
67637-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, isMa…
67639 <span class="hljs-keyword">if</span> (isMainThread) {
67640 …<span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class…
67641 …span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span cl…
67642 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
67644 …worker.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'Hello, wor…
67645 } <span class="hljs-keyword">else</span> {
67646 …<span class="hljs-comment">// When a message from the parent thread is received, send it back:</sp…
67647 …span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span cl…
67648 parentPort.<span class="hljs-title function_">postMessage</span>(message);
67660 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
67667-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</spa…
67668 <span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <s…
67669 port1.<span class="hljs-title function_">postMessage</span>({ <span class="hljs-attr">hello</span>:…
67671 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67672 <span class="hljs-comment">// Prints: { message: { hello: 'world' } }</span>
67673 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67674 <span class="hljs-comment">// Prints: undefined</span></code></pre>
67682 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
67684 <li><code>maxYoungGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
67685 <li><code>maxOldGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
67686 <li><code>codeRangeSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
67687 <li><code>stackSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
67700 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" c…
67705-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, <spa…
67706-keyword">new</span> <span class="hljs-title class_">Worker</span>(<span class="hljs-string">'proc…
67707 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
67708-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span cl…
67714 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
67716 <li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
67717 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" clas…
67718 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
67730 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
67742 according to the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structur…
67743-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, isMa…
67745 <span class="hljs-keyword">if</span> (isMainThread) {
67746-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title clas…
67747 } <span class="hljs-keyword">else</span> {
67748 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
67755 two-way communications channel.
67759-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</spa…
67761 <span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <s…
67762-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function…
67763 port2.<span class="hljs-title function_">postMessage</span>({ <span class="hljs-attr">foo</span>: <…
67764 <span class="hljs-comment">// Prints: received { foo: 'bar' } from the `port1.on('message')` listen…
67781 asynchronous, two-way communications channel. It can be used to transfer
67784 <p>This implementation matches <a href="https://developer.mozilla.org/en-US/docs/Web/API/MessagePor…
67791-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</spa…
67792 <span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <s…
67794 <span class="hljs-comment">// Prints:</span>
67795 <span class="hljs-comment">// foobar</span>
67796 <span class="hljs-comment">// closed!</span>
67797-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function…
67798-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">…
67800 port1.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'foobar'</spa…
67801 port1.<span class="hljs-title function_">close</span>();</code></pre>
67807 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
67818 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
67854 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
67855 <li><code>transferList</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
67859 the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algo…
67867 <li><code>value</code> may contain <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
67868 <li><code>value</code> may not contain native (C++-backed) objects other than:
67879-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</spa…
67880 <span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <s…
67882-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function…
67884 <span class="hljs-keyword">const</span> circularData = {};
67885 circularData.<span class="hljs-property">foo</span> = circularData;
67886 <span class="hljs-comment">// Prints: { foo: [Circular] }</span>
67887 port2.<span class="hljs-title function_">postMessage</span>(circularData);</code></pre>
67888 <p><code>transferList</code> may be a list of <a href="https://developer.mozilla.org/en-US/docs/Web…
67894 <p>If <code>value</code> contains <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
67898-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</spa…
67899 <span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <s…
67901-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function…
67903-keyword">const</span> uint8Array = <span class="hljs-keyword">new</span> <span class="hljs-title …
67904 <span class="hljs-comment">// This posts a copy of `uint8Array`:</span>
67905 port2.<span class="hljs-title function_">postMessage</span>(uint8Array);
67906 <span class="hljs-comment">// This does not copy data, but renders `uint8Array` unusable:</span>
67907 port2.<span class="hljs-title function_">postMessage</span>(uint8Array, [ uint8Array.<span class="h…
67909 <span class="hljs-comment">// The memory for the `sharedUint8Array` will be accessible from both th…
67910 <span class="hljs-comment">// original and the copy received by `.on('message')`:</span>
67911-keyword">const</span> sharedUint8Array = <span class="hljs-keyword">new</span> <span class="hljs-
67912 port2.<span class="hljs-title function_">postMessage</span>(sharedUint8Array);
67914 <span class="hljs-comment">// This transfers a freshly created message port to the receiver.</span>
67915 <span class="hljs-comment">// This can be used, for example, to create communication channels betwe…
67916 <span class="hljs-comment">// multiple `Worker` threads that are children of the same parent thread…
67917 <span class="hljs-keyword">const</span> otherChannel = <span class="hljs-keyword">new</span> <span …
67918-title function_">postMessage</span>({ <span class="hljs-attr">port</span>: otherChannel.<span cla…
67920 non-enumerable properties, property accessors, and object prototypes are
67922 plain <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uin…
67936-js"><span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">new</span> <span clas…
67938 … class="hljs-keyword">const</span> u1 = <span class="hljs-keyword">new</span> <span class="hljs-ti…
67939 … class="hljs-keyword">const</span> u2 = <span class="hljs-keyword">new</span> <span class="hljs-ti…
67941 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(u2.<span class…
67943 port.<span class="hljs-title function_">postMessage</span>(u1, [u1.<span class="hljs-property">buff…
67945 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(u2.<span class…
68018 threads, and will not be visible to native add-ons (unless
68027 <li>Native add-ons can only be loaded from multiple threads if they fulfill
68031 …er.mozilla.org/en-US/docs/Web/API/Web_Workers_API">Web Workers</a> and the <a href="cluster.html">…
68032 achieved through inter-thread message passing. Internally, a <code>Worker</code> has a
68033 built-in pair of <a href="#worker_threads_class_messageport"><code>MessagePort</code></a>s that are…
68042 pre-existing channel, such as the global one.</p>
68046 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
68047 <span class="hljs-keyword">const</span> {
68048 …<span class="hljs-title class_">Worker</span>, <span class="hljs-title class_">MessageChannel</spa…
68049 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'worker_threads'</span>);
68050 <span class="hljs-keyword">if</span> (isMainThread) {
68051 …<span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class…
68052 …<span class="hljs-keyword">const</span> subChannel = <span class="hljs-keyword">new</span> <span c…
68053-title function_">postMessage</span>({ <span class="hljs-attr">hereIsYourPort</span>: subChannel.<…
68054-property">port2</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'me…
68055 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
68057 } <span class="hljs-keyword">else</span> {
68058 …span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span cl…
68059 …s-title function_">assert</span>(value.<span class="hljs-property">hereIsYourPort</span> <span cla…
68060 …value.<span class="hljs-property">hereIsYourPort</span>.<span class="hljs-title function_">postMes…
68061 …value.<span class="hljs-property">hereIsYourPort</span>.<span class="hljs-title function_">close</…
68089 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
68093 When using a <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs"><…
68097 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
68099 <li><code>argv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
68103 <li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
68108 <li><code>eval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
68111 <li><code>execArgv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
68112 V8 options (such as <code>--max-old-space-size</code>) and options that affect the
68113 process (such as <code>--title</code>) are not supported. If set, this will be provided
68116 <li><code>stdin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
68119 <li><code>stdout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
68121 <li><code>stderr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
68123 <li><code>workerData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
68125 occur as described in the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API…
68128 <li><code>trackUnmanagedFds</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
68134 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object[]></…
68138 <li><code>resourceLimits</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
68142 may still abort if it encounters a global out-of-memory situation.
68144 <li><code>maxOldGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
68145 <li><code>maxYoungGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
68147 …/developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;n…
68149 <li><code>stackSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
68161 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
68170 <li><code>exitCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
68182 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
68194 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
68208 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
68227 <li><code>utilization1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
68229 <li><code>utilization2</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
68231 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objec…
68233 <li><code>idle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
68234 <li><code>active</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
68235 <li><code>utilization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
68248-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, isMa…
68250 <span class="hljs-keyword">if</span> (isMainThread) {
68251 …<span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class…
68252 <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> {
68253 …worker.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'hi'</span>…
68254-variable language_">console</span>.<span class="hljs-title function_">log</span>(worker.<span cla…
68255 }, <span class="hljs-number">100</span>).<span class="hljs-title function_">unref</span>();
68256 <span class="hljs-keyword">return</span>;
68259-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function…
68260 (<span class="hljs-keyword">function</span> <span class="hljs-title function_">r</span>(<span class…
68261 …<span class="hljs-keyword">if</span> (--n &#x3C; <span class="hljs-number">0</span>) <span class="…
68262 …<span class="hljs-keyword">const</span> t = <span class="hljs-title class_">Date</span>.<span clas…
68263 …hljs-keyword">while</span> (<span class="hljs-title class_">Date</span>.<span class="hljs-title fu…
68264 <span class="hljs-title function_">setImmediate</span>(r, n);
68265 })(<span class="hljs-number">10</span>);</code></pre>
68274 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
68275 <li><code>transferList</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
68293 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
68295 <li><code>maxYoungGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
68296 <li><code>maxOldGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
68297 <li><code>codeRangeSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
68298 <li><code>stackSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
68322 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" cla…
68351 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
68361 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
68380 … class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><co…
68381 <span class="hljs-title class_">Worker</span>,
68383 } <span class="hljs-keyword">from</span> <span class="hljs-string">'worker_threads'</span>;
68385 <span class="hljs-keyword">if</span> (isMainThread) {
68386-keyword">new</span> <span class="hljs-title class_">Worker</span>(<span class="hljs-keyword">new<…
68387 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-num…
68388 } <span class="hljs-keyword">else</span> {
68389 …<span class="hljs-comment">// This output will be blocked by the for loop in the main thread.</spa…
68390 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
68391 }</code><code class="language-js cjs"><span class="hljs-meta">'use strict'</span>;
68393 <span class="hljs-keyword">const</span> {
68394 <span class="hljs-title class_">Worker</span>,
68396 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'worker_threads'</span>);
68398 <span class="hljs-keyword">if</span> (isMainThread) {
68399 <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename);
68400 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-num…
68401 } <span class="hljs-keyword">else</span> {
68402 …<span class="hljs-comment">// This output will be blocked by the for loop in the main thread.</spa…
68403 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
68407 and run using the <code>-r</code> command line flag). Unless the <code>execArgv</code> option is
68415 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
68420 …code class="language-js"><span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in…
68425 …class="language-js"><span class="hljs-keyword">const</span> { createGzip } = <span class="hljs-bui…
68426 <span class="hljs-keyword">const</span> { pipeline } = <span class="hljs-built_in">require</span>(<…
68427 <span class="hljs-keyword">const</span> {
68430 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
68432 <span class="hljs-keyword">const</span> gzip = <span class="hljs-title function_">createGzip</span>…
68433 <span class="hljs-keyword">const</span> source = <span class="hljs-title function_">createReadStrea…
68434 <span class="hljs-keyword">const</span> destination = <span class="hljs-title function_">createWrit…
68436 <span class="hljs-title function_">pipeline</span>(source, gzip, destination, <span class="hljs-fun…
68437 <span class="hljs-keyword">if</span> (err) {
68438 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68439 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68443 <span class="hljs-comment">// Or, Promisified</span>
68445 <span class="hljs-keyword">const</span> { promisify } = <span class="hljs-built_in">require</span>(…
68446 <span class="hljs-keyword">const</span> pipe = <span class="hljs-title function_">promisify</span>(…
68448 …ass="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title…
68449 …<span class="hljs-keyword">const</span> gzip = <span class="hljs-title function_">createGzip</span…
68450 …<span class="hljs-keyword">const</span> source = <span class="hljs-title function_">createReadStre…
68451 …<span class="hljs-keyword">const</span> destination = <span class="hljs-title function_">createWri…
68452 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">pipe</span>(source, gzi…
68455 <span class="hljs-title function_">do_gzip</span>(<span class="hljs-string">'input.txt'</span>, <sp…
68456 ….<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-pa…
68457 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68458 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68461 …ass="language-js"><span class="hljs-keyword">const</span> { deflate, unzip } = <span class="hljs-b…
68463 <span class="hljs-keyword">const</span> input = <span class="hljs-string">'........................…
68464 <span class="hljs-title function_">deflate</span>(input, <span class="hljs-function">(<span class="…
68465 <span class="hljs-keyword">if</span> (err) {
68466 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68467 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68469-variable language_">console</span>.<span class="hljs-title function_">log</span>(buffer.<span cla…
68472-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-ti…
68473 <span class="hljs-title function_">unzip</span>(buffer, <span class="hljs-function">(<span class="h…
68474 <span class="hljs-keyword">if</span> (err) {
68475 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68476 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68478 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
68481 <span class="hljs-comment">// Or, Promisified</span>
68483 <span class="hljs-keyword">const</span> { promisify } = <span class="hljs-built_in">require</span>(…
68484 <span class="hljs-keyword">const</span> do_unzip = <span class="hljs-title function_">promisify</sp…
68486 <span class="hljs-title function_">do_unzip</span>(buffer)
68487-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">buf</span>) =…
68488 ….<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-pa…
68489 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68490 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68498 …code class="language-js"><span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in…
68500 …hljs-keyword">const</span> payload = <span class="hljs-title class_">Buffer</span>.<span class="hl…
68502 <span class="hljs-comment">// WARNING: DO NOT DO THIS!</span>
68503 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
68504 …zlib.<span class="hljs-title function_">deflate</span>(payload, <span class="hljs-function">(<span…
68513 and <code>br</code> content-encoding mechanisms defined by
68514 <a href="https://tools.ietf.org/html/rfc7230#section-4.2">HTTP</a>.</p>
68515 <p>The HTTP <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3"><code>Accept-
68516 … The <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.11"><code>Content-Enco…
68523 <pre><code class="language-js"><span class="hljs-comment">// Client request example</span>
68524 <span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in">require</span>(<span cla…
68525 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
68526 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
68527 <span class="hljs-keyword">const</span> { pipeline } = <span class="hljs-built_in">require</span>(<…
68529 …="hljs-keyword">const</span> request = http.<span class="hljs-title function_">get</span>({ <span …
68530 … <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
68531 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
68532 …<span class="hljs-attr">headers</span>: { <span class="hljs-string">'Accept-Encoding'</span>: <spa…
68533 …span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span cla…
68534 …<span class="hljs-keyword">const</span> output = fs.<span class="hljs-title function_">createWrite…
68536 …<span class="hljs-keyword">const</span> <span class="hljs-title function_">onError</span> = (<span…
68537 <span class="hljs-keyword">if</span> (err) {
68538 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68539 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68543 …<span class="hljs-keyword">switch</span> (response.<span class="hljs-property">headers</span>[<spa…
68544 <span class="hljs-keyword">case</span> <span class="hljs-string">'br'</span>:
68545 …<span class="hljs-title function_">pipeline</span>(response, zlib.<span class="hljs-title function…
68546 <span class="hljs-keyword">break</span>;
68547 …<span class="hljs-comment">// Or, just use zlib.createUnzip() to handle both of the following case…
68548 <span class="hljs-keyword">case</span> <span class="hljs-string">'gzip'</span>:
68549 …<span class="hljs-title function_">pipeline</span>(response, zlib.<span class="hljs-title function…
68550 <span class="hljs-keyword">break</span>;
68551 <span class="hljs-keyword">case</span> <span class="hljs-string">'deflate'</span>:
68552 …<span class="hljs-title function_">pipeline</span>(response, zlib.<span class="hljs-title function…
68553 <span class="hljs-keyword">break</span>;
68554 <span class="hljs-attr">default</span>:
68555 <span class="hljs-title function_">pipeline</span>(response, output, onError);
68556 <span class="hljs-keyword">break</span>;
68559 <pre><code class="language-js"><span class="hljs-comment">// server example</span>
68560 <span class="hljs-comment">// Running a gzip operation on every request is quite expensive.</span>
68561 <span class="hljs-comment">// It would be much more efficient to cache the compressed buffer.</span>
68562 <span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in">require</span>(<span cla…
68563 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
68564 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
68565 <span class="hljs-keyword">const</span> { pipeline } = <span class="hljs-built_in">require</span>(<…
68567 http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span clas…
68568 …<span class="hljs-keyword">const</span> raw = fs.<span class="hljs-title function_">createReadStre…
68569 …<span class="hljs-comment">// Store both a compressed and an uncompressed version of the resource.…
68570 …nse.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Vary'</span>, <…
68571 …pan class="hljs-keyword">let</span> acceptEncoding = request.<span class="hljs-property">headers</…
68572 <span class="hljs-keyword">if</span> (!acceptEncoding) {
68573 acceptEncoding = <span class="hljs-string">''</span>;
68576 …<span class="hljs-keyword">const</span> <span class="hljs-title function_">onError</span> = (<span…
68577 <span class="hljs-keyword">if</span> (err) {
68578 <span class="hljs-comment">// If an error occurs, there's not much we can do because</span>
68579 <span class="hljs-comment">// the server has already sent the 200 response code and</span>
68580 <span class="hljs-comment">// some amount of data has already been sent to the client.</span>
68581 <span class="hljs-comment">// The best we can do is terminate the response immediately</span>
68582 <span class="hljs-comment">// and log the error.</span>
68583 response.<span class="hljs-title function_">end</span>();
68584 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68588 <span class="hljs-comment">// Note: This is not a conformant accept-encoding parser.</span>
68589 …<span class="hljs-comment">// See https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3<…
68590 …<span class="hljs-keyword">if</span> (<span class="hljs-regexp">/\bdeflate\b/</span>.<span class="…
68591 …ljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-st…
68592 …<span class="hljs-title function_">pipeline</span>(raw, zlib.<span class="hljs-title function_">cr…
68593 …pan class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-regex…
68594 …ljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-st…
68595 …<span class="hljs-title function_">pipeline</span>(raw, zlib.<span class="hljs-title function_">cr…
68596 …span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-rege…
68597 …ljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-st…
68598 …<span class="hljs-title function_">pipeline</span>(raw, zlib.<span class="hljs-title function_">cr…
68599 } <span class="hljs-keyword">else</span> {
68600 …response.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>,…
68601 <span class="hljs-title function_">pipeline</span>(raw, response, onError);
68603 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);</code><…
68609 <pre><code class="language-js"><span class="hljs-comment">// This is a truncated version of the buf…
68610-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-ti…
68612 zlib.<span class="hljs-title function_">unzip</span>(
68614 …<span class="hljs-comment">// For Brotli, the equivalent is zlib.constants.BROTLI_OPERATION_FLUSH.…
68615 …{ <span class="hljs-attr">finishFlush</span>: zlib.<span class="hljs-property">constants</span>.<s…
68616 <span class="hljs-function">(<span class="hljs-params">err, buffer</span>) =></span> {
68617 <span class="hljs-keyword">if</span> (err) {
68618 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68619 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68621 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
68623 <p>This will not change the behavior in other error-throwing situations, e.g.
68630 <h4>For zlib-based streams<span><a class="mark" href="#zlib_for_zlib_based_streams" id="zlib_for_zl…
68633 <!-- eslint-disable semi -->
68634-js">(<span class="hljs-number">1</span> &#x3C;&#x3C; (windowBits + <span class="hljs-number">2</s…
68639-js"><span class="hljs-keyword">const</span> options = { <span class="hljs-attr">windowBits</span>…
68654 <h4>For Brotli-based streams<span><a class="mark" href="#zlib_for_brotli_based_streams" id="zlib_fo…
68655 <p>There are equivalents to the zlib options for Brotli-based streams, although
68661 <p>See <a href="#zlib_brotli_constants">below</a> for more details on Brotli-specific options.</p>
68668 …code class="language-js"><span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in…
68669 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
68670 <span class="hljs-keyword">const</span> { pipeline } = <span class="hljs-built_in">require</span>(<…
68672 http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span clas…
68673 …<span class="hljs-comment">// For the sake of simplicity, the Accept-Encoding checks are omitted.<…
68674 …ljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-st…
68675 …<span class="hljs-keyword">const</span> output = zlib.<span class="hljs-title function_">createGzi…
68676 <span class="hljs-keyword">let</span> i;
68678 …<span class="hljs-title function_">pipeline</span>(output, response, <span class="hljs-function">(…
68679 <span class="hljs-keyword">if</span> (err) {
68680 <span class="hljs-comment">// If an error occurs, there's not much we can do because</span>
68681 <span class="hljs-comment">// the server has already sent the 200 response code and</span>
68682 <span class="hljs-comment">// some amount of data has already been sent to the client.</span>
68683 <span class="hljs-comment">// The best we can do is terminate the response immediately</span>
68684 <span class="hljs-comment">// and log the error.</span>
68685 <span class="hljs-built_in">clearInterval</span>(i);
68686 response.<span class="hljs-title function_">end</span>();
68687 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68691 i = <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> {
68692-title function_">write</span>(<span class="hljs-string">`The current time is <span class="hljs-su…
68693 …<span class="hljs-comment">// The data has been passed to zlib, but the compression algorithm may<…
68694 …<span class="hljs-comment">// have decided to buffer the data for more efficient compression.</spa…
68695 …<span class="hljs-comment">// Calling .flush() will make the data available as soon as the client<…
68696 <span class="hljs-comment">// is ready to receive it.</span>
68697 output.<span class="hljs-title function_">flush</span>();
68699 }, <span class="hljs-number">1000</span>);
68700 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);</code><…
68758 <p>There are several options and other constants available for Brotli-based
68761 <p>The following values are valid flush operations for Brotli-based streams:</p>
68784 <li><code>BROTLI_MODE_TEXT</code>, adjusted for UTF-8 text</li>
68826 Brotli format as standardized in <a href="https://www.rfc-editor.org/rfc/rfc7932.txt">RFC 7932</a>)…
68851 Brotli format as standardized in <a href="https://www.rfc-editor.org/rfc/rfc7932.txt">RFC 7932</a>)…
68874 <p>Each zlib-based class takes an <code>options</code> object. No options are required.</p>
68878 <li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
68879 <li><code>finishFlush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
68880 <li><code>chunkSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
68881 <li><code>windowBits</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
68882 <li><code>level</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
68883 <li><code>memLevel</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
68884 <li><code>strategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
68885-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
68887 <li><code>info</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
68888 <li><code>maxOutputLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
68906 <p>Each Brotli-based class takes an <code>options</code> object. All options are optional.</p>
68908 <li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
68909 <li><code>finishFlush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
68910 <li><code>chunkSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
68911 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type…
68912 <li><code>maxOutputLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
68916 <pre><code class="language-js"><span class="hljs-keyword">const</span> stream = zlib.<span class="h…
68917 …<span class="hljs-attr">chunkSize</span>: <span class="hljs-number">32</span> * <span class="hljs-
68918 <span class="hljs-attr">params</span>: {
68919 …s-property">constants</span>.<span class="hljs-property">BROTLI_PARAM_MODE</span>]: zlib.<span cla…
68920 …[zlib.<span class="hljs-property">constants</span>.<span class="hljs-property">BROTLI_PARAM_QUALIT…
68921-property">constants</span>.<span class="hljs-property">BROTLI_PARAM_SIZE_HINT</span>]: fs.<span c…
68998 <p>Decompress either a Gzip- or Deflate-compressed stream by auto-detecting
69020 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
69022 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
69033 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
69043 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69051 <li><code>kind</code> <strong>Default:</strong> <code>zlib.constants.Z_FULL_FLUSH</code> for zlib-b…
69052 <code>zlib.constants.BROTLI_OPERATION_FLUSH</code> for Brotli-based streams.</li>
69053 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69066 <li><code>level</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
69067 <li><code>strategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
69068 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69070 <p>This function is only available for zlib-based streams, i.e. not Brotli.</p>
69083 <p>Provides an object enumerating Zlib-related constants.</p>
69121 that effectively uses an 8-bit window only.</p>
69165 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>…
69166 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuff…
69176-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69178 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69185-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69194-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69196 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69203-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69224-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69226 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69245-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69264-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69266 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69285-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69306-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69308 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69327-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69348-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69350 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69369-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69390-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69392 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69411-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69432-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69434 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69453-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69474-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69476 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69495-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69500 <!-- API END -->
69509 const themeToggleButton = document.getElementById('theme-toggle-btn');
69511 const mq = window.matchMedia('(prefers-color-scheme: dark)');
69514 document.body.classList.toggle('dark-mode', e.matches);
69524 document.body.classList.add('dark-mode');
69527 document.body.classList.add('dark-mode');
69534 document.body.classList.toggle('dark-mode')