• Home
  • Raw
  • Download

Lines Matching +full:srv +full:- +full:ipv6 +full:- +full:single +full:- +full:target

4   <meta charset="utf-8">
5 <meta name="viewport" content="width=device-width">
6 <meta name="nodejs.org:node-version" content="v14.19.1">
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…
111 <a href="all.html">View on single page</a>
117 <li class="version-picker">
119 …<ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v17.x/api/all.html">17.x</a…
120 <li><a href="https://nodejs.org/docs/latest-v16.x/api/all.html">16.x <b>LTS</b></a></li>
121 <li><a href="https://nodejs.org/docs/latest-v15.x/api/all.html">15.x</a></li>
122 <li><a href="https://nodejs.org/docs/latest-v14.x/api/all.html">14.x <b>LTS</b></a></li>
123 <li><a href="https://nodejs.org/docs/latest-v13.x/api/all.html">13.x</a></li>
124 <li><a href="https://nodejs.org/docs/latest-v12.x/api/all.html">12.x <b>LTS</b></a></li>
125 <li><a href="https://nodejs.org/docs/latest-v11.x/api/all.html">11.x</a></li>
126 <li><a href="https://nodejs.org/docs/latest-v10.x/api/all.html">10.x</a></li>
127 <li><a href="https://nodejs.org/docs/latest-v9.x/api/all.html">9.x</a></li>
128 <li><a href="https://nodejs.org/docs/latest-v8.x/api/all.html">8.x</a></li>
129 <li><a href="https://nodejs.org/docs/latest-v7.x/api/all.html">7.x</a></li>
130 <li><a href="https://nodejs.org/docs/latest-v6.x/api/all.html">6.x</a></li>
131 <li><a href="https://nodejs.org/docs/latest-v5.x/api/all.html">5.x</a></li>
132 <li><a href="https://nodejs.org/docs/latest-v4.x/api/all.html">4.x</a></li>
133 <li><a href="https://nodejs.org/docs/latest-v0.12.x/api/all.html">0.12.x</a></li>
134 <li><a href="https://nodejs.org/docs/latest-v0.10.x/api/all.html">0.10.x</a></li></ol>
143 <!-- TOC --><details id="toc" open><summary>Table of contents</summary>
308 …re_target_targetstart_targetend_sourcestart_sourceend"><code>buf.compare(target[, targetStart[, ta…
309 <li><a href="#buffer_buf_copy_target_targetstart_sourcestart_sourceend"><code>buf.copy(target[, tar…
402 …uffer_the_zero_fill_buffers_command_line_option">The <code>--zero-fill-buffers</code> command-line…
412 <li><a href="#addons_context_aware_addons">Context-aware addons</a>
423 <li><span class="stability_2"><a href="#addons_node_api">Node-API</a></span></li>
437 <li><span class="stability_2"><a href="#n_api_node_api">Node-API</a></span>
444 <li><a href="#n_api_node_gyp">node-gyp</a></li>
450 <li><a href="#n_api_node_pre_gyp">node-pre-gyp</a></li>
458 <li><a href="#n_api_node_api_version_matrix">Node-API version matrix</a></li>
465 <li><a href="#n_api_basic_node_api_data_types">Basic Node-API data types</a>
474 <li><a href="#n_api_node_api_memory_management_types">Node-API memory management types</a>
483 <li><a href="#n_api_node_api_callback_types">Node-API callback types</a>
584 …pi_functions_to_convert_from_c_types_to_node_api">Functions to convert from C types to Node-API</a>
598 <li><a href="#n_api_functions_to_convert_from_node_api_to_c_types">Functions to convert from Node-A…
748 <li><a href="#n_api_asynchronous_thread_safe_function_calls">Asynchronous thread-safe function call…
750 <li><a href="#n_api_calling_a_thread_safe_function">Calling a thread-safe function</a></li>
751 <li><a href="#n_api_reference_counting_of_thread_safe_functions">Reference counting of thread-safe …
770 <li><a href="#embedding_setting_up_per_process_state">Setting up per-process state</a></li>
771 <li><a href="#embedding_per_instance_state">Per-instance state</a></li>
881 <li><a href="#cli_command_line_options">Command-line options</a>
886 <li><a href="#cli"><code>-</code></a></li>
887 <li><a href="#cli_1"><code>--</code></a></li>
888 <li><a href="#cli_abort_on_uncaught_exception"><code>--abort-on-uncaught-exception</code></a></li>
889 <li><a href="#cli_completion_bash"><code>--completion-bash</code></a></li>
890 …lity_1"><a href="#cli_c_condition_conditions_condition"><code>-C=condition</code>, <code>--conditi…
891 <li><span class="stability_1"><a href="#cli_cpu_prof"><code>--cpu-prof</code></a></span></li>
892 <li><span class="stability_1"><a href="#cli_cpu_prof_dir"><code>--cpu-prof-dir</code></a></span></l…
893 <li><span class="stability_1"><a href="#cli_cpu_prof_interval"><code>--cpu-prof-interval</code></a>…
894 <li><span class="stability_1"><a href="#cli_cpu_prof_name"><code>--cpu-prof-name</code></a></span><…
895 <li><a href="#cli_diagnostic_dir_directory"><code>--diagnostic-dir=directory</code></a></li>
896 <li><a href="#cli_disable_proto_mode"><code>--disable-proto=mode</code></a></li>
897 <li><a href="#cli_disallow_code_generation_from_strings"><code>--disallow-code-generation-from-stri…
898 <li><a href="#cli_dns_result_order_order"><code>--dns-result-order=order</code></a></li>
899 <li><a href="#cli_enable_fips"><code>--enable-fips</code></a></li>
900 <li><a href="#cli_enable_source_maps"><code>--enable-source-maps</code></a></li>
901 <li><a href="#cli_experimental_abortcontroller"><code>--experimental-abortcontroller</code></a></li>
902 <li><a href="#cli_experimental_import_meta_resolve"><code>--experimental-import-meta-resolve</code>…
903 <li><a href="#cli_experimental_json_modules"><code>--experimental-json-modules</code></a></li>
904 <li><a href="#cli_experimental_loader_module"><code>--experimental-loader=module</code></a></li>
905 <li><a href="#cli_experimental_modules"><code>--experimental-modules</code></a></li>
906 <li><a href="#cli_experimental_policy"><code>--experimental-policy</code></a></li>
907 <li><a href="#cli_experimental_repl_await"><code>--experimental-repl-await</code></a></li>
908 <li><a href="#cli_experimental_specifier_resolution_mode"><code>--experimental-specifier-resolution…
909 <li><a href="#cli_experimental_vm_modules"><code>--experimental-vm-modules</code></a></li>
910 <li><a href="#cli_experimental_wasi_unstable_preview1"><code>--experimental-wasi-unstable-preview1<…
911 <li><a href="#cli_experimental_wasm_modules"><code>--experimental-wasm-modules</code></a></li>
912 <li><a href="#cli_force_context_aware"><code>--force-context-aware</code></a></li>
913 <li><a href="#cli_force_fips"><code>--force-fips</code></a></li>
914 <li><span class="stability_1"><a href="#cli_frozen_intrinsics"><code>--frozen-intrinsics</code></a>…
915 …_1"><a href="#cli_heapsnapshot_near_heap_limit_max_count"><code>--heapsnapshot-near-heap-limit=max…
916 <li><a href="#cli_heapsnapshot_signal_signal"><code>--heapsnapshot-signal=signal</code></a></li>
917 <li><span class="stability_1"><a href="#cli_heap_prof"><code>--heap-prof</code></a></span></li>
918 <li><span class="stability_1"><a href="#cli_heap_prof_dir"><code>--heap-prof-dir</code></a></span><…
919 <li><span class="stability_1"><a href="#cli_heap_prof_interval"><code>--heap-prof-interval</code></…
920 <li><span class="stability_1"><a href="#cli_heap_prof_name"><code>--heap-prof-name</code></a></span…
921 <li><a href="#cli_icu_data_dir_file"><code>--icu-data-dir=file</code></a></li>
922 <li><a href="#cli_input_type_type"><code>--input-type=type</code></a></li>
923 <li><a href="#cli_inspect_brk_host_port"><code>--inspect-brk[=[host:]port]</code></a></li>
924 <li><a href="#cli_inspect_port_host_port"><code>--inspect-port=[host:]port</code></a></li>
925 <li><a href="#cli_inspect_host_port"><code>--inspect[=[host:]port]</code></a>
930 <li><a href="#cli_inspect_publish_uid_stderr_http"><code>--inspect-publish-uid=stderr,http</code></…
931 <li><a href="#cli_insecure_http_parser"><code>--insecure-http-parser</code></a></li>
932 <li><a href="#cli_jitless"><code>--jitless</code></a></li>
933 <li><a href="#cli_max_http_header_size_size"><code>--max-http-header-size=size</code></a></li>
934 <li><a href="#cli_napi_modules"><code>--napi-modules</code></a></li>
935 <li><a href="#cli_no_addons"><code>--no-addons</code></a></li>
936 <li><a href="#cli_no_deprecation"><code>--no-deprecation</code></a></li>
937 <li><a href="#cli_no_force_async_hooks_checks"><code>--no-force-async-hooks-checks</code></a></li>
938 <li><a href="#cli_no_warnings"><code>--no-warnings</code></a></li>
939 <li><a href="#cli_node_memory_debug"><code>--node-memory-debug</code></a></li>
940 <li><a href="#cli_openssl_config_file"><code>--openssl-config=file</code></a></li>
941 <li><a href="#cli_pending_deprecation"><code>--pending-deprecation</code></a></li>
942 <li><span class="stability_1"><a href="#cli_policy_integrity_sri"><code>--policy-integrity=sri</cod…
943 <li><a href="#cli_preserve_symlinks"><code>--preserve-symlinks</code></a></li>
944 <li><a href="#cli_preserve_symlinks_main"><code>--preserve-symlinks-main</code></a></li>
945 <li><a href="#cli_prof"><code>--prof</code></a></li>
946 <li><a href="#cli_prof_process"><code>--prof-process</code></a></li>
947 <li><a href="#cli_redirect_warnings_file"><code>--redirect-warnings=file</code></a></li>
948 <li><a href="#cli_report_compact"><code>--report-compact</code></a></li>
949 …eport_dir_directory_report_directory_directory"><code>--report-dir=directory</code>, <code>report-
950 <li><a href="#cli_report_filename_filename"><code>--report-filename=filename</code></a></li>
951 <li><a href="#cli_report_on_fatalerror"><code>--report-on-fatalerror</code></a></li>
952 <li><a href="#cli_report_on_signal"><code>--report-on-signal</code></a></li>
953 <li><a href="#cli_report_signal_signal"><code>--report-signal=signal</code></a></li>
954 <li><a href="#cli_report_uncaught_exception"><code>--report-uncaught-exception</code></a></li>
955 <li><a href="#cli_throw_deprecation"><code>--throw-deprecation</code></a></li>
956 <li><a href="#cli_title_title"><code>--title=title</code></a></li>
957 <li><a href="#cli_tls_cipher_list_list"><code>--tls-cipher-list=list</code></a></li>
958 <li><a href="#cli_tls_keylog_file"><code>--tls-keylog=file</code></a></li>
959 <li><a href="#cli_tls_max_v1_2"><code>--tls-max-v1.2</code></a></li>
960 <li><a href="#cli_tls_max_v1_3"><code>--tls-max-v1.3</code></a></li>
961 <li><a href="#cli_tls_min_v1_0"><code>--tls-min-v1.0</code></a></li>
962 <li><a href="#cli_tls_min_v1_1"><code>--tls-min-v1.1</code></a></li>
963 <li><a href="#cli_tls_min_v1_2"><code>--tls-min-v1.2</code></a></li>
964 <li><a href="#cli_tls_min_v1_3"><code>--tls-min-v1.3</code></a></li>
965 <li><a href="#cli_trace_atomics_wait"><code>--trace-atomics-wait</code></a></li>
966 <li><a href="#cli_trace_deprecation"><code>--trace-deprecation</code></a></li>
967 <li><a href="#cli_trace_event_categories"><code>--trace-event-categories</code></a></li>
968 <li><a href="#cli_trace_event_file_pattern"><code>--trace-event-file-pattern</code></a></li>
969 <li><a href="#cli_trace_events_enabled"><code>--trace-events-enabled</code></a></li>
970 <li><a href="#cli_trace_exit"><code>--trace-exit</code></a></li>
971 <li><a href="#cli_trace_sigint"><code>--trace-sigint</code></a></li>
972 <li><a href="#cli_trace_sync_io"><code>--trace-sync-io</code></a></li>
973 <li><a href="#cli_trace_tls"><code>--trace-tls</code></a></li>
974 <li><a href="#cli_trace_uncaught"><code>--trace-uncaught</code></a></li>
975 <li><a href="#cli_trace_warnings"><code>--trace-warnings</code></a></li>
976 <li><a href="#cli_track_heap_objects"><code>--track-heap-objects</code></a></li>
977 <li><a href="#cli_unhandled_rejections_mode"><code>--unhandled-rejections=mode</code></a></li>
978 …li><a href="#cli_use_bundled_ca_use_openssl_ca"><code>--use-bundled-ca</code>, <code>--use-openssl
979 <li><a href="#cli_use_largepages_mode"><code>--use-largepages=mode</code></a></li>
980 <li><a href="#cli_v8_options"><code>--v8-options</code></a></li>
981 <li><a href="#cli_v8_pool_size_num"><code>--v8-pool-size=num</code></a></li>
982 <li><a href="#cli_zero_fill_buffers"><code>--zero-fill-buffers</code></a></li>
983 <li><a href="#cli_c_check"><code>-c</code>, <code>--check</code></a></li>
984 <li><a href="#cli_e_eval_script"><code>-e</code>, <code>--eval "script"</code></a></li>
985 <li><a href="#cli_h_help"><code>-h</code>, <code>--help</code></a></li>
986 <li><a href="#cli_i_interactive"><code>-i</code>, <code>--interactive</code></a></li>
987 <li><a href="#cli_p_print_script"><code>-p</code>, <code>--print "script"</code></a></li>
988 <li><a href="#cli_r_require_module"><code>-r</code>, <code>--require module</code></a></li>
989 <li><a href="#cli_v_version"><code>-v</code>, <code>--version</code></a></li>
1026 <li><a href="#cli_max_old_space_size_size_in_megabytes"><code>--max-old-space-size=SIZE</code> (in …
1081 <li><a href="#corepack_running_npm_install_g_yarn_doesn_t_work">Running <code>npm install -g yarn</…
1331 <li><a href="#DEP0062">DEP0062: <code>node --debug</code></a></li>
1359 <li><a href="#DEP0092">DEP0092: Top-level <code>this</code> bound to <code>module.exports</code></a…
1366 <li><a href="#DEP0099">DEP0099: Async context-unaware <code>node::MakeCallback</code> C++ APIs</a><…
1368 <li><a href="#DEP0101">DEP0101: <code>--with-lttng</code></a></li>
1448 <li><a href="#dns_resolver_setlocaladdress_ipv4_ipv6"><code>resolver.setLocalAddress([ipv4][, ipv6]…
1534 <li><a href="#errors_error_first_callbacks">Error-first callbacks</a></li>
1950 <li><a href="#events_event_target"><code>event.target</code></a></li>
2029 <li><a href="#fs_fspromises_symlink_target_path_type"><code>fsPromises.symlink(target, path[, type]…
2085 <li><a href="#fs_fs_symlink_target_path_type_callback"><code>fs.symlink(target, path[, type], callb…
2149 <li><a href="#fs_fs_symlinksync_target_path_type"><code>fs.symlinkSync(target, path[, type])</code>…
2273 …g_of_callback_and_promise_based_operations">Ordering of callback and promise-based operations</a><…
2279 <li><a href="#fs_platform_specific_considerations">Platform-specific considerations</a></li>
2283 <li><a href="#fs_per_drive_working_directories_on_windows">Per-drive working directories on Windows…
2510 <li><a href="#http2_server_side_example">Server-side example</a></li>
2511 <li><a href="#http2_client_side_example">Client-side example</a></li>
2772 <li><a href="#inspector_event_inspector_protocol_method">Event: <code>&#x3C;inspector-protocol-meth…
2792 …tl_build_with_a_pre_installed_icu_system_icu">Build with a pre-installed ICU (<code>system-icu</co…
2793 …d_a_limited_set_of_icu_data_small_icu">Embed a limited set of ICU data (<code>small-icu</code>)</a>
2798 <li><a href="#intl_embed_the_entire_icu_full_icu">Embed the entire ICU (<code>full-icu</code>)</a><…
2911 <li><span class="stability_1"><a href="#esm_top_level_await">Top-level <code>await</code></a></span…
2971 <li><a href="#packages_input_type_flag"><code>--input-type</code> flag</a></li>
2986 <li><a href="#packages_self_referencing_a_package_using_its_name">Self-referencing a package using …
3157 <li><a href="#os_windows_specific_error_constants">Windows-specific error constants</a></li>
3486 <li><a href="#readline_example_read_file_stream_line_by_line">Example: Read file stream line-by-Lin…
3505 <li><a href="#repl_reverse_i_search">Reverse-i-search</a></li>
3531 …node_js_repl_with_advanced_line_editors">Using the Node.js REPL with advanced line-editors</a></li>
3532 …st_a_single_running_instance">Starting multiple REPL instances against a single running instance</…
3750 <li><a href="#tls_pre_shared_keys">Pre-shared keys</a></li>
3751 <li><a href="#tls_client_initiated_renegotiation_attack_mitigation">Client-initiated renegotiation …
3918 <li><a href="#dgram_example_ipv6_outgoing_multicast_interface">Example: IPv6 outgoing multicast int…
4023 <li><a href="#url_percent_encoding_in_urls">Percent-encoding in URLs</a>
4073 …l_icu_option">Encodings supported when Node.js is built with the <code>small-icu</code> option</a>…
4138 <li><span class="stability_0"><a href="#util_util_extend_target_source"><code>util._extend(target, …
4338 <li><a href="#zlib_for_zlib_based_streams">For zlib-based streams</a></li>
4339 <li><a href="#zlib_for_brotli_based_streams">For Brotli-based streams</a></li>
4429 <p></p><div class="api_stability api_stability_0">Stability: 0 - Deprecated. The feature may emit w…
4431 <!-- separator -->
4432 <p></p><div class="api_stability api_stability_1">Stability: 1 - Experimental. The feature is not s…
4433 <a href="https://semver.org/">Semantic Versioning</a> rules. Non-backward compatible changes or rem…
4436 <!-- separator -->
4437 <p></p><div class="api_stability api_stability_2">Stability: 2 - Stable. Compatibility with the npm…
4439 <!-- separator -->
4440 <p></p><div class="api_stability api_stability_3">Stability: 3 - Legacy. The feature is no longer r…
4441 likely will not be removed, and is still covered by semantic-versioning
4447 a command-line flag. Experimental features may also emit a <a href="#process_event_warning">warning…
4449 <!-- STABILITY_OVERVIEW_SLOT_BEGIN -->
4450 <!-- STABILITY_OVERVIEW_SLOT_END -->
4467 <p><code>node [options] [V8 options] [script.js | -e "script" | - ] [arguments]</code></p>
4468 <p>Please see the <a href="#cli_command_line_options">Command-line options</a> document for more in…
4478 <a href="https://nodejs.org/en/download/package-manager/">Installing Node.js via package manager</a…
4481 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">mk…
4482 <span class="hljs-meta">$ </span><span class="language-bash"><span class="hljs-built_in">cd</span> …
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 folder and call it <code>hello-world.js</code>.</p>
4491 <p>Open <code>hello-world.js</code> in any preferred text editor and
4493 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
4495 <span class="hljs-keyword">const</span> hostname = <span class="hljs-string">'127.0.0.1'</span>;
4496 <span class="hljs-keyword">const</span> port = <span class="hljs-number">3000</span>;
4498 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
4499 res.<span class="hljs-property">statusCode</span> = <span class="hljs-number">200</span>;
4500 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</…
4501 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Hello, World!\n'</spa…
4504 server.<span class="hljs-title function_">listen</span>(port, hostname, <span class="hljs-function"…
4505-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
4508 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
4510 <pre><code class="language-console">Server running at http://127.0.0.1:3000/</code></pre>
4517 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
4537 <p>In strict assertion mode, non-strict methods behave like their corresponding
4543-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
4545-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
4547-title function_">deepEqual</span>([[[<span class="hljs-number">1</span>, <span class="hljs-number…
4548 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
4549 <span class="hljs-comment">// + actual - expected ... Lines skipped</span>
4550 <span class="hljs-comment">//</span>
4551 <span class="hljs-comment">// [</span>
4552 <span class="hljs-comment">// [</span>
4553 <span class="hljs-comment">// ...</span>
4554 <span class="hljs-comment">// 2,</span>
4555 <span class="hljs-comment">// + 3</span>
4556 <span class="hljs-comment">// - '3'</span>
4557 <span class="hljs-comment">// ],</span>
4558 <span class="hljs-comment">// ...</span>
4559 <span class="hljs-comment">// 5</span>
4560 <span class="hljs-comment">// ]</span></code></pre>
4566 <p>Legacy assertion mode uses the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-co…
4574 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4576 <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Compar…
4579 <pre><code class="language-js"><span class="hljs-comment">// WARNING: This does not throw an Assert…
4580 …class="hljs-title function_">deepEqual</span>(<span class="hljs-regexp">/a/gi</span>, <span class=…
4592 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
4594 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
4595 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
4596 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4597 <li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4598 <li><code>stackStartFn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
4604 <p>All instances contain the built-in <code>Error</code> properties (<code>message</code> and <code…
4607 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
4609 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4611 …r.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean><…
4613 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
4615 <li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4617 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4619 <span class="hljs-comment">// Generate an AssertionError to compare the error message later:</span>
4620 <span class="hljs-keyword">const</span> { message } = <span class="hljs-keyword">new</span> assert.…
4621 <span class="hljs-attr">actual</span>: <span class="hljs-number">1</span>,
4622 <span class="hljs-attr">expected</span>: <span class="hljs-number">2</span>,
4623 <span class="hljs-attr">operator</span>: <span class="hljs-string">'strictEqual'</span>
4626 <span class="hljs-comment">// Verify error output:</span>
4627 <span class="hljs-keyword">try</span> {
4628 …assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <…
4629 } <span class="hljs-keyword">catch</span> (err) {
4630 …<span class="hljs-title function_">assert</span>(err <span class="hljs-keyword">instanceof</span> …
4631 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">messa…
4632 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">name<…
4633 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">actua…
4634 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">expec…
4635 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">code<…
4636 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">opera…
4637 …rt.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">generated…
4643 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
4653 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4655 <span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<spa…
4657 <span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span cla…
4659 <span class="hljs-comment">// callsfunc() must be called exactly 1 time before tracker.verify().</s…
4660 <span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">call…
4662 <span class="hljs-title function_">callsfunc</span>();
4664 <span class="hljs-comment">// Calls tracker.verify() and verifies if all tracker.calls() functions …
4665 <span class="hljs-comment">// been called exact times.</span>
4666 process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span…
4667 tracker.<span class="hljs-title function_">verify</span>();
4674 ….mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Funct…
4675 <li><code>exact</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
4676 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
4682 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4684 <span class="hljs-comment">// Creates call tracker.</span>
4685 <span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<spa…
4687 <span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span cla…
4689 <span class="hljs-comment">// Returns a function that wraps func() that must be called exact times<…
4690 <span class="hljs-comment">// before tracker.verify().</span>
4691 <span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">call…
4697 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
4699 <li>Object <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Object…
4701 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
4702 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
4703 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4705 <li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4706 <li><code>stack</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
4712 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4714 <span class="hljs-comment">// Creates call tracker.</span>
4715 <span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<spa…
4717 <span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span cla…
4719 <span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span clas…
4721 <span class="hljs-comment">// Returns a function that wraps func() that must be called exact times<…
4722 <span class="hljs-comment">// before tracker.verify().</span>
4723 <span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">call…
4725 <span class="hljs-comment">// Returns an array containing information on callsfunc()</span>
4726 tracker.<span class="hljs-title function_">report</span>();
4727 <span class="hljs-comment">// [</span>
4728 <span class="hljs-comment">// {</span>
4729 <span class="hljs-comment">// message: 'Expected the func function to be executed 2 time(s) but …
4730 <span class="hljs-comment">// executed 0 time(s).',</span>
4731 <span class="hljs-comment">// actual: 0,</span>
4732 <span class="hljs-comment">// expected: 2,</span>
4733 <span class="hljs-comment">// operator: 'func',</span>
4734 <span class="hljs-comment">// stack: stack trace</span>
4735 <span class="hljs-comment">// }</span>
4736 <span class="hljs-comment">// ]</span></code></pre>
4744 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4746 <span class="hljs-comment">// Creates call tracker.</span>
4747 <span class="hljs-keyword">const</span> tracker = <span class="hljs-keyword">new</span> assert.<spa…
4749 <span class="hljs-keyword">function</span> <span class="hljs-title function_">func</span>(<span cla…
4751 <span class="hljs-comment">// Returns a function that wraps func() that must be called exact times<…
4752 <span class="hljs-comment">// before tracker.verify().</span>
4753 <span class="hljs-keyword">const</span> callsfunc = tracker.<span class="hljs-title function_">call…
4755 <span class="hljs-title function_">callsfunc</span>();
4757 <span class="hljs-comment">// Will throw an error since callsfunc() was only called once.</span>
4758 tracker.<span class="hljs-title function_">verify</span>();</code></pre>
4764 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
4765 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
4788 <td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
4795 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
4796 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4797 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
4802 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use <a href=…
4810 … are compared with the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">…
4813 <li><a href="https://tc39.github.io/ecma262/#sec-object.prototype.tostring">Type tags</a> of object…
4814 <li>Only <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownersh…
4817 <li><a href="https://developer.mozilla.org/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_…
4819 …ozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><code>Map</code></a> keys and <…
4822 …test the <a href="https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-interna…
4824 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbo…
4825 …zilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and…
4828 …onsidered equal by the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">…
4830 <pre><code class="language-js"><span class="hljs-comment">// WARNING: This does not throw an Assert…
4831 assert.<span class="hljs-title function_">deepEqual</span>(<span class="hljs-string">'+00000000'</s…
4834 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
4836 <span class="hljs-keyword">const</span> obj1 = {
4837 <span class="hljs-attr">a</span>: {
4838 <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>
4841 <span class="hljs-keyword">const</span> obj2 = {
4842 <span class="hljs-attr">a</span>: {
4843 <span class="hljs-attr">b</span>: <span class="hljs-number">2</span>
4846 <span class="hljs-keyword">const</span> obj3 = {
4847 <span class="hljs-attr">a</span>: {
4848 <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>
4851 <span class="hljs-keyword">const</span> obj4 = <span class="hljs-title class_">Object</span>.<span …
4853 assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj1);
4854 <span class="hljs-comment">// OK</span>
4856 <span class="hljs-comment">// Values of b are different:</span>
4857 assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj2);
4858 <span class="hljs-comment">// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }</span>
4860 assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj3);
4861 <span class="hljs-comment">// OK</span>
4863 <span class="hljs-comment">// Prototypes are ignored:</span>
4864 assert.<span class="hljs-title function_">deepEqual</span>(obj1, obj4);
4865 <span class="hljs-comment">// AssertionError: { a: { b: 1 } } deepEqual {}</span></code></pre>
4879 <td><p>The <code>NaN</code> is now compared using the <a href="https://tc39.github.io/ecma262/#sec-
4889 <td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
4896 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
4897 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
4898 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
4905 <li>Primitive values are compared using the <a href="https://tc39.github.io/ecma262/#sec-samevalue"…
4906 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is…
4907 <li><a href="https://tc39.github.io/ecma262/#sec-object.prototype.tostring">Type tags</a> of object…
4908 <li><a href="https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slot…
4909 the <a href="https://tc39.github.io/ecma262/#sec-strict-equality-comparison">Strict Equality Compar…
4910 <li>Only <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownersh…
4913 <li>Enumerable own <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Globa…
4914 <li><a href="https://developer.mozilla.org/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_…
4916 …ozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><code>Map</code></a> keys and <…
4919 …zilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and…
4922-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
4924 <span class="hljs-comment">// This fails because 1 !== '1'.</span>
4925-title function_">deepStrictEqual</span>({ <span class="hljs-attr">a</span>: <span class="hljs-num…
4926 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
4927 <span class="hljs-comment">// + actual - expected</span>
4928 <span class="hljs-comment">//</span>
4929 <span class="hljs-comment">// {</span>
4930 <span class="hljs-comment">// + a: 1</span>
4931 <span class="hljs-comment">// - a: '1'</span>
4932 <span class="hljs-comment">// }</span>
4934 <span class="hljs-comment">// The following objects don't have own properties</span>
4935 <span class="hljs-keyword">const</span> date = <span class="hljs-keyword">new</span> <span class="h…
4936 <span class="hljs-keyword">const</span> object = {};
4937 <span class="hljs-keyword">const</span> fakeDate = {};
4938-title class_">Object</span>.<span class="hljs-title function_">setPrototypeOf</span>(fakeDate, <s…
4940 <span class="hljs-comment">// Different [[Prototype]]:</span>
4941 assert.<span class="hljs-title function_">deepStrictEqual</span>(object, fakeDate);
4942 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
4943 <span class="hljs-comment">// + actual - expected</span>
4944 <span class="hljs-comment">//</span>
4945 <span class="hljs-comment">// + {}</span>
4946 <span class="hljs-comment">// - Date {}</span>
4948 <span class="hljs-comment">// Different type tags:</span>
4949 assert.<span class="hljs-title function_">deepStrictEqual</span>(date, fakeDate);
4950 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
4951 <span class="hljs-comment">// + actual - expected</span>
4952 <span class="hljs-comment">//</span>
4953 <span class="hljs-comment">// + 2018-04-26T00:49:08.604Z</span>
4954 <span class="hljs-comment">// - Date {}</span>
4956 assert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-title class_">Na…
4957 <span class="hljs-comment">// OK, because of the SameValue comparison</span>
4959 <span class="hljs-comment">// Different unwrapped numbers:</span>
4960-title function_">deepStrictEqual</span>(<span class="hljs-keyword">new</span> <span class="hljs-t…
4961 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
4962 <span class="hljs-comment">// + actual - expected</span>
4963 <span class="hljs-comment">//</span>
4964 <span class="hljs-comment">// + [Number: 1]</span>
4965 <span class="hljs-comment">// - [Number: 2]</span>
4967-title function_">deepStrictEqual</span>(<span class="hljs-keyword">new</span> <span class="hljs-t…
4968 <span class="hljs-comment">// OK because the object and the string are identical when unwrapped.</s…
4970 assert.<span class="hljs-title function_">deepStrictEqual</span>(-<span class="hljs-number">0</span…
4971 <span class="hljs-comment">// OK</span>
4973 <span class="hljs-comment">// Different zeros using the SameValue Comparison:</span>
4974 assert.<span class="hljs-title function_">deepStrictEqual</span>(<span class="hljs-number">0</span>…
4975 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
4976 <span class="hljs-comment">// + actual - expected</span>
4977 <span class="hljs-comment">//</span>
4978 <span class="hljs-comment">// + 0</span>
4979 <span class="hljs-comment">// - -0</span>
4981 <span class="hljs-keyword">const</span> symbol1 = <span class="hljs-title class_">Symbol</span>();
4982 <span class="hljs-keyword">const</span> symbol2 = <span class="hljs-title class_">Symbol</span>();
4983 …pan class="hljs-title function_">deepStrictEqual</span>({ [symbol1]: <span class="hljs-number">1</…
4984 <span class="hljs-comment">// OK, because it is the same symbol on both objects.</span>
4986 …pan class="hljs-title function_">deepStrictEqual</span>({ [symbol1]: <span class="hljs-number">1</…
4987 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Inputs identical but not reference eq…
4988 <span class="hljs-comment">//</span>
4989 <span class="hljs-comment">// {</span>
4990 <span class="hljs-comment">// [Symbol()]: 1</span>
4991 <span class="hljs-comment">// }</span>
4993 <span class="hljs-keyword">const</span> weakMap1 = <span class="hljs-keyword">new</span> <span clas…
4994 <span class="hljs-keyword">const</span> weakMap2 = <span class="hljs-keyword">new</span> <span clas…
4995 <span class="hljs-keyword">const</span> weakMap3 = <span class="hljs-keyword">new</span> <span clas…
4996 weakMap3.<span class="hljs-property">unequal</span> = <span class="hljs-literal">true</span>;
4998 assert.<span class="hljs-title function_">deepStrictEqual</span>(weakMap1, weakMap2);
4999 <span class="hljs-comment">// OK, because it is impossible to compare the entries</span>
5001 <span class="hljs-comment">// Fails because weakMap3 has a property that weakMap1 does not contain:…
5002 assert.<span class="hljs-title function_">deepStrictEqual</span>(weakMap1, weakMap3);
5003 <span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
5004 <span class="hljs-comment">// + actual - expected</span>
5005 <span class="hljs-comment">//</span>
5006 <span class="hljs-comment">// WeakMap {</span>
5007 <span class="hljs-comment">// + [items unknown]</span>
5008 <span class="hljs-comment">// - [items unknown],</span>
5009 <span class="hljs-comment">// - unequal: true</span>
5010 <span class="hljs-comment">// }</span></code></pre>
5021 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5022 <li><code>regexp</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
5023 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5025 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
5029-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5031 assert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-string">'I will fai…
5032 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The input was expected to not match t…
5034 assert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-number">123</span>,…
5035 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The "string" argument must be of type…
5037 assert.<span class="hljs-title function_">doesNotMatch</span>(<span class="hljs-string">'I will pas…
5038 <span class="hljs-comment">// OK</span></code></pre>
5050 …a.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a…
5051 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type">&#x3C;RegExp></a> …
5052 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
5066 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code>Class</code></a>, <a href="…
5070 <!-- eslint-disable no-restricted-syntax -->
5071 <pre><code class="language-js">(<span class="hljs-keyword">async</span> () => {
5072 …<span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">doesNotReject</s…
5073 <span class="hljs-keyword">async</span> () => {
5074 … class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title c…
5076 <span class="hljs-title class_">SyntaxError</span>
5079 <!-- eslint-disable no-restricted-syntax -->
5080-js">assert.<span class="hljs-title function_">doesNotReject</span>(<span class="hljs-title class_…
5081 .<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> {
5082 <span class="hljs-comment">// ...</span>
5099 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
5100 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type">&#x3C;RegExp></a> …
5101 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
5114 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code>Class</code></a>, <a href="…
5118 <!-- eslint-disable no-restricted-syntax -->
5119 <pre><code class="language-js">assert.<span class="hljs-title function_">doesNotThrow</span>(
5120 <span class="hljs-function">() =></span> {
5121 … class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title c…
5123 <span class="hljs-title class_">SyntaxError</span>
5127 <!-- eslint-disable no-restricted-syntax -->
5128 <pre><code class="language-js">assert.<span class="hljs-title function_">doesNotThrow</span>(
5129 <span class="hljs-function">() =></span> {
5130 … class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title c…
5132 <span class="hljs-title class_">TypeError</span>
5137 <!-- eslint-disable no-restricted-syntax -->
5138 <pre><code class="language-js">assert.<span class="hljs-title function_">doesNotThrow</span>(
5139 <span class="hljs-function">() =></span> {
5140 … class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title c…
5142 <span class="hljs-regexp">/Wrong value/</span>,
5143 <span class="hljs-string">'Whoops'</span>
5145 <span class="hljs-comment">// Throws: AssertionError: Got unwanted exception: Whoops</span></code><…
5161 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5162 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5163 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5168 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use <a href=…
5170 using the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equal…
5172 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
5174 assert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span cl…
5175 <span class="hljs-comment">// OK, 1 == 1</span>
5176 assert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span cl…
5177 <span class="hljs-comment">// OK, 1 == '1'</span>
5178 assert.<span class="hljs-title function_">equal</span>(<span class="hljs-title class_">Na</span>N, …
5179 <span class="hljs-comment">// OK</span>
5181 assert.<span class="hljs-title function_">equal</span>(<span class="hljs-number">1</span>, <span cl…
5182 <span class="hljs-comment">// AssertionError: 1 == 2</span>
5183-title function_">equal</span>({ <span class="hljs-attr">a</span>: { <span class="hljs-attr">b</sp…
5184 <span class="hljs-comment">// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }</span></code></pre>
5195 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5200-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5202 assert.<span class="hljs-title function_">fail</span>();
5203 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Failed</span>
5205 assert.<span class="hljs-title function_">fail</span>(<span class="hljs-string">'boom'</span>);
5206 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: boom</span>
5208 …ass="hljs-title function_">fail</span>(<span class="hljs-keyword">new</span> <span class="hljs-tit…
5209 <span class="hljs-comment">// TypeError: need array</span></code></pre>
5224 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
5227 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5228 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5229 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5230 <li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5231 <li><code>stackStartFn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
5241-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5243 assert.<span class="hljs-title function_">fail</span>(<span class="hljs-string">'a'</span>, <span c…
5244 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 'a' != 'b'</span>
5246-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</spa…
5247 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 1 > 2</span>
5249 …span class="hljs-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hlj…
5250 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: fail</span>
5252-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</spa…
5253 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: whoops</span>
5255-title function_">fail</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</spa…
5256 <span class="hljs-comment">// TypeError: need array</span></code></pre>
5260 …class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">…
5261-title function_">fail</span>(<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'<…
5263 <span class="hljs-title function_">suppressFrame</span>();
5264 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 'a' !== 'b'</span>
5265 <span class="hljs-comment">// at repl:1:1</span>
5266 <span class="hljs-comment">// at ContextifyScript.Script.runInThisContext (vm.js:44:33)</span>
5267 <span class="hljs-comment">// ...</span></code></pre>
5283 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
5289-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5291 assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-literal">null</span>);
5292 <span class="hljs-comment">// OK</span>
5293 assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-number">0</span>);
5294 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0</sp…
5295 assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-string">'error'</span>);
5296 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'erro…
5297 assert.<span class="hljs-title function_">ifError</span>(<span class="hljs-keyword">new</span> <spa…
5298 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error…
5300 <span class="hljs-comment">// Create some random error frames.</span>
5301 <span class="hljs-keyword">let</span> err;
5302 (<span class="hljs-keyword">function</span> <span class="hljs-title function_">errorFrame</span>(<s…
5303 …err = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span cla…
5306 (<span class="hljs-keyword">function</span> <span class="hljs-title function_">ifErrorFrame</span>(…
5307 assert.<span class="hljs-title function_">ifError</span>(err);
5309 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test …
5310 <span class="hljs-comment">// at ifErrorFrame</span>
5311 <span class="hljs-comment">// at errorFrame</span></code></pre>
5317 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5318 <li><code>regexp</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
5319 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5321 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
5325-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5327 assert.<span class="hljs-title function_">match</span>(<span class="hljs-string">'I will fail'</spa…
5328 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The input did not match the regular .…
5330 assert.<span class="hljs-title function_">match</span>(<span class="hljs-number">123</span>, <span …
5331 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: The "string" argument must be of type…
5333 assert.<span class="hljs-title function_">match</span>(<span class="hljs-string">'I will pass'</spa…
5334 <span class="hljs-comment">// OK</span></code></pre>
5359 <td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
5366 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5367 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5368 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5373 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use <a href=…
5375 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
5377 <span class="hljs-keyword">const</span> obj1 = {
5378 <span class="hljs-attr">a</span>: {
5379 <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>
5382 <span class="hljs-keyword">const</span> obj2 = {
5383 <span class="hljs-attr">a</span>: {
5384 <span class="hljs-attr">b</span>: <span class="hljs-number">2</span>
5387 <span class="hljs-keyword">const</span> obj3 = {
5388 <span class="hljs-attr">a</span>: {
5389 <span class="hljs-attr">b</span>: <span class="hljs-number">1</span>
5392 <span class="hljs-keyword">const</span> obj4 = <span class="hljs-title class_">Object</span>.<span …
5394 assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj1);
5395 <span class="hljs-comment">// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }</span>
5397 assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj2);
5398 <span class="hljs-comment">// OK</span>
5400 assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj3);
5401 <span class="hljs-comment">// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }</span>
5403 assert.<span class="hljs-title function_">notDeepEqual</span>(obj1, obj4);
5404 <span class="hljs-comment">// OK</span></code></pre>
5416 <td><p>The <code>-0</code> and <code>+0</code> are not considered equal anymore.</p></td></tr>
5418 <td><p>The <code>NaN</code> is now compared using the <a href="https://tc39.github.io/ecma262/#sec-
5428 <td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
5435 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5436 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5437 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5440-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5442-title function_">notDeepStrictEqual</span>({ <span class="hljs-attr">a</span>: <span class="hljs-
5443 <span class="hljs-comment">// OK</span></code></pre>
5464 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5465 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5466 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5471 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use <a href=…
5472 …ve inequality with the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">…
5475 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
5477 assert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span…
5478 <span class="hljs-comment">// OK</span>
5480 assert.<span class="hljs-title function_">notEqual</span>(<span class="hljs-number">1</span>, <span…
5481 <span class="hljs-comment">// AssertionError: 1 != 1</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></code></pre>
5503 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5504 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5505 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5508 determined by the <a href="https://tc39.github.io/ecma262/#sec-samevalue">SameValue Comparison</a>.…
5509-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5511 assert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>,…
5512 <span class="hljs-comment">// OK</span>
5514 assert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>,…
5515 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected "actual" to be strictly uneq…
5516 <span class="hljs-comment">//</span>
5517 <span class="hljs-comment">// 1</span>
5519 assert.<span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-number">1</span>,…
5520 <span class="hljs-comment">// OK</span></code></pre>
5539 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
5540 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5553-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5555 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">true</span>);
5556 <span class="hljs-comment">// OK</span>
5557 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-number">1</span>);
5558 <span class="hljs-comment">// OK</span>
5560 assert.<span class="hljs-title function_">ok</span>();
5561 <span class="hljs-comment">// AssertionError: No value argument passed to `assert.ok()`</span>
5563 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">false</span>, <span …
5564 <span class="hljs-comment">// AssertionError: it's false</span>
5566 <span class="hljs-comment">// In the repl:</span>
5567 … class="hljs-title function_">ok</span>(<span class="hljs-keyword">typeof</span> <span class="hljs
5568 <span class="hljs-comment">// AssertionError: false == true</span>
5570 <span class="hljs-comment">// In a file (e.g. test.js):</span>
5571 … class="hljs-title function_">ok</span>(<span class="hljs-keyword">typeof</span> <span class="hljs
5572 <span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
5573 <span class="hljs-comment">//</span>
5574 <span class="hljs-comment">// assert.ok(typeof 123 === 'string')</span>
5576 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-literal">false</span>);
5577 <span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
5578 <span class="hljs-comment">//</span>
5579 <span class="hljs-comment">// assert.ok(false)</span>
5581 assert.<span class="hljs-title function_">ok</span>(<span class="hljs-number">0</span>);
5582 <span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
5583 <span class="hljs-comment">//</span>
5584 <span class="hljs-comment">// assert.ok(0)</span>
5586 <span class="hljs-comment">// Using `assert()` works the same:</span>
5587 <span class="hljs-title function_">assert</span>(<span class="hljs-number">0</span>);
5588 <span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
5589 <span class="hljs-comment">//</span>
5590 <span class="hljs-comment">// assert(0)</span></code></pre>
5596 …a.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a…
5597-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type">&#x3C;RegExp></a> | <a href=…
5598 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
5610 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code>Class</code></a>, <a href="…
5612 each property will be tested for including the non-enumerable <code>message</code> and
5616 <pre><code class="language-js">(<span class="hljs-keyword">async</span> () => {
5617 <span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">rejects</span>(
5618 <span class="hljs-keyword">async</span> () => {
5619 … class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title c…
5622 <span class="hljs-attr">name</span>: <span class="hljs-string">'TypeError'</span>,
5623 <span class="hljs-attr">message</span>: <span class="hljs-string">'Wrong value'</span>
5627 <pre><code class="language-js">(<span class="hljs-keyword">async</span> () => {
5628 <span class="hljs-keyword">await</span> assert.<span class="hljs-title function_">rejects</span>(
5629 <span class="hljs-keyword">async</span> () => {
5630 … class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title c…
5632 <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
5633 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">name<…
5634 …assert.<span class="hljs-title function_">strictEqual</span>(err.<span class="hljs-property">messa…
5635 <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
5639 <pre><code class="language-js">assert.<span class="hljs-title function_">rejects</span>(
5640-title class_">Promise</span>.<span class="hljs-title function_">reject</span>(<span class="hljs-k…
5641 <span class="hljs-title class_">Error</span>
5642 ).<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> {
5643 <span class="hljs-comment">// ...</span>
5647 <code>message</code> instead. This can lead to easy-to-miss mistakes. Please read the
5663 <li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
5664 <li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
5665 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
5668 determined by the <a href="https://tc39.github.io/ecma262/#sec-samevalue">SameValue Comparison</a>.…
5669-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<…
5671 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <s…
5672 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:…
5673 <span class="hljs-comment">//</span>
5674 <span class="hljs-comment">// 1 !== 2</span>
5676 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <s…
5677 <span class="hljs-comment">// OK</span>
5679 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-string">'Hello fooba…
5680 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:…
5681 <span class="hljs-comment">// + actual - expected</span>
5682 <span class="hljs-comment">//</span>
5683 <span class="hljs-comment">// + 'Hello foobar'</span>
5684 <span class="hljs-comment">// - 'Hello World!'</span>
5685 <span class="hljs-comment">// ^</span>
5687 <span class="hljs-keyword">const</span> apples = <span class="hljs-number">1</span>;
5688 <span class="hljs-keyword">const</span> oranges = <span class="hljs-number">2</span>;
5689-title function_">strictEqual</span>(apples, oranges, <span class="hljs-string">`apples <span clas…
5690 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2</span>
5692-title function_">strictEqual</span>(<span class="hljs-number">1</span>, <span class="hljs-string"…
5693 <span class="hljs-comment">// TypeError: Inputs are not identical</span></code></pre>
5716 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
5717-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type">&#x3C;RegExp></a> | <a href=…
5718 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
5721 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code>Class</code></a>, <a href="…
5724 equality including the non-enumerable <code>message</code> and <code>name</code> properties. When
5731-js"><span class="hljs-keyword">const</span> err = <span class="hljs-keyword">new</span> <span cla…
5732 err.<span class="hljs-property">code</span> = <span class="hljs-number">404</span>;
5733 err.<span class="hljs-property">foo</span> = <span class="hljs-string">'bar'</span>;
5734 err.<span class="hljs-property">info</span> = {
5735 <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>,
5736 <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>
5738 err.<span class="hljs-property">reg</span> = <span class="hljs-regexp">/abc/i</span>;
5740 assert.<span class="hljs-title function_">throws</span>(
5741 <span class="hljs-function">() =></span> {
5742 <span class="hljs-keyword">throw</span> err;
5745 <span class="hljs-attr">name</span>: <span class="hljs-string">'TypeError'</span>,
5746 <span class="hljs-attr">message</span>: <span class="hljs-string">'Wrong value'</span>,
5747 <span class="hljs-attr">info</span>: {
5748 <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>,
5749 <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>
5751 … <span class="hljs-comment">// Only properties on the validation object will be tested for.</span>
5752 …<span class="hljs-comment">// Using nested objects requires all properties to be present. Otherwis…
5753 <span class="hljs-comment">// the validation is going to fail.</span>
5757 <span class="hljs-comment">// Using regular expressions to validate error properties:</span>
5758 assert.<span class="hljs-title function_">throws</span>(
5759 <span class="hljs-function">() =></span> {
5760 <span class="hljs-keyword">throw</span> err;
5763 …<span class="hljs-comment">// The `name` and `message` properties are strings and using regular</s…
5764 …<span class="hljs-comment">// expressions on those will match against the string. If they fail, an…
5765 <span class="hljs-comment">// error is thrown.</span>
5766 <span class="hljs-attr">name</span>: <span class="hljs-regexp">/^TypeError$/</span>,
5767 <span class="hljs-attr">message</span>: <span class="hljs-regexp">/Wrong/</span>,
5768 <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span>,
5769 <span class="hljs-attr">info</span>: {
5770 <span class="hljs-attr">nested</span>: <span class="hljs-literal">true</span>,
5771 …<span class="hljs-comment">// It is not possible to use regular expressions for nested properties!…
5772 <span class="hljs-attr">baz</span>: <span class="hljs-string">'text'</span>
5774 …<span class="hljs-comment">// The `reg` property contains a regular expression and only if the</sp…
5775 …<span class="hljs-comment">// validation object contains an identical regular expression, it is go…
5776 <span class="hljs-comment">// to pass.</span>
5777 <span class="hljs-attr">reg</span>: <span class="hljs-regexp">/abc/i</span>
5781 <span class="hljs-comment">// Fails due to the different `message` and `name` properties:</span>
5782 assert.<span class="hljs-title function_">throws</span>(
5783 <span class="hljs-function">() =></span> {
5784 …ass="hljs-keyword">const</span> otherErr = <span class="hljs-keyword">new</span> <span class="hljs
5785 <span class="hljs-comment">// Copy all enumerable properties from `err` to `otherErr`.</span>
5786-keyword">for</span> (<span class="hljs-keyword">const</span> [key, value] <span class="hljs-keywo…
5789 <span class="hljs-keyword">throw</span> otherErr;
5791 …<span class="hljs-comment">// The error's `message` and `name` properties will also be checked whe…
5792 <span class="hljs-comment">// an error as validation object.</span>
5796 <pre><code class="language-js">assert.<span class="hljs-title function_">throws</span>(
5797 <span class="hljs-function">() =></span> {
5798 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
5800 <span class="hljs-title class_">Error</span>
5802 <p>Validate error message using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Gu…
5805 <pre><code class="language-js">assert.<span class="hljs-title function_">throws</span>(
5806 <span class="hljs-function">() =></span> {
5807 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
5809 <span class="hljs-regexp">/^Error: Wrong value$/</span>
5814 <pre><code class="language-js">assert.<span class="hljs-title function_">throws</span>(
5815 <span class="hljs-function">() =></span> {
5816 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
5818 <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
5819 …<span class="hljs-title function_">assert</span>(err <span class="hljs-keyword">instanceof</span> …
5820 …<span class="hljs-title function_">assert</span>(<span class="hljs-regexp">/value/</span>.<span cl…
5821 …<span class="hljs-comment">// Avoid returning anything from validation functions besides `true`.</…
5822 …<span class="hljs-comment">// Otherwise, it's not clear what part of the validation failed. Instea…
5823 …<span class="hljs-comment">// throw an error about the specific validation that failed (as done in…
5824 …<span class="hljs-comment">// example) and add as much helpful debugging information to that error…
5825 <span class="hljs-comment">// possible.</span>
5826 <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
5828 <span class="hljs-string">'unexpected error'</span>
5832 <code>message</code> instead. This can lead to easy-to-miss mistakes. Using the same
5836 <!-- eslint-disable no-restricted-syntax -->
5837 …class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">…
5838 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
5841 <span class="hljs-keyword">function</span> <span class="hljs-title function_">throwingSecond</span>…
5842 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
5845 <span class="hljs-keyword">function</span> <span class="hljs-title function_">notThrowing</span>(<s…
5847 <span class="hljs-comment">// The second argument is a string and the input function threw an Error…
5848 <span class="hljs-comment">// The first case will not throw as it does not match for the error mess…
5849 <span class="hljs-comment">// thrown by the input function!</span>
5850 assert.<span class="hljs-title function_">throws</span>(throwingFirst, <span class="hljs-string">'S…
5851 <span class="hljs-comment">// In the next example the message has no benefit over the message from …
5852 <span class="hljs-comment">// error and since it is not clear if the user intended to actually matc…
5853 <span class="hljs-comment">// against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT`…
5854 assert.<span class="hljs-title function_">throws</span>(throwingSecond, <span class="hljs-string">'…
5855 <span class="hljs-comment">// TypeError [ERR_AMBIGUOUS_ARGUMENT]</span>
5857 <span class="hljs-comment">// The string is only used (as message) in case the function does not th…
5858 assert.<span class="hljs-title function_">throws</span>(notThrowing, <span class="hljs-string">'Sec…
5859 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Missing expected exception: Second</s…
5861 <span class="hljs-comment">// If it was intended to match for the error message do this instead:</s…
5862 <span class="hljs-comment">// It does not throw because the error messages match.</span>
5863 assert.<span class="hljs-title function_">throws</span>(throwingSecond, <span class="hljs-regexp">/…
5865 <span class="hljs-comment">// If the error message does not match, an AssertionError is thrown.</sp…
5866 assert.<span class="hljs-title function_">throws</span>(throwingFirst, <span class="hljs-regexp">/S…
5867 <span class="hljs-comment">// AssertionError [ERR_ASSERTION]</span></code></pre>
5868 <p>Due to the confusing error-prone notation, avoid a string as the second
5873 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
5877 … class="language-js"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built…
5881 event in <code>net.createServer()</code>, or just a single time like in <code>fs.open()</code>.
5889 … class="language-js"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built…
5891 <span class="hljs-comment">// Return the ID of the current execution context.</span>
5892 <span class="hljs-keyword">const</span> eid = async_hooks.<span class="hljs-title function_">execut…
5894 <span class="hljs-comment">// Return the ID of the handle responsible for triggering the callback o…
5895 <span class="hljs-comment">// current execution scope to call.</span>
5896 <span class="hljs-keyword">const</span> tid = async_hooks.<span class="hljs-title function_">trigge…
5898 <span class="hljs-comment">// Create a new AsyncHook instance. All of these callbacks are optional.…
5899 <span class="hljs-keyword">const</span> asyncHook =
5900 …async_hooks.<span class="hljs-title function_">createHook</span>({ init, before, after, destroy, p…
5902 <span class="hljs-comment">// Allow callbacks of this AsyncHook instance to call. This is not an im…
5903 <span class="hljs-comment">// action after running the constructor, and must be explicitly run to b…
5904 <span class="hljs-comment">// executing callbacks.</span>
5905 asyncHook.<span class="hljs-title function_">enable</span>();
5907 <span class="hljs-comment">// Disable listening for new asynchronous events.</span>
5908 asyncHook.<span class="hljs-title function_">disable</span>();
5910 <span class="hljs-comment">//</span>
5911 <span class="hljs-comment">// The following are the callbacks that can be passed to createHook().</…
5912 <span class="hljs-comment">//</span>
5914 <span class="hljs-comment">// init is called during object construction. The resource may not have<…
5915 <span class="hljs-comment">// completed construction when this callback runs, therefore all fields …
5916 <span class="hljs-comment">// resource referenced by "asyncId" may not have been populated.</span>
5917 <span class="hljs-keyword">function</span> <span class="hljs-title function_">init</span>(<span cla…
5919 <span class="hljs-comment">// Before is called just before the resource's callback is called. It ca…
5920 <span class="hljs-comment">// called 0-N times for handles (such as TCPWrap), and will be called ex…
5921 <span class="hljs-comment">// time for requests (such as FSReqCallback).</span>
5922 <span class="hljs-keyword">function</span> <span class="hljs-title function_">before</span>(<span c…
5924 <span class="hljs-comment">// After is called just after the resource's callback has finished.</spa…
5925 <span class="hljs-keyword">function</span> <span class="hljs-title function_">after</span>(<span cl…
5927 <span class="hljs-comment">// Destroy is called when the resource is destroyed.</span>
5928 <span class="hljs-keyword">function</span> <span class="hljs-title function_">destroy</span>(<span …
5930 <span class="hljs-comment">// promiseResolve is called only for promise resources, when the</span>
5931 <span class="hljs-comment">// `resolve` function passed to the `Promise` constructor is invoked</sp…
5932 <span class="hljs-comment">// (either directly or through other means of resolving a promise).</spa…
5933 <span class="hljs-keyword">function</span> <span class="hljs-title function_">promiseResolve</span>…
5939 <li><code>callbacks</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
5941 <li><code>init</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
5942 <li><code>before</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
5943 <li><code>after</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
5944 <li><code>destroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
5945 <li><code>promiseResolve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
5958 … class="language-js"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built…
5960 <span class="hljs-keyword">const</span> asyncHook = async_hooks.<span class="hljs-title function_">…
5961 …<span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsy…
5962 <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) { }
5965 <pre><code class="language-js"><span class="hljs-keyword">class</span> <span class="hljs-title clas…
5966 …<span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsy…
5967 <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) {}
5970 …s="hljs-keyword">class</span> <span class="hljs-title class_">MyAddedCallbacks</span> <span class=…
5971 <span class="hljs-title function_">before</span>(<span class="hljs-params">asyncId</span>) { }
5972 <span class="hljs-title function_">after</span>(<span class="hljs-params">asyncId</span>) { }
5975-keyword">const</span> asyncHook = async_hooks.<span class="hljs-title function_">createHook</span…
5984 with <code>--abort-on-uncaught-exception</code>, in which case a stack trace will be
6001 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
6002 <span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span cla…
6004 <span class="hljs-keyword">function</span> <span class="hljs-title function_">debug</span>(<span cl…
6005 …<span class="hljs-comment">// Use a function like this one when debugging inside an AsyncHooks cal…
6006-title function_">writeFileSync</span>(<span class="hljs-string">'log.out'</span>, <span class="hl…
6021 provided, enabling is a no-op.</p>
6024 … class="language-js"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built…
6026 …an class="hljs-keyword">const</span> hook = async_hooks.<span class="hljs-title function_">createH…
6041 <li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6042 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
6043 <li><code>triggerAsyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
6045 <li><code>resource</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
6055-js"><span class="hljs-built_in">require</span>(<span class="hljs-string">'net'</span>).<span clas…
6056 <span class="hljs-comment">// OR</span>
6057 …="hljs-built_in">clearTimeout</span>(<span class="hljs-built_in">setTimeout</span>(<span class="hl…
6064 <pre><code class="language-text">FSEVENTWRAP, FSREQCALLBACK, GETADDRINFOREQWRAP, GETNAMEINFOREQWRAP…
6081 <pre><code class="language-js"><span class="hljs-keyword">const</span> { fd } = process.<span class…
6083 async_hooks.<span class="hljs-title function_">createHook</span>({
6084 …<span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsy…
6085 …<span class="hljs-keyword">const</span> eid = async_hooks.<span class="hljs-title function_">execu…
6086 fs.<span class="hljs-title function_">writeSync</span>(
6088-string">`<span class="hljs-subst">${type}</span>(<span class="hljs-subst">${asyncId}</span>): tri…
6090 }).<span class="hljs-title function_">enable</span>();
6092-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">conn<…
6094 <pre><code class="language-console">TCPSERVERWRAP(5): trigger: 1 execution: 1
6120 <pre><code class="language-js"><span class="hljs-keyword">const</span> { fd } = process.<span class…
6122 <span class="hljs-keyword">let</span> indent = <span class="hljs-number">0</span>;
6123 async_hooks.<span class="hljs-title function_">createHook</span>({
6124 …<span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsy…
6125 …<span class="hljs-keyword">const</span> eid = async_hooks.<span class="hljs-title function_">execu…
6126 …<span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span cla…
6127 fs.<span class="hljs-title function_">writeSync</span>(
6129 …<span class="hljs-string">`<span class="hljs-subst">${indentStr}</span><span class="hljs-subst">${…
6130 …<span class="hljs-string">` trigger: <span class="hljs-subst">${triggerAsyncId}</span> execution: …
6132 <span class="hljs-title function_">before</span>(<span class="hljs-params">asyncId</span>) {
6133 …<span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span cla…
6134 …ss="hljs-title function_">writeSync</span>(fd, <span class="hljs-string">`<span class="hljs-subst"…
6135 indent += <span class="hljs-number">2</span>;
6137 <span class="hljs-title function_">after</span>(<span class="hljs-params">asyncId</span>) {
6138 indent -= <span class="hljs-number">2</span>;
6139 …<span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span cla…
6140 …ss="hljs-title function_">writeSync</span>(fd, <span class="hljs-string">`<span class="hljs-subst"…
6142 <span class="hljs-title function_">destroy</span>(<span class="hljs-params">asyncId</span>) {
6143 …<span class="hljs-keyword">const</span> indentStr = <span class="hljs-string">' '</span>.<span cla…
6144 …s="hljs-title function_">writeSync</span>(fd, <span class="hljs-string">`<span class="hljs-subst">…
6146 }).<span class="hljs-title function_">enable</span>();
6148-title function_">createServer</span>(<span class="hljs-function">() =></span> {}).<span class="hl…
6149 <span class="hljs-comment">// Let's wait 10ms before logging the server started.</span>
6150 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
6151-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6152 }, <span class="hljs-number">10</span>);
6155 <pre><code class="language-console">TCPSERVERWRAP(5): trigger: 1 execution: 1
6162 <span class="hljs-meta">></span><span class="language-bash">>> 7</span>
6171 <pre><code class="language-console"> root(1)
6187 <pre><code class="language-console"> bootstrap(1)
6199 <li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6214 <li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6222 <li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6235 <li><code>asyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6242-js"><span class="hljs-keyword">new</span> <span class="hljs-title class_">Promise</span>(<span cl…
6244 <pre><code class="language-text">init for PROMISE with id 5, trigger id: 1
6255 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
6261 <p>Using <code>executionAsyncResource()</code> in the top-level execution context will
6263 but having an object representing the top-level can be helpful.</p>
6264 …de class="language-js"><span class="hljs-keyword">const</span> { open } = <span class="hljs-built_…
6265 …class="hljs-keyword">const</span> { executionAsyncId, executionAsyncResource } = <span class="hljs
6267-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6268 …n class="hljs-title function_">open</span>(__filename, <span class="hljs-string">'r'</span>, <span…
6269-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6273 …lass="language-js"><span class="hljs-keyword">const</span> { createServer } = <span class="hljs-bu…
6274 <span class="hljs-keyword">const</span> {
6278 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'async_hooks'</span>);
6279 …ss="hljs-keyword">const</span> sym = <span class="hljs-title class_">Symbol</span>(<span class="hl…
6281 <span class="hljs-title function_">createHook</span>({
6282 …<span class="hljs-title function_">init</span>(<span class="hljs-params">asyncId, type, triggerAsy…
6283 …<span class="hljs-keyword">const</span> cr = <span class="hljs-title function_">executionAsyncReso…
6284 <span class="hljs-keyword">if</span> (cr) {
6288 }).<span class="hljs-title function_">enable</span>();
6290 …ass="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>(<s…
6291 …<span class="hljs-title function_">executionAsyncResource</span>()[sym] = { <span class="hljs-attr…
6292 …<span class="hljs-built_in">setTimeout</span>(<span class="hljs-keyword">function</span>(<span cla…
6293 …js-title function_">end</span>(<span class="hljs-variable constant_">JSON</span>.<span class="hljs
6294 }, <span class="hljs-number">100</span>);
6295 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code><…
6309 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
6312 … class="language-js"><span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built…
6314-variable language_">console</span>.<span class="hljs-title function_">log</span>(async_hooks.<spa…
6315 …span class="hljs-title function_">open</span>(path, <span class="hljs-string">'r'</span>, <span cl…
6316-variable language_">console</span>.<span class="hljs-title function_">log</span>(async_hooks.<spa…
6320-js"><span class="hljs-keyword">const</span> server = net.<span class="hljs-title function_">creat…
6321 …<span class="hljs-comment">// Returns the ID of the server, not of the new connection, because the…
6322 …<span class="hljs-comment">// callback runs in the execution scope of the server's MakeCallback().…
6323 async_hooks.<span class="hljs-title function_">executionAsyncId</span>();
6325 }).<span class="hljs-title function_">listen</span>(port, <span class="hljs-function">() =></span> {
6326 …<span class="hljs-comment">// Returns the ID of a TickObject (process.nextTick()) because all</spa…
6327 <span class="hljs-comment">// callbacks passed to .listen() are wrapped in a nextTick().</span>
6328 async_hooks.<span class="hljs-title function_">executionAsyncId</span>();
6334 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
6337-js"><span class="hljs-keyword">const</span> server = net.<span class="hljs-title function_">creat…
6338 …<span class="hljs-comment">// The resource that caused (or triggered) this callback to be called</…
6339 …<span class="hljs-comment">// was that of the new connection. Thus the return value of triggerAsyn…
6340 <span class="hljs-comment">// is the asyncId of "conn".</span>
6341 async_hooks.<span class="hljs-title function_">triggerAsyncId</span>();
6343 }).<span class="hljs-title function_">listen</span>(port, <span class="hljs-function">() =></span> {
6344 …<span class="hljs-comment">// Even though all callbacks passed to .listen() are wrapped in a nextT…
6345 …<span class="hljs-comment">// the callback itself exists because the call to the server's .listen(…
6346 <span class="hljs-comment">// was made. So the return value would be the ID of the server.</span>
6347 async_hooks.<span class="hljs-title function_">triggerAsyncId</span>();
6356 …<code class="language-js"><span class="hljs-keyword">const</span> ah = <span class="hljs-built_in"…
6357-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>(<span class="hljs-
6358-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6360 <span class="hljs-comment">// produces:</span>
6361 <span class="hljs-comment">// eid 1 tid 0</span></code></pre>
6368 …<code class="language-js"><span class="hljs-keyword">const</span> ah = <span class="hljs-built_in"…
6369-title function_">createHook</span>({ <span class="hljs-title function_">init</span>(<span class="…
6370-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>(<span class="hljs-
6371-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6373 <span class="hljs-comment">// produces:</span>
6374 <span class="hljs-comment">// eid 7 tid 6</span></code></pre>
6396-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">AsyncResource</span…
6398 <span class="hljs-comment">// AsyncResource() is meant to be extended. Instantiating a</span>
6399 <span class="hljs-comment">// new AsyncResource() also triggers init. If triggerAsyncId is omitted …
6400 <span class="hljs-comment">// async_hook.executionAsyncId() is used.</span>
6401 <span class="hljs-keyword">const</span> asyncResource = <span class="hljs-keyword">new</span> <span…
6402-attr">triggerAsyncId</span>: <span class="hljs-title function_">executionAsyncId</span>(), <span …
6405 <span class="hljs-comment">// Run a function in the execution context of the resource. This will</s…
6406 <span class="hljs-comment">// * establish the context of the resource</span>
6407 <span class="hljs-comment">// * trigger the AsyncHooks before callbacks</span>
6408 <span class="hljs-comment">// * call the provided function `fn` with the supplied arguments</span>
6409 <span class="hljs-comment">// * trigger the AsyncHooks after callbacks</span>
6410 <span class="hljs-comment">// * restore the original execution context</span>
6411 asyncResource.<span class="hljs-title function_">runInAsyncScope</span>(fn, thisArg, ...args);
6413 <span class="hljs-comment">// Call AsyncHooks destroy callbacks.</span>
6414 asyncResource.<span class="hljs-title function_">emitDestroy</span>();
6416 <span class="hljs-comment">// Return the unique ID assigned to the AsyncResource instance.</span>
6417 asyncResource.<span class="hljs-title function_">asyncId</span>();
6419 <span class="hljs-comment">// Return the trigger ID for the AsyncResource instance.</span>
6420 asyncResource.<span class="hljs-title function_">triggerAsyncId</span>();</code></pre>
6423 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
6424 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
6426 <li><code>triggerAsyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
6428 <li><code>requireManualDestroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
6439-js"><span class="hljs-keyword">class</span> <span class="hljs-title class_">DBQuery</span> <span …
6440 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">db</span>) {
6441 <span class="hljs-variable language_">super</span>(<span class="hljs-string">'DBQuery'</span>);
6442 <span class="hljs-variable language_">this</span>.<span class="hljs-property">db</span> = db;
6445 …<span class="hljs-title function_">getInfo</span>(<span class="hljs-params">query, callback</span>…
6446-variable language_">this</span>.<span class="hljs-property">db</span>.<span class="hljs-title fun…
6447 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">runInAsyncSco…
6451 <span class="hljs-title function_">close</span>(<span class="hljs-params"></span>) {
6452 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">db</span> = <span cl…
6453 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emitDestroy</…
6461 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
6462 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
6473 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
6483 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
6485 <li><code>thisArg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6486 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6502 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
6506 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
6509 <p><a id="async-resource-worker-pool"></a></p>
6516 …class="language-js"><span class="hljs-keyword">const</span> { parentPort } = <span class="hljs-bui…
6517 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
6518 …ntPort.<span class="hljs-title function_">postMessage</span>(task.<span class="hljs-property">a</s…
6521-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">AsyncResource</span…
6522 …"hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span> } = <span class=…
6523 <span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span cla…
6524 …ss="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span> } = <span class="hl…
6526 <span class="hljs-keyword">const</span> kTaskInfo = <span class="hljs-title class_">Symbol</span>(<…
6527 <span class="hljs-keyword">const</span> kWorkerFreedEvent = <span class="hljs-title class_">Symbol<…
6529 …="hljs-keyword">class</span> <span class="hljs-title class_">WorkerPoolTaskInfo</span> <span class…
6530 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">callback</span>) {
6531 …<span class="hljs-variable language_">super</span>(<span class="hljs-string">'WorkerPoolTaskInfo'<…
6532 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">callback</span> = ca…
6535 <span class="hljs-title function_">done</span>(<span class="hljs-params">err, result</span>) {
6536-variable language_">this</span>.<span class="hljs-title function_">runInAsyncScope</span>(<span c…
6537 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emitDestroy</…
6541 …lass="hljs-keyword">class</span> <span class="hljs-title class_">WorkerPool</span> <span class="hl…
6542 …<span class="hljs-title function_">constructor</span>(<span class="hljs-params">numThreads</span>)…
6543 <span class="hljs-variable language_">super</span>();
6544 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">numThreads</span> = …
6545 … <span class="hljs-variable language_">this</span>.<span class="hljs-property">workers</span> = [];
6546 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">freeWorkers</span> =…
6547 <span class="hljs-variable language_">this</span>.<span class="hljs-property">tasks</span> = [];
6549 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs
6550 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">addNewWorker<…
6552 <span class="hljs-comment">// Any time the kWorkerFreedEvent is emitted, dispatch</span>
6553 <span class="hljs-comment">// the next task pending in the queue, if any.</span>
6554 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">on</span>(kWo…
6555-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property"…
6556-keyword">const</span> { task, callback } = <span class="hljs-variable language_">this</span>.<spa…
6557 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">runTask</span…
6562 <span class="hljs-title function_">addNewWorker</span>(<span class="hljs-params"></span>) {
6563-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title clas…
6564 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
6565 …<span class="hljs-comment">// In case of success: Call the callback that was passed to `runTask`,<…
6566 …<span class="hljs-comment">// remove the `TaskInfo` associated with the Worker, and mark it as fre…
6567 <span class="hljs-comment">// again.</span>
6568 …worker[kTaskInfo].<span class="hljs-title function_">done</span>(<span class="hljs-literal">null</…
6569 worker[kTaskInfo] = <span class="hljs-literal">null</span>;
6570 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">freeWorkers</span>.<…
6571 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(k…
6573 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
6574 …<span class="hljs-comment">// In case of an uncaught exception: Call the callback that was passed …
6575 <span class="hljs-comment">// `runTask` with the error.</span>
6576 <span class="hljs-keyword">if</span> (worker[kTaskInfo])
6577 …worker[kTaskInfo].<span class="hljs-title function_">done</span>(err, <span class="hljs-literal">n…
6578 <span class="hljs-keyword">else</span>
6579 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<…
6580 …<span class="hljs-comment">// Remove the worker from the list and start a new Worker to replace th…
6581 <span class="hljs-comment">// current one.</span>
6582-variable language_">this</span>.<span class="hljs-property">workers</span>.<span class="hljs-titl…
6583 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">addNewWorker<…
6585 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">workers</span>.<span…
6586 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">freeWorkers</span>.<…
6587 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(k…
6590 …<span class="hljs-title function_">runTask</span>(<span class="hljs-params">task, callback</span>)…
6591-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property"…
6592 <span class="hljs-comment">// No free threads, wait until a worker thread becomes free.</span>
6593 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">tasks</span>.<span c…
6594 <span class="hljs-keyword">return</span>;
6597 …ljs-keyword">const</span> worker = <span class="hljs-variable language_">this</span>.<span class="…
6598 …worker[kTaskInfo] = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WorkerPo…
6599 worker.<span class="hljs-title function_">postMessage</span>(task);
6602 <span class="hljs-title function_">close</span>(<span class="hljs-params"></span>) {
6603-keyword">for</span> (<span class="hljs-keyword">const</span> worker <span class="hljs-keyword">of…
6607 <span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <s…
6614-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">WorkerPool</span> = <…
6615 <span class="hljs-keyword">const</span> os = <span class="hljs-built_in">require</span>(<span class…
6617-keyword">const</span> pool = <span class="hljs-keyword">new</span> <span class="hljs-title class_…
6619 <span class="hljs-keyword">let</span> finished = <span class="hljs-number">0</span>;
6620 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
6621-title function_">runTask</span>({ <span class="hljs-attr">a</span>: <span class="hljs-number">42<…
6622 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
6623 <span class="hljs-keyword">if</span> (++finished === <span class="hljs-number">10</span>)
6624 pool.<span class="hljs-title function_">close</span>();
6633 approach can be applied to a <a href="#stream_stream"><code>Stream</code></a> or a similar event-dr…
6634 …lass="language-js"><span class="hljs-keyword">const</span> { createServer } = <span class="hljs-bu…
6635-keyword">const</span> { <span class="hljs-title class_">AsyncResource</span>, executionAsyncId } …
6637 …ass="hljs-keyword">const</span> server = <span class="hljs-title function_">createServer</span>(<s…
6638-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-title clas…
6639 <span class="hljs-comment">// Execution context is bound to the current outer scope.</span>
6641 …req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span c…
6642 …<span class="hljs-comment">// Execution context is bound to the scope that caused 'close' to emit.…
6644 res.<span class="hljs-title function_">end</span>();
6645 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code><…
6652 or any other asynchronous duration. It is similar to thread-local storage
6656 implementation that involves significant optimizations that are non-obvious to
6661 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
6662 …ljs-keyword">const</span> { <span class="hljs-title class_">AsyncLocalStorage</span> } = <span cla…
6664 <span class="hljs-keyword">const</span> asyncLocalStorage = <span class="hljs-keyword">new</span> <…
6666 <span class="hljs-keyword">function</span> <span class="hljs-title function_">logWithId</span>(<spa…
6667 …<span class="hljs-keyword">const</span> id = asyncLocalStorage.<span class="hljs-title function_">…
6668-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6671 <span class="hljs-keyword">let</span> idSeq = <span class="hljs-number">0</span>;
6672 http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span clas…
6673 …asyncLocalStorage.<span class="hljs-title function_">run</span>(idSeq++, <span class="hljs-functio…
6674 <span class="hljs-title function_">logWithId</span>(<span class="hljs-string">'start'</span>);
6675 <span class="hljs-comment">// Imagine any chain of async operations here</span>
6676 … <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> {
6677 … <span class="hljs-title function_">logWithId</span>(<span class="hljs-string">'finish'</span>);
6678 res.<span class="hljs-title function_">end</span>();
6681 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8080</span>);
6683 http.<span class="hljs-title function_">get</span>(<span class="hljs-string">'http://localhost:8080…
6684 http.<span class="hljs-title function_">get</span>(<span class="hljs-string">'http://localhost:8080…
6685 <span class="hljs-comment">// Prints:</span>
6686 <span class="hljs-comment">// 0: start</span>
6687 <span class="hljs-comment">// 1: start</span>
6688 <span class="hljs-comment">// 0: finish</span>
6689 <span class="hljs-comment">// 1: finish</span></code></pre>
6718 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
6729 <li><code>store</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
6735 …><code class="language-js"><span class="hljs-keyword">const</span> store = { <span class="hljs-att…
6736 <span class="hljs-comment">// Replaces previous store with the given store object</span>
6737 asyncLocalStorage.<span class="hljs-title function_">enterWith</span>(store);
6738 asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment">…
6739 <span class="hljs-title function_">someAsyncOperation</span>(<span class="hljs-function">() =></spa…
6740 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6748 …><code class="language-js"><span class="hljs-keyword">const</span> store = { <span class="hljs-att…
6750 emitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'my-event'</span>, <…
6751 asyncLocalStorage.<span class="hljs-title function_">enterWith</span>(store);
6753 emitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'my-event'</span>, <…
6754 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6757 asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment">…
6758 emitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'my-event'</span>);
6759 asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment">…
6765 <li><code>store</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
6766 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
6767 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6777 …><code class="language-js"><span class="hljs-keyword">const</span> store = { <span class="hljs-att…
6778 <span class="hljs-keyword">try</span> {
6779 …asyncLocalStorage.<span class="hljs-title function_">run</span>(store, <span class="hljs-function"…
6780 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6781 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
6782 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6783 }, <span class="hljs-number">200</span>);
6784 …<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-ti…
6786 } <span class="hljs-keyword">catch</span> (e) {
6787 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6788 <span class="hljs-comment">// The error will be caught here</span>
6795 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
6796 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
6804 The stacktrace is not impacted by this call and the context is re-entered.</p>
6806 <pre><code class="language-js"><span class="hljs-comment">// Within a call to run</span>
6807 <span class="hljs-keyword">try</span> {
6808 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6809 …asyncLocalStorage.<span class="hljs-title function_">exit</span>(<span class="hljs-function">() =>…
6810 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6811 …<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-ti…
6813 } <span class="hljs-keyword">catch</span> (e) {
6814 …asyncLocalStorage.<span class="hljs-title function_">getStore</span>(); <span class="hljs-comment"…
6815 <span class="hljs-comment">// The error will be caught here</span>
6820 …e-js"><span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span cla…
6821-keyword">await</span> asyncLocalStorage.<span class="hljs-title function_">run</span>(<span class…
6822 …lStorage.<span class="hljs-title function_">getStore</span>().<span class="hljs-title function_">s…
6823 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">foo</span>(); <span cl…
6834 <p>If your code is callback-based, it is enough to promisify it with
6836 <p>If you need to keep using callback-based API, or your code assumes
6842 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
6844 <p><code>Buffer</code> objects are used to represent a fixed-length sequence of bytes. Many
6846 …lass is a subclass of JavaScript's <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
6848 plain <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uin…
6851 <pre><code class="language-js"><span class="hljs-comment">// Creates a zero-filled Buffer of length…
6852 …"hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs
6854 <span class="hljs-comment">// Creates a Buffer of length 10,</span>
6855 <span class="hljs-comment">// filled with bytes which all have the value `1`.</span>
6856-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
6858 <span class="hljs-comment">// Creates an uninitialized buffer of length 10.</span>
6859 <span class="hljs-comment">// This is faster than calling Buffer.alloc() but the returned</span>
6860 <span class="hljs-comment">// Buffer instance might contain old data that needs to be</span>
6861 <span class="hljs-comment">// overwritten using fill(), write(), or other functions that fill the B…
6862 <span class="hljs-comment">// contents.</span>
6863 …js-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-ti…
6865 <span class="hljs-comment">// Creates a Buffer containing the bytes [1, 2, 3].</span>
6866-keyword">const</span> buf4 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
6868 <span class="hljs-comment">// Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries</spa…
6869 <span class="hljs-comment">// are all truncated using `(value &#x26; 255)` to fit into the range 0–…
6870-keyword">const</span> buf5 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
6872 <span class="hljs-comment">// Creates a Buffer containing the UTF-8-encoded bytes for the string 't…
6873 <span class="hljs-comment">// [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation)</span>
6874 <span class="hljs-comment">// [116, 195, 169, 115, 116] (in decimal notation)</span>
6875 …="hljs-keyword">const</span> buf6 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
6877 <span class="hljs-comment">// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74…
6878-keyword">const</span> buf7 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
6894 specified. If no character encoding is specified, UTF-8 will be used as the
6896-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
6898-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
6899 <span class="hljs-comment">// Prints: 68656c6c6f20776f726c64</span>
6900-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
6901 <span class="hljs-comment">// Prints: aGVsbG8gd29ybGQ=</span>
6903-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6904 <span class="hljs-comment">// Prints: &#x3C;Buffer 66 68 71 77 68 67 61 64 73></span>
6905-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
6906 <span class="hljs-comment">// Prints: &#x3C;Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 …
6910 <p><code>'utf8'</code>: Multi-byte encoded Unicode characters. Many web pages and other
6911 document formats use <a href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. This is the default c…
6913 valid UTF-8 data, the Unicode replacement character <code>U+FFFD</code> � will be used
6917 <p><code>'utf16le'</code>: Multi-byte encoded Unicode characters. Unlike <code>'utf8'</code>, each
6919 …n.wikipedia.org/wiki/Endianness">little-endian</a> variant of <a href="https://en.wikipedia.org/wi…
6922 <p><code>'latin1'</code>: Latin-1 stands for <a href="https://en.wikipedia.org/wiki/ISO-8859-1">ISO
6924 encoded using a single byte. Characters that do not fit into that range are
6930 <p>Node.js also supports the following binary-to-text encodings. For
6931 binary-to-text encodings, the naming convention is reversed: Converting a
6938 specified in <a href="https://tools.ietf.org/html/rfc4648#section-5">RFC 4648, Section 5</a>. White…
6939 tabs, and new lines contained within the base64-encoded string are ignored.</p>
6942 <p><code>'base64url'</code>: <a href="https://tools.ietf.org/html/rfc4648#section-5">base64url</a> …
6943 <a href="https://tools.ietf.org/html/rfc4648#section-5">RFC 4648, Section 5</a>. When creating a <c…
6944 encoding will also correctly accept regular base64-encoded strings. When
6956 <p><code>'ascii'</code>: For 7-bit <a href="https://en.wikipedia.org/wiki/ASCII">ASCII</a> data onl…
6961 (or, if the data is known to always be ASCII-only, <code>'latin1'</code>) will be a
6962 better choice when encoding or decoding ASCII-only text. It is only provided
6966 …ias for <code>'latin1'</code>. See <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMSt…
6969 between strings and <code>Buffer</code>s, typically <code>'utf-8'</code> is the right choice.</p>
6972 <p><code>'ucs2'</code>: Alias of <code>'utf16le'</code>. UCS-2 used to refer to a variant of UTF-16
6977-js"><span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(…
6978 <span class="hljs-comment">// Prints &#x3C;Buffer 1a>, data truncated when first non-hexadecimal va…
6979 <span class="hljs-comment">// ('g') encountered.</span>
6981 …ass="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="…
6982 <span class="hljs-comment">// Prints &#x3C;Buffer 1a>, data truncated when data ends in single digi…
6984 …ass="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="…
6985 <span class="hljs-comment">// Prints &#x3C;Buffer 16 34>, all data represented.</span></code></pre>
6987 both <code>'latin1'</code> and <code>'ISO-8859-1'</code> to <code>'win-1252'</code>. This means tha…
6990 <code>'win-1252'</code>-encoded data, and using <code>'latin1'</code> encoding may incorrectly deco…
7002 …la.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> …
7003 instances. All <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Ob…
7005 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArra…
7008 <li>While <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects…
7011 compatibility. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Ob…
7017 <p>There are two ways to create new <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
7019 <li>Passing a <code>Buffer</code> to a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
7021 of the target type.</li>
7023-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7024 …s="hljs-keyword">const</span> uint32array = <span class="hljs-keyword">new</span> <span class="hlj…
7026 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7028 <span class="hljs-comment">// Prints: Uint32Array(4) [ 1, 2, 3, 4 ]</span></code></pre>
7030 <li>Passing the <code>Buffer</code>s underlying <a href="https://developer.mozilla.org/en-US/docs/W…
7031 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArra…
7033-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7034 …s="hljs-keyword">const</span> uint16array = <span class="hljs-keyword">new</span> <span class="hlj…
7035 buf.<span class="hljs-property">buffer</span>,
7036 buf.<span class="hljs-property">byteOffset</span>,
7037 …class="hljs-property">length</span> / <span class="hljs-title class_">Uint</span>16<span class="hl…
7039 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7041 <span class="hljs-comment">// Prints: Uint16Array(5) [ 104, 101, 108, 108, 111 ]</span></code></pre>
7043 memory as a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objec…
7046-js"><span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span cla…
7048 arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>;
7049 arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>;
7051 <span class="hljs-comment">// Copies the contents of `arr`.</span>
7052 <span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span …
7054 <span class="hljs-comment">// Shares memory with `arr`.</span>
7055 …hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-
7057 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7058 <span class="hljs-comment">// Prints: &#x3C;Buffer 88 a0></span>
7059 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7060 <span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 a0 0f></span>
7062 arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>;
7064 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7065 <span class="hljs-comment">// Prints: &#x3C;Buffer 88 a0></span>
7066 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7067 <span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 70 17></span></code></pre>
7068 <p>When creating a <code>Buffer</code> using a <a href="https://developer.mozilla.org/en-US/docs/We…
7069 possible to use only a portion of the underlying <a href="https://developer.mozilla.org/en-US/docs/…
7071-js"><span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span cla…
7072-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title…
7074 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7075 <span class="hljs-comment">// Prints: 16</span></code></pre>
7076 <p>The <code>Buffer.from()</code> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
7077 implementations. Specifically, the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
7093-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7095 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> b <span class="hljs-
7096 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
7098 <span class="hljs-comment">// Prints:</span>
7099 <span class="hljs-comment">// 1</span>
7100 <span class="hljs-comment">// 2</span>
7101 <span class="hljs-comment">// 3</span></code></pre>
7108 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
7109 <p>A <a href="https://developer.mozilla.org/en-US/docs/Web/API/Blob"><code>Blob</code></a> encapsul…
7116-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="h…
7117-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a>,…
7119 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
7121 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7123 …//developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;…
7130-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a>,…
7138 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
7140 <p>Returns a promise that fulfills with an <a href="https://developer.mozilla.org/en-US/docs/Web/Ja…
7152 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
7153 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
7154 …s://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3…
7163 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
7165 <p>Returns a promise that resolves the contents of the <code>Blob</code> decoded as a UTF-8
7172 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
7174 <p>The content-type of the <code>Blob</code>.</p>
7180-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Blob</span> } = <sp…
7181 …class="hljs-keyword">const</span> blob = <span class="hljs-keyword">new</span> <span class="hljs-t…
7182 …s="hljs-keyword">const</span> { <span class="hljs-attr">setTimeout</span>: delay } = <span class="…
7184 <span class="hljs-keyword">const</span> mc1 = <span class="hljs-keyword">new</span> <span class="hl…
7185 <span class="hljs-keyword">const</span> mc2 = <span class="hljs-keyword">new</span> <span class="hl…
7187 mc1.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span c…
7188 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class=…
7189 mc1.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>();
7192 mc2.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span c…
7193 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">delay</span>(<span clas…
7194 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class=…
7195 mc2.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>();
7198 mc1.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(…
7199 mc2.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(…
7201 <span class="hljs-comment">// The Blob is still usable after posting.</span>
7202 …s-title function_">text</span>().<span class="hljs-title function_">then</span>(<span class="hljs-
7212 <td><p>Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exce…
7216 <td><p>Specifying an invalid string for <code>fill</code> now results in a zero-filled buffer.</p><…
7223 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
7224-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
7226 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7230 <code>Buffer</code> will be zero-filled.</p>
7231-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7233 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7234 <span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 00></span></code></pre>
7240-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7242 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7243 <span class="hljs-comment">// Prints: &#x3C;Buffer 61 61 61 61 61></span></code></pre>
7246-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7248 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7249 <span class="hljs-comment">// Prints: &#x3C;Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64></span></code><…
7268 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
7277-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7279 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7280 <span class="hljs-comment">// Prints (contents may vary): &#x3C;Buffer a0 8b 28 3f 01 00 00 00 50 3…
7282 buf.<span class="hljs-title function_">fill</span>(<span class="hljs-number">0</span>);
7284 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7285 <span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 00 00 00 00 00 00></span></code></pr…
7287 <p>The <code>Buffer</code> module pre-allocates an internal <code>Buffer</code> instance of
7293 <p>Use of this pre-allocated internal memory pool is a key difference between
7305 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
7309 is thrown. A zero-length <code>Buffer</code> is created if <code>size</code> is 0.</p>
7315 allocations under 4KB are sliced from a single pre-allocated <code>Buffer</code>. This
7322 to create an un-pooled <code>Buffer</code> instance using <code>Buffer.allocUnsafeSlow()</code> and
7324 <pre><code class="language-js"><span class="hljs-comment">// Need to keep around a few small chunks…
7325 <span class="hljs-keyword">const</span> store = [];
7327 socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <s…
7328 <span class="hljs-keyword">let</span> data;
7329 …<span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (data = readab…
7330 <span class="hljs-comment">// Allocate for retained data.</span>
7331 …s-keyword">const</span> sb = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title…
7333 <span class="hljs-comment">// Copy the data into the new allocation.</span>
7334 …an class="hljs-title function_">copy</span>(sb, <span class="hljs-number">0</span>, <span class="h…
7336 store.<span class="hljs-title function_">push</span>(sb);
7355-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
7357 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7359 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
7362 This is not the same as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
7365 For strings that contain non-base64/hex-encoded data (e.g. whitespace), the
7368 <pre><code class="language-js"><span class="hljs-keyword">const</span> str = <span class="hljs-stri…
7370-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
7371 …<span class="hljs-string">`<span class="hljs-subst">${Buffer.byteLength(str, <span class="hljs-str…
7372 <span class="hljs-comment">// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes</span></code></pre>
7373-US/docs/Web/JavaScript/Reference/Global_Objects/DataView"><code>DataView</code></a>/<a href="http…
7374 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArr…
7389 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
7390 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
7391 …://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C…
7397-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
7398 …="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
7399 <span class="hljs-keyword">const</span> arr = [buf1, buf2];
7401-variable language_">console</span>.<span class="hljs-title function_">log</span>(arr.<span class=…
7402 <span class="hljs-comment">// Prints: [ &#x3C;Buffer 30 31 32 33>, &#x3C;Buffer 31 32 33 34> ]</spa…
7403 <span class="hljs-comment">// (This result is equal to: [buf2, buf1].)</span></code></pre>
7417-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array[]></a> …
7419 <li><code>totalLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
7425 <p>If the list has no items, or if the <code>totalLength</code> is 0, then a new zero-length
7432 <pre><code class="language-js"><span class="hljs-comment">// Create a single `Buffer` from a list o…
7434 …"hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs
7435 …"hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs
7436 …"hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs
7437-keyword">const</span> totalLength = buf1.<span class="hljs-property">length</span> + buf2.<span c…
7439 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7440 <span class="hljs-comment">// Prints: 42</span>
7442 <span class="hljs-keyword">const</span> bufA = <span class="hljs-title class_">Buffer</span>.<span …
7444 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7445 <span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 ...></span>
7446 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7447 <span class="hljs-comment">// Prints: 42</span></code></pre>
7455 <li><code>array</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
7459 <pre><code class="language-js"><span class="hljs-comment">// Creates a new Buffer containing the UT…
7460-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title…
7470-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> …
7471 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArr…
7472 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArra…
7473 <li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
7474 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
7475 <strong>Default:</strong> <code>arrayBuffer.byteLength - byteOffset</code>.</li>
7477 <p>This creates a view of the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
7479 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArra…
7480 allocated memory as the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
7481-js"><span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span cla…
7483 arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>;
7484 arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>;
7486 <span class="hljs-comment">// Shares memory with `arr`.</span>
7487 …hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-t…
7489 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7490 <span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 a0 0f></span>
7492 <span class="hljs-comment">// Changing the original Uint16Array changes the Buffer also.</span>
7493 arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>;
7495 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7496 <span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 70 17></span></code></pre>
7499-js"><span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">new</span> <span clas…
7500-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title…
7502 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7503 <span class="hljs-comment">// Prints: 2</span></code></pre>
7504 …<code>arrayBuffer</code> is not an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
7505 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArr…
7511-js"><span class="hljs-keyword">const</span> arrA = <span class="hljs-title class_">Uint</span>8<s…
7512-keyword">const</span> arrB = <span class="hljs-keyword">new</span> <span class="hljs-title class_…
7513-variable language_">console</span>.<span class="hljs-title function_">log</span>(arrA.<span class…
7515 …hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-t…
7516 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7517 <span class="hljs-comment">// Prints: &#x3C;Buffer 63 64 65 66></span></code></pre>
7523-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> An…
7527-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
7528 <span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span …
7530 buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">0x61</span>;
7532 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7533 <span class="hljs-comment">// Prints: auffer</span>
7534 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7535 <span class="hljs-comment">// Prints: buffer</span></code></pre>
7543 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
7544-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="ht…
7545 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
7549-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7550 <span class="hljs-comment">// Prints: &#x3C;Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74></span…
7553 <pre><code class="language-js"><span class="hljs-keyword">class</span> <span class="hljs-title clas…
7554 [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toPrimitive</span>]() {
7555 <span class="hljs-keyword">return</span> <span class="hljs-string">'this is a test'</span>;
7559-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title…
7560 <span class="hljs-comment">// Prints: &#x3C;Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74></span…
7568 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
7569 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7573-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
7574-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
7576 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7577 <span class="hljs-comment">// Prints: this is a tést</span>
7578 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7579 <span class="hljs-comment">// Prints: this is a tést</span>
7580-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class…
7581 <span class="hljs-comment">// Prints: this is a tést</span></code></pre>
7589 <li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
7590 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
7593-js"><span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</sp…
7594-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-
7595 …hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="h…
7596 <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>([]…
7597-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-
7603 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7604 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
7608-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
7609 <span class="hljs-comment">// Prints: true</span>
7611-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
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: false</span>
7617-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
7618 <span class="hljs-comment">// Prints: false</span></code></pre>
7624 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
7626 <p>This is the size (in bytes) of pre-allocated internal <code>Buffer</code> instances used
7630 <li><code>index</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
7635 <p>This operator is inherited from <code>Uint8Array</code>, so its behavior on out-of-bounds
7640 <pre><code class="language-js"><span class="hljs-comment">// Copy an ASCII string into a `Buffer` o…
7641 <span class="hljs-comment">// (This only works for ASCII-only strings. In general, one should use</…
7642 <span class="hljs-comment">// `Buffer.from()` to perform this conversion.)</span>
7644 <span class="hljs-keyword">const</span> str = <span class="hljs-string">'Node.js'</span>;
7645 …s-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
7647 …class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-numbe…
7648 buf[i] = str.<span class="hljs-title function_">charCodeAt</span>(i);
7651-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7652 <span class="hljs-comment">// Prints: Node.js</span></code></pre>
7655 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array…
7660-js"><span class="hljs-keyword">const</span> arrayBuffer = <span class="hljs-keyword">new</span> <…
7661 <span class="hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<spa…
7663 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7664 <span class="hljs-comment">// Prints: true</span></code></pre>
7667 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
7677 <pre><code class="language-js"><span class="hljs-comment">// Create a buffer smaller than `Buffer.p…
7678-keyword">const</span> nodeBuffer = <span class="hljs-keyword">new</span> <span class="hljs-title …
7680 <span class="hljs-comment">// When casting the Node.js Buffer to an Int8Array, use the byteOffset</…
7681 <span class="hljs-comment">// to refer only to the part of `nodeBuffer.buffer` that contains the me…
7682 <span class="hljs-comment">// for `nodeBuffer`.</span>
7683-keyword">new</span> <span class="hljs-title class_">Int</span>8<span class="hljs-built_in">Array<…
7684 <h4><code>buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])</code><span><…
7690 <td><p>The <code>target</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
7699target</code> <a href="#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://dev…
7701 …oper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer…
7703 …oper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer…
7704 (not inclusive). <strong>Default:</strong> <code>target.length</code>.</li>
7705 <li><code>sourceStart</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
7707 <li><code>sourceEnd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
7709 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
7711 <p>Compares <code>buf</code> with <code>target</code> and returns a number indicating whether <code…
7712 comes before, after, or is the same as <code>target</code> in sort order.
7715 <li><code>0</code> is returned if <code>target</code> is the same as <code>buf</code></li>
7716 <li><code>1</code> is returned if <code>target</code> should come <em>before</em> <code>buf</code> …
7717 <li><code>-1</code> is returned if <code>target</code> should come <em>after</em> <code>buf</code> …
7719-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
7720 …="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
7721 …="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
7723 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7724 <span class="hljs-comment">// Prints: 0</span>
7725 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7726 <span class="hljs-comment">// Prints: -1</span>
7727 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7728 <span class="hljs-comment">// Prints: -1</span>
7729 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7730 <span class="hljs-comment">// Prints: 1</span>
7731 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7732 <span class="hljs-comment">// Prints: 1</span>
7733-variable language_">console</span>.<span class="hljs-title function_">log</span>([buf1, buf2, buf…
7734 <span class="hljs-comment">// Prints: [ &#x3C;Buffer 41 42 43>, &#x3C;Buffer 41 42 43 44>, &#x3C;Bu…
7735 <span class="hljs-comment">// (This result is equal to: [buf1, buf3, buf2].)</span></code></pre>
7737 arguments can be used to limit the comparison to specific ranges within <code>target</code>
7739-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
7740-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
7742-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class…
7743 <span class="hljs-comment">// Prints: 0</span>
7744-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class…
7745 <span class="hljs-comment">// Prints: -1</span>
7746-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class…
7747 <span class="hljs-comment">// Prints: 1</span></code></pre>
7749 <code>targetEnd > target.byteLength</code>, or <code>sourceEnd > source.byteLength</code>.</p>
7750 <h4><code>buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])</code><span><a class="mark" h…
7755target</code> <a href="#buffer_class_buffer" class="type">&#x3C;Buffer></a> | <a href="https://dev…
7756 …oper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer…
7758 <li><code>sourceStart</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
7760 <li><code>sourceEnd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
7762 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
7764 …a from a region of <code>buf</code> to a region in <code>target</code>, even if the <code>target</…
7766 <p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedA…
7769 <pre><code class="language-js"><span class="hljs-comment">// Create two `Buffer` instances.</span>
7770 …js-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-ti…
7771-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
7773 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
7774 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
7775 buf1[i] = i + <span class="hljs-number">97</span>;
7778 <span class="hljs-comment">// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `bu…
7779 …n class="hljs-title function_">copy</span>(buf2, <span class="hljs-number">8</span>, <span class="…
7780 <span class="hljs-comment">// This is equivalent to:</span>
7781 <span class="hljs-comment">// buf2.set(buf1.subarray(16, 20), 8);</span>
7783-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class…
7784 <span class="hljs-comment">// Prints: !!!!!!!!qrst!!!!!!!!!!!!!</span></code></pre>
7785 <pre><code class="language-js"><span class="hljs-comment">// Create a `Buffer` and copy data from o…
7786 <span class="hljs-comment">// within the same `Buffer`.</span>
7788 …ljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-ti…
7790 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
7791 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
7792 buf[i] = i + <span class="hljs-number">97</span>;
7795 …an class="hljs-title function_">copy</span>(buf, <span class="hljs-number">0</span>, <span class="…
7797 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7798 <span class="hljs-comment">// Prints: efghijghijklmnopqrstuvwxyz</span></code></pre>
7804 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
7806 <p>Creates and returns an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
7808 <pre><code class="language-js"><span class="hljs-comment">// Log the entire contents of a `Buffer`.…
7810 …="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs
7812 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> pair <span class="hljs-
7813 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
7815 <span class="hljs-comment">// Prints:</span>
7816 <span class="hljs-comment">// [0, 98]</span>
7817 <span class="hljs-comment">// [1, 117]</span>
7818 <span class="hljs-comment">// [2, 102]</span>
7819 <span class="hljs-comment">// [3, 102]</span>
7820 <span class="hljs-comment">// [4, 101]</span>
7821 <span class="hljs-comment">// [5, 114]</span></code></pre>
7835-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> A …
7837 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
7842-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
7843-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
7844 …="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
7846 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7847 <span class="hljs-comment">// Prints: true</span>
7848 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7849 <span class="hljs-comment">// Prints: false</span></code></pre>
7860 <td><p>Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exce…
7871-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
7872 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
7874 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
7876 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7882 <pre><code class="language-js"><span class="hljs-comment">// Fill a `Buffer` with the ASCII charact…
7884-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title f…
7886 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
7887 <span class="hljs-comment">// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh</span></co…
7891 <p>If the final write of a <code>fill()</code> operation falls on a multi-byte character,
7893 <pre><code class="language-js"><span class="hljs-comment">// Fill a `Buffer` with character that ta…
7895-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
7896 <span class="hljs-comment">// Prints: &#x3C;Buffer c8 a2 c8 a2 c8></span></code></pre>
7899-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7901 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class…
7902 <span class="hljs-comment">// Prints: &#x3C;Buffer 61 61 61 61 61></span>
7903-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7904 <span class="hljs-comment">// Prints: &#x3C;Buffer aa aa aa aa aa></span>
7905-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7906 <span class="hljs-comment">// Throws an exception.</span></code></pre>
7912-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
7913 <li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
7915 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7917 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
7919 <p>Equivalent to <a href="#buffer_buf_indexof_value_byteoffset_encoding"><code>buf.indexOf() !== -1…
7920-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7922-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7923 <span class="hljs-comment">// Prints: true</span>
7924-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7925 <span class="hljs-comment">// Prints: true</span>
7926-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7927 <span class="hljs-comment">// Prints: true</span>
7928-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7929 <span class="hljs-comment">// Prints: true (97 is the decimal ASCII value for 'a')</span>
7930-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7931 <span class="hljs-comment">// Prints: false</span>
7932-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7933 <span class="hljs-comment">// Prints: true</span>
7934-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7935 <span class="hljs-comment">// Prints: false</span></code></pre>
7951-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
7952 <li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
7954 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
7957 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
7958 <code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li>
7964 <li>a <code>Buffer</code> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer…
7966 <li>a number, <code>value</code> will be interpreted as an unsigned 8-bit integer
7969-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
7971-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7972 <span class="hljs-comment">// Prints: 0</span>
7973-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7974 <span class="hljs-comment">// Prints: 2</span>
7975-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7976 <span class="hljs-comment">// Prints: 8</span>
7977-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7978 <span class="hljs-comment">// Prints: 8 (97 is the decimal ASCII value for 'a')</span>
7979-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7980 <span class="hljs-comment">// Prints: -1</span>
7981-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
7982 <span class="hljs-comment">// Prints: 8</span>
7984-keyword">const</span> utf16Buffer = <span class="hljs-title class_">Buffer</span>.<span class="hl…
7986-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<spa…
7987 <span class="hljs-comment">// Prints: 4</span>
7988-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<spa…
7989 <span class="hljs-comment">// Prints: 6</span></code></pre>
7995 behavior matches <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_…
7996-js"><span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<sp…
7998 <span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span>
7999 <span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span>
8000-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8001-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8003 <span class="hljs-comment">// Passing a byteOffset that coerces to NaN or 0.</span>
8004 <span class="hljs-comment">// Prints: 1, searching the whole buffer.</span>
8005-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8006-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8007-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8008-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8017 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
8019 <p>Creates and returns an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
8020-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8022 …an class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> key <span class="hljs-
8023 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
8025 <span class="hljs-comment">// Prints:</span>
8026 <span class="hljs-comment">// 0</span>
8027 <span class="hljs-comment">// 1</span>
8028 <span class="hljs-comment">// 2</span>
8029 <span class="hljs-comment">// 3</span>
8030 <span class="hljs-comment">// 4</span>
8031 <span class="hljs-comment">// 5</span></code></pre>
8045-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
8046 <li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
8048 <code>buf.length - 1</code>.</li>
8049 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
8052 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8053 <code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li>
8057-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8059-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8060 <span class="hljs-comment">// Prints: 0</span>
8061-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8062 <span class="hljs-comment">// Prints: 17</span>
8063-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8064 <span class="hljs-comment">// Prints: 17</span>
8065-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8066 <span class="hljs-comment">// Prints: 15 (97 is the decimal ASCII value for 'a')</span>
8067-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8068 <span class="hljs-comment">// Prints: -1</span>
8069-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8070 <span class="hljs-comment">// Prints: 5</span>
8071-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8072 <span class="hljs-comment">// Prints: -1</span>
8074-keyword">const</span> utf16Buffer = <span class="hljs-title class_">Buffer</span>.<span class="hl…
8076-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<spa…
8077 <span class="hljs-comment">// Prints: 6</span>
8078-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<spa…
8079 <span class="hljs-comment">// Prints: 4</span></code></pre>
8085 This behavior matches <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
8086-js"><span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<sp…
8088 <span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span>
8089 <span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span>
8090-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8091-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8093 <span class="hljs-comment">// Passing a byteOffset that coerces to NaN.</span>
8094 <span class="hljs-comment">// Prints: 1, searching the whole buffer.</span>
8095-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8096-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8098 <span class="hljs-comment">// Passing a byteOffset that coerces to 0.</span>
8099 <span class="hljs-comment">// Prints: -1, equivalent to passing 0.</span>
8100-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8101-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="h…
8108 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
8111 <pre><code class="language-js"><span class="hljs-comment">// Create a `Buffer` and write a shorter …
8113 …="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs
8115 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8116 <span class="hljs-comment">// Prints: 1234</span>
8118 …lass="hljs-title function_">write</span>(<span class="hljs-string">'some string'</span>, <span cla…
8120 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8121 <span class="hljs-comment">// Prints: 1234</span></code></pre>
8126 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
8133 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8134 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
8135 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
8137 <p>Reads a signed, big-endian 64-bit integer from <code>buf</code> at the specified <code>offset</c…
8145 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8146 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
8147 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
8149 <p>Reads a signed, little-endian 64-bit integer from <code>buf</code> at the specified
8166 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8167 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
8168 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
8170 <p>Reads an unsigned, big-endian 64-bit integer from <code>buf</code> at the specified
8173-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8175-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8176 <span class="hljs-comment">// Prints: 4294967295n</span></code></pre>
8190 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8191 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
8192 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
8194 <p>Reads an unsigned, little-endian 64-bit integer from <code>buf</code> at the specified
8197-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8199-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8200 <span class="hljs-comment">// Prints: 18446744069414584320n</span></code></pre>
8214 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8215 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</co…
8216 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8218 <p>Reads a 64-bit, big-endian double from <code>buf</code> at the specified <code>offset</code>.</p>
8219-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8221-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8222 <span class="hljs-comment">// Prints: 8.20788039913184e-304</span></code></pre>
8236 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8237 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</co…
8238 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8240 <p>Reads a 64-bit, little-endian double from <code>buf</code> at the specified <code>offset</code>.…
8241-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8243-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8244 <span class="hljs-comment">// Prints: 5.447603722011605e-270</span>
8245-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8246 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8260 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8261 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
8262 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8264 <p>Reads a 32-bit, big-endian float from <code>buf</code> at the specified <code>offset</code>.</p>
8265-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8267-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8268 <span class="hljs-comment">// Prints: 2.387939260590663e-38</span></code></pre>
8282 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8283 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
8284 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8286 <p>Reads a 32-bit, little-endian float from <code>buf</code> at the specified <code>offset</code>.<…
8287-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8289-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8290 <span class="hljs-comment">// Prints: 1.539989614439558e-36</span>
8291-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8292 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8306 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8307 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</co…
8308 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8310 <p>Reads a signed 8-bit integer from <code>buf</code> at the specified <code>offset</code>.</p>
8312-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8314-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8315 <span class="hljs-comment">// Prints: -1</span>
8316-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8317 <span class="hljs-comment">// Prints: 5</span>
8318-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8319 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8333 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8334 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
8335 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8337 <p>Reads a signed, big-endian 16-bit integer from <code>buf</code> at the specified <code>offset</c…
8339-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8341-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8342 <span class="hljs-comment">// Prints: 5</span></code></pre>
8356 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8357 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
8358 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8360 <p>Reads a signed, little-endian 16-bit integer from <code>buf</code> at the specified
8363-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8365-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8366 <span class="hljs-comment">// Prints: 1280</span>
8367-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8368 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8382 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8383 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
8384 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8386 <p>Reads a signed, big-endian 32-bit integer from <code>buf</code> at the specified <code>offset</c…
8388-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8390-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8391 <span class="hljs-comment">// Prints: 5</span></code></pre>
8405 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8406 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
8407 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8409 <p>Reads a signed, little-endian 32-bit integer from <code>buf</code> at the specified
8412-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8414-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8415 <span class="hljs-comment">// Prints: 83886080</span>
8416-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8417 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8431 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8432 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
8433 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
8435 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8438 and interprets the result as a big-endian, two's complement signed value
8440-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8442-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8443 <span class="hljs-comment">// Prints: 1234567890ab</span>
8444-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8445 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span>
8446-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8447 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8461 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8462 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
8463 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
8465 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8468 and interprets the result as a little-endian, two's complement signed value
8470-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8472-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8473 <span class="hljs-comment">// Prints: -546f87a9cbee</span></code></pre>
8489 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8490 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</co…
8491 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8493 <p>Reads an unsigned 8-bit integer from <code>buf</code> at the specified <code>offset</code>.</p>
8495-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8497-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8498 <span class="hljs-comment">// Prints: 1</span>
8499-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8500 <span class="hljs-comment">// Prints: 254</span>
8501-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8502 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8518 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8519 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
8520 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8522 <p>Reads an unsigned, big-endian 16-bit integer from <code>buf</code> at the specified
8525-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8527-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8528 <span class="hljs-comment">// Prints: 1234</span>
8529-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8530 <span class="hljs-comment">// Prints: 3456</span></code></pre>
8546 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8547 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
8548 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8550 <p>Reads an unsigned, little-endian 16-bit integer from <code>buf</code> at the specified
8553-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8555-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8556 <span class="hljs-comment">// Prints: 3412</span>
8557-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8558 <span class="hljs-comment">// Prints: 5634</span>
8559-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8560 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8576 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8577 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
8578 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8580 <p>Reads an unsigned, big-endian 32-bit integer from <code>buf</code> at the specified
8583-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8585-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8586 <span class="hljs-comment">// Prints: 12345678</span></code></pre>
8602 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8603 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
8604 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8606 <p>Reads an unsigned, little-endian 32-bit integer from <code>buf</code> at the specified
8609-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8611-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8612 <span class="hljs-comment">// Prints: 78563412</span>
8613-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8614 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8630 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8631 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
8632 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
8634 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8637 and interprets the result as an unsigned big-endian integer supporting
8640-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8642-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8643 <span class="hljs-comment">// Prints: 1234567890ab</span>
8644-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8645 <span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
8661 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8662 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
8663 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
8665 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8668 and interprets the result as an unsigned, little-endian integer supporting
8671-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8673-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8674 <span class="hljs-comment">// Prints: ab9078563412</span></code></pre>
8680 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
8681 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
8689 <p>This method is inherited from <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
8692 <pre><code class="language-js"><span class="hljs-comment">// Create a `Buffer` with the ASCII alpha…
8693 <span class="hljs-comment">// from the original `Buffer`.</span>
8695 …js-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-ti…
8697 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
8698 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
8699 buf1[i] = i + <span class="hljs-number">97</span>;
8702 …="hljs-keyword">const</span> buf2 = buf1.<span class="hljs-title function_">subarray</span>(<span …
8704-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class…
8705 <span class="hljs-comment">// Prints: abc</span>
8707 buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">33</span>;
8709-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class…
8710 <span class="hljs-comment">// Prints: !bc</span></code></pre>
8713-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8715-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8716 <span class="hljs-comment">// Prints: buffe</span>
8717 <span class="hljs-comment">// (Equivalent to buf.subarray(0, 5).)</span>
8719-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8720 <span class="hljs-comment">// Prints: buff</span>
8721 <span class="hljs-comment">// (Equivalent to buf.subarray(0, 4).)</span>
8723-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
8724 <span class="hljs-comment">// Prints: uff</span>
8725 <span class="hljs-comment">// (Equivalent to buf.subarray(1, 4).)</span></code></pre>
8734 <td><p>Coercing the offsets to integers now handles values outside the 32-bit integer range properl…
8741 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
8742 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
8752-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8754-keyword">const</span> copiedBuf = <span class="hljs-title class_">Uint</span>8<span class="hljs-b…
8755 copiedBuf[<span class="hljs-number">0</span>]++;
8756 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8757 <span class="hljs-comment">// Prints: cuffer</span>
8759 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8760 <span class="hljs-comment">// Prints: buffer</span></code></pre>
8768 <p>Interprets <code>buf</code> as an array of unsigned 16-bit integers and swaps the
8769 byte order <em>in-place</em>. Throws <a href="#ERR_INVALID_BUFFER_SIZE"><code>ERR_INVALID_BUFFER_SI…
8771-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
8773 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8774 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span>
8776 buf1.<span class="hljs-title function_">swap16</span>();
8778 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8779 <span class="hljs-comment">// Prints: &#x3C;Buffer 02 01 04 03 06 05 08 07></span>
8781-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
8783 buf2.<span class="hljs-title function_">swap16</span>();
8784 <span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code></pre>
8785 <p>One convenient use of <code>buf.swap16()</code> is to perform a fast in-place conversion
8786 between UTF-16 little-endian and UTF-16 big-endian:</p>
8787-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8788 buf.<span class="hljs-title function_">swap16</span>(); <span class="hljs-comment">// Convert to bi…
8796 <p>Interprets <code>buf</code> as an array of unsigned 32-bit integers and swaps the
8797 byte order <em>in-place</em>. Throws <a href="#ERR_INVALID_BUFFER_SIZE"><code>ERR_INVALID_BUFFER_SI…
8799-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
8801 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8802 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span>
8804 buf1.<span class="hljs-title function_">swap32</span>();
8806 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8807 <span class="hljs-comment">// Prints: &#x3C;Buffer 04 03 02 01 08 07 06 05></span>
8809-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
8811 buf2.<span class="hljs-title function_">swap32</span>();
8812 <span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code></pre>
8820 <p>Interprets <code>buf</code> as an array of 64-bit numbers and swaps byte order <em>in-place</em>.
8822-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
8824 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8825 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span>
8827 buf1.<span class="hljs-title function_">swap64</span>();
8829 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8830 <span class="hljs-comment">// Prints: &#x3C;Buffer 08 07 06 05 04 03 02 01></span>
8832-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
8834 buf2.<span class="hljs-title function_">swap64</span>();
8835 <span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code></pre>
8841 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
8843 <p>Returns a JSON representation of <code>buf</code>. <a href="https://developer.mozilla.org/en-US/…
8847-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8848 <span class="hljs-keyword">const</span> json = <span class="hljs-variable constant_">JSON</span>.<s…
8850 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8851 <span class="hljs-comment">// Prints: {"type":"Buffer","data":[1,2,3,4,5]}</span>
8853-keyword">const</span> copy = <span class="hljs-variable constant_">JSON</span>.<span class="hljs-
8854 …<span class="hljs-keyword">return</span> value &#x26;&#x26; value.<span class="hljs-property">type…
8855 …<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(value…
8859 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8860 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05></span></code></pre>
8866 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
8867 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
8868 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
8870 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
8874 …f <code>encoding</code> is <code>'utf8'</code> and a byte sequence in the input is not valid UTF-8,
8876 <p>The maximum length of a string instance (in UTF-16 code units) is available
8878-js"><span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.…
8880 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
8881 <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
8882 buf1[i] = i + <span class="hljs-number">97</span>;
8885-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class…
8886 <span class="hljs-comment">// Prints: abcdefghijklmnopqrstuvwxyz</span>
8887-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class…
8888 <span class="hljs-comment">// Prints: abcde</span>
8890 …="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
8892-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class…
8893 <span class="hljs-comment">// Prints: 74c3a97374</span>
8894-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class…
8895 <span class="hljs-comment">// Prints: té</span>
8896-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class…
8897 <span class="hljs-comment">// Prints: té</span></code></pre>
8903 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
8905 <p>Creates and returns an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
8907-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8909 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="hljs
8910 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
8912 <span class="hljs-comment">// Prints:</span>
8913 <span class="hljs-comment">// 98</span>
8914 <span class="hljs-comment">// 117</span>
8915 <span class="hljs-comment">// 102</span>
8916 <span class="hljs-comment">// 102</span>
8917 <span class="hljs-comment">// 101</span>
8918 <span class="hljs-comment">// 114</span>
8920 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="h…
8921 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
8923 <span class="hljs-comment">// Prints:</span>
8924 <span class="hljs-comment">// 98</span>
8925 <span class="hljs-comment">// 117</span>
8926 <span class="hljs-comment">// 102</span>
8927 <span class="hljs-comment">// 102</span>
8928 <span class="hljs-comment">// 101</span>
8929 <span class="hljs-comment">// 114</span></code></pre>
8935 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8936 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8938 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8939 exceed <code>buf.length - offset</code>). <strong>Default:</strong> <code>buf.length - offset</code…
8940 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
8941 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8947-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8949-keyword">const</span> len = buf.<span class="hljs-title function_">write</span>(<span class="hljs
8951-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
8952 <span class="hljs-comment">// Prints: 12 bytes: ½ + ¼ = ¾</span>
8954 …hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
8956 …ljs-keyword">const</span> length = buffer.<span class="hljs-title function_">write</span>(<span cl…
8958-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
8959 <span class="hljs-comment">// Prints: 2 bytes : ab</span></code></pre>
8965 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
8966 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8967 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
8968 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8970 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
8972-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8974 …uf.<span class="hljs-title function_">writeBigInt64BE</span>(<span class="hljs-number">0x010203040…
8976 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8977 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span></code></pre>
8983 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
8984 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
8985 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
8986 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
8988 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
8990-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
8992 …uf.<span class="hljs-title function_">writeBigInt64LE</span>(<span class="hljs-number">0x010203040…
8994 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
8995 <span class="hljs-comment">// Prints: &#x3C;Buffer 08 07 06 05 04 03 02 01></span></code></pre>
9009 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
9010 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9011 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
9012 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9014 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9016-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9018 …f.<span class="hljs-title function_">writeBigUInt64BE</span>(<span class="hljs-number">0xdecafafec…
9020 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9021 <span class="hljs-comment">// Prints: &#x3C;Buffer de ca fa fe ca ce fa de></span></code></pre>
9035 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
9036 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9037 satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</c…
9038 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9040 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9041-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9043 …f.<span class="hljs-title function_">writeBigUInt64LE</span>(<span class="hljs-number">0xdecafafec…
9045 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9046 <span class="hljs-comment">// Prints: &#x3C;Buffer de fa ce ca fe fa ca de></span></code></pre>
9061 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9062 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9063 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</co…
9064 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9066 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9069-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9071 buf.<span class="hljs-title function_">writeDoubleBE</span>(<span class="hljs-number">123.456</span…
9073 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9074 <span class="hljs-comment">// Prints: &#x3C;Buffer 40 5e dd 2f 1a 9f be 77></span></code></pre>
9088 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9089 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9090 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</co…
9091 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9093 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9096-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9098 buf.<span class="hljs-title function_">writeDoubleLE</span>(<span class="hljs-number">123.456</span…
9100 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9101 <span class="hljs-comment">// Prints: &#x3C;Buffer 77 be 9f 1a 2f dd 5e 40></span></code></pre>
9115 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9116 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9117 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
9118 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9120 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9122-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9124 buf.<span class="hljs-title function_">writeFloatBE</span>(<span class="hljs-number">0xcafebabe</sp…
9126 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9127 <span class="hljs-comment">// Prints: &#x3C;Buffer 4f 4a fe bb></span></code></pre>
9141 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9142 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9143 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
9144 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9146 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9148-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9150 buf.<span class="hljs-title function_">writeFloatLE</span>(<span class="hljs-number">0xcafebabe</sp…
9152 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9153 <span class="hljs-comment">// Prints: &#x3C;Buffer bb fe 4a 4f></span></code></pre>
9167 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9168 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9169 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</co…
9170 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9173 signed 8-bit integer. Behavior is undefined when <code>value</code> is anything other than
9174 a signed 8-bit integer.</p>
9176-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9178 buf.<span class="hljs-title function_">writeInt8</span>(<span class="hljs-number">2</span>, <span c…
9179 buf.<span class="hljs-title function_">writeInt8</span>(-<span class="hljs-number">2</span>, <span …
9181 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9182 <span class="hljs-comment">// Prints: &#x3C;Buffer 02 fe></span></code></pre>
9196 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9197 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9198 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
9199 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9201 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9202 must be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is
9203 anything other than a signed 16-bit integer.</p>
9205-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9207 buf.<span class="hljs-title function_">writeInt16BE</span>(<span class="hljs-number">0x0102</span>,…
9209 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9210 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02></span></code></pre>
9224 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9225 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9226 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
9227 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9229 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9230 must be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is
9231 anything other than a signed 16-bit integer.</p>
9233-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9235 buf.<span class="hljs-title function_">writeInt16LE</span>(<span class="hljs-number">0x0304</span>,…
9237 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9238 <span class="hljs-comment">// Prints: &#x3C;Buffer 04 03></span></code></pre>
9252 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9253 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9254 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
9255 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9257 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9258 must be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is
9259 anything other than a signed 32-bit integer.</p>
9261-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9263 buf.<span class="hljs-title function_">writeInt32BE</span>(<span class="hljs-number">0x01020304</sp…
9265 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9266 <span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04></span></code></pre>
9280 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9281 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9282 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
9283 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9285 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9286 must be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is
9287 anything other than a signed 32-bit integer.</p>
9289-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9291 buf.<span class="hljs-title function_">writeInt32LE</span>(<span class="hljs-number">0x05060708</sp…
9293 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9294 <span class="hljs-comment">// Prints: &#x3C;Buffer 08 07 06 05></span></code></pre>
9308 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9309 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9310 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
9311 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
9313 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9316 as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined when
9318-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9320 …s="hljs-title function_">writeIntBE</span>(<span class="hljs-number">0x1234567890ab</span>, <span …
9322 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9323 <span class="hljs-comment">// Prints: &#x3C;Buffer 12 34 56 78 90 ab></span></code></pre>
9337 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9338 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9339 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
9340 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
9342 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9345 as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined
9347-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9349 …s="hljs-title function_">writeIntLE</span>(<span class="hljs-number">0x1234567890ab</span>, <span …
9351 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9352 <span class="hljs-comment">// Prints: &#x3C;Buffer ab 90 78 56 34 12></span></code></pre>
9368 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9369 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9370 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</co…
9371 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9374 valid unsigned 8-bit integer. Behavior is undefined when <code>value</code> is anything
9375 other than an unsigned 8-bit integer.</p>
9377-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9379 buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x3</span>, <spa…
9380 buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x4</span>, <spa…
9381 buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x23</span>, <sp…
9382 buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x42</span>, <sp…
9384 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9385 <span class="hljs-comment">// Prints: &#x3C;Buffer 03 04 23 42></span></code></pre>
9401 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9402 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9403 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
9404 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9406 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9407 must be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code>
9408 is anything other than an unsigned 16-bit integer.</p>
9410-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9412 buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xdead</span>…
9413 buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xbeef</span>…
9415 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9416 <span class="hljs-comment">// Prints: &#x3C;Buffer de ad be ef></span></code></pre>
9432 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9433 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9434 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</co…
9435 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9437 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9438 must be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code> is
9439 anything other than an unsigned 16-bit integer.</p>
9441-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9443 buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xdead</span>…
9444 buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xbeef</span>…
9446 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9447 <span class="hljs-comment">// Prints: &#x3C;Buffer ad de ef be></span></code></pre>
9463 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9464 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9465 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
9466 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9468 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian…
9469 must be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code>
9470 is anything other than an unsigned 32-bit integer.</p>
9472-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9474 buf.<span class="hljs-title function_">writeUInt32BE</span>(<span class="hljs-number">0xfeedface</s…
9476 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9477 <span class="hljs-comment">// Prints: &#x3C;Buffer fe ed fa ce></span></code></pre>
9493 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9494 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9495 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</co…
9496 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9498 <p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-end…
9499 must be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code> is
9500 anything other than an unsigned 32-bit integer.</p>
9502-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9504 buf.<span class="hljs-title function_">writeUInt32LE</span>(<span class="hljs-number">0xfeedface</s…
9506 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9507 <span class="hljs-comment">// Prints: &#x3C;Buffer ce fa ed fe></span></code></pre>
9523 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9524 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9525 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
9526 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
9528 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9531 as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined
9534-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9536 …="hljs-title function_">writeUIntBE</span>(<span class="hljs-number">0x1234567890ab</span>, <span …
9538 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9539 <span class="hljs-comment">// Prints: &#x3C;Buffer 12 34 56 78 90 ab></span></code></pre>
9555 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9556 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9557 satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
9558 <li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
9560 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
9563 as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined
9566-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
9568 …="hljs-title function_">writeUIntLE</span>(<span class="hljs-number">0x1234567890ab</span>, <span …
9570 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
9571 <span class="hljs-comment">// Prints: &#x3C;Buffer ab 90 78 56 34 12></span></code></pre>
9588 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
9590 <li><code>array</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
9613 …bility api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use
9617-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> …
9618-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code>…
9619 <li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
9620 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9621 <strong>Default:</strong> <code>arrayBuffer.byteLength - byteOffset</code>.</li>
9641 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
9643-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> An…
9655 <td><p>The <code>new Buffer(size)</code> will return zero-filled memory by default.</p></td></tr>
9665 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
9668 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
9688 …_stability api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated:
9691 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
9692 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
9697 <code>Buffer</code>-related APIs that are available only via the <code>buffer</code> module
9704 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#…
9706 <p>Decodes a string of Base64-encoded data into bytes, and encodes those bytes
9707 into a string using Latin-1 (ISO-8859-1).</p>
9708 <p>The <code>data</code> may be any JavaScript-value that can be coerced into a string.</p>
9712 For code running using Node.js APIs, converting between base64-encoded strings
9720 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
9722 <p>Decodes a string into bytes using Latin-1 (ISO-8859), and encodes those bytes
9724 <p>The <code>data</code> may be any JavaScript-value that can be coerced into a string.</p>
9728 For code running using Node.js APIs, converting between base64-encoded strings
9736 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
9746 ….mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a…
9754 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a>…
9770 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
9771 <li><code>fromEnc</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
9772 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#…
9775 <p>Re-encodes the given <code>Buffer</code> or <code>Uint8Array</code> instance from one character
9782 sequence cannot be adequately represented in the target encoding. For instance:</p>
9783 …ode class="language-js"><span class="hljs-keyword">const</span> buffer = <span class="hljs-built_i…
9785-keyword">const</span> newBuf = buffer.<span class="hljs-title function_">transcode</span>(<span c…
9786-variable language_">console</span>.<span class="hljs-title function_">log</span>(newBuf.<span cla…
9787 <span class="hljs-comment">// Prints: '?'</span></code></pre>
9788 <p>Because the Euro (<code>€</code>) sign is not representable in US-ASCII, it is replaced
9794 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
9802 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
9804 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
9817 <td><p>Value is changed from 2<sup>31</sup> - 1 to 2<sup>32</sup> - 1 on 64-bit architectures.</p><…
9824 ….mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a…
9826 <p>On 32-bit architectures, this value currently is 2<sup>30</sup> - 1 (~1GB).</p>
9827 <p>On 64-bit architectures, this value currently is 2<sup>32</sup> - 1 (~4GB).</p>
9835 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a>…
9838 in UTF-16 code units.</p>
9853 required between creating a fast-but-uninitialized <code>Buffer</code> versus creating a
9854 slower-but-safer <code>Buffer</code>. Since Node.js 8.0.0, <code>Buffer(num)</code> and <code>new B…
9857 ….org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> …
9872 arbitrary pre-existing in-memory data, so may be used to expose in-memory
9874 occur because the data is zero-filled. However, other attacks are still
9877 <p>To make the creation of <code>Buffer</code> instances more reliable and less error-prone,
9888 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuff…
9909 <h4>The <code>--zero-fill-buffers</code> command-line option<span><a class="mark" href="#buffer_the…
9913 <p>Node.js can be started using the <code>--zero-fill-buffers</code> command-line option to
9914 cause all newly-allocated <code>Buffer</code> instances to be zero-filled upon creation by
9916 …ode>Buffer.allocUnsafeSlow()</code></a>, and <code>new SlowBuffer(size)</code> are not zero-filled.
9918 <code>--zero-fill-buffers</code> option only when necessary to enforce that newly allocated
9920 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
9921 <span class="hljs-meta">> </span><span class="language-bash">Buffer.allocUnsafe(5);</span>
9925 segment of allocated memory is <em>uninitialized</em> (it is not zeroed-out). While
9937 <p><em>Addons</em> are dynamically-linked shared objects written in C++. The
9940 <p>There are three options for implementing addons: Node-API, nan, or direct
9942 direct access to functionality which is not exposed by Node-API, use Node-API.
9943 Refer to <a href="n-api.html">C/C++ addons with Node-API</a> for more information on
9944 Node-API.</p>
9945 <p>When not using Node-API, implementing addons is complicated,
9958 serves as a cross-platform abstraction library, giving easy, POSIX-like
9964 avoid blocking the event loop with I/O or other time-intensive tasks by
9965 offloading work via libuv to non-blocking system operations, worker threads,
9976 re-exported by Node.js and may be used to various extents by addons. See
9980 <p>All of the following examples are available for <a href="https://github.com/nodejs/node-addon-ex…
9981 be used as the starting-point for an addon.</p>
9985-js"><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span…
9987 <pre><code class="language-cpp"><span class="hljs-comment">// hello.cc</span>
9988 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
9990 <span class="hljs-keyword">namespace</span> demo {
9992 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
9993 <span class="hljs-keyword">using</span> v8::Isolate;
9994 <span class="hljs-keyword">using</span> v8::Local;
9995 <span class="hljs-keyword">using</span> v8::Object;
9996 <span class="hljs-keyword">using</span> v8::String;
9997 <span class="hljs-keyword">using</span> v8::Value;
9999 …class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Method</span><s…
10000 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10001 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(St…
10002 …isolate, <span class="hljs-string">"world"</span>).<span class="hljs-built_in">ToLocalChecked</spa…
10005 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Initialize…
10006 …<span class="hljs-built_in">NODE_SET_METHOD</span>(exports, <span class="hljs-string">"hello"</spa…
10009 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Initialize)
10011 } <span class="hljs-comment">// namespace demo</span></code></pre>
10014 …s="language-cpp"><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs
10015 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Initialize)</code></pre>
10016 <p>There is no semi-colon after <code>NODE_MODULE</code> as it's not a function (see
10022 <p>When building addons with <code>node-gyp</code>, using the macro <code>NODE_GYP_MODULE_NAME</cod…
10025 <h4>Context-aware addons<span><a class="mark" href="#addons_context_aware_addons" id="addons_contex…
10028 instances of Node.js in a single process. Each instance will have its own
10032 <p>A context-aware addon can be constructed by using the macro
10036 <pre><code class="language-cpp"><span class="hljs-keyword">using</span> <span class="hljs-keyword">…
10038 …n class="hljs-keyword">extern</span> <span class="hljs-string">"C"</span> <span class="hljs-functi…
10039 <span class="hljs-title">NODE_MODULE_INITIALIZER</span><span class="hljs-params">(Local&#x3C;Object…
10040 Local&#x3C;Value> <span class="hljs-keyword">module</span>,
10042 <span class="hljs-comment">/* Perform addon initialization steps here. */</span>
10045 construct a context-aware addon. Unlike <code>NODE_MODULE()</code>, which is used to
10056 <p>The choice to build a context-aware addon carries with it the responsibility of
10064 <p>The context-aware addon can be structured to avoid global static data by
10067 <li>Define a class which will hold per-addon-instance data and which has a static
10069-cpp"><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-type"…
10070 <span class="hljs-comment">// Cast `data` to an instance of the class and delete it.</span>
10073 <li>Heap-allocate an instance of this class in the addon initializer. This can be
10075 <li>Call <code>node::AddEnvironmentCleanupHook()</code>, passing it the above-created
10081 native-backed JavaScript functions. The third parameter of
10086 <p>This will ensure that the per-addon-instance data reaches each binding that can
10087 be called from JavaScript. The per-addon-instance data must also be passed into
10089 <p>The following example illustrates the implementation of a context-aware addon:</p>
10090 <pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> …
10092 <span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> v8;
10094 <span class="hljs-keyword">class</span> <span class="hljs-title class_">AddonData</span> {
10095 <span class="hljs-keyword">public</span>:
10096 …<span class="hljs-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">Ad…
10097 call_count(<span class="hljs-number">0</span>) {</span>
10098 …<span class="hljs-comment">// Ensure this per-addon-instance data is deleted at environment cleanu…
10099 …node::<span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, DeleteInstance, <span …
10102 <span class="hljs-comment">// Per-addon data.</span>
10103 <span class="hljs-type">int</span> call_count;
10105-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10106 …<span class="hljs-keyword">delete</span> <span class="hljs-built_in">static_cast</span>&#x3C;Addon…
10110-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10111 <span class="hljs-comment">// Retrieve the per-addon-instance data.</span>
10113-built_in">reinterpret_cast</span>&#x3C;AddonData*>(info.<span class="hljs-built_in">Data</span>()…
10114 data->call_count++;
10115 …<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>((<span …
10118 <span class="hljs-comment">// Initialize this addon to be context-aware.</span>
10119 <span class="hljs-built_in">NODE_MODULE_INIT</span>(<span class="hljs-comment">/* exports, module, …
10120 Isolate* isolate = context-><span class="hljs-built_in">GetIsolate</span>();
10122 …<span class="hljs-comment">// Create a new instance of `AddonData` for this instance of the addon …
10123 <span class="hljs-comment">// tie its life cycle to that of the Node.js environment.</span>
10124 …AddonData* data = <span class="hljs-keyword">new</span> <span class="hljs-built_in">AddonData</spa…
10126 …<span class="hljs-comment">// Wrap the data in a `v8::External` so we can pass it to the method we…
10127 <span class="hljs-comment">// expose.</span>
10128 Local&#x3C;External> external = External::<span class="hljs-built_in">New</span>(isolate, data);
10130 …<span class="hljs-comment">// Expose the method `Method` to JavaScript, and make sure it receives …
10131 …<span class="hljs-comment">// per-addon-instance data we created above by passing `external` as th…
10132 <span class="hljs-comment">// third parameter to the `FunctionTemplate` constructor.</span>
10133 exports-><span class="hljs-built_in">Set</span>(context,
10134 …String::<span class="hljs-built_in">NewFromUtf8</span>(isolate, <span class="hljs-string">"method"…
10135 FunctionTemplate::<span class="hljs-built_in">New</span>(isolate, Method, external)
10136-><span class="hljs-built_in">GetFunction</span>(context).<span class="hljs-built_in">ToLocalCheck…
10149 such as a main thread and a Worker thread, an add-on needs to either:</p>
10151 <li>Be an Node-API addon, or</li>
10152 <li>Be declared as context-aware using <code>NODE_MODULE_INIT()</code> as described above</li>
10157 …uage-cpp"><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title"…
10158 … <span class="hljs-type">void</span> (*fun)(<span class="hljs-type">void</span>* arg),
10159 <span class="hljs-type">void</span>* arg)</span></span>;</code></pre>
10163 run in last-in first-out order.</p>
10169 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10170 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10171 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10172 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10174 <span class="hljs-keyword">using</span> node::AddEnvironmentCleanupHook;
10175 <span class="hljs-keyword">using</span> v8::HandleScope;
10176 <span class="hljs-keyword">using</span> v8::Isolate;
10177 <span class="hljs-keyword">using</span> v8::Local;
10178 <span class="hljs-keyword">using</span> v8::Object;
10180 <span class="hljs-comment">// Note: In a real-world application, do not rely on static/global data.…
10181 <span class="hljs-keyword">static</span> <span class="hljs-type">char</span> cookie[] = <span class…
10182 <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> cleanup_cb1_called = <s…
10183 <span class="hljs-keyword">static</span> <span class="hljs-type">int</span> cleanup_cb2_called = <s…
10185-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10186 Isolate* isolate = <span class="hljs-built_in">static_cast</span>&#x3C;Isolate*>(arg);
10187 …<span class="hljs-function">HandleScope <span class="hljs-title">scope</span><span class="hljs-par…
10188 Local&#x3C;Object> obj = Object::<span class="hljs-built_in">New</span>(isolate);
10189 …<span class="hljs-built_in">assert</span>(!obj.<span class="hljs-built_in">IsEmpty</span>()); <sp…
10190 <span class="hljs-built_in">assert</span>(obj-><span class="hljs-built_in">IsObject</span>());
10194-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10195 …<span class="hljs-built_in">assert</span>(arg == <span class="hljs-built_in">static_cast</span>&#x…
10199-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10200 …<span class="hljs-built_in">assert</span>(cleanup_cb1_called == <span class="hljs-number">1</span>…
10201 …<span class="hljs-built_in">assert</span>(cleanup_cb2_called == <span class="hljs-number">1</span>…
10204 <span class="hljs-comment">// Initialize this addon to be context-aware.</span>
10205 <span class="hljs-built_in">NODE_MODULE_INIT</span>(<span class="hljs-comment">/* exports, module, …
10206 Isolate* isolate = context-><span class="hljs-built_in">GetIsolate</span>();
10208 …<span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, sanity_check, <span class="h…
10209 <span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, cleanup_cb2, cookie);
10210 <span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, cleanup_cb1, isolate);
10213 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10214 <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/addon'</span>…
10218 top-level of the project describing the build configuration of the module
10219 using a JSON-like format. This file is used by <a href="https://github.com/nodejs/node-gyp">node-gy…
10221 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
10222 …<span class="hljs-attr">"targets"</span><span class="hljs-punctuation">:</span> <span class="hljs-
10223 <span class="hljs-punctuation">{</span>
10224 …an class="hljs-attr">"target_name"</span><span class="hljs-punctuation">:</span> <span class="hljs
10225-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</s…
10226 <span class="hljs-punctuation">}</span>
10227 <span class="hljs-punctuation">]</span>
10228 <span class="hljs-punctuation">}</span></code></pre>
10229 <p>A version of the <code>node-gyp</code> utility is bundled and distributed with
10233 use <code>node-gyp</code> directly can install it using the command
10234 <code>npm install -g node-gyp</code>. See the <code>node-gyp</code> <a href="https://github.com/nod…
10235 more information, including platform-specific requirements.</p>
10236 <p>Once the <code>binding.gyp</code> file has been created, use <code>node-gyp configure</code> to
10240 <p>Next, invoke the <code>node-gyp build</code> command to generate the compiled <code>addon.node</…
10243 version of <code>node-gyp</code> to perform this same set of actions, generating a
10247 <pre><code class="language-js"><span class="hljs-comment">// hello.js</span>
10248 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
10250 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10251 <span class="hljs-comment">// Prints: 'world'</span></code></pre>
10254 the <a href="https://github.com/TooTallNate/node-bindings">bindings</a> package to load the compile…
10257 <pre><code class="language-js"><span class="hljs-keyword">try</span> {
10258 …<span class="hljs-keyword">return</span> <span class="hljs-built_in">require</span>(<span class="h…
10259 } <span class="hljs-keyword">catch</span> (err) {
10260 …<span class="hljs-keyword">return</span> <span class="hljs-built_in">require</span>(<span class="h…
10266 ….></code> statements (e.g. <code>#include &#x3C;v8.h></code>) and <code>node-gyp</code> will locate
10271 <p>When <code>node-gyp</code> runs, it will detect the specific release version of Node.js
10278 <p><code>node-gyp</code> can be run using the <code>--nodedir</code> flag pointing at a local Node.…
10286 files with the <code>.node</code> file extension and initialize those as dynamically-linked
10307 </section><section><h3>Node-API<span><a class="mark" href="#addons_node_api" id="addons_node_api">#…
10308 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
10309 <p>Node-API is an API for building native addons. It is independent from
10316 outlined in this document (node-gyp, etc.). The only difference is the
10319 in the Node-API are used.</p>
10321 provided by Node-API carries with it certain
10323 <p>To use Node-API in the above "Hello world" example, replace the content of
10325 <pre><code class="language-cpp"><span class="hljs-comment">// hello.cc using Node-API</span>
10326 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10328 <span class="hljs-keyword">namespace</span> demo {
10330 <span class="hljs-function">napi_value <span class="hljs-title">Method</span><span class="hljs-para…
10334 …status = <span class="hljs-built_in">napi_create_string_utf8</span>(env, <span class="hljs-string"…
10335 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
10336 <span class="hljs-keyword">return</span> greeting;
10339 <span class="hljs-function">napi_value <span class="hljs-title">init</span><span class="hljs-params…
10343 …js-built_in">napi_create_function</span>(env, <span class="hljs-literal">nullptr</span>, <span cla…
10344 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
10346 …status = <span class="hljs-built_in">napi_set_named_property</span>(env, exports, <span class="hlj…
10347 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
10348 <span class="hljs-keyword">return</span> exports;
10351 <span class="hljs-built_in">NAPI_MODULE</span>(NODE_GYP_MODULE_NAME, init)
10353 } <span class="hljs-comment">// namespace demo</span></code></pre>
10355 <a href="n-api.html">C/C++ addons with Node-API</a>.</p>
10363 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
10364 …<span class="hljs-attr">"targets"</span><span class="hljs-punctuation">:</span> <span class="hljs-
10365 <span class="hljs-punctuation">{</span>
10366 …an class="hljs-attr">"target_name"</span><span class="hljs-punctuation">:</span> <span class="hljs
10367-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</s…
10368 <span class="hljs-punctuation">}</span>
10369 <span class="hljs-punctuation">]</span>
10370 <span class="hljs-punctuation">}</span></code></pre>
10373-json"><span class="hljs-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class…
10375 built using <code>node-gyp</code>:</p>
10376 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
10384 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10385 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10387 <span class="hljs-keyword">namespace</span> demo {
10389 <span class="hljs-keyword">using</span> v8::Exception;
10390 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10391 <span class="hljs-keyword">using</span> v8::Isolate;
10392 <span class="hljs-keyword">using</span> v8::Local;
10393 <span class="hljs-keyword">using</span> v8::Number;
10394 <span class="hljs-keyword">using</span> v8::Object;
10395 <span class="hljs-keyword">using</span> v8::String;
10396 <span class="hljs-keyword">using</span> v8::Value;
10398 <span class="hljs-comment">// This is the implementation of the "add" method</span>
10399 <span class="hljs-comment">// Input arguments are passed using the</span>
10400 <span class="hljs-comment">// const FunctionCallbackInfo&#x3C;Value>&#x26; args struct</span>
10401 …n class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Add</span><sp…
10402 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10404 <span class="hljs-comment">// Check the number of arguments passed.</span>
10405 …<span class="hljs-keyword">if</span> (args.<span class="hljs-built_in">Length</span>() &#x3C; <spa…
10406 <span class="hljs-comment">// Throw an Error that is passed back to JavaScript</span>
10407 …isolate-><span class="hljs-built_in">ThrowException</span>(Exception::<span class="hljs-built_in">…
10408 String::<span class="hljs-built_in">NewFromUtf8</span>(isolate,
10409 …<span class="hljs-string">"Wrong number of arguments"</span>).<span class="hljs-built_in">ToLocalC…
10410 <span class="hljs-keyword">return</span>;
10413 <span class="hljs-comment">// Check the argument types</span>
10414-keyword">if</span> (!args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">IsNumb…
10415 …isolate-><span class="hljs-built_in">ThrowException</span>(Exception::<span class="hljs-built_in">…
10416 String::<span class="hljs-built_in">NewFromUtf8</span>(isolate,
10417 …<span class="hljs-string">"Wrong arguments"</span>).<span class="hljs-built_in">ToLocalChecked</sp…
10418 <span class="hljs-keyword">return</span>;
10421 <span class="hljs-comment">// Perform the operation</span>
10422 <span class="hljs-type">double</span> value =
10423-number">0</span>].<span class="hljs-built_in">As</span>&#x3C;Number>()-><span class="hljs-built_i…
10424 Local&#x3C;Number> num = Number::<span class="hljs-built_in">New</span>(isolate, value);
10426 <span class="hljs-comment">// Set the return value (using the passed in</span>
10427 <span class="hljs-comment">// FunctionCallbackInfo&#x3C;Value>&#x26;)</span>
10428 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(nu…
10431 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Init</span…
10432 …<span class="hljs-built_in">NODE_SET_METHOD</span>(exports, <span class="hljs-string">"add"</span>…
10435 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Init)
10437 } <span class="hljs-comment">// namespace demo</span></code></pre>
10439 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10440 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
10442-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
10447 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10448 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10450 <span class="hljs-keyword">namespace</span> demo {
10452 <span class="hljs-keyword">using</span> v8::Context;
10453 <span class="hljs-keyword">using</span> v8::Function;
10454 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10455 <span class="hljs-keyword">using</span> v8::Isolate;
10456 <span class="hljs-keyword">using</span> v8::Local;
10457 <span class="hljs-keyword">using</span> v8::Null;
10458 <span class="hljs-keyword">using</span> v8::Object;
10459 <span class="hljs-keyword">using</span> v8::String;
10460 <span class="hljs-keyword">using</span> v8::Value;
10462 …ass="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">RunCallback</span…
10463 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10464 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10465 …unction> cb = Local&#x3C;Function>::<span class="hljs-built_in">Cast</span>(args[<span class="hljs
10466 …<span class="hljs-keyword">const</span> <span class="hljs-keyword">unsigned</span> argc = <span cl…
10468 String::<span class="hljs-built_in">NewFromUtf8</span>(isolate,
10469 …<span class="hljs-string">"hello world"</span>).<span class="hljs-built_in">ToLocalChecked</span>(…
10470 …cb-><span class="hljs-built_in">Call</span>(context, <span class="hljs-built_in">Null</span>(isola…
10473-function"><span class="hljs-type">void</span> <span class="hljs-title">Init</span><span class="hl…
10474 …<span class="hljs-built_in">NODE_SET_METHOD</span>(<span class="hljs-keyword">module</span>, <span…
10477 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Init)
10479 } <span class="hljs-comment">// namespace demo</span></code></pre>
10480 <p>This example uses a two-argument form of <code>Init()</code> that receives the full
10482 overwrite <code>exports</code> with a single function instead of adding the function as a
10485 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10486 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
10488 <span class="hljs-title function_">addon</span>(<span class="hljs-function">(<span class="hljs-para…
10489 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
10490 <span class="hljs-comment">// Prints: 'hello world'</span>
10497 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10498 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10500 <span class="hljs-keyword">namespace</span> demo {
10502 <span class="hljs-keyword">using</span> v8::Context;
10503 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10504 <span class="hljs-keyword">using</span> v8::Isolate;
10505 <span class="hljs-keyword">using</span> v8::Local;
10506 <span class="hljs-keyword">using</span> v8::Object;
10507 <span class="hljs-keyword">using</span> v8::String;
10508 <span class="hljs-keyword">using</span> v8::Value;
10510 …ss="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">CreateObject</span…
10511 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10512 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10514 Local&#x3C;Object> obj = Object::<span class="hljs-built_in">New</span>(isolate);
10515 obj-><span class="hljs-built_in">Set</span>(context,
10516 String::<span class="hljs-built_in">NewFromUtf8</span>(isolate,
10517 … <span class="hljs-string">"msg"</span>).<span class="hljs-built_in">ToLocalChecked</span>(),
10518 …args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">ToString</span>(context).<sp…
10519 .<span class="hljs-built_in">FromJust</span>();
10521 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(ob…
10524-function"><span class="hljs-type">void</span> <span class="hljs-title">Init</span><span class="hl…
10525 …<span class="hljs-built_in">NODE_SET_METHOD</span>(<span class="hljs-keyword">module</span>, <span…
10528 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Init)
10530 } <span class="hljs-comment">// namespace demo</span></code></pre>
10532 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10533 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
10535 <span class="hljs-keyword">const</span> obj1 = <span class="hljs-title function_">addon</span>(<spa…
10536 <span class="hljs-keyword">const</span> obj2 = <span class="hljs-title function_">addon</span>(<spa…
10537 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(obj1.<span clas…
10538 <span class="hljs-comment">// Prints: 'hello world'</span></code></pre>
10542 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10543 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10545 <span class="hljs-keyword">namespace</span> demo {
10547 <span class="hljs-keyword">using</span> v8::Context;
10548 <span class="hljs-keyword">using</span> v8::Function;
10549 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10550 <span class="hljs-keyword">using</span> v8::FunctionTemplate;
10551 <span class="hljs-keyword">using</span> v8::Isolate;
10552 <span class="hljs-keyword">using</span> v8::Local;
10553 <span class="hljs-keyword">using</span> v8::Object;
10554 <span class="hljs-keyword">using</span> v8::String;
10555 <span class="hljs-keyword">using</span> v8::Value;
10557 …ass="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyFunction</span>…
10558 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10559 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(St…
10560 …isolate, <span class="hljs-string">"hello world"</span>).<span class="hljs-built_in">ToLocalChecke…
10563 …s="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">CreateFunction</spa…
10564 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10566 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10567 …Local&#x3C;FunctionTemplate> tpl = FunctionTemplate::<span class="hljs-built_in">New</span>(isolat…
10568 …Local&#x3C;Function> fn = tpl-><span class="hljs-built_in">GetFunction</span>(context).<span class…
10570 <span class="hljs-comment">// omit this to make it anonymous</span>
10571 …fn-><span class="hljs-built_in">SetName</span>(String::<span class="hljs-built_in">NewFromUtf8</sp…
10572 …isolate, <span class="hljs-string">"theFunction"</span>).<span class="hljs-built_in">ToLocalChecke…
10574 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(fn…
10577-function"><span class="hljs-type">void</span> <span class="hljs-title">Init</span><span class="hl…
10578 …<span class="hljs-built_in">NODE_SET_METHOD</span>(<span class="hljs-keyword">module</span>, <span…
10581 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, Init)
10583 } <span class="hljs-comment">// namespace demo</span></code></pre>
10585 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10586 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
10588 <span class="hljs-keyword">const</span> fn = <span class="hljs-title function_">addon</span>();
10589 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10590 <span class="hljs-comment">// Prints: 'hello world'</span></code></pre>
10594 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10595 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10596 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobj…
10598 <span class="hljs-keyword">namespace</span> demo {
10600 <span class="hljs-keyword">using</span> v8::Local;
10601 <span class="hljs-keyword">using</span> v8::Object;
10603 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">InitAll</s…
10604 MyObject::<span class="hljs-built_in">Init</span>(exports);
10607 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, InitAll)
10609 } <span class="hljs-comment">// namespace demo</span></code></pre>
10611 <pre><code class="language-cpp"><span class="hljs-comment">// myobject.h</span>
10612 <span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> MYOBJECT_H</span>
10613 <span class="hljs-meta">#<span class="hljs-keyword">define</span> MYOBJECT_H</span>
10615 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10616 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10618 <span class="hljs-keyword">namespace</span> demo {
10620 <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyObject</span> : <span cla…
10621 <span class="hljs-keyword">public</span>:
10622 …"hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span…
10624 <span class="hljs-keyword">private</span>:
10625-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">MyObject</span><spa…
10626 ~<span class="hljs-built_in">MyObject</span>();
10628-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10629-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10631 <span class="hljs-type">double</span> value_;
10634 } <span class="hljs-comment">// namespace demo</span>
10636 <span class="hljs-meta">#<span class="hljs-keyword">endif</span></span></code></pre>
10640 <pre><code class="language-cpp"><span class="hljs-comment">// myobject.cc</span>
10641 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobj…
10643 <span class="hljs-keyword">namespace</span> demo {
10645 <span class="hljs-keyword">using</span> v8::Context;
10646 <span class="hljs-keyword">using</span> v8::Function;
10647 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10648 <span class="hljs-keyword">using</span> v8::FunctionTemplate;
10649 <span class="hljs-keyword">using</span> v8::Isolate;
10650 <span class="hljs-keyword">using</span> v8::Local;
10651 <span class="hljs-keyword">using</span> v8::Number;
10652 <span class="hljs-keyword">using</span> v8::Object;
10653 <span class="hljs-keyword">using</span> v8::ObjectTemplate;
10654 <span class="hljs-keyword">using</span> v8::String;
10655 <span class="hljs-keyword">using</span> v8::Value;
10657 MyObject::<span class="hljs-built_in">MyObject</span>(<span class="hljs-type">double</span> value) …
10660 MyObject::~<span class="hljs-built_in">MyObject</span>() {
10663 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::…
10664 Isolate* isolate = exports-><span class="hljs-built_in">GetIsolate</span>();
10665 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10667 …Local&#x3C;ObjectTemplate> addon_data_tpl = ObjectTemplate::<span class="hljs-built_in">New</span>…
10668 …addon_data_tpl-><span class="hljs-built_in">SetInternalFieldCount</span>(<span class="hljs-number"…
10670 …addon_data_tpl-><span class="hljs-built_in">NewInstance</span>(context).<span class="hljs-built_in…
10672 <span class="hljs-comment">// Prepare constructor template</span>
10673 …Local&#x3C;FunctionTemplate> tpl = FunctionTemplate::<span class="hljs-built_in">New</span>(isolat…
10674-><span class="hljs-built_in">SetClassName</span>(String::<span class="hljs-built_in">NewFromUtf8<…
10675 …tpl-><span class="hljs-built_in">InstanceTemplate</span>()-><span class="hljs-built_in">SetInterna…
10677 <span class="hljs-comment">// Prototype</span>
10678 …<span class="hljs-built_in">NODE_SET_PROTOTYPE_METHOD</span>(tpl, <span class="hljs-string">"plusO…
10680 …Local&#x3C;Function> constructor = tpl-><span class="hljs-built_in">GetFunction</span>(context).<s…
10681 …addon_data-><span class="hljs-built_in">SetInternalField</span>(<span class="hljs-number">0</span>…
10682 …exports-><span class="hljs-built_in">Set</span>(context, String::<span class="hljs-built_in">NewFr…
10683 …isolate, <span class="hljs-string">"MyObject"</span>).<span class="hljs-built_in">ToLocalChecked</…
10684 constructor).<span class="hljs-built_in">FromJust</span>();
10687 …ss="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::New</spa…
10688 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10689 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10691 <span class="hljs-keyword">if</span> (args.<span class="hljs-built_in">IsConstructCall</span>()) {
10692 <span class="hljs-comment">// Invoked as constructor: `new MyObject(...)`</span>
10693 …<span class="hljs-type">double</span> value = args[<span class="hljs-number">0</span>]-><span clas…
10694-number">0</span> : args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">NumberVa…
10695 …MyObject* obj = <span class="hljs-keyword">new</span> <span class="hljs-built_in">MyObject</span>(…
10696 obj-><span class="hljs-built_in">Wrap</span>(args.<span class="hljs-built_in">This</span>());
10697 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(ar…
10698 } <span class="hljs-keyword">else</span> {
10699 …<span class="hljs-comment">// Invoked as plain function `MyObject(...)`, turn into construct call.…
10700 …<span class="hljs-keyword">const</span> <span class="hljs-type">int</span> argc = <span class="hlj…
10701 Local&#x3C;Value> argv[argc] = { args[<span class="hljs-number">0</span>] };
10703-built_in">Data</span>().<span class="hljs-built_in">As</span>&#x3C;Object>()-><span class="hljs-b…
10705 …cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_…
10706 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(re…
10710 …="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::PlusOne</s…
10711 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10713 …MyObject* obj = ObjectWrap::<span class="hljs-built_in">Unwrap</span>&#x3C;MyObject>(args.<span cl…
10714 obj->value_ += <span class="hljs-number">1</span>;
10716 …ass="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(Number::<span c…
10719 } <span class="hljs-comment">// namespace demo</span></code></pre>
10722 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
10723 …<span class="hljs-attr">"targets"</span><span class="hljs-punctuation">:</span> <span class="hljs-
10724 <span class="hljs-punctuation">{</span>
10725 …an class="hljs-attr">"target_name"</span><span class="hljs-punctuation">:</span> <span class="hljs
10726 …<span class="hljs-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-
10727 <span class="hljs-string">"addon.cc"</span><span class="hljs-punctuation">,</span>
10728 <span class="hljs-string">"myobject.cc"</span>
10729 <span class="hljs-punctuation">]</span>
10730 <span class="hljs-punctuation">}</span>
10731 <span class="hljs-punctuation">]</span>
10732 <span class="hljs-punctuation">}</span></code></pre>
10734 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10735 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
10737 …s="hljs-keyword">const</span> obj = <span class="hljs-keyword">new</span> addon.<span class="hljs-
10738 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10739 <span class="hljs-comment">// Prints: 11</span>
10740 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10741 <span class="hljs-comment">// Prints: 12</span>
10742 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10743 <span class="hljs-comment">// Prints: 13</span></code></pre>
10745 garbage-collected. For destructor testing, there are command-line flags that
10753 <pre><code class="language-js"><span class="hljs-keyword">const</span> obj = addon.<span class="hlj…
10754 <span class="hljs-comment">// instead of:</span>
10755 <span class="hljs-comment">// const obj = new addon.Object();</span></code></pre>
10757 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10758 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10759 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobj…
10761 <span class="hljs-keyword">namespace</span> demo {
10763 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10764 <span class="hljs-keyword">using</span> v8::Isolate;
10765 <span class="hljs-keyword">using</span> v8::Local;
10766 <span class="hljs-keyword">using</span> v8::Object;
10767 <span class="hljs-keyword">using</span> v8::String;
10768 <span class="hljs-keyword">using</span> v8::Value;
10770 …ss="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">CreateObject</span…
10771 MyObject::<span class="hljs-built_in">NewInstance</span>(args);
10774-function"><span class="hljs-type">void</span> <span class="hljs-title">InitAll</span><span class=…
10775 …MyObject::<span class="hljs-built_in">Init</span>(exports-><span class="hljs-built_in">GetIsolate<…
10777 …<span class="hljs-built_in">NODE_SET_METHOD</span>(<span class="hljs-keyword">module</span>, <span…
10780 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, InitAll)
10782 } <span class="hljs-comment">// namespace demo</span></code></pre>
10786 <pre><code class="language-cpp"><span class="hljs-comment">// myobject.h</span>
10787 <span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> MYOBJECT_H</span>
10788 <span class="hljs-meta">#<span class="hljs-keyword">define</span> MYOBJECT_H</span>
10790 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10791 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10793 <span class="hljs-keyword">namespace</span> demo {
10795 <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyObject</span> : <span cla…
10796 <span class="hljs-keyword">public</span>:
10797 …"hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span…
10798-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10800 <span class="hljs-keyword">private</span>:
10801-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">MyObject</span><spa…
10802 ~<span class="hljs-built_in">MyObject</span>();
10804-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10805-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
10806 <span class="hljs-keyword">static</span> v8::Global&#x3C;v8::Function> constructor;
10807 <span class="hljs-type">double</span> value_;
10810 } <span class="hljs-comment">// namespace demo</span>
10812 <span class="hljs-meta">#<span class="hljs-keyword">endif</span></span></code></pre>
10814 <pre><code class="language-cpp"><span class="hljs-comment">// myobject.cc</span>
10815 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10816 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobj…
10818 <span class="hljs-keyword">namespace</span> demo {
10820 <span class="hljs-keyword">using</span> node::AddEnvironmentCleanupHook;
10821 <span class="hljs-keyword">using</span> v8::Context;
10822 <span class="hljs-keyword">using</span> v8::Function;
10823 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10824 <span class="hljs-keyword">using</span> v8::FunctionTemplate;
10825 <span class="hljs-keyword">using</span> v8::Global;
10826 <span class="hljs-keyword">using</span> v8::Isolate;
10827 <span class="hljs-keyword">using</span> v8::Local;
10828 <span class="hljs-keyword">using</span> v8::Number;
10829 <span class="hljs-keyword">using</span> v8::Object;
10830 <span class="hljs-keyword">using</span> v8::String;
10831 <span class="hljs-keyword">using</span> v8::Value;
10833 <span class="hljs-comment">// Warning! This is not thread-safe, this addon cannot be used for worke…
10834 <span class="hljs-comment">// threads.</span>
10837 MyObject::<span class="hljs-built_in">MyObject</span>(<span class="hljs-type">double</span> value) …
10840 MyObject::~<span class="hljs-built_in">MyObject</span>() {
10843 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::…
10844 <span class="hljs-comment">// Prepare constructor template</span>
10845 …Local&#x3C;FunctionTemplate> tpl = FunctionTemplate::<span class="hljs-built_in">New</span>(isolat…
10846-><span class="hljs-built_in">SetClassName</span>(String::<span class="hljs-built_in">NewFromUtf8<…
10847 …tpl-><span class="hljs-built_in">InstanceTemplate</span>()-><span class="hljs-built_in">SetInterna…
10849 <span class="hljs-comment">// Prototype</span>
10850 …<span class="hljs-built_in">NODE_SET_PROTOTYPE_METHOD</span>(tpl, <span class="hljs-string">"plusO…
10852 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10853 …or.<span class="hljs-built_in">Reset</span>(isolate, tpl-><span class="hljs-built_in">GetFunction<…
10855 …<span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, [](<span class="hljs-type">v…
10856 constructor.<span class="hljs-built_in">Reset</span>();
10857 }, <span class="hljs-literal">nullptr</span>);
10860 …ss="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::New</spa…
10861 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10862 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10864 <span class="hljs-keyword">if</span> (args.<span class="hljs-built_in">IsConstructCall</span>()) {
10865 <span class="hljs-comment">// Invoked as constructor: `new MyObject(...)`</span>
10866 …<span class="hljs-type">double</span> value = args[<span class="hljs-number">0</span>]-><span clas…
10867-number">0</span> : args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">NumberVa…
10868 …MyObject* obj = <span class="hljs-keyword">new</span> <span class="hljs-built_in">MyObject</span>(…
10869 obj-><span class="hljs-built_in">Wrap</span>(args.<span class="hljs-built_in">This</span>());
10870 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(ar…
10871 } <span class="hljs-keyword">else</span> {
10872 …<span class="hljs-comment">// Invoked as plain function `MyObject(...)`, turn into construct call.…
10873 …<span class="hljs-keyword">const</span> <span class="hljs-type">int</span> argc = <span class="hlj…
10874 Local&#x3C;Value> argv[argc] = { args[<span class="hljs-number">0</span>] };
10875 …Local&#x3C;Function> cons = Local&#x3C;Function>::<span class="hljs-built_in">New</span>(isolate, …
10877 …cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_…
10878 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(in…
10882 …hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::NewInstance<…
10883 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10885 …<span class="hljs-keyword">const</span> <span class="hljs-keyword">unsigned</span> argc = <span cl…
10886 Local&#x3C;Value> argv[argc] = { args[<span class="hljs-number">0</span>] };
10887 …Local&#x3C;Function> cons = Local&#x3C;Function>::<span class="hljs-built_in">New</span>(isolate, …
10888 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10890 …cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_…
10892 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(in…
10895 …="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::PlusOne</s…
10896 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10898 …MyObject* obj = ObjectWrap::<span class="hljs-built_in">Unwrap</span>&#x3C;MyObject>(args.<span cl…
10899 obj->value_ += <span class="hljs-number">1</span>;
10901 …ass="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(Number::<span c…
10904 } <span class="hljs-comment">// namespace demo</span></code></pre>
10907 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
10908 …<span class="hljs-attr">"targets"</span><span class="hljs-punctuation">:</span> <span class="hljs-
10909 <span class="hljs-punctuation">{</span>
10910 …an class="hljs-attr">"target_name"</span><span class="hljs-punctuation">:</span> <span class="hljs
10911 …<span class="hljs-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-
10912 <span class="hljs-string">"addon.cc"</span><span class="hljs-punctuation">,</span>
10913 <span class="hljs-string">"myobject.cc"</span>
10914 <span class="hljs-punctuation">]</span>
10915 <span class="hljs-punctuation">}</span>
10916 <span class="hljs-punctuation">]</span>
10917 <span class="hljs-punctuation">}</span></code></pre>
10919 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
10920 <span class="hljs-keyword">const</span> createObject = <span class="hljs-built_in">require</span>(<…
10922 <span class="hljs-keyword">const</span> obj = <span class="hljs-title function_">createObject</span…
10923 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10924 <span class="hljs-comment">// Prints: 11</span>
10925 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10926 <span class="hljs-comment">// Prints: 12</span>
10927 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10928 <span class="hljs-comment">// Prints: 13</span>
10930 <span class="hljs-keyword">const</span> obj2 = <span class="hljs-title function_">createObject</spa…
10931 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10932 <span class="hljs-comment">// Prints: 21</span>
10933 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10934 <span class="hljs-comment">// Prints: 22</span>
10935 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
10936 <span class="hljs-comment">// Prints: 23</span></code></pre>
10942 <pre><code class="language-cpp"><span class="hljs-comment">// addon.cc</span>
10943 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10944 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10945 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobj…
10947 <span class="hljs-keyword">namespace</span> demo {
10949 <span class="hljs-keyword">using</span> v8::Context;
10950 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
10951 <span class="hljs-keyword">using</span> v8::Isolate;
10952 <span class="hljs-keyword">using</span> v8::Local;
10953 <span class="hljs-keyword">using</span> v8::Number;
10954 <span class="hljs-keyword">using</span> v8::Object;
10955 <span class="hljs-keyword">using</span> v8::String;
10956 <span class="hljs-keyword">using</span> v8::Value;
10958 …ss="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">CreateObject</span…
10959 MyObject::<span class="hljs-built_in">NewInstance</span>(args);
10962 …n class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">Add</span><sp…
10963 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
10964 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
10966 MyObject* obj1 = node::ObjectWrap::<span class="hljs-built_in">Unwrap</span>&#x3C;MyObject>(
10967 …args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">ToObject</span>(context).<sp…
10968 MyObject* obj2 = node::ObjectWrap::<span class="hljs-built_in">Unwrap</span>&#x3C;MyObject>(
10969 …args[<span class="hljs-number">1</span>]-><span class="hljs-built_in">ToObject</span>(context).<sp…
10971 …<span class="hljs-type">double</span> sum = obj1-><span class="hljs-built_in">value</span>() + obj…
10972 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(Nu…
10975 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">InitAll</s…
10976 …MyObject::<span class="hljs-built_in">Init</span>(exports-><span class="hljs-built_in">GetIsolate<…
10978 …<span class="hljs-built_in">NODE_SET_METHOD</span>(exports, <span class="hljs-string">"createObjec…
10979 …<span class="hljs-built_in">NODE_SET_METHOD</span>(exports, <span class="hljs-string">"add"</span>…
10982 <span class="hljs-built_in">NODE_MODULE</span>(NODE_GYP_MODULE_NAME, InitAll)
10984 } <span class="hljs-comment">// namespace demo</span></code></pre>
10987 <pre><code class="language-cpp"><span class="hljs-comment">// myobject.h</span>
10988 <span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> MYOBJECT_H</span>
10989 <span class="hljs-meta">#<span class="hljs-keyword">define</span> MYOBJECT_H</span>
10991 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10992 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
10994 <span class="hljs-keyword">namespace</span> demo {
10996 <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyObject</span> : <span cla…
10997 <span class="hljs-keyword">public</span>:
10998 …"hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span…
10999-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
11000-function"><span class="hljs-keyword">inline</span> <span class="hljs-type">double</span> <span cl…
11002 <span class="hljs-keyword">private</span>:
11003-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">MyObject</span><spa…
11004 ~<span class="hljs-built_in">MyObject</span>();
11006-function"><span class="hljs-keyword">static</span> <span class="hljs-type">void</span> <span clas…
11007 <span class="hljs-keyword">static</span> v8::Global&#x3C;v8::Function> constructor;
11008 <span class="hljs-type">double</span> value_;
11011 } <span class="hljs-comment">// namespace demo</span>
11013 <span class="hljs-meta">#<span class="hljs-keyword">endif</span></span></code></pre>
11015 <pre><code class="language-cpp"><span class="hljs-comment">// myobject.cc</span>
11016 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11017 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"myobj…
11019 <span class="hljs-keyword">namespace</span> demo {
11021 <span class="hljs-keyword">using</span> node::AddEnvironmentCleanupHook;
11022 <span class="hljs-keyword">using</span> v8::Context;
11023 <span class="hljs-keyword">using</span> v8::Function;
11024 <span class="hljs-keyword">using</span> v8::FunctionCallbackInfo;
11025 <span class="hljs-keyword">using</span> v8::FunctionTemplate;
11026 <span class="hljs-keyword">using</span> v8::Global;
11027 <span class="hljs-keyword">using</span> v8::Isolate;
11028 <span class="hljs-keyword">using</span> v8::Local;
11029 <span class="hljs-keyword">using</span> v8::Object;
11030 <span class="hljs-keyword">using</span> v8::String;
11031 <span class="hljs-keyword">using</span> v8::Value;
11033 <span class="hljs-comment">// Warning! This is not thread-safe, this addon cannot be used for worke…
11034 <span class="hljs-comment">// threads.</span>
11037 MyObject::<span class="hljs-built_in">MyObject</span>(<span class="hljs-type">double</span> value) …
11040 MyObject::~<span class="hljs-built_in">MyObject</span>() {
11043 <span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::…
11044 <span class="hljs-comment">// Prepare constructor template</span>
11045 …Local&#x3C;FunctionTemplate> tpl = FunctionTemplate::<span class="hljs-built_in">New</span>(isolat…
11046-><span class="hljs-built_in">SetClassName</span>(String::<span class="hljs-built_in">NewFromUtf8<…
11047 …tpl-><span class="hljs-built_in">InstanceTemplate</span>()-><span class="hljs-built_in">SetInterna…
11049 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
11050 …or.<span class="hljs-built_in">Reset</span>(isolate, tpl-><span class="hljs-built_in">GetFunction<…
11052 …<span class="hljs-built_in">AddEnvironmentCleanupHook</span>(isolate, [](<span class="hljs-type">v…
11053 constructor.<span class="hljs-built_in">Reset</span>();
11054 }, <span class="hljs-literal">nullptr</span>);
11057 …ss="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::New</spa…
11058 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
11059 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
11061 <span class="hljs-keyword">if</span> (args.<span class="hljs-built_in">IsConstructCall</span>()) {
11062 <span class="hljs-comment">// Invoked as constructor: `new MyObject(...)`</span>
11063 …<span class="hljs-type">double</span> value = args[<span class="hljs-number">0</span>]-><span clas…
11064-number">0</span> : args[<span class="hljs-number">0</span>]-><span class="hljs-built_in">NumberVa…
11065 …MyObject* obj = <span class="hljs-keyword">new</span> <span class="hljs-built_in">MyObject</span>(…
11066 obj-><span class="hljs-built_in">Wrap</span>(args.<span class="hljs-built_in">This</span>());
11067 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(ar…
11068 } <span class="hljs-keyword">else</span> {
11069 …<span class="hljs-comment">// Invoked as plain function `MyObject(...)`, turn into construct call.…
11070 …<span class="hljs-keyword">const</span> <span class="hljs-type">int</span> argc = <span class="hlj…
11071 Local&#x3C;Value> argv[argc] = { args[<span class="hljs-number">0</span>] };
11072 …Local&#x3C;Function> cons = Local&#x3C;Function>::<span class="hljs-built_in">New</span>(isolate, …
11074 …cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_…
11075 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(in…
11079 …hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">MyObject::NewInstance<…
11080 Isolate* isolate = args.<span class="hljs-built_in">GetIsolate</span>();
11082 …<span class="hljs-keyword">const</span> <span class="hljs-keyword">unsigned</span> argc = <span cl…
11083 Local&#x3C;Value> argv[argc] = { args[<span class="hljs-number">0</span>] };
11084 …Local&#x3C;Function> cons = Local&#x3C;Function>::<span class="hljs-built_in">New</span>(isolate, …
11085 Local&#x3C;Context> context = isolate-><span class="hljs-built_in">GetCurrentContext</span>();
11087 …cons-><span class="hljs-built_in">NewInstance</span>(context, argc, argv).<span class="hljs-built_…
11089 …args.<span class="hljs-built_in">GetReturnValue</span>().<span class="hljs-built_in">Set</span>(in…
11092 } <span class="hljs-comment">// namespace demo</span></code></pre>
11094 <pre><code class="language-js"><span class="hljs-comment">// test.js</span>
11095 <span class="hljs-keyword">const</span> addon = <span class="hljs-built_in">require</span>(<span cl…
11097 <span class="hljs-keyword">const</span> obj1 = addon.<span class="hljs-title function_">createObjec…
11098 <span class="hljs-keyword">const</span> obj2 = addon.<span class="hljs-title function_">createObjec…
11099 <span class="hljs-keyword">const</span> result = addon.<span class="hljs-title function_">add</span…
11101 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
11102 <span class="hljs-comment">// Prints: 30</span></code></pre></section>
11104 <section><h2>Node-API<span><a class="mark" href="#n_api_node_api" id="n_api_node_api">#</a></span><…
11107 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
11108 <p>Node-API (formerly N-API) is an API for building native Addons. It is
11114 …e <a href="https://nodejs.org/en/docs/guides/abi-stability/">ABI Stability</a> guide provides a mo…
11118 APIs, the functions available in Node-API are used.</p>
11119 <p>APIs exposed by Node-API are generally used to create and manipulate
11121 in the ECMA-262 Language Specification. The APIs have the following
11124 <li>All Node-API calls return a status code of type <code>napi_status</code>. This
11133 <p>Node-API is a C API that ensures ABI stability across Node.js versions
11136 C++ wrapper module called <a href="https://github.com/nodejs/node-addon-api"><code>node-addon-api</…
11138 with <code>node-addon-api</code> will depend on the symbols for the Node-API C-based
11139 functions exported by Node.js. <code>node-addon-api</code> is a more
11140 efficient way to write code that calls Node-API. Take, for example, the
11141 following <code>node-addon-api</code> code. The first section shows the
11142 <code>node-addon-api</code> code and the second section shows what actually gets
11144 <pre><code class="language-cpp">Object obj = Object::<span class="hljs-built_in">New</span>(env);
11145 obj[<span class="hljs-string">"foo"</span>] = String::<span class="hljs-built_in">New</span>(env, <…
11146 <pre><code class="language-cpp">napi_status status;
11148 status = <span class="hljs-built_in">napi_create_object</span>(env, &#x26;object);
11149 <span class="hljs-keyword">if</span> (status != napi_ok) {
11150 <span class="hljs-built_in">napi_throw_error</span>(env, ...);
11151 <span class="hljs-keyword">return</span>;
11154 status = <span class="hljs-built_in">napi_create_string_utf8</span>(env, <span class="hljs-string">…
11155 <span class="hljs-keyword">if</span> (status != napi_ok) {
11156 <span class="hljs-built_in">napi_throw_error</span>(env, ...);
11157 <span class="hljs-keyword">return</span>;
11160 status = <span class="hljs-built_in">napi_set_named_property</span>(env, object, <span class="hljs-
11161 <span class="hljs-keyword">if</span> (status != napi_ok) {
11162 <span class="hljs-built_in">napi_throw_error</span>(env, ...);
11163 <span class="hljs-keyword">return</span>;
11167 …en using <code>node-addon-api</code> instead of the C APIs, start with the API <a href="https://gi…
11168 for <code>node-addon-api</code>.</p>
11169 <p>The <a href="https://nodejs.github.io/node-addon-examples/">Node-API Resource</a> offers
11171 Node-API and <code>node-addon-api</code>.</p>
11173 <p>Although Node-API provides an ABI stability guarantee, other parts of Node.js do
11180 <pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> …
11181 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11182 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11183 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11187 <pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> …
11191 <pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> …
11194 <p>Thus, for an addon to remain ABI-compatible across Node.js major versions, it
11195 must use Node-API exclusively by restricting itself to using</p>
11196 <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <s…
11198 library makes ABI stability guarantees similar to Node-API.</p>
11201 native addons using Node-API requires an additional set of tools. Besides the
11213 <pre><code class="language-bash">xcode-select --install</code></pre>
11217 <pre><code class="language-bash">npm install --global windows-build-tools</code></pre>
11224 <h5>node-gyp<span><a class="mark" href="#n_api_node_gyp" id="n_api_node_gyp">#</a></span></h5>
11225 …ps://github.com/nodejs/node-gyp">node-gyp</a> is a build system based on the <a href="https://gith…
11226 …<a href="https://gyp.gsrc.io">GYP</a> tool and comes bundled with npm. GYP, and therefore node-gyp,
11228 <p>Historically, node-gyp has been the tool of choice for building native
11230 developers have run into limitations in node-gyp.</p>
11232 <p><a href="https://github.com/cmake-js/cmake-js">CMake.js</a> is an alternative build system based…
11234 developers affected by limitations in node-gyp.</p>
11238 typically integrated with CI/CD build systems like <a href="https://travis-ci.org">Travis CI</a> and
11242 <h5>node-pre-gyp<span><a class="mark" href="#n_api_node_pre_gyp" id="n_api_node_pre_gyp">#</a></spa…
11243 <p><a href="https://github.com/mapbox/node-pre-gyp">node-pre-gyp</a> is a tool based on node-gyp th…
11244 upload binaries to a server of the developer's choice. node-pre-gyp has
11247 …/github.com/prebuild/prebuild">prebuild</a> is a tool that supports builds using either node-gyp or
11248 CMake.js. Unlike node-pre-gyp which supports a variety of servers, prebuild
11249 …s only to <a href="https://help.github.com/en/github/administering-a-repository/about-releases">Gi…
11252 <p><a href="https://github.com/prebuild/prebuildify">prebuildify</a> is a tool based on node-gyp. T…
11257 <p>In order to use the Node-API functions, include the file <a href="https://github.com/nodejs/node…
11259 <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <s…
11261 In order to ensure compatibility with specific versions of Node-API, the version
11263 <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAP…
11264 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11265 <p>This restricts the Node-API surface to just the functionality that was available
11267 <p>Some of the Node-API surface is experimental and requires explicit opt-in:</p>
11268 <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAP…
11269 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11272 </section><section><h3>Node-API version matrix<span><a class="mark" href="#n_api_node_api_version_m…
11273 <p>Node-API versions are additive and versioned independently from Node.js.
11278 <!-- For accessibility purposes, this table needs row headers. That means we
11279 can't do it in markdown. Hence, the raw HTML. -->
11378 <p>* Node-API was experimental.</p>
11379 <p>** Node.js 8.0.0 included Node-API as experimental. It was released as
11380 Node-API version 1 but continued to evolve until Node.js 8.6.0. The API is
11381 different in versions prior to Node.js 8.6.0. We recommend Node-API version 3 or
11383 <p>Each API documented for Node-API will have a header named <code>added in:</code>, and APIs
11384 which are stable will have the additional header <code>Node-API version:</code>.
11386 the Node-API version shown in <code>Node-API version:</code> or higher.
11388 <code>Node-API version:</code> listed or if there is no <code>Node-API version:</code> listed,
11394 <p>The Node-APIs associated strictly with accessing ECMAScript features from native
11398 implementations of Node-API outside of Node.js. For those implementations the
11400 <p>The Node.js-specific parts of an addon can be separated from the code that
11402 latter may be used with multiple implementations of Node-API. In the example
11405 implementation of Node-API or any implementation of Node-API outside of Node.js.</p>
11409 <pre><code class="language-c"><span class="hljs-comment">// addon.h</span>
11410 <span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> _ADDON_H_</span>
11411 <span class="hljs-meta">#<span class="hljs-keyword">define</span> _ADDON_H_</span>
11412 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11413 napi_value <span class="hljs-title function_">create_addon</span><span class="hljs-params">(napi_en…
11414 <span class="hljs-meta">#<span class="hljs-keyword">endif</span> <span class="hljs-comment">// _AD…
11415 <pre><code class="language-c"><span class="hljs-comment">// addon.c</span>
11416 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"addon…
11418 <span class="hljs-meta">#<span class="hljs-keyword">define</span> NAPI_CALL(env, call) …
11421 … <span class="hljs-keyword">if</span> (status != napi_ok) { \
11426 … <span class="hljs-keyword">if</span> (!is_pending) { \
11427 const char* message = (error_info->error_message == NULL) \
11428 ? <span class="hljs-string">"empty error message"</span> \
11429 : error_info->error_message; \
11436 <span class="hljs-keyword">static</span> napi_value
11437 <span class="hljs-title function_">DoSomethingUseful</span><span class="hljs-params">(napi_env env,…
11438 <span class="hljs-comment">// Do something useful.</span>
11439 <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
11442 napi_value <span class="hljs-title function_">create_addon</span><span class="hljs-params">(napi_en…
11448 <span class="hljs-string">"doSomethingUseful"</span>,
11451 <span class="hljs-literal">NULL</span>,
11456 <span class="hljs-string">"doSomethingUseful"</span>,
11459 <span class="hljs-keyword">return</span> result;
11461 <pre><code class="language-c"><span class="hljs-comment">// addon_node.c</span>
11462 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
11463 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"addon…
11466 <span class="hljs-comment">// This function body is expected to return a `napi_value`.</span>
11467 …<span class="hljs-comment">// The variables `napi_env env` and `napi_value exports` may be used wi…
11468 …<span class="hljs-comment">// the body, as they are provided by the definition of `NAPI_MODULE_INI…
11469 <span class="hljs-keyword">return</span> create_addon(env);
11472 <p><a href="https://tc39.es/ecma262/#sec-agents">Section 8.7</a> of the <a href="https://tc39.githu…
11473 of an "Agent" as a self-contained environment in which JavaScript code runs.
11490 <p>To this end, Node-API provides a way to allocate data such that its life cycle
11495 <span>N-API version: 6</span>
11497 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_instance_data…
11498 <span class="hljs-type">void</span>* data,
11500 … <span class="hljs-type">void</span>* finalize_hint)</span>;</code></pre>
11502 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
11519 <span>N-API version: 6</span>
11521 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_instance_data…
11522 <span class="hljs-type">void</span>** data)</span>;</code></pre>
11524 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
11532 </section><section><h3>Basic Node-API data types<span><a class="mark" href="#n_api_basic_node_api_d…
11533 <p>Node-API exposes the following fundamental datatypes as abstractions that are
11535 introspectable only with other Node-API calls.</p>
11539 <span>N-API version: 1</span>
11541 <p>Integral status code indicating the success or failure of a Node-API call.
11543 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
11565 napi_would_deadlock, <span class="hljs-comment">/* unused */</span>
11572 <span>N-API version: 1</span>
11574 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
11575 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* error_message;
11576 <span class="hljs-type">void</span>* engine_reserved;
11577 <span class="hljs-type">uint32_t</span> engine_error_code;
11581 <li><code>error_message</code>: UTF8-encoded string containing a VM-neutral description of
11583 <li><code>engine_reserved</code>: Reserved for VM-specific error details. This is currently
11585 <li><code>engine_error_code</code>: VM-specific error code. This is currently
11587 <li><code>error_code</code>: The Node-API status code that originated with the last error.</li>
11591 <p><code>napi_env</code> is used to represent a context that the underlying Node-API
11592 implementation can use to persist VM-specific state. This structure is passed
11594 making Node-API calls. Specifically, the same <code>napi_env</code> that was passed in when
11596 nested Node-API calls. Caching the <code>napi_env</code> for the purpose of general reuse,
11607 <span>N-API version: 4</span>
11615 <span>N-API version: 4</span>
11618 the thread-safe function is to be closed immediately (<code>napi_tsfn_abort</code>) or
11621 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
11628 <span>N-API version: 4</span>
11631 the call should block whenever the queue associated with the thread-safe
11633 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
11637 <h4>Node-API memory management types<span><a class="mark" href="#n_api_node_api_memory_management_t…
11640 created within a particular scope. In general, Node-API values are created
11643 create a new handle scope, Node-API values will be created in the default handle
11656 <span>N-API version: 1</span>
11663 <span>N-API version: 1</span>
11672 <span>N-API version: 8</span>
11674 <p>A 128-bit value stored as two unsigned 64-bit integers. It serves as a UUID
11678 manipulated. Type-tagging is most useful in conjunction with <a href="#n_api_napi_wrap"><code>napi_…
11682 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
11683 <span class="hljs-type">uint64_t</span> lower;
11684 <span class="hljs-type">uint64_t</span> upper;
11693 <h4>Node-API callback types<span><a class="mark" href="#n_api_node_api_callback_types" id="n_api_no…
11697 <span>N-API version: 1</span>
11705 <span>N-API version: 1</span>
11707 <p>Function pointer type for user-provided native functions which are to be
11708 exposed to JavaScript via Node-API. Callback functions should satisfy the
11710-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">napi_value</span>…
11716 <span>N-API version: 1</span>
11718 <p>Function pointer type for add-on provided functions that allow the user to be
11719 notified when externally-owned data is ready to be cleaned up because the
11720 object with which it was associated with, has been garbage-collected. The user
11724-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span…
11725 <span class="hljs-type">void</span>* finalize_data,
11726 … <span class="hljs-type">void</span>* finalize_hint)</span>;</code></pre>
11732 <span>N-API version: 1</span>
11736-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span…
11737 <p>Implementations of this function must avoid making Node-API calls that execute
11738 JavaScript or interact with JavaScript objects. Node-API calls should be in the
11744 <span>N-API version: 1</span>
11748-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span…
11750 … <span class="hljs-type">void</span>* data)</span>;</code></pre>
11756 <span>N-API version: 4</span>
11758 <p>Function pointer used with asynchronous thread-safe function calls. The callback
11766 <p>Node-API sets up the environment prior to calling this callback, so it is
11770-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span…
11772 <span class="hljs-type">void</span>* context,
11773 … <span class="hljs-type">void</span>* data)</span>;</code></pre>
11775 …code>[in] env</code>: The environment to use for API calls, or <code>NULL</code> if the thread-safe
11778 thread-safe function is being torn down and <code>data</code> may need to be freed. It
11779 may also be <code>NULL</code> if the thread-safe function was created without
11781 <li><code>[in] context</code>: The optional data with which the thread-safe function was
11784 the callback to convert this native data to JavaScript values (with Node-API
11798-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span…
11799 … <span class="hljs-type">void</span>* data)</span>;</code></pre>
11810 <p>Node-API uses both return values and JavaScript exceptions for error handling.
11813 <p>All of the Node-API functions share the same error handling pattern. The
11826 <p>The <code>napi_status</code> return value provides a VM-independent representation of
11829 VM (engine)-specific information.</p>
11835 <span>N-API version: 1</span>
11837 …s="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hlj…
11838 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* error_message;
11839 <span class="hljs-type">void</span>* engine_reserved;
11840 <span class="hljs-type">uint32_t</span> engine_error_code;
11847 <li><code>error_code</code>: Node-API status code for the last error.</li>
11850 Node-API call that was made.</p>
11857 <span>N-API version: 1</span>
11859 <pre><code class="language-c">napi_status
11860 <span class="hljs-title function_">napi_get_last_error_info</span><span class="hljs-params">(napi_e…
11861 … <span class="hljs-keyword">const</span> napi_extended_error_info** result)</span>;</code></pre>
11871 a Node-API function is called on the same <code>env</code>.</p>
11877 <p>Any Node-API function call may result in a pending JavaScript exception. This is
11886 <p>In many cases when a Node-API function is called and an exception is
11889 for all functions. Node-API allows a subset of the functions to be
11898 code where the native method was invoked. The behavior of most Node-API calls
11910 it can be re-thrown it with <a href="#n_api_napi_throw"><code>napi_throw</code></a> where error is …
11927 SemVer applying. In order to support this model with Node-API, both
11934 <pre><code class="language-text">originalName [code]</code></pre>
11938 <pre><code class="language-text">TypeError [ERR_ERROR_1]</code></pre>
11942 <span>N-API version: 1</span>
11944 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw…
11954 <span>N-API version: 1</span>
11956 …re><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_…
11957 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* code,
11958 …<span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* msg)</span>;</code></…
11969 <span>N-API version: 1</span>
11971 …><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_ty…
11972 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* code,
11973 …<span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* msg)</span>;</code></…
11984 <span>N-API version: 1</span>
11986 …<code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_ran…
11987 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* code,
11988 …<span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* msg)</span>;</code></…
11999 <span>N-API version: 1</span>
12001 <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_is_er…
12003 … <span class="hljs-type">bool</span>* result)</span>;</code></pre>
12015 <span>N-API version: 1</span>
12017 …re><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create…
12034 <span>N-API version: 1</span>
12036 …<code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_ty…
12053 <span>N-API version: 1</span>
12055 …<code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_ra…
12072 <span>N-API version: 1</span>
12074 …><code class="language-c">napi_status <span class="hljs-title function_">napi_get_and_clear_last_e…
12085 <span>N-API version: 1</span>
12087 …guage-c">napi_status <span class="hljs-title function_">napi_is_exception_pending</span><span clas…
12097 <span>N-API version: 3</span>
12099 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_fatal_exception</…
12112 <span>N-API version: 1</span>
12114-c">NAPI_NO_RETURN <span class="hljs-type">void</span> <span class="hljs-title function_">napi_fat…
12115 <span class="hljs-type">size_t</span> location_len,
12116 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* message,
12117 … <span class="hljs-type">size_t</span> message_len)</span>;</code></pre>
12121 <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li>
12124 if it is null-terminated.</li>
12129 <p>As Node-API calls are made, handles to objects in the heap for the underlying
12141 The sections which follow describe the Node-API functions that can be used
12147 …uage-c"><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class=…
12150 <span class="hljs-keyword">if</span> (status != napi_ok) {
12151 <span class="hljs-keyword">break</span>;
12153 <span class="hljs-comment">// do something with element</span>
12159 <p>To handle this case, Node-API provides the ability to establish a new 'scope' to
12164 <p>Node-API only supports a single nested hierarchy of scopes. There is only one
12170 …se_handle_scope"><code>napi_close_handle_scope</code></a> would ensure that at most a single handle
12172 …uage-c"><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class=…
12175 <span class="hljs-keyword">if</span> (status != napi_ok) {
12176 <span class="hljs-keyword">break</span>;
12180 <span class="hljs-keyword">if</span> (status != napi_ok) {
12181 <span class="hljs-keyword">break</span>;
12183 <span class="hljs-comment">// do something with element</span>
12185 <span class="hljs-keyword">if</span> (status != napi_ok) {
12186 <span class="hljs-keyword">break</span>;
12190 inner scope needs to live beyond the lifespan of that scope. Node-API supports
12203 <span>N-API version: 1</span>
12205 …<code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_open_hand…
12216 <span>N-API version: 1</span>
12218 …<code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_close_han…
12231 <span>N-API version: 1</span>
12233 <pre><code class="language-c">NAPI_EXTERN napi_status
12234 …<span class="hljs-title function_">napi_open_escapable_handle_scope</span><span class="hljs-params…
12246 <span>N-API version: 1</span>
12248 <pre><code class="language-c">NAPI_EXTERN napi_status
12249 …<span class="hljs-title function_">napi_close_escapable_handle_scope</span><span class="hljs-param…
12262 <span>N-API version: 1</span>
12264 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_escape_handle</sp…
12283 with a lifespan longer than that of a single native method invocation. For
12291 <p>Node-API provides methods to create persistent references to an object.
12317 <span>N-API version: 1</span>
12319 …><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_r…
12321 … <span class="hljs-type">uint32_t</span> initial_refcount,
12336 <span>N-API version: 1</span>
12338 …><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_delete_r…
12349 <span>N-API version: 1</span>
12351 …e><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_referen…
12353 … <span class="hljs-type">uint32_t</span>* result)</span>;</code></pre>
12365 <span>N-API version: 1</span>
12367 …><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_referenc…
12369 … <span class="hljs-type">uint32_t</span>* result)</span>;</code></pre>
12381 <span>N-API version: 1</span>
12383 …code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_get_refere…
12401 clean-up hooks that will be run once the current Node.js instance exits.</p>
12402 <p>Node-API provides functions for registering and un-registering such callbacks.
12408 <span>N-API version: 3</span>
12410 …code class="language-c">NODE_EXTERN napi_status <span class="hljs-title function_">napi_add_env_cl…
12411 … <span class="hljs-type">void</span> (*fun)(<span class="hljs-type">void</span>* arg),
12412 … <span class="hljs-type">void</span>* arg)</span>;</code></pre>
12428 <span>N-API version: 3</span>
12430 …de class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_remove_env_c…
12431 … <span class="hljs-type">void</span> (*fun)(<span class="hljs-type">void</span>* arg),
12432 … <span class="hljs-type">void</span>* arg)</span>;</code></pre>
12449 <span>N-API version: 8</span>
12451 …ode class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_add_async_c…
12454 <span class="hljs-type">void</span>* arg,
12485 …e class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_remove_async_…
12496 <p>Node-API modules are registered in a manner similar to other modules
12499 <pre><code class="language-c">NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)</code></pre>
12500 <p>The next difference is the signature for the <code>Init</code> method. For a Node-API
12502 <pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class=…
12506 exported by the module. Node-API modules cannot modify the <code>module</code> object but
12510 <pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class=…
12513 <span class="hljs-string">"hello"</span>,
12514 <span class="hljs-literal">NULL</span>,
12516 <span class="hljs-literal">NULL</span>,
12517 <span class="hljs-literal">NULL</span>,
12518 <span class="hljs-literal">NULL</span>,
12520 <span class="hljs-literal">NULL</span>
12522 status = napi_define_properties(env, exports, <span class="hljs-number">1</span>, &#x26;desc);
12523 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12524 <span class="hljs-keyword">return</span> exports;
12527 <pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class=…
12530 …pi_create_function(env, <span class="hljs-string">"exports"</span>, NAPI_AUTO_LENGTH, Method, <spa…
12531 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12532 <span class="hljs-keyword">return</span> method;
12536 <pre><code class="language-c"><span class="hljs-comment">// <span class="hljs-doctag">NOTE:</span> …
12537 napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, n…
12540-string">"value"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</…
12541 DECLARE_NAPI_METHOD(<span class="hljs-string">"plusOne"</span>, PlusOne),
12542 DECLARE_NAPI_METHOD(<span class="hljs-string">"multiply"</span>, Multiply),
12547 …ne_class(env, <span class="hljs-string">"MyObject"</span>, New, <span class="hljs-literal">NULL</s…
12548 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12550 status = napi_create_reference(env, cons, <span class="hljs-number">1</span>, &#x26;constructor);
12551 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12553 status = napi_set_named_property(env, exports, <span class="hljs-string">"MyObject"</span>, cons);
12554 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12556 <span class="hljs-keyword">return</span> exports;
12560 <pre><code class="language-c">NAPI_MODULE_INIT() {
12564 status = napi_create_int64(env, <span class="hljs-number">42</span>, &#x26;answer);
12565 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12567 status = napi_set_named_property(env, exports, <span class="hljs-string">"answer"</span>, answer);
12568 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
12570 <span class="hljs-keyword">return</span> exports;
12572 <p>All Node-API addons are context-aware, meaning they may be loaded multiple
12574 The documentation on <a href="#addons_context_aware_addons">context-aware addons</a> provides more …
12582 <p>Node-API exposes a set of APIs to create all types of JavaScript values.
12583 …e documented under <a href="https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values">…
12588 <li>Convert from a primitive C type to a Node-API value</li>
12589 <li>Convert from Node-API value to a primitive C type</li>
12592 <p>Node-API values are represented by the type <code>napi_value</code>.
12593 Any Node-API call that requires a JavaScript value takes in a <code>napi_value</code>.
12594 In some cases, the API does check the type of the <code>napi_value</code> up-front.
12601 <span>N-API version: 6</span>
12603 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
12615 <span>N-API version: 6</span>
12617 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
12618 napi_key_all_properties = <span class="hljs-number">0</span>,
12619 napi_key_writable = <span class="hljs-number">1</span>,
12620 …napi_key_enumerable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">1<…
12621 …napi_key_configurable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">…
12622 …napi_key_skip_strings = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">…
12623 …napi_key_skip_symbols = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">…
12629 <span>N-API version: 6</span>
12631 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
12639 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
12640 <span class="hljs-comment">// ES6 types (corresponds to typeof)</span>
12653 described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types">Section 6.1</a…
12659 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
12674 <a href="https://tc39.github.io/ecma262/#sec-typedarray-objects">Section 22.2</a> of the <a href="h…
12679 <span>N-API version: 1</span>
12681 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_array</spa…
12683 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
12687 <p>This API returns a Node-API value corresponding to a JavaScript <code>Array</code> type.
12689 <a href="https://tc39.github.io/ecma262/#sec-array-objects">Section 22.1</a> of the ECMAScript Lang…
12693 <span>N-API version: 1</span>
12695 …re><code class="language-c">napi_status <span class="hljs-title function_">napi_create_array_with_…
12696 <span class="hljs-type">size_t</span> length,
12704 <p>This API returns a Node-API value corresponding to a JavaScript <code>Array</code> type.
12705 The <code>Array</code>'s length property is set to the passed-in length parameter.
12706 However, the underlying buffer is not guaranteed to be pre-allocated by the VM
12712 <a href="https://tc39.github.io/ecma262/#sec-array-objects">Section 22.1</a> of the ECMAScript Lang…
12716 <span>N-API version: 1</span>
12718 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_arraybuffe…
12719 <span class="hljs-type">size_t</span> byte_length,
12720 <span class="hljs-type">void</span>** data,
12729 <p>This API returns a Node-API value corresponding to a JavaScript <code>ArrayBuffer</code>.
12730 <code>ArrayBuffer</code>s are used to represent fixed-length binary data buffers. They are
12731 normally used as a backing-buffer for <code>TypedArray</code> objects.
12739 <a href="https://tc39.github.io/ecma262/#sec-arraybuffer-objects">Section 24.1</a> of the ECMAScrip…
12743 <span>N-API version: 1</span>
12745 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_buffer</sp…
12746 <span class="hljs-type">size_t</span> size,
12747 <span class="hljs-type">void</span>** data,
12757 fully-supported data structure, in most cases using a <code>TypedArray</code> will suffice.</p>
12761 <span>N-API version: 1</span>
12763 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_buffer_cop…
12764 <span class="hljs-type">size_t</span> length,
12765 … <span class="hljs-keyword">const</span> <span class="hljs-type">void</span>* data,
12766 <span class="hljs-type">void</span>** result_data,
12778 from the passed-in buffer. While this is still a fully-supported data
12783 <span>N-API version: 5</span>
12785 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_date</span…
12786 <span class="hljs-type">double</span> time,
12798 <a href="https://tc39.github.io/ecma262/#sec-date-objects">Section 20.3</a> of the ECMAScript Langu…
12802 <span>N-API version: 1</span>
12804 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external</…
12805 <span class="hljs-type">void</span>* data,
12807 <span class="hljs-type">void</span>* finalize_hint,
12836 <span>N-API version: 1</span>
12838 <pre><code class="language-c">napi_status
12839 <span class="hljs-title function_">napi_create_external_arraybuffer</span><span class="hljs-params"…
12840 <span class="hljs-type">void</span>* external_data,
12841 <span class="hljs-type">size_t</span> byte_length,
12843 <span class="hljs-type">void</span>* finalize_hint,
12857 <p>This API returns a Node-API value corresponding to a JavaScript <code>ArrayBuffer</code>.
12870 <a href="https://tc39.github.io/ecma262/#sec-arraybuffer-objects">Section 24.1</a> of the ECMAScrip…
12874 <span>N-API version: 1</span>
12876 …pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external_b…
12877 <span class="hljs-type">size_t</span> length,
12878 <span class="hljs-type">void</span>* data,
12880 <span class="hljs-type">void</span>* finalize_hint,
12895 backed by the passed in buffer. While this is still a fully-supported data
12909 <span>N-API version: 1</span>
12911 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_object</sp…
12919 …/code> type is described in <a href="https://tc39.github.io/ecma262/#sec-object-type">Section 6.1.…
12924 <span>N-API version: 1</span>
12926 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_symbol</sp…
12936 <p>This API creates a JavaScript <code>symbol</code> value from a UTF8-encoded C string.</p>
12937 …/code> type is described in <a href="https://tc39.github.io/ecma262/#sec-symbol-objects">Section 1…
12942 <span>N-API version: 1</span>
12944 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_typedarray…
12946 <span class="hljs-type">size_t</span> length,
12948 <span class="hljs-type">size_t</span> byte_offset,
12961 <code>ArrayBuffer</code>. <code>TypedArray</code> objects provide an array-like view over an
12968 <a href="https://tc39.github.io/ecma262/#sec-typedarray-objects">Section 22.2</a> of the ECMAScript…
12972 <span>N-API version: 1</span>
12974 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_dataview</…
12975 <span class="hljs-type">size_t</span> byte_length,
12977 <span class="hljs-type">size_t</span> byte_offset,
12989 <code>DataView</code> objects provide an array-like view over an underlying data buffer,
12995 <a href="https://tc39.github.io/ecma262/#sec-dataview-objects">Section 24.3</a> of the ECMAScript L…
12996 <h4>Functions to convert from C types to Node-API<span><a class="mark" href="#n_api_functions_to_co…
13000 <span>N-API version: 1</span>
13002 …"language-c">napi_status <span class="hljs-title function_">napi_create_int32</span><span class="h…
13012 <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</…
13016 <span>N-API version: 1</span>
13018 …"language-c">napi_status <span class="hljs-title function_">napi_create_uint32</span><span class="…
13028 <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</…
13032 <span>N-API version: 1</span>
13034 …"language-c">napi_status <span class="hljs-title function_">napi_create_int64</span><span class="h…
13043 …s described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type"…
13046 …//tc39.github.io/ecma262/#sec-number.min_safe_integer"><code>Number.MIN_SAFE_INTEGER</code></a> <c…
13047 …"https://tc39.github.io/ecma262/#sec-number.max_safe_integer"><code>Number.MAX_SAFE_INTEGER</code>…
13051 <span>N-API version: 1</span>
13053 …"language-c">napi_status <span class="hljs-title function_">napi_create_double</span><span class="…
13056 <li><code>[in] value</code>: Double-precision value to be represented in JavaScript.</li>
13063 <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</…
13067 <span>N-API version: 6</span>
13069 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_int…
13070 <span class="hljs-type">int64_t</span> value,
13082 <span>N-API version: 6</span>
13084 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_uin…
13085 <span class="hljs-type">uint64_t</span> value,
13097 <span>N-API version: 6</span>
13099 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_wor…
13100 <span class="hljs-type">int</span> sign_bit,
13101 <span class="hljs-type">size_t</span> word_count,
13102 … <span class="hljs-keyword">const</span> <span class="hljs-type">uint64_t</span>* words,
13109 <li><code>[in] words</code>: An array of <code>uint64_t</code> little-endian 64-bit words.</li>
13113 <p>This API converts an array of unsigned 64-bit words into a single <code>BigInt</code>
13120 <span>N-API version: 1</span>
13122 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_lat…
13123 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* str,
13124 <span class="hljs-type">size_t</span> length,
13128 <li><code>[in] str</code>: Character buffer representing an ISO-8859-1-encoded string.</li>
13130 is null-terminated.</li>
13134 <p>This API creates a JavaScript <code>string</code> value from an ISO-8859-1-encoded C
13137 <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</…
13141 <span>N-API version: 1</span>
13143 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_utf…
13144 … <span class="hljs-keyword">const</span> <span class="hljs-type">char16_t</span>* str,
13145 <span class="hljs-type">size_t</span> length,
13149 <li><code>[in] str</code>: Character buffer representing a UTF16-LE-encoded string.</li>
13150 <li><code>[in] length</code>: The length of the string in two-byte code units, or
13151 <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li>
13155 <p>This API creates a JavaScript <code>string</code> value from a UTF16-LE-encoded C string.
13158 <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</…
13162 <span>N-API version: 1</span>
13164 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_utf…
13165 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* str,
13166 <span class="hljs-type">size_t</span> length,
13170 <li><code>[in] str</code>: Character buffer representing a UTF8-encoded string.</li>
13172 is null-terminated.</li>
13176 <p>This API creates a JavaScript <code>string</code> value from a UTF8-encoded C string.
13179 <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</…
13180 <h4>Functions to convert from Node-API to C types<span><a class="mark" href="#n_api_functions_to_co…
13184 <span>N-API version: 1</span>
13186 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_array_length<…
13188 … <span class="hljs-type">uint32_t</span>* result)</span></code></pre>
13197 … is described in <a href="https://tc39.github.io/ecma262/#sec-properties-of-array-instances-length…
13202 <span>N-API version: 1</span>
13204 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_arraybuffer_i…
13206 <span class="hljs-type">void</span>** data,
13207 … <span class="hljs-type">size_t</span>* byte_length)</span></code></pre>
13228 <span>N-API version: 1</span>
13230 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_buffer_info</…
13232 <span class="hljs-type">void</span>** data,
13233 <span class="hljs-type">size_t</span>* length)</span></code></pre>
13249 <span>N-API version: 1</span>
13251 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_prototype</sp…
13265 <span>N-API version: 1</span>
13267 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_typedarray_in…
13270 <span class="hljs-type">size_t</span>* length,
13271 <span class="hljs-type">void</span>** data,
13273 … <span class="hljs-type">size_t</span>* byte_offset)</span></code></pre>
13289 <code>data - byte_offset</code>.</li>
13298 <span>N-API version: 1</span>
13300 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_dataview_info…
13302 <span class="hljs-type">size_t</span>* byte_length,
13303 <span class="hljs-type">void</span>** data,
13305 … <span class="hljs-type">size_t</span>* byte_offset)</span></code></pre>
13322 <span>N-API version: 5</span>
13324 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_date_value</s…
13326 <span class="hljs-type">double</span>* result)</span></code></pre>
13335 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-date <code>napi_value</code> is pass…
13342 <span>N-API version: 1</span>
13344-c">napi_status <span class="hljs-title function_">napi_get_value_bool</span><span class="hljs-par…
13351 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-boolean <code>napi_value</code> is
13358 <span>N-API version: 1</span>
13360 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_double<…
13362 <span class="hljs-type">double</span>* result)</span></code></pre>
13369 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code> is pa…
13376 <span>N-API version: 6</span>
13378 …pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_…
13380 <span class="hljs-type">int64_t</span>* result,
13381 … <span class="hljs-type">bool</span>* lossless)</span>;</code></pre>
13390 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>BigInt</code> is passed in it
13397 <span>N-API version: 6</span>
13399 …re><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_u…
13401 <span class="hljs-type">uint64_t</span>* result,
13402 … <span class="hljs-type">bool</span>* lossless)</span>;</code></pre>
13411 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>BigInt</code> is passed in it
13418 <span>N-API version: 6</span>
13420 …pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_…
13422 <span class="hljs-type">int</span>* sign_bit,
13423 <span class="hljs-type">size_t</span>* word_count,
13424 … <span class="hljs-type">uint64_t</span>* words)</span>;</code></pre>
13433 <li><code>[out] words</code>: Pointer to a pre-allocated 64-bit word array.</li>
13436 <p>This API converts a single <code>BigInt</code> value into a sign bit, 64-bit little-endian
13442 <span>N-API version: 1</span>
13444 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_externa…
13446 … <span class="hljs-type">void</span>** result)</span></code></pre>
13452 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-external <code>napi_value</code> is
13459 <span>N-API version: 1</span>
13461 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_int32</…
13463 <span class="hljs-type">int32_t</span>* result)</span></code></pre>
13470 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
13476 positive number becoming a negative number if the value is > 2<sup>31</sup> - 1.</p>
13477 <p>Non-finite number values (<code>NaN</code>, <code>+Infinity</code>, or <code>-Infinity</code>) s…
13482 <span>N-API version: 1</span>
13484 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_int64</…
13486 <span class="hljs-type">int64_t</span>* result)</span></code></pre>
13493 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
13497 <p><code>number</code> values outside the range of <a href="https://tc39.github.io/ecma262/#sec-num…
13498 …de>-(2**53 - 1)</code> - <a href="https://tc39.github.io/ecma262/#sec-number.max_safe_integer"><co…
13500 <p>Non-finite number values (<code>NaN</code>, <code>+Infinity</code>, or <code>-Infinity</code>) s…
13505 <span>N-API version: 1</span>
13507 …re><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_l…
13509 <span class="hljs-type">char</span>* buf,
13510 <span class="hljs-type">size_t</span> bufsize,
13511 … <span class="hljs-type">size_t</span>* result)</span></code></pre>
13515 <li><code>[in] buf</code>: Buffer to write the ISO-8859-1-encoded string into. If <code>NULL</code>…
13519 insufficient, the returned string is truncated and null-terminated.</li>
13523 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value…
13525 <p>This API returns the ISO-8859-1-encoded string corresponding the value passed
13530 <span>N-API version: 1</span>
13532 …pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_…
13534 <span class="hljs-type">char</span>* buf,
13535 <span class="hljs-type">size_t</span> bufsize,
13536 … <span class="hljs-type">size_t</span>* result)</span></code></pre>
13540 <li><code>[in] buf</code>: Buffer to write the UTF8-encoded string into. If <code>NULL</code> is pa…
13544 insufficient, the returned string is truncated and null-terminated.</li>
13548 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value…
13550 <p>This API returns the UTF8-encoded string corresponding the value passed in.</p>
13554 <span>N-API version: 1</span>
13556 …pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_…
13558 <span class="hljs-type">char16_t</span>* buf,
13559 <span class="hljs-type">size_t</span> bufsize,
13560 … <span class="hljs-type">size_t</span>* result)</span></code></pre>
13564 <li><code>[in] buf</code>: Buffer to write the UTF16-LE-encoded string into. If <code>NULL</code> is
13565 passed in, the length of the string in 2-byte code units and excluding the
13568 insufficient, the returned string is truncated and null-terminated.</li>
13569 <li><code>[out] result</code>: Number of 2-byte code units copied into the buffer, excluding
13572 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value…
13574 <p>This API returns the UTF16-encoded string corresponding the value passed in.</p>
13578 <span>N-API version: 1</span>
13580 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_uint32<…
13582 … <span class="hljs-type">uint32_t</span>* result)</span></code></pre>
13589 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code>
13597 <span>N-API version: 1</span>
13599 …="language-c">napi_status <span class="hljs-title function_">napi_get_boolean</span><span class="h…
13612 <span>N-API version: 1</span>
13614 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_global</span>…
13624 <span>N-API version: 1</span>
13626 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_null</span><s…
13636 <span>N-API version: 1</span>
13638 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_undefined</sp…
13646 <p>Node-API exposes a set of APIs to perform some abstract operations on JavaScript
13647 …tions are documented under <a href="https://tc39.github.io/ecma262/#sec-abstract-operations">Secti…
13659 <span>N-API version: 1</span>
13661 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_bool</s…
13671 <a href="https://tc39.github.io/ecma262/#sec-toboolean">Section 7.1.2</a> of the ECMAScript Languag…
13672 This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p>
13676 <span>N-API version: 1</span>
13678 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_number<…
13688 <a href="https://tc39.github.io/ecma262/#sec-tonumber">Section 7.1.3</a> of the ECMAScript Language…
13689 This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p>
13693 <span>N-API version: 1</span>
13695 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_object<…
13705 <a href="https://tc39.github.io/ecma262/#sec-toobject">Section 7.1.13</a> of the ECMAScript Languag…
13706 This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p>
13710 <span>N-API version: 1</span>
13712 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_string<…
13722 <a href="https://tc39.github.io/ecma262/#sec-toobject">Section 7.1.13</a> of the ECMAScript Languag…
13723 This API can be re-entrant if getters are defined on the passed-in <code>Object</code>.</p>
13727 <span>N-API version: 1</span>
13729 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_typeof</span><spa…
13741 the object as defined in <a href="https://tc39.github.io/ecma262/#sec-typeof-operator">Section 12.5…
13752 <span>N-API version: 1</span>
13754 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_instanceof</span>…
13757 <span class="hljs-type">bool</span>* result)</span></code></pre>
13768 defined in <a href="https://tc39.github.io/ecma262/#sec-instanceofoperator">Section 12.10.4</a> of …
13772 <span>N-API version: 1</span>
13774 …ge-c">napi_status <span class="hljs-title function_">napi_is_array</span><span class="hljs-params"…
13782 as defined in <a href="https://tc39.github.io/ecma262/#sec-isarray">Section 7.2.2</a> of the ECMASc…
13786 <span>N-API version: 1</span>
13788-c">napi_status <span class="hljs-title function_">napi_is_arraybuffer</span><span class="hljs-par…
13799 <span>N-API version: 1</span>
13801 …ge-c">napi_status <span class="hljs-title function_">napi_is_buffer</span><span class="hljs-params…
13813 <span>N-API version: 5</span>
13815 …age-c">napi_status <span class="hljs-title function_">napi_is_date</span><span class="hljs-params"…
13827 <span>N-API version: 1</span>
13829 …ge-c">napi_status <span class="hljs-title function_">napi_is_error</span><span class="hljs-params"…
13840 <span>N-API version: 1</span>
13842-c">napi_status <span class="hljs-title function_">napi_is_typedarray</span><span class="hljs-para…
13853 <span>N-API version: 1</span>
13855 …e-c">napi_status <span class="hljs-title function_">napi_is_dataview</span><span class="hljs-param…
13866 <span>N-API version: 1</span>
13868 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_strict_equals</sp…
13871 <span class="hljs-type">bool</span>* result)</span></code></pre>
13880 defined in <a href="https://tc39.github.io/ecma262/#sec-strict-equality-comparison">Section 7.2.14<…
13884 <span>N-API version: 7</span>
13886 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_detach_arraybuffe…
13892 <p>Returns <code>napi_ok</code> if the API succeeded. If a non-detachable <code>ArrayBuffer</code> …
13894 <p>Generally, an <code>ArrayBuffer</code> is non-detachable if it has been detached before.
13899 defined in <a href="https://tc39.es/ecma262/#sec-detacharraybuffer">Section 24.1.1.3</a> of the ECM…
13903 <span>N-API version: 7</span>
13905 …re><code class="language-c">napi_status <span class="hljs-title function_">napi_is_detached_arrayb…
13907 … <span class="hljs-type">bool</span>* result)</span></code></pre>
13916 operation as defined in <a href="https://tc39.es/ecma262/#sec-isdetachedbuffer">Section 24.1.1.2</a…
13919 <p>Node-API exposes a set of APIs to get and set properties on JavaScript
13920 …types are documented under <a href="https://tc39.github.io/ecma262/#sec-abstract-operations">Secti…
13923 Fundamentally, all property keys in Node-API can be represented in one of the
13926 <li>Named: a simple UTF8-encoded string</li>
13927 <li>Integer-Indexed: an index value represented by <code>uint32_t</code></li>
13928 <li>JavaScript value: these are represented in Node-API by <code>napi_value</code>. This can
13931 <p>Node-API values are represented by the type <code>napi_value</code>.
13932 Any Node-API call that requires a JavaScript value takes in a <code>napi_value</code>.
13939 <pre><code class="language-js"><span class="hljs-keyword">const</span> obj = {};
13940 obj.<span class="hljs-property">myProp</span> = <span class="hljs-number">123</span>;</code></pre>
13941 <p>The equivalent can be done using Node-API values with the following snippet:</p>
13942 <pre><code class="language-c">napi_status status = napi_generic_failure;
13944 <span class="hljs-comment">// const obj = {}</span>
13947 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13949 <span class="hljs-comment">// Create a napi_value for 123</span>
13950 status = napi_create_int32(env, <span class="hljs-number">123</span>, &#x26;value);
13951 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13953 <span class="hljs-comment">// obj.myProp = 123</span>
13954 status = napi_set_named_property(env, obj, <span class="hljs-string">"myProp"</span>, value);
13955 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13958 <pre><code class="language-js"><span class="hljs-keyword">const</span> arr = [];
13959 arr[<span class="hljs-number">123</span>] = <span class="hljs-string">'hello'</span>;</code></pre>
13960 <p>The equivalent can be done using Node-API values with the following snippet:</p>
13961 <pre><code class="language-c">napi_status status = napi_generic_failure;
13963 <span class="hljs-comment">// const arr = [];</span>
13966 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13968 <span class="hljs-comment">// Create a napi_value for 'hello'</span>
13969 status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &…
13970 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13972 <span class="hljs-comment">// arr[123] = 'hello';</span>
13973 status = napi_set_element(env, arr, <span class="hljs-number">123</span>, value);
13974 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13977 <pre><code class="language-js"><span class="hljs-keyword">const</span> arr = [];
13978 <span class="hljs-keyword">const</span> value = arr[<span class="hljs-number">123</span>];</code></…
13979 <p>The following is the approximate equivalent of the Node-API counterpart:</p>
13980 <pre><code class="language-c">napi_status status = napi_generic_failure;
13982 <span class="hljs-comment">// const arr = []</span>
13985 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13987 <span class="hljs-comment">// const value = arr[123]</span>
13988 status = napi_get_element(env, arr, <span class="hljs-number">123</span>, &#x26;value);
13989 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
13992 <pre><code class="language-js"><span class="hljs-keyword">const</span> obj = {};
13993 <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">defineProperties</…
13994-string">'foo'</span>: { <span class="hljs-attr">value</span>: <span class="hljs-number">123</span…
13995-string">'bar'</span>: { <span class="hljs-attr">value</span>: <span class="hljs-number">456</span…
13997 <p>The following is the approximate equivalent of the Node-API counterpart:</p>
13998 <pre><code class="language-c">napi_status status = napi_status_generic_failure;
14000 <span class="hljs-comment">// const obj = {};</span>
14003 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
14005 <span class="hljs-comment">// Create napi_values for 123 and 456</span>
14007 status = napi_create_int32(env, <span class="hljs-number">123</span>, &#x26;fooValue);
14008 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
14009 status = napi_create_int32(env, <span class="hljs-number">456</span>, &#x26;barValue);
14010 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
14012 <span class="hljs-comment">// Set the properties</span>
14014-string">"foo"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</sp…
14015-string">"bar"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</sp…
14019 …<span class="hljs-keyword">sizeof</span>(descriptors) / <span class="hljs-keyword">sizeof</span>(d…
14021 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> s…
14033 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
14034 napi_default = <span class="hljs-number">0</span>,
14035 …napi_writable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">0</span>,
14036 …napi_enumerable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">1</spa…
14037 …napi_configurable = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">2</s…
14039 <span class="hljs-comment">// Used with napi_define_class to distinguish static properties</span>
14040 <span class="hljs-comment">// from instance properties. Ignored by napi_define_properties.</span>
14041 napi_static = <span class="hljs-number">1</span> &#x3C;&#x3C; <span class="hljs-number">10</span>,
14043 <span class="hljs-comment">// Default for class methods.</span>
14046 <span class="hljs-comment">// Default for object properties, like in JS obj[prop].</span>
14053 attributes listed in <a href="https://tc39.github.io/ecma262/#table-2">Section 6.1.7.1</a>
14062 <a href="https://tc39.github.io/ecma262/#table-2">Section 6.1.7.1</a> of the <a href="https://tc39.…
14072 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
14073 <span class="hljs-comment">// One of utf8name or name should be NULL.</span>
14074 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8name;
14083 <span class="hljs-type">void</span>* data;
14099 performed using a Node-API call). <a href="#n_api_napi_callback"><code>napi_callback</code></a> pro…
14104 performed using a Node-API call). <a href="#n_api_napi_callback"><code>napi_callback</code></a> pro…
14118 <span>N-API version: 1</span>
14120 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_property_name…
14124 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14138 <span>N-API version: 6</span>
14140 <pre><code class="language-c">napi_get_all_property_names(napi_env env,
14147 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14163 <span>N-API version: 1</span>
14165 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_property</spa…
14170 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14180 <span>N-API version: 1</span>
14182 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_property</spa…
14187 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14197 <span>N-API version: 1</span>
14199 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_property</spa…
14202 <span class="hljs-type">bool</span>* result)</span>;</code></pre>
14204 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14214 <span>N-API version: 1</span>
14216 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_property</…
14219 <span class="hljs-type">bool</span>* result)</span>;</code></pre>
14221 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14232 <span>N-API version: 1</span>
14234 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_own_property<…
14237 <span class="hljs-type">bool</span>* result)</span>;</code></pre>
14239 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14246 be a <code>string</code> or a <code>symbol</code>, or an error will be thrown. Node-API will not
14251 <span>N-API version: 1</span>
14253 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_named_propert…
14255 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8Name,
14258 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14269 <span>N-API version: 1</span>
14271 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_named_propert…
14273 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8Name,
14276 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14287 <span>N-API version: 1</span>
14289 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_named_propert…
14291 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8Name,
14292 … <span class="hljs-type">bool</span>* result)</span>;</code></pre>
14294 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14305 <span>N-API version: 1</span>
14307 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_element</span…
14309 <span class="hljs-type">uint32_t</span> index,
14312 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14322 <span>N-API version: 1</span>
14324 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_element</span…
14326 <span class="hljs-type">uint32_t</span> index,
14329 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14339 <span>N-API version: 1</span>
14341 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_element</span…
14343 <span class="hljs-type">uint32_t</span> index,
14344 <span class="hljs-type">bool</span>* result)</span>;</code></pre>
14346 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14357 <span>N-API version: 1</span>
14359 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_element</s…
14361 <span class="hljs-type">uint32_t</span> index,
14362 <span class="hljs-type">bool</span>* result)</span>;</code></pre>
14364 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14375 <span>N-API version: 1</span>
14377 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_define_properties…
14379 <span class="hljs-type">size_t</span> property_count,
14380 … <span class="hljs-keyword">const</span> napi_property_descriptor* properties)</span>;</code></pre>
14382 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14392 …github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-des…
14397 <span>N-API version: 8</span>
14399 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_object_freeze</sp…
14402 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14411 in <a href="https://tc39.es/ecma262/#sec-object.freeze">Section 19.1.2.6</a> of the
14412 ECMA-262 specification.</p>
14416 <span>N-API version: 8</span>
14418 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_object_seal</span…
14421 <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li>
14426 added to it, as well as marking all existing properties as non-configurable.
14427 This is described in <a href="https://tc39.es/ecma262/#sec-object.seal">Section 19.1.2.20</a>
14428 of the ECMA-262 specification.</p>
14430 <p>Node-API provides a set of APIs that allow JavaScript code to
14431 call back into native code. Node-APIs that support calling back
14442 <p>Additionally, Node-API provides a set of functions which allow calling
14446 <p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> field of the
14448 whenever <code>object</code> is garbage-collected by passing both <code>object</code> and the data …
14453 <span>N-API version: 1</span>
14455 …e><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_call_fu…
14458 <span class="hljs-type">size_t</span> argc,
14459 <span class="hljs-keyword">const</span> napi_value* argv,
14472 add-on. This is the primary mechanism of calling back <em>from</em> the add-on's
14477 …ode class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title functio…
14478 <span class="hljs-keyword">return</span> num + <span class="hljs-number">2</span>;
14480 <p>Then, the above function can be invoked from a native add-on using the
14482 <pre><code class="language-c"><span class="hljs-comment">// Get the function named "AddTwo" on the …
14485 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14487 status = napi_get_named_property(env, global, <span class="hljs-string">"AddTwo"</span>, &#x26;add_…
14488 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14490 <span class="hljs-comment">// const arg = 1337</span>
14491 status = napi_create_int32(env, <span class="hljs-number">1337</span>, &#x26;arg);
14492 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14495 <span class="hljs-type">size_t</span> argc = <span class="hljs-number">1</span>;
14497 <span class="hljs-comment">// AddTwo(arg);</span>
14500 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14502 <span class="hljs-comment">// Convert the result back to a native type</span>
14503 <span class="hljs-type">int32_t</span> result;
14505 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;<…
14509 <span>N-API version: 1</span>
14511 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_function</…
14512 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8name,
14513 <span class="hljs-type">size_t</span> length,
14515 <span class="hljs-type">void</span>* data,
14522 it is null-terminated.</li>
14525 <li><code>[in] data</code>: User-provided data context. This will be passed back into the
14531 <p>This API allows an add-on author to create a function object in native code.
14532 This is the primary mechanism to allow calling <em>into</em> the add-on's native code
14538 add-on's module exports, set the newly created function on the exports
14540 <pre><code class="language-c">napi_value <span class="hljs-title function_">SayHello</span><span cl…
14541 <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello\n"</span>);
14542 <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
14545 napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, n…
14549 …e_function(env, <span class="hljs-literal">NULL</span>, <span class="hljs-number">0</span>, SayHel…
14550 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14552 status = napi_set_named_property(env, exports, <span class="hljs-string">"sayHello"</span>, fn);
14553 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14555 <span class="hljs-keyword">return</span> exports;
14559 <p>Given the above code, the add-on can be used from JavaScript as follows:</p>
14560 …de class="language-js"><span class="hljs-keyword">const</span> myaddon = <span class="hljs-built_i…
14561 myaddon.<span class="hljs-title function_">sayHello</span>();</code></pre>
14562 <p>The string passed to <code>require()</code> is the name of the target in <code>binding.gyp</code>
14564 <p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> parameter c…
14566 <code>result</code> parameter) and freed whenever the function is garbage-collected by
14568 …n</code>s are described in <a href="https://tc39.github.io/ecma262/#sec-function-objects">Section …
14573 <span>N-API version: 1</span>
14575 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_cb_info</span…
14577 <span class="hljs-type">size_t</span>* argc,
14580 <span class="hljs-type">void</span>** data)</span></code></pre>
14584 <li><code>[in-out] argc</code>: Specifies the length of the provided <code>argv</code> array and
14600 <span>N-API version: 1</span>
14602 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_new_target</s…
14608 <li><code>[out] result</code>: The <code>new.target</code> of the constructor call.</li>
14611 <p>This API returns the <code>new.target</code> of the constructor call. If the current
14616 <span>N-API version: 1</span>
14618 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_new_instance</spa…
14620 <span class="hljs-type">size_t</span> argc,
14636 …de class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function…
14637 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">param</span> = param;
14640 <span class="hljs-keyword">const</span> arg = <span class="hljs-string">'hello'</span>;
14641 <span class="hljs-keyword">const</span> value = <span class="hljs-keyword">new</span> <span class="…
14642 <p>The following can be approximated in Node-API using the following snippet:</p>
14643 <pre><code class="language-c"><span class="hljs-comment">// Get the constructor function MyObject</…
14646 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14648 status = napi_get_named_property(env, global, <span class="hljs-string">"MyObject"</span>, &#x26;co…
14649 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14651 <span class="hljs-comment">// const arg = "hello"</span>
14652 status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &…
14653 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;
14656 <span class="hljs-type">size_t</span> argc = <span class="hljs-number">1</span>;
14658 <span class="hljs-comment">// const value = new MyObject(arg)</span>
14662 <p>Node-API offers a way to "wrap" C++ classes and instances so that the class
14673 …ef="#n_api_napi_unwrap"><code>napi_unwrap</code></a> obtains the C++ instance that is the target of
14680 <pre><code class="language-c">napi_value MyClass_constructor = <span class="hljs-literal">NULL</spa…
14683 <span class="hljs-type">bool</span> is_instance = <span class="hljs-literal">false</span>;
14686 <span class="hljs-keyword">if</span> (is_instance) {
14687 <span class="hljs-comment">// napi_unwrap() ...</span>
14688 } <span class="hljs-keyword">else</span> {
14689 <span class="hljs-comment">// otherwise...</span>
14697 …de class="language-js"><span class="hljs-keyword">const</span> myAddon = <span class="hljs-built_i…
14699 <span class="hljs-comment">// `openDatabase()` returns a JavaScript object that wraps a native data…
14700 <span class="hljs-comment">// handle.</span>
14701 <span class="hljs-keyword">const</span> dbHandle = myAddon.<span class="hljs-title function_">openD…
14703 <span class="hljs-comment">// `query()` returns a JavaScript object that wraps a native query handl…
14704 …span class="hljs-keyword">const</span> queryHandle = myAddon.<span class="hljs-title function_">qu…
14706 <span class="hljs-comment">// There is an accidental error in the line below. The first parameter t…
14707 <span class="hljs-comment">// `myAddon.queryHasRecords()` should be the database handle (`dbHandle`…
14708 <span class="hljs-comment">// the query handle (`query`), so the correct condition for the while-lo…
14709 <span class="hljs-comment">// should be</span>
14710 <span class="hljs-comment">//</span>
14711 <span class="hljs-comment">// myAddon.queryHasRecords(dbHandle, queryHandle)</span>
14712 <span class="hljs-comment">//</span>
14713 <span class="hljs-keyword">while</span> (myAddon.<span class="hljs-title function_">queryHasRecords…
14714 <span class="hljs-comment">// retrieve records</span>
14737 <p>To this end, Node-API provides type-tagging capabilities.</p>
14738 <p>A type tag is a 128-bit integer unique to the addon. Node-API provides the
14745 previously "marked" with the type tag. This creates a type-checking capability
14746 of a higher fidelity than <code>napi_instanceof()</code> can provide, because such type-
14751 <pre><code class="language-c"><span class="hljs-comment">// This value is the type tag for a databa…
14752 <span class="hljs-comment">//</span>
14753 <span class="hljs-comment">// uuidgen | sed -r -e 's/-//g' -e 's/(.{16})(.*)/0x\1, 0x\2/'</span>
14754 <span class="hljs-comment">//</span>
14755 <span class="hljs-comment">// can be used to obtain the two values with which to initialize the str…
14756 <span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> napi_type_tag Data…
14757 …<span class="hljs-number">0x1edf75a38336451d</span>, <span class="hljs-number">0xa5ed9ce2e4c00c38<…
14760 <span class="hljs-comment">// This value is the type tag for a query handle.</span>
14761 <span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> napi_type_tag Quer…
14762 …<span class="hljs-number">0x9c73317f9fad44a3</span>, <span class="hljs-number">0x93c3920bf3b0ad6a<…
14765 <span class="hljs-keyword">static</span> napi_value
14766 <span class="hljs-title function_">openDatabase</span><span class="hljs-params">(napi_env env, napi…
14770 …<span class="hljs-comment">// Perform the underlying action which results in a database handle.</s…
14773 <span class="hljs-comment">// Create a new, empty JS object.</span>
14775 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14777 …<span class="hljs-comment">// Tag the object to indicate that it holds a pointer to a `DatabaseHan…
14779 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14781 …<span class="hljs-comment">// Store the pointer to the `DatabaseHandle` structure inside the JS ob…
14782 … result, dbHandle, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>,…
14783 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14785 <span class="hljs-keyword">return</span> result;
14788 <span class="hljs-comment">// Later when we receive a JavaScript object purporting to be a database…
14789 <span class="hljs-comment">// we can use `napi_check_object_type_tag()` to ensure that it is indeed…
14790 <span class="hljs-comment">// handle.</span>
14792 <span class="hljs-keyword">static</span> napi_value
14793 <span class="hljs-title function_">query</span><span class="hljs-params">(napi_env env, napi_callba…
14795 <span class="hljs-type">size_t</span> argc = <span class="hljs-number">2</span>;
14796 napi_value argv[<span class="hljs-number">2</span>];
14797 <span class="hljs-type">bool</span> is_db_handle;
14799 …t_cb_info(env, info, &#x26;argc, argv, <span class="hljs-literal">NULL</span>, <span class="hljs-l…
14800 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14802 …<span class="hljs-comment">// Check that the object passed as the first parameter has the previous…
14803 <span class="hljs-comment">// applied tag.</span>
14805 argv[<span class="hljs-number">0</span>],
14808 …<span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> …
14810 <span class="hljs-comment">// Throw a `TypeError` if it doesn't.</span>
14811 <span class="hljs-keyword">if</span> (!is_db_handle) {
14812 <span class="hljs-comment">// Throw a TypeError.</span>
14813 <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
14819 <span>N-API version: 1</span>
14821 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_define_class</spa…
14822 … <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* utf8name,
14823 <span class="hljs-type">size_t</span> length,
14825 <span class="hljs-type">void</span>* data,
14826 <span class="hljs-type">size_t</span> property_count,
14827 … <span class="hljs-keyword">const</span> napi_property_descriptor* properties,
14835 if it is null-terminated.</li>
14860 C++ class, <em>non-static</em> data properties, accessors, and methods of the C++
14873 that case, to prevent the function value from being garbage-collected, a
14876 <p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> parameter o…
14879 parameter) and freed whenever the class is garbage-collected by passing both
14884 <span>N-API version: 1</span>
14886 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_wrap</span><span …
14888 <span class="hljs-type">void</span>* native_object,
14890 <span class="hljs-type">void</span>* finalize_hint,
14899 native instance when the JavaScript object is ready for garbage-collection.
14911 <code>napi_wrap()</code> to wrap the newly constructed instance in the already-created
14932 <span>N-API version: 1</span>
14934 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_unwrap</span><spa…
14936 <span class="hljs-type">void</span>** result)</span>;</code></pre>
14948 object; the wrapped C++ instance that is the target of the call can be obtained
14953 <span>N-API version: 1</span>
14955 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_remove_wrap</span…
14957 <span class="hljs-type">void</span>** result)</span>;</code></pre>
14967 JavaScript object becomes garbage-collected.</p>
14971 <span>N-API version: 8</span>
14973 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_type_tag_object</…
14975 … <span class="hljs-keyword">const</span> napi_type_tag* type_tag)</span>;</code></pre>
14991 <span>N-API version: 8</span>
14993 …pre><code class="language-c">napi_status <span class="hljs-title function_">napi_check_object_type…
14995 … <span class="hljs-keyword">const</span> napi_type_tag* type_tag,
14996 … <span class="hljs-type">bool</span>* result)</span>;</code></pre>
15012 <span>N-API version: 5</span>
15014 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_add_finalizer</sp…
15016 <span class="hljs-type">void</span>* native_object,
15018 <span class="hljs-type">void</span>* finalize_hint,
15027 native data when the JavaScript object is ready for garbage-collection.
15054 <p>Node-API provides an ABI-stable interface for these
15056 <p>Node-API defines the <code>napi_async_work</code> structure which is used to manage
15062 <p>The <code>execute</code> function should avoid making any Node-API calls
15064 JavaScript objects. Most often, any code that needs to make Node-API
15069-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span…
15070 <span class="hljs-type">void</span>* data)</span>;
15071-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">…
15073 … <span class="hljs-type">void</span>* data)</span>;</code></pre>
15075 addon-provided <code>void*</code> data that was passed into the
15079 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_queue_async_work<…
15098 <span>N-API version: 1</span>
15100 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_async_work…
15105 <span class="hljs-type">void</span>* data,
15120 <li><code>[in] data</code>: User-provided data context. This will be passed back into the
15129 <p><code>async_resource_name</code> should be a null-terminated, UTF-8-encoded string.</p>
15137 <span>N-API version: 1</span>
15139 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_async_work…
15151 <span>N-API version: 1</span>
15153 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_queue_async_work<…
15166 <span>N-API version: 1</span>
15168 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_cancel_async_work…
15190 <span>N-API version: 1</span>
15192 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_async_init</span>…
15224 <span>N-API version: 1</span>
15226 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_async_destroy</sp…
15245 <span>N-API version: 1</span>
15247 …e><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_make_ca…
15251 <span class="hljs-type">size_t</span> argc,
15252 <span class="hljs-keyword">const</span> napi_value* argv,
15272 add-on. This API is similar to <code>napi_call_function</code>. However, it is used to call
15287 <span>N-API version: 3</span>
15289 …code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_open_callb…
15305 in place when making certain Node-API calls. If there is no other script on
15312 <span>N-API version: 3</span>
15314 …code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_close_call…
15325 <span>N-API version: 1</span>
15327 <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><s…
15328 <span class="hljs-type">uint32_t</span> major;
15329 <span class="hljs-type">uint32_t</span> minor;
15330 <span class="hljs-type">uint32_t</span> patch;
15331 <span class="hljs-keyword">const</span> <span class="hljs-type">char</span>* release;
15334 napi_status <span class="hljs-title function_">napi_get_node_version</span><span class="hljs-params…
15335 … <span class="hljs-keyword">const</span> napi_node_version** version)</span>;</code></pre>
15348 <span>N-API version: 1</span>
15350 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_version</span…
15351 <span class="hljs-type">uint32_t</span>* result)</span>;</code></pre>
15354 <li><code>[out] result</code>: The highest version of Node-API supported.</li>
15357 <p>This API returns the highest Node-API version supported by the
15358 Node.js runtime. Node-API is planned to be additive such that
15375 <span>N-API version: 1</span>
15377 …ode class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_adjust_exte…
15378 … <span class="hljs-type">int64_t</span> change_in_bytes,
15379 … <span class="hljs-type">int64_t</span>* result)</span>;</code></pre>
15393 <p>Node-API provides facilities for creating <code>Promise</code> objects as described in
15394 <a href="https://tc39.github.io/ecma262/#sec-promise-objects">Section 25.4</a> of the ECMA specific…
15403 <pre><code class="language-c">napi_deferred deferred;
15407 <span class="hljs-comment">// Create the promise.</span>
15409 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <…
15411 <span class="hljs-comment">// Pass the deferred to a function that performs an asynchronous action.…
15414 <span class="hljs-comment">// Return the promise to JS</span>
15415 <span class="hljs-keyword">return</span> promise;</code></pre>
15419 <pre><code class="language-c">napi_deferred deferred;
15423 <span class="hljs-comment">// Create a value with which to conclude the deferred.</span>
15425 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <…
15427 <span class="hljs-comment">// Resolve or reject the promise associated with the deferred depending …
15428 <span class="hljs-comment">// whether the asynchronous action succeeded.</span>
15429 <span class="hljs-keyword">if</span> (asynchronous_action_succeeded) {
15431 } <span class="hljs-keyword">else</span> {
15434 <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <…
15436 <span class="hljs-comment">// At this point the deferred has been freed, so we should assign NULL t…
15437 deferred = <span class="hljs-literal">NULL</span>;</code></pre>
15441 <span>N-API version: 1</span>
15443 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_promise</s…
15458 <span>N-API version: 1</span>
15460 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_resolve_deferred<…
15478 <span>N-API version: 1</span>
15480 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_reject_deferred</…
15498 <span>N-API version: 1</span>
15500 <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_promise</span>…
15502 <span class="hljs-type">bool</span>* is_promise)</span>;</code></pre>
15510 <p>Node-API provides an API for executing a string containing JavaScript using the
15515 <span>N-API version: 1</span>
15517 …pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_run_s…
15530 <a href="#modules_the_module_scope">module scope</a>, meaning that pseudo-globals such as <code>req…
15539 <p>Node-API provides a function for getting the current event loop associated with
15544 <span>N-API version: 2</span>
15546 …<code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_get_uv_ev…
15547 … <span class="hljs-keyword">struct</span> uv_loop_s** loop)</span>;</code></pre>
15552 </section><section><h3>Asynchronous thread-safe function calls<span><a class="mark" href="#n_api_as…
15554 thread. If an addon creates additional threads, then Node-API functions that
15560 JavaScript function on their behalf. The thread-safe function APIs provide an
15570 provided. This callback will be invoked on the main thread when the thread-safe
15574 no threads should be using the thread-safe function after the finalize callback
15579 <h4>Calling a thread-safe function<span><a class="mark" href="#n_api_calling_a_thread_safe_function…
15583 behaves non-blockingly, returning <code>napi_queue_full</code> if the queue was full,
15586 <code>napi_call_threadsafe_function()</code> never blocks if the thread-safe function was
15604 Node.js process exits while there is a thread-safe function still active.</p>
15606 Node-API runs <code>call_js_cb</code> in a context appropriate for callbacks.</p>
15607 <h4>Reference counting of thread-safe functions<span><a class="mark" href="#n_api_reference_countin…
15611 indicate that a new thread will start making use of the thread-safe function.
15613 existing thread will stop making use of the thread-safe function.</p>
15622 reason, do not use a thread-safe function
15628 number of aquisitions of the thread-safe functions, instead of calling
15635 <p>The thread-safe function can be "aborted" by giving a value of <code>napi_tsfn_abort</code>
15637 associated with the thread-safe function except
15641 possible to make asynchronous calls to the thread-safe function. This can be
15644 the thread-safe function anymore because it is no longer guaranteed to
15647 <p>Similarly to libuv handles, thread-safe functions can be "referenced" and
15648 "unreferenced". A "referenced" thread-safe function will cause the event loop on
15649 the thread on which it is created to remain alive until the thread-safe function
15650 is destroyed. In contrast, an "unreferenced" thread-safe function will not
15653 <p>Neither does <code>napi_unref_threadsafe_function</code> mark the thread-safe functions as
15667 <span>N-API version: 4</span>
15669 <pre><code class="language-c">NAPI_EXTERN napi_status
15670 <span class="hljs-title function_">napi_create_threadsafe_function</span><span class="hljs-params">…
15674 <span class="hljs-type">size_t</span> max_queue_size,
15675 <span class="hljs-type">size_t</span> initial_thread_count,
15676 <span class="hljs-type">void</span>* thread_finalize_data,
15678 <span class="hljs-type">void</span>* context,
15704 <li><code>[out] result</code>: The asynchronous thread-safe JavaScript function.</li>
15709 <span>N-API version: 4</span>
15711 <pre><code class="language-c">NAPI_EXTERN napi_status
15712 <span class="hljs-title function_">napi_get_threadsafe_function_context</span><span class="hljs-par…
15713 … <span class="hljs-type">void</span>** result)</span>;</code></pre>
15715 <li><code>[in] func</code>: The thread-safe function for which to retrieve the context.</li>
15732 <span>N-API version: 4</span>
15734 <pre><code class="language-c">NAPI_EXTERN napi_status
15735 <span class="hljs-title function_">napi_call_threadsafe_function</span><span class="hljs-params">(n…
15736 <span class="hljs-type">void</span>* data,
15739 <li><code>[in] func</code>: The asynchronous thread-safe JavaScript function to invoke.</li>
15741 provided during the creation of the thread-safe JavaScript function.</li>
15757 <span>N-API version: 4</span>
15759 <pre><code class="language-c">NAPI_EXTERN napi_status
15760 <span class="hljs-title function_">napi_acquire_threadsafe_function</span><span class="hljs-params"…
15762 <li><code>[in] func</code>: The asynchronous thread-safe JavaScript function to start making
15765 <p>A thread should call this API before passing <code>func</code> to any other thread-safe
15773 <span>N-API version: 4</span>
15775 <pre><code class="language-c">NAPI_EXTERN napi_status
15776 <span class="hljs-title function_">napi_release_threadsafe_function</span><span class="hljs-params"…
15779 <li><code>[in] func</code>: The asynchronous thread-safe JavaScript function whose reference
15782 that the current thread will make no further calls to the thread-safe
15784 thread, no other thread should make any further calls to the thread-safe
15790 to any thread-safe APIs after having called this API has undefined results, as
15796 <span>N-API version: 4</span>
15798 <pre><code class="language-c">NAPI_EXTERN napi_status
15799 <span class="hljs-title function_">napi_ref_threadsafe_function</span><span class="hljs-params">(na…
15802 <li><code>[in] func</code>: The thread-safe function to reference.</li>
15807 <p>Neither does <code>napi_unref_threadsafe_function</code> mark the thread-safe functions as
15815 <span>N-API version: 4</span>
15817 <pre><code class="language-c">NAPI_EXTERN napi_status
15818 <span class="hljs-title function_">napi_unref_threadsafe_function</span><span class="hljs-params">(…
15821 <li><code>[in] func</code>: The thread-safe function to unreference.</li>
15832 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
15833 <pre><code class="language-c">NAPI_EXTERN napi_status
15834-title function_">node_api_get_module_file_name</span><span class="hljs-params">(napi_env env, <sp…
15839 location from which the add-on was loaded. For a file on the local
15840 file system it will start with <code>file://</code>. The string is null-terminated and
15843 <p><code>result</code> may be an empty string if the add-on loading process fails to establish
15844 the add-on's file name during loading.</p></section>
15855 <a href="deprecations.html">deprecation policy</a> and may occur on each semver-major release witho…
15860 <code>node -e &#x3C;code></code>, i.e. that will take a piece of JavaScript and run it in
15861 a Node.js-specific environment.</p>
15863 <h4>Setting up per-process state<span><a class="mark" href="#embedding_setting_up_per_process_state…
15864 <p>Node.js requires some per-process state management in order to run:</p>
15867 <li>V8 per-process requirements, such as a <code>v8::Platform</code> instance.</li>
15871-cpp"><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">main…
15872 argv = <span class="hljs-built_in">uv_setup_args</span>(argc, argv);
15873 …<span class="hljs-function">std::vector&#x3C;std::string> <span class="hljs-title">args</span><spa…
15876 …<span class="hljs-comment">// Parse Node.js CLI options, and print any errors that have occurred w…
15877 <span class="hljs-comment">// trying to parse them.</span>
15878 …<span class="hljs-type">int</span> exit_code = node::<span class="hljs-built_in">InitializeNodeWit…
15879 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> std::string&#x26; e…
15880-built_in">fprintf</span>(stderr, <span class="hljs-string">"%s: %s\n"</span>, args[<span class="h…
15881 <span class="hljs-keyword">if</span> (exit_code != <span class="hljs-number">0</span>) {
15882 <span class="hljs-keyword">return</span> exit_code;
15885 …<span class="hljs-comment">// Create a v8::Platform instance. `MultiIsolatePlatform::Create()` is …
15886 …<span class="hljs-comment">// to create a v8::Platform instance that Node.js can use when creating…
15887 …<span class="hljs-comment">// Worker threads. When no `MultiIsolatePlatform` instance is present,<…
15888 <span class="hljs-comment">// Worker threads are disabled.</span>
15890 …MultiIsolatePlatform::<span class="hljs-built_in">Create</span>(<span class="hljs-number">4</span>…
15891 …V8::<span class="hljs-built_in">InitializePlatform</span>(platform.<span class="hljs-built_in">get…
15892 V8::<span class="hljs-built_in">Initialize</span>();
15894 <span class="hljs-comment">// See below for the contents of this function.</span>
15895 …<span class="hljs-type">int</span> ret = <span class="hljs-built_in">RunNodeInstance</span>(platfo…
15897 V8::<span class="hljs-built_in">Dispose</span>();
15898 V8::<span class="hljs-built_in">ShutdownPlatform</span>();
15899 <span class="hljs-keyword">return</span> ret;
15901 <h4>Per-instance state<span><a class="mark" href="#embedding_per_instance_state" id="embedding_per_…
15923 sets it up with some Node.js-specific hooks (e.g. the Node.js error handler),
15925 …"language-cpp"><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-ti…
15926 … <span class="hljs-keyword">const</span> std::vector&#x3C;std::string>&#x26; args,
15927 …<span class="hljs-keyword">const</span> std::vector&#x3C;std::string>&#x26; exec_args)</span> </sp…
15928 <span class="hljs-type">int</span> exit_code = <span class="hljs-number">0</span>;
15929 <span class="hljs-comment">// Set up a libuv event loop.</span>
15930 <span class="hljs-type">uv_loop_t</span> loop;
15931 …<span class="hljs-type">int</span> ret = <span class="hljs-built_in">uv_loop_init</span>(&#x26;loo…
15932 <span class="hljs-keyword">if</span> (ret != <span class="hljs-number">0</span>) {
15933 …<span class="hljs-built_in">fprintf</span>(stderr, <span class="hljs-string">"%s: Failed to initia…
15934 args[<span class="hljs-number">0</span>].<span class="hljs-built_in">c_str</span>(),
15935 <span class="hljs-built_in">uv_err_name</span>(ret));
15936 <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
15940 ArrayBufferAllocator::<span class="hljs-built_in">Create</span>();
15942 Isolate* isolate = <span class="hljs-built_in">NewIsolate</span>(allocator, &#x26;loop, platform);
15943 <span class="hljs-keyword">if</span> (isolate == <span class="hljs-literal">nullptr</span>) {
15944-built_in">fprintf</span>(stderr, <span class="hljs-string">"%s: Failed to initialize V8 Isolate\n…
15945 <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
15949 …<span class="hljs-function">Locker <span class="hljs-title">locker</span><span class="hljs-params"…
15950 …<span class="hljs-function">Isolate::Scope <span class="hljs-title">isolate_scope</span><span clas…
15952 …<span class="hljs-comment">// Create a node::IsolateData instance that will later be released usin…
15953 <span class="hljs-comment">// node::FreeIsolateData().</span>
15954-function">std::unique_ptr&#x3C;IsolateData, <span class="hljs-title">decltype</span><span class="…
15958 <span class="hljs-comment">// Set up a new v8::Context.</span>
15959 …<span class="hljs-function">HandleScope <span class="hljs-title">handle_scope</span><span class="h…
15960 Local&#x3C;Context> context = node::<span class="hljs-built_in">NewContext</span>(isolate);
15961 <span class="hljs-keyword">if</span> (context.<span class="hljs-built_in">IsEmpty</span>()) {
15962-built_in">fprintf</span>(stderr, <span class="hljs-string">"%s: Failed to initialize V8 Context\n…
15963 <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
15966 …<span class="hljs-comment">// The v8::Context needs to be entered when node::CreateEnvironment() a…
15967 <span class="hljs-comment">// node::LoadEnvironment() are being called.</span>
15968 …<span class="hljs-function">Context::Scope <span class="hljs-title">context_scope</span><span clas…
15970 …<span class="hljs-comment">// Create a node::Environment instance that will later be released usin…
15971 <span class="hljs-comment">// node::FreeEnvironment().</span>
15972-function">std::unique_ptr&#x3C;Environment, <span class="hljs-title">decltype</span><span class="…
15976 …<span class="hljs-comment">// Set up the Node.js instance for execution, and run code inside of it…
15977 …<span class="hljs-comment">// There is also a variant that takes a callback and provides it with</…
15978 …<span class="hljs-comment">// the `require` and `process` objects, so that it can manually compile…
15979 <span class="hljs-comment">// and run scripts as needed.</span>
15980 …<span class="hljs-comment">// The `require` function inside this script does *not* access the file…
15981 <span class="hljs-comment">// system, and can only load built-in Node.js modules.</span>
15982 …<span class="hljs-comment">// `module.createRequire()` is being used to create one that is able to…
15983 …<span class="hljs-comment">// load files from the disk, and uses the standard CommonJS file loader…
15984 <span class="hljs-comment">// instead of the internal-only `require` function.</span>
15985 MaybeLocal&#x3C;Value> loadenv_ret = node::<span class="hljs-built_in">LoadEnvironment</span>(
15986 env.<span class="hljs-built_in">get</span>(),
15987 <span class="hljs-string">"const publicRequire ="</span>
15988 <span class="hljs-string">" require('module').createRequire(process.cwd() + '/');"</span>
15989 <span class="hljs-string">"globalThis.require = publicRequire;"</span>
15990 <span class="hljs-string">"require('vm').runInThisContext(process.argv[1]);"</span>);
15992 …<span class="hljs-keyword">if</span> (loadenv_ret.<span class="hljs-built_in">IsEmpty</span>()) <…
15993 <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
15996 … <span class="hljs-comment">// SealHandleScope protects against handle leaks from callbacks.</span>
15997 …<span class="hljs-function">SealHandleScope <span class="hljs-title">seal</span><span class="hljs-
15998 <span class="hljs-type">bool</span> more;
15999 <span class="hljs-keyword">do</span> {
16000 <span class="hljs-built_in">uv_run</span>(&#x26;loop, UV_RUN_DEFAULT);
16002 …<span class="hljs-comment">// V8 tasks on background threads may end up scheduling new tasks in th…
16003 …<span class="hljs-comment">// foreground, which in turn can keep the event loop going. For example…
16004 <span class="hljs-comment">// WebAssembly.compile() may do so.</span>
16005 platform-><span class="hljs-built_in">DrainTasks</span>(isolate);
16007 <span class="hljs-comment">// If there are new tasks, continue.</span>
16008 more = <span class="hljs-built_in">uv_loop_alive</span>(&#x26;loop);
16009 <span class="hljs-keyword">if</span> (more) <span class="hljs-keyword">continue</span>;
16011 …<span class="hljs-comment">// node::EmitProcessBeforeExit() is used to emit the 'beforeExit' event…
16012 <span class="hljs-comment">// on the `process` object.</span>
16013 …js-keyword">if</span> (node::<span class="hljs-built_in">EmitProcessBeforeExit</span>(env.<span cl…
16014 <span class="hljs-keyword">break</span>;
16016 …<span class="hljs-comment">// 'beforeExit' can also schedule new work that keeps the event loop</s…
16017 <span class="hljs-comment">// running.</span>
16018 more = <span class="hljs-built_in">uv_loop_alive</span>(&#x26;loop);
16019 } <span class="hljs-keyword">while</span> (more == <span class="hljs-literal">true</span>);
16022 <span class="hljs-comment">// node::EmitProcessExit() returns the current exit code.</span>
16023 …"hljs-built_in">EmitProcessExit</span>(env.<span class="hljs-built_in">get</span>()).<span class="…
16025 …<span class="hljs-comment">// node::Stop() can be used to explicitly stop the event loop and keep<…
16026 …<span class="hljs-comment">// further JavaScript from running. It can be called from any thread,</…
16027 …<span class="hljs-comment">// and will act like worker.terminate() if called from another thread.<…
16028 node::<span class="hljs-built_in">Stop</span>(env.<span class="hljs-built_in">get</span>());
16031 …<span class="hljs-comment">// Unregister the Isolate with the platform and add a listener that is …
16032 …<span class="hljs-comment">// when the Platform is done cleaning up any state it had associated wi…
16033 <span class="hljs-comment">// the Isolate.</span>
16034 <span class="hljs-type">bool</span> platform_finished = <span class="hljs-literal">false</span>;
16035 …platform-><span class="hljs-built_in">AddIsolateFinishedCallback</span>(isolate, [](<span class="h…
16036 …*<span class="hljs-built_in">static_cast</span>&#x3C;<span class="hljs-type">bool</span>*>(data) =…
16038 platform-><span class="hljs-built_in">UnregisterIsolate</span>(isolate);
16039 isolate-><span class="hljs-built_in">Dispose</span>();
16041 …<span class="hljs-comment">// Wait until the platform has cleaned up all relevant resources.</span>
16042 <span class="hljs-keyword">while</span> (!platform_finished)
16043 <span class="hljs-built_in">uv_run</span>(&#x26;loop, UV_RUN_ONCE);
16044 …<span class="hljs-type">int</span> err = <span class="hljs-built_in">uv_loop_close</span>(&#x26;lo…
16045 <span class="hljs-built_in">assert</span>(err == <span class="hljs-number">0</span>);
16047 <span class="hljs-keyword">return</span> exit_code;
16052 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
16055 a manner that is similar, but not identical, to <a href="http://man7.org/linux/man-pages/man3/popen…
16057 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16058-keyword">const</span> ls = <span class="hljs-title function_">spawn</span>(<span class="hljs-stri…
16060-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16061 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
16064-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16065 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span …
16068 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span clas…
16069-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
16073 limited (and platform-specific) capacity. If the subprocess writes to
16081 <p>On Windows, environment variables are case-insensitive. Node.js
16083 case-insensitively matches. Only first (in lexicographic order) entry will be
16127 …ptions_callback"><code>child_process.execFile()</code></a> can vary based on platform. On Unix-type
16138 <pre><code class="language-js"><span class="hljs-comment">// On Windows Only...</span>
16139 <span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<spa…
16140-keyword">const</span> bat = <span class="hljs-title function_">spawn</span>(<span class="hljs-str…
16142-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16143 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16146-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16147 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
16150 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
16151 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hl…
16153 <pre><code class="language-js"><span class="hljs-comment">// OR...</span>
16154 <span class="hljs-keyword">const</span> { exec, spawn } = <span class="hljs-built_in">require</span…
16155 …span class="hljs-title function_">exec</span>(<span class="hljs-string">'my.bat'</span>, <span cla…
16156 <span class="hljs-keyword">if</span> (err) {
16157 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
16158 <span class="hljs-keyword">return</span>;
16160 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16163 <span class="hljs-comment">// Script with spaces in the filename:</span>
16164-keyword">const</span> bat = <span class="hljs-title function_">spawn</span>(<span class="hljs-str…
16165 <span class="hljs-comment">// or:</span>
16166 …lass="hljs-title function_">exec</span>(<span class="hljs-string">'"my script.cmd" a b'</span>, <s…
16167 <span class="hljs-comment">// ...</span>
16186 …oper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string>…
16187 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
16189 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16191 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16192 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16193 <li><code>shell</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
16198 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16199 <li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
16203 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16204-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16205-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16206 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
16210 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
16212 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
16213 <li><code>stdout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
16214 <li><code>stderr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
16222 <a href="https://en.wikipedia.org/wiki/List_of_command-line_interpreters">shell</a>)
16224 …de class="language-js"><span class="hljs-keyword">const</span> { exec } = <span class="hljs-built_…
16226 <span class="hljs-title function_">exec</span>(<span class="hljs-string">'"/path/to/test file/test.…
16227 <span class="hljs-comment">// Double quotes are used so that the space in the path is not interpret…
16228 <span class="hljs-comment">// a delimiter of multiple arguments.</span>
16230 <span class="hljs-title function_">exec</span>(<span class="hljs-string">'echo "The \\$HOME variabl…
16231 <span class="hljs-comment">// The $HOME variable is escaped in the first instance, but not in the s…
16242 the output as UTF-8 and pass strings to the callback. The <code>encoding</code> option
16246 …de class="language-js"><span class="hljs-keyword">const</span> { exec } = <span class="hljs-built_…
16247 …"hljs-title function_">exec</span>(<span class="hljs-string">'cat *.js missing_file | wc -l'</span…
16248 <span class="hljs-keyword">if</span> (error) {
16249 …="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span cl…
16250 <span class="hljs-keyword">return</span>;
16252 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
16253 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span …
16258 <p>Unlike the <a href="http://man7.org/linux/man-pages/man3/exec.3.html"><code>exec(3)</code></a> P…
16266 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
16267-keyword">const</span> exec = util.<span class="hljs-title function_">promisify</span>(<span class…
16269 …ss="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title …
16270 …js-keyword">const</span> { stdout, stderr } = <span class="hljs-keyword">await</span> <span class=…
16271 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16272 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
16274 <span class="hljs-title function_">lsExample</span>();</code></pre>
16278 …de class="language-js"><span class="hljs-keyword">const</span> { exec } = <span class="hljs-built_…
16279 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span cl…
16280 <span class="hljs-keyword">const</span> { signal } = controller;
16281-keyword">const</span> child = <span class="hljs-title function_">exec</span>(<span class="hljs-st…
16282 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16284 controller.<span class="hljs-title function_">abort</span>();</code></pre>
16302 <li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
16303 <li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
16304 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
16306 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16307 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16308 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16309 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16310 <li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
16314 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16315-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16316-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16317 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
16319 <li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
16321 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
16329 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
16331 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
16332 <li><code>stdout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
16333 <li><code>stderr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
16345 … class="language-js"><span class="hljs-keyword">const</span> { execFile } = <span class="hljs-buil…
16346-keyword">const</span> child = <span class="hljs-title function_">execFile</span>(<span class="hlj…
16347 <span class="hljs-keyword">if</span> (error) {
16348 <span class="hljs-keyword">throw</span> error;
16350 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16354 the output as UTF-8 and pass strings to the callback. The <code>encoding</code> option
16364 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
16365-keyword">const</span> execFile = util.<span class="hljs-title function_">promisify</span>(<span c…
16366 …ss="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title …
16367-keyword">const</span> { stdout } = <span class="hljs-keyword">await</span> <span class="hljs-titl…
16368 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16370 <span class="hljs-title function_">getVersion</span>();</code></pre>
16377 … class="language-js"><span class="hljs-keyword">const</span> { execFile } = <span class="hljs-buil…
16378 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span cl…
16379 <span class="hljs-keyword">const</span> { signal } = controller;
16380-keyword">const</span> child = <span class="hljs-title function_">execFile</span>(<span class="hlj…
16381 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16383 controller.<span class="hljs-title function_">abort</span>();</code></pre>
16409 <li><code>modulePath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
16410 <li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
16411 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
16413 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16414 <li><code>detached</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16417 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16418 <li><code>execPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16419 <li><code>execArgv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16421-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16422 <li><code>serialization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
16427 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16429 <li><code>silent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
16433 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> |…
16437-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16438 <li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
16440 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16450 built-in that allows messages to be passed back and forth between the parent and
16464 <p>Unlike the <a href="http://man7.org/linux/man-pages/man2/fork.2.html"><code>fork(2)</code></a> P…
16471 …ge-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">argv</span>[<span…
16472 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
16473-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
16474 }, <span class="hljs-number">1_000</span>);
16475 } <span class="hljs-keyword">else</span> {
16476 …<span class="hljs-keyword">const</span> { fork } = <span class="hljs-built_in">require</span>(<spa…
16477 …<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span c…
16478 <span class="hljs-keyword">const</span> { signal } = controller;
16479 …<span class="hljs-keyword">const</span> child = <span class="hljs-title function_">fork</span>(__f…
16480 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
16481 …<span class="hljs-comment">// This will be called with err being an AbortError if the controller a…
16483 …controller.<span class="hljs-title function_">abort</span>(); <span class="hljs-comment">// Stops …
16513 <p><code>command</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
16516 <p><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16519 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
16521 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16522 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16523 <li><code>argv0</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
16525 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a> |…
16527 <li><code>detached</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16530-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16531-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16532 <li><code>serialization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
16535 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
16539 <li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
16542 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
16546 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16548 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16557 <code>command</code>, with command-line arguments in <code>args</code>. If omitted, <code>args</cod…
16563 <pre><code class="language-js"><span class="hljs-keyword">const</span> defaults = {
16564 <span class="hljs-attr">cwd</span>: <span class="hljs-literal">undefined</span>,
16565 <span class="hljs-attr">env</span>: process.<span class="hljs-property">env</span>
16575 <p>Example of running <code>ls -lh /usr</code>, capturing <code>stdout</code>, <code>stderr</code>,…
16577 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16578-keyword">const</span> ls = <span class="hljs-title function_">spawn</span>(<span class="hljs-stri…
16580-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16581 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
16584-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16585 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span …
16588 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span clas…
16589-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
16592 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16593 …ass="hljs-keyword">const</span> ps = <span class="hljs-title function_">spawn</span>(<span class="…
16594 …s="hljs-keyword">const</span> grep = <span class="hljs-title function_">spawn</span>(<span class="…
16596-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16597 …grep.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">write</span>(data…
16600-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16601 …="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span cl…
16604 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span clas…
16605 <span class="hljs-keyword">if</span> (code !== <span class="hljs-number">0</span>) {
16606-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
16608 grep.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">end</span>();
16611-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16612 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
16615-property">stderr</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
16616 …"hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span cla…
16619 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span clas…
16620 <span class="hljs-keyword">if</span> (code !== <span class="hljs-number">0</span>) {
16621-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
16625 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16626 <span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span…
16628 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
16629 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
16640 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16641 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span cl…
16642 <span class="hljs-keyword">const</span> { signal } = controller;
16643 …s="hljs-keyword">const</span> grep = <span class="hljs-title function_">spawn</span>(<span class="…
16644 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
16645 …<span class="hljs-comment">// This will be called with err being an AbortError if the controller a…
16647 controller.<span class="hljs-title function_">abort</span>(); <span class="hljs-comment">// Stops t…
16656 <p>On non-Windows platforms, if <code>options.detached</code> is set to <code>true</code>, the child
16659 they are detached or not. See <a href="http://man7.org/linux/man-pages/man2/setsid.2.html"><code>se…
16666 <p>When using the <code>detached</code> option to start a long-running process, the process
16671 <p>Example of a long-running process, by detaching and also ignoring its parent
16673 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16675-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(process.<span …
16676 <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>,
16677 <span class="hljs-attr">stdio</span>: <span class="hljs-string">'ignore'</span>
16680 subprocess.<span class="hljs-title function_">unref</span>();</code></pre>
16682 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
16683 <span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<spa…
16684 …ljs-keyword">const</span> out = fs.<span class="hljs-title function_">openSync</span>(<span class=…
16685 …ljs-keyword">const</span> err = fs.<span class="hljs-title function_">openSync</span>(<span class=…
16687 <span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span…
16688 <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>,
16689 <span class="hljs-attr">stdio</span>: [ <span class="hljs-string">'ignore'</span>, out, err ]
16692 subprocess.<span class="hljs-title function_">unref</span>();</code></pre>
16736 <a href="https://docs.microsoft.com/en-us/windows/win32/fileio/synchronous-and-asynchronous-i-o">do…
16737 for more details. This is exactly the same as <code>'pipe'</code> on non-Windows
16784 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
16786 <span class="hljs-comment">// Child will use parent's stdios.</span>
16787 …lass="hljs-title function_">spawn</span>(<span class="hljs-string">'prg'</span>, [], { <span class…
16789 <span class="hljs-comment">// Spawn child sharing only stderr.</span>
16790-title function_">spawn</span>(<span class="hljs-string">'prg'</span>, [], { <span class="hljs-att…
16792 <span class="hljs-comment">// Open an extra fd=4, to interact with programs presenting a</span>
16793 <span class="hljs-comment">// startd-style interface.</span>
16794-title function_">spawn</span>(<span class="hljs-string">'prg'</span>, [], { <span class="hljs-att…
16801 <p>On Unix-like operating systems, the <a href="#child_process_child_process_spawn_command_args_opt…
16812 <p>Blocking calls like these are mostly useful for simplifying general-purpose
16836 <li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
16837 <li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
16838 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
16840 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16841-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
16844 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16847 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16848-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16849-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16850 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16852 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16854 <li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
16857 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16859 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
16861 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
16867 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
16877 <p>If the process times out or has a non-zero exit code, this method will throw an
16902 <li><code>command</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16903 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
16905 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16906-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
16909 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16912 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16913 <li><code>shell</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
16916-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16917-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16918 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16920 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16922 <li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
16926 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16928 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
16932 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
16941 <p>If the process times out or has a non-zero exit code, this method will throw.
16969 <li><code>command</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16970 <li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
16971 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
16973 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
16974-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
16977 <li><code>argv0</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
16979 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16980 …developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3…
16981-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
16982-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
16983 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
16985 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
16987 <li><code>maxBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
16991 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
16993 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
16997 <li><code>windowsVerbatimArguments</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
17000 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
17004 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
17006 <li><code>pid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
17007 <li><code>output</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
17008 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
17009 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
17010 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
17012 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
17014 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
17045 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
17046 <li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
17053 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
17054-keyword">const</span> ls = <span class="hljs-title function_">spawn</span>(<span class="hljs-stri…
17056-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
17057 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
17060 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span clas…
17061-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
17064 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
17065-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
17078 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
17095 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
17096 <li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
17101 the signal, otherwise <code>null</code>. One of the two will always be non-<code>null</code>.</p>
17107 re-raise the handled signal.</p>
17108 <p>See <a href="http://man7.org/linux/man-pages/man2/waitpid.2.html"><code>waitpid(2)</code></a>.</…
17114 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
17136 the spawned process. For example, if <code>bash some-command</code> spawns successfully,
17137 the <code>'spawn'</code> event will fire, though <code>bash</code> may fail to spawn <code>some-com…
17152 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
17173 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
17195 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
17204 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
17205 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
17209 <a href="http://man7.org/linux/man-pages/man7/signal.7.html"><code>signal(7)</code></a> for a list …
17210 <a href="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code></a> succeeds, and <…
17211 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
17212 …s="hljs-keyword">const</span> grep = <span class="hljs-title function_">spawn</span>(<span class="…
17214 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span clas…
17215 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
17216 …<span class="hljs-string">`child process terminated due to receipt of signal <span class="hljs-sub…
17219 <span class="hljs-comment">// Send SIGHUP to process.</span>
17220 grep.<span class="hljs-title function_">kill</span>(<span class="hljs-string">'SIGHUP'</span>);</co…
17228 <p>See <a href="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code></a> for refe…
17236 <pre><code class="language-js"><span class="hljs-meta">'use strict'</span>;
17237 <span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_in">require</span>(<spa…
17239 <span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span…
17240 <span class="hljs-string">'sh'</span>,
17242 <span class="hljs-string">'-c'</span>,
17243 <span class="hljs-string">`node -e "setInterval(() => {
17247 … class="hljs-attr">stdio</span>: [<span class="hljs-string">'inherit'</span>, <span class="hljs-st…
17251 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
17252 …subprocess.<span class="hljs-title function_">kill</span>(); <span class="hljs-comment">// Does no…
17253 }, <span class="hljs-number">2000</span>);</code></pre>
17259 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
17270 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
17275 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
17276 …s="hljs-keyword">const</span> grep = <span class="hljs-title function_">spawn</span>(<span class="…
17278 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
17279 grep.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">end</span>();</cod…
17287 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
17289-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(process.<span …
17290 <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>,
17291 <span class="hljs-attr">stdio</span>: <span class="hljs-string">'ignore'</span>
17294 subprocess.<span class="hljs-title function_">unref</span>();
17295 subprocess.<span class="hljs-title function_">ref</span>();</code></pre>
17313 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
17315 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
17319 <li><code>keepOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
17324 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
17325 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
17334 …<code class="language-js"><span class="hljs-keyword">const</span> cp = <span class="hljs-built_in"…
17335 …pan class="hljs-keyword">const</span> n = cp.<span class="hljs-title function_">fork</span>(<span …
17337 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
17338 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
17341 <span class="hljs-comment">// Causes the child to print: CHILD got message: { hello: 'world' }</spa…
17342 n.<span class="hljs-title function_">send</span>({ <span class="hljs-attr">hello</span>: <span clas…
17344 …ge-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</s…
17345 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
17348 <span class="hljs-comment">// Causes the parent to print: PARENT got message: { foo: 'bar', baz: nu…
17349-title function_">send</span>({ <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar…
17366 single argument: <code>null</code> on success, or an <a href="#errors_class_error"><code>Error</cod…
17377-js"><span class="hljs-keyword">const</span> subprocess = <span class="hljs-built_in">require</spa…
17379 <span class="hljs-comment">// Open up the server object and send the handle.</span>
17380 …lass="hljs-keyword">const</span> server = <span class="hljs-built_in">require</span>(<span class="…
17381 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, <span cl…
17382 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'handled by parent'…
17384 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>, <spa…
17385 …subprocess.<span class="hljs-title function_">send</span>(<span class="hljs-string">'server'</span…
17388 …ge-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</s…
17389 <span class="hljs-keyword">if</span> (m === <span class="hljs-string">'server'</span>) {
17390 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, <span cl…
17391 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'handled by child'<…
17406 …de class="language-js"><span class="hljs-keyword">const</span> { fork } = <span class="hljs-built_…
17407 …js-keyword">const</span> normal = <span class="hljs-title function_">fork</span>(<span class="hljs
17408 …js-keyword">const</span> special = <span class="hljs-title function_">fork</span>(<span class="hlj…
17410 <span class="hljs-comment">// Open up the server and send sockets to child. Use pauseOnConnect to p…
17411 <span class="hljs-comment">// the sockets from being read before they are sent to the child process…
17412-keyword">const</span> server = <span class="hljs-built_in">require</span>(<span class="hljs-strin…
17413 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, <span cl…
17415 <span class="hljs-comment">// If this is special priority...</span>
17416 …<span class="hljs-keyword">if</span> (socket.<span class="hljs-property">remoteAddress</span> === …
17417 …special.<span class="hljs-title function_">send</span>(<span class="hljs-string">'socket'</span>, …
17418 <span class="hljs-keyword">return</span>;
17420 <span class="hljs-comment">// This is normal priority.</span>
17421 …normal.<span class="hljs-title function_">send</span>(<span class="hljs-string">'socket'</span>, s…
17423 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);</co…
17426 …ge-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</s…
17427 <span class="hljs-keyword">if</span> (m === <span class="hljs-string">'socket'</span>) {
17428 <span class="hljs-keyword">if</span> (socket) {
17429 <span class="hljs-comment">// Check that the client socket exists.</span>
17430 …<span class="hljs-comment">// It is possible for the socket to be closed between the time it is</s…
17431 <span class="hljs-comment">// sent and the time it is received in the child process.</span>
17432 …ass="hljs-title function_">end</span>(<span class="hljs-string">`Request handled with <span class=…
17443 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
17449 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array…
17451 <p>The <code>subprocess.spawnargs</code> property represents the full list of command-line
17455 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
17500 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array…
17510 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
17511 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
17512 <span class="hljs-keyword">const</span> child_process = <span class="hljs-built_in">require</span>(…
17514 <span class="hljs-keyword">const</span> subprocess = child_process.<span class="hljs-title function…
17515 <span class="hljs-attr">stdio</span>: [
17516 …<span class="hljs-number">0</span>, <span class="hljs-comment">// Use parent's stdin for child.</s…
17517 …<span class="hljs-string">'pipe'</span>, <span class="hljs-comment">// Pipe child's stdout to pare…
17518 …ass="hljs-title function_">openSync</span>(<span class="hljs-string">'err.out'</span>, <span class…
17522 …hljs-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span…
17523-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span cla…
17525 <span class="hljs-title function_">assert</span>(subprocess.<span class="hljs-property">stdout</spa…
17526-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span cla…
17528 …hljs-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span…
17529-title function_">strictEqual</span>(subprocess.<span class="hljs-property">stdio</span>[<span cla…
17544 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
17546 <span class="hljs-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span…
17548-property">stdout</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'d…
17549 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
17563 …e class="language-js"><span class="hljs-keyword">const</span> { spawn } = <span class="hljs-built_…
17565-keyword">const</span> subprocess = <span class="hljs-title function_">spawn</span>(process.<span …
17566 <span class="hljs-attr">detached</span>: <span class="hljs-literal">true</span>,
17567 <span class="hljs-attr">stdio</span>: <span class="hljs-string">'ignore'</span>
17570 subprocess.<span class="hljs-title function_">unref</span>();</code></pre>
17574 This impacts output that includes multibyte character encodings such as UTF-8 or
17575 UTF-16. For instance, <code>console.log('中文测试')</code> will send 13 UTF-8 encoded bytes
17578 <p>The shell should understand the <code>-c</code> switch. If the shell is <code>'cmd.exe'</code>, …
17579 should understand the <code>/d /s /c</code> switches and command-line parsing should be
17593 <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorith…
17594 supports more built-in JavaScript object types, such as <code>BigInt</code>, <code>Map</code>
17597 objects of such built-in types will not be passed on through the serialization
17606 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
17608 <p>A single instance of Node.js runs in a single thread. To take advantage of
17609 multi-core systems, the user will sometimes want to launch a cluster of Node.js
17613 …de class="language-js"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_i…
17614 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
17615-keyword">const</span> numCPUs = <span class="hljs-built_in">require</span>(<span class="hljs-stri…
17617 <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isMaster</span>) {
17618 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
17620 <span class="hljs-comment">// Fork workers.</span>
17621 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs
17622 cluster.<span class="hljs-title function_">fork</span>();
17625 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
17626 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
17628 } <span class="hljs-keyword">else</span> {
17629 <span class="hljs-comment">// Workers can share any TCP connection</span>
17630 <span class="hljs-comment">// In this case it is an HTTP server</span>
17631 …http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span cla…
17632 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>);
17633 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>…
17634 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);
17636 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
17639 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
17654 is the round-robin approach, where the master process listens on a
17656 in a round-robin fashion, with some built-in smarts to avoid
17684 application such that it does not rely too heavily on in-memory data objects for
17687 re-spawned depending on a program's needs, without affecting other
17710-js">cluster.<span class="hljs-title function_">fork</span>().<span class="hljs-title function_">o…
17711 <span class="hljs-comment">// Worker has disconnected</span>
17724 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
17725 <li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
17729 <pre><code class="language-js"><span class="hljs-keyword">const</span> worker = cluster.<span class…
17730 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
17731 <span class="hljs-keyword">if</span> (signal) {
17732-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
17733 …} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (code !== <span clas…
17734-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
17735 } <span class="hljs-keyword">else</span> {
17736 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
17744 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
17747-js">cluster.<span class="hljs-title function_">fork</span>().<span class="hljs-title function_">o…
17748 <span class="hljs-comment">// Worker is listening</span>
17756 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
17757 …la.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a>…
17764 …de class="language-js"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_i…
17765 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
17767 <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isMaster</span>) {
17769 <span class="hljs-comment">// Keep track of http requests</span>
17770 <span class="hljs-keyword">let</span> numReqs = <span class="hljs-number">0</span>;
17771 <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> {
17772 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
17773 }, <span class="hljs-number">1000</span>);
17775 <span class="hljs-comment">// Count requests</span>
17776 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">messageHandler</span…
17777 …"hljs-keyword">if</span> (msg.<span class="hljs-property">cmd</span> &#x26;&#x26; msg.<span class=…
17778 numReqs += <span class="hljs-number">1</span>;
17782 …<span class="hljs-comment">// Start workers and listen for messages containing notifyRequest</span>
17783-keyword">const</span> numCPUs = <span class="hljs-built_in">require</span>(<span class="hljs-stri…
17784 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs
17785 cluster.<span class="hljs-title function_">fork</span>();
17788 … class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> id <span class="hljs-key…
17789 …cluster.<span class="hljs-property">workers</span>[id].<span class="hljs-title function_">on</span…
17792 } <span class="hljs-keyword">else</span> {
17794 <span class="hljs-comment">// Worker processes have a http server.</span>
17795 …http.<span class="hljs-title class_">Server</span>(<span class="hljs-function">(<span class="hljs-
17796 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>);
17797 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>…
17799 <span class="hljs-comment">// Notify master about the request</span>
17800 …process.<span class="hljs-title function_">send</span>({ <span class="hljs-attr">cmd</span>: <span…
17801 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);
17808-js">cluster.<span class="hljs-title function_">fork</span>().<span class="hljs-title function_">o…
17809 <span class="hljs-comment">// Worker is online</span>
17846 <pre><code class="language-js"><span class="hljs-keyword">if</span> (cluster.<span class="hljs-prop…
17847 …<span class="hljs-keyword">const</span> worker = cluster.<span class="hljs-title function_">fork</…
17848 <span class="hljs-keyword">let</span> timeout;
17850 …span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</span>, <span cl…
17851 …worker.<span class="hljs-title function_">send</span>(<span class="hljs-string">'shutdown'</span>);
17852 worker.<span class="hljs-title function_">disconnect</span>();
17853 … timeout = <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
17854 worker.<span class="hljs-title function_">kill</span>();
17855 }, <span class="hljs-number">2000</span>);
17858 …worker.<span class="hljs-title function_">on</span>(<span class="hljs-string">'disconnect'</span>,…
17859 <span class="hljs-built_in">clearTimeout</span>(timeout);
17862 } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (cluster.<span class=…
17863 …<span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span cla…
17864 …s="hljs-keyword">const</span> server = net.<span class="hljs-title function_">createServer</span>(…
17865 <span class="hljs-comment">// Connections never end</span>
17868 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);
17870 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
17871 <span class="hljs-keyword">if</span> (msg === <span class="hljs-string">'shutdown'</span>) {
17872 <span class="hljs-comment">// Initiate graceful close of any connections to server</span>
17881 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
17889 …age-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</spa…
17890 …<span class="hljs-keyword">if</span> (worker.<span class="hljs-property">exitedAfterDisconnect</sp…
17891 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
17895 <span class="hljs-comment">// kill worker</span>
17896 worker.<span class="hljs-title function_">kill</span>();</code></pre>
17902 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
17921 …de class="language-js"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_i…
17922 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
17923-keyword">const</span> numCPUs = <span class="hljs-built_in">require</span>(<span class="hljs-stri…
17925 <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isMaster</span>) {
17926 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
17928 <span class="hljs-comment">// Fork workers.</span>
17929 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs
17930 cluster.<span class="hljs-title function_">fork</span>();
17933 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'fork'</span>, <span class…
17934-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
17937 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
17938-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
17940 } <span class="hljs-keyword">else</span> {
17941 …<span class="hljs-comment">// Workers can share any TCP connection. In this case, it is an HTTP se…
17942 …http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span cla…
17943 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>);
17944 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">`Current process\n <sp…
17945 …process.<span class="hljs-title function_">kill</span>(process.<span class="hljs-property">pid</sp…
17946 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);
17953 <li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
17994 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
17996 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
18000 <li><code>keepOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
18005 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
18006 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
18014 <pre><code class="language-js"><span class="hljs-keyword">if</span> (cluster.<span class="hljs-prop…
18015 …<span class="hljs-keyword">const</span> worker = cluster.<span class="hljs-title function_">fork</…
18016 …worker.<span class="hljs-title function_">send</span>(<span class="hljs-string">'hi there'</span>);
18018 } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (cluster.<span class=…
18019 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
18020 process.<span class="hljs-title function_">send</span>(msg);
18035 are long-living connections.</p>
18036-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'disconnect'</…
18037 …s="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cla…
18045 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
18046 <li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
18051 …age-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</spa…
18052 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
18053 …worker.<span class="hljs-property">process</span>.<span class="hljs-property">pid</span>, signal |…
18054 cluster.<span class="hljs-title function_">fork</span>();
18066 <pre><code class="language-js"><span class="hljs-keyword">const</span> timeouts = [];
18067 <span class="hljs-keyword">function</span> <span class="hljs-title function_">errorMsg</span>(<span…
18068 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
18071 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'fork'</span>, <span class…
18072 …ts[worker.<span class="hljs-property">id</span>] = <span class="hljs-built_in">setTimeout</span>(e…
18074 …span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</span>, <span cl…
18075 …<span class="hljs-built_in">clearTimeout</span>(timeouts[worker.<span class="hljs-property">id</sp…
18077 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
18078 …<span class="hljs-built_in">clearTimeout</span>(timeouts[worker.<span class="hljs-property">id</sp…
18079 <span class="hljs-title function_">errorMsg</span>();
18087 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
18096 …e-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</…
18097 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
18098 …<span class="hljs-string">`A worker is now connected to <span class="hljs-subst">${address.address…
18104 <li><code>-1</code> (Unix domain socket)</li>
18121 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
18122 …la.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a>…
18137 …ge-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'online'</sp…
18138 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
18145 <li><code>settings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
18150 …ter_cluster_setupmaster_settings"><code>.setupMaster()</code></a> can be made in a single tick.</p>
18157 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
18171 <li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
18181 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
18191 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
18198 <p>The scheduling policy, either <code>cluster.SCHED_RR</code> for round-robin or
18229 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
18231 <li><code>execArgv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
18233 <li><code>exec</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
18234 <li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
18236 <li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
18238 <li><code>serialization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
18242 <li><code>silent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
18244 <li><code>stdio</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
18247-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the user …
18248-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Sets the group…
18249 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
18253 <li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
18274 <li><code>settings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
18284 …de class="language-js"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_i…
18285 cluster.<span class="hljs-title function_">setupMaster</span>({
18286 <span class="hljs-attr">exec</span>: <span class="hljs-string">'worker.js'</span>,
18287 …<span class="hljs-attr">args</span>: [<span class="hljs-string">'--use'</span>, <span class="hljs-
18288 <span class="hljs-attr">silent</span>: <span class="hljs-literal">true</span>
18290 cluster.<span class="hljs-title function_">fork</span>(); <span class="hljs-comment">// https worke…
18291 cluster.<span class="hljs-title function_">setupMaster</span>({
18292 <span class="hljs-attr">exec</span>: <span class="hljs-string">'worker.js'</span>,
18293 …<span class="hljs-attr">args</span>: [<span class="hljs-string">'--use'</span>, <span class="hljs-
18295 cluster.<span class="hljs-title function_">fork</span>(); <span class="hljs-comment">// http worker…
18302 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
18305 …de class="language-js"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_i…
18307 <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isMaster</span>) {
18308 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
18309 cluster.<span class="hljs-title function_">fork</span>();
18310 cluster.<span class="hljs-title function_">fork</span>();
18311 } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (cluster.<span class=…
18312 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
18319 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
18328 <pre><code class="language-js"><span class="hljs-comment">// Go through all workers</span>
18329 <span class="hljs-keyword">function</span> <span class="hljs-title function_">eachWorker</span>(<sp…
18330 … class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> id <span class="hljs-key…
18331 …<span class="hljs-title function_">callback</span>(cluster.<span class="hljs-property">workers</sp…
18334 <span class="hljs-title function_">eachWorker</span>(<span class="hljs-function">(<span class="hljs
18335 …worker.<span class="hljs-title function_">send</span>(<span class="hljs-string">'big announcement …
18338 …uage-js">socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</spa…
18339 …<span class="hljs-keyword">const</span> worker = cluster.<span class="hljs-property">workers</span…
18342 <section><h2>Command-line options<span><a class="mark" href="#cli_command_line_options" id="cli_com…
18345 <p>Node.js comes with a variety of CLI options. These options expose built-in
18349 <p><code>node [options] [V8 options] [script.js | -e "script" | -] [--] [arguments]</code></p>
18350 <p><code>node inspect [script.js | -e "script" | &#x3C;host>:&#x3C;port>] …</code></p>
18351 <p><code>node --v8-options</code></p>
18365 dashes (<code>-</code>) or underscores (<code>_</code>). For example, <code>--pending-deprecation</…
18366 equivalent to <code>--pending_deprecation</code>.</p>
18367 <p>If an option that takes a single value (such as <code>--max-http-header-size</code>) is
18371 <h4><code>-</code><span><a class="mark" href="#cli" id="cli">#</a></span></h4>
18375 <p>Alias for stdin. Analogous to the use of <code>-</code> in other command-line utilities,
18378 <h4><code>--</code><span><a class="mark" href="#cli_1" id="cli_1">#</a></span></h4>
18385 <h4><code>--abort-on-uncaught-exception</code><span><a class="mark" href="#cli_abort_on_uncaught_ex…
18389 <p>Aborting instead of exiting causes a core file to be generated for post-mortem
18394 <h4><code>--completion-bash</code><span><a class="mark" href="#cli_completion_bash" id="cli_complet…
18398 <p>Print source-able bash completion script for Node.js.</p>
18399 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
18400 <span class="hljs-meta">$ </span><span class="language-bash"><span class="hljs-built_in">source</sp…
18401 <h4><code>-C=condition</code>, <code>--conditions=condition</code><span><a class="mark" href="#cli_…
18405 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18412 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
18413 <h4><code>--cpu-prof</code><span><a class="mark" href="#cli_cpu_prof" id="cli_cpu_prof">#</a></span…
18417 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18420 <p>If <code>--cpu-prof-dir</code> is not specified, the generated profile is placed
18422 <p>If <code>--cpu-prof-name</code> is not specified, the generated profile is
18424 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
18425 <span class="hljs-meta">$ </span><span class="language-bash">ls *.cpuprofile</span>
18427 <h4><code>--cpu-prof-dir</code><span><a class="mark" href="#cli_cpu_prof_dir" id="cli_cpu_prof_dir"…
18431 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18432 <p>Specify the directory where the CPU profiles generated by <code>--cpu-prof</code> will
18435 <a href="#cli_diagnostic_dir_directory">--diagnostic-dir</a> command-line option.</p>
18436 <h4><code>--cpu-prof-interval</code><span><a class="mark" href="#cli_cpu_prof_interval" id="cli_cpu…
18440 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18442 by <code>--cpu-prof</code>. The default is 1000 microseconds.</p>
18443 <h4><code>--cpu-prof-name</code><span><a class="mark" href="#cli_cpu_prof_name" id="cli_cpu_prof_na…
18447 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18448 <p>Specify the file name of the CPU profile generated by <code>--cpu-prof</code>.</p>
18449 <h4><code>--diagnostic-dir=directory</code><span><a class="mark" href="#cli_diagnostic_dir_director…
18454 <li><a href="#cli_cpu_prof_dir">--cpu-prof-dir</a></li>
18455 <li><a href="#cli_heap_prof_dir">--heap-prof-dir</a></li>
18456 <li><a href="#cli_redirect_warnings_file">--redirect-warnings</a></li>
18458 <h4><code>--disable-proto=mode</code><span><a class="mark" href="#cli_disable_proto_mode" id="cli_d…
18465 <h4><code>--disallow-code-generation-from-strings</code><span><a class="mark" href="#cli_disallow_c…
18469 <p>Make built-in language features like <code>eval</code> and <code>new Function</code> that genera…
18472 <h4><code>--dns-result-order=order</code><span><a class="mark" href="#cli_dns_result_order_order" i…
18483 priority than <code>--dns-result-order</code>.</p>
18484 <h4><code>--enable-fips</code><span><a class="mark" href="#cli_enable_fips" id="cli_enable_fips">#<…
18488 <p>Enable FIPS-compliant crypto at startup. (Requires Node.js to be built
18489 against FIPS-compatible OpenSSL.)</p>
18490 <h4><code>--enable-source-maps</code><span><a class="mark" href="#cli_enable_source_maps" id="cli_e…
18505 <code>--enable-source-maps</code> enables caching of Source Maps and makes a best
18507 <p>Overriding <code>Error.prepareStackTrace</code> prevents <code>--enable-source-maps</code> from
18509 <h4><code>--experimental-abortcontroller</code><span><a class="mark" href="#cli_experimental_abortc…
18514 <h4><code>--experimental-import-meta-resolve</code><span><a class="mark" href="#cli_experimental_im…
18519 <h4><code>--experimental-json-modules</code><span><a class="mark" href="#cli_experimental_json_modu…
18524 <h4><code>--experimental-loader=module</code><span><a class="mark" href="#cli_experimental_loader_m…
18530 <h4><code>--experimental-modules</code><span><a class="mark" href="#cli_experimental_modules" id="c…
18535 <h4><code>--experimental-policy</code><span><a class="mark" href="#cli_experimental_policy" id="cli…
18540 <h4><code>--experimental-repl-await</code><span><a class="mark" href="#cli_experimental_repl_await"…
18544 <p>Enable experimental top-level <code>await</code> keyword support in REPL.</p>
18545 <h4><code>--experimental-specifier-resolution=mode</code><span><a class="mark" href="#cli_experimen…
18555 <h4><code>--experimental-vm-modules</code><span><a class="mark" href="#cli_experimental_vm_modules"…
18560 <h4><code>--experimental-wasi-unstable-preview1</code><span><a class="mark" href="#cli_experimental…
18566 <td><p>changed from <code>--experimental-wasi-unstable-preview0</code> to <code>--experimental-wasi
18573 <h4><code>--experimental-wasm-modules</code><span><a class="mark" href="#cli_experimental_wasm_modu…
18578 <h4><code>--force-context-aware</code><span><a class="mark" href="#cli_force_context_aware" id="cli…
18582 <p>Disable loading native addons that are not <a href="#addons_context_aware_addons">context-aware<…
18583 <h4><code>--force-fips</code><span><a class="mark" href="#cli_force_fips" id="cli_force_fips">#</a>…
18587 <p>Force FIPS-compliant crypto on startup. (Cannot be disabled from script code.)
18588 (Same requirements as <code>--enable-fips</code>.)</p>
18589 <h4><code>--frozen-intrinsics</code><span><a class="mark" href="#cli_frozen_intrinsics" id="cli_fro…
18593 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18598 <p><code>--require</code> runs prior to freezing intrinsics in order to allow polyfills to
18600 <h4><code>--heapsnapshot-near-heap-limit=max_count</code><span><a class="mark" href="#cli_heapsnaps…
18604 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18606 heap limit. <code>count</code> should be a non-negative integer (in which case
18623 …ass="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node --max-old
18628 &#x3C;--- Last few GCs --->
18630 [49580:0x110000000] 4826 ms: Mark-sweep 130.6 (147.8) -> 130.5 (147.8) MB, 27.4 / 0.0 ms (aver…
18631 [49580:0x110000000] 4845 ms: Mark-sweep 130.6 (147.8) -> 130.6 (147.8) MB, 18.8 / 0.0 ms (aver…
18634 &#x3C;--- JS stacktrace --->
18636 FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of m…
18638 <h4><code>--heapsnapshot-signal=signal</code><span><a class="mark" href="#cli_heapsnapshot_signal_s…
18645 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
18646 <span class="hljs-meta">$ </span><span class="language-bash">ps aux</span>
18648 node 1 5.5 6.1 787252 247004 ? Ssl 16:43 0:02 node --heapsnapshot-signal=SIGUSR2…
18649 <span class="hljs-meta">$ </span><span class="language-bash"><span class="hljs-built_in">kill</span…
18650 <span class="hljs-meta">$ </span><span class="language-bash">ls</span>
18652 <h4><code>--heap-prof</code><span><a class="mark" href="#cli_heap_prof" id="cli_heap_prof">#</a></s…
18656 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18659 <p>If <code>--heap-prof-dir</code> is not specified, the generated profile is placed
18661 <p>If <code>--heap-prof-name</code> is not specified, the generated profile is
18663 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
18664 <span class="hljs-meta">$ </span><span class="language-bash">ls *.heapprofile</span>
18666 <h4><code>--heap-prof-dir</code><span><a class="mark" href="#cli_heap_prof_dir" id="cli_heap_prof_d…
18670 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18671 <p>Specify the directory where the heap profiles generated by <code>--heap-prof</code> will
18674 <a href="#cli_diagnostic_dir_directory">--diagnostic-dir</a> command-line option.</p>
18675 <h4><code>--heap-prof-interval</code><span><a class="mark" href="#cli_heap_prof_interval" id="cli_h…
18679 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18681 by <code>--heap-prof</code>. The default is 512 * 1024 bytes.</p>
18682 <h4><code>--heap-prof-name</code><span><a class="mark" href="#cli_heap_prof_name" id="cli_heap_prof…
18686 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18687 <p>Specify the file name of the heap profile generated by <code>--heap-prof</code>.</p>
18688 <h4><code>--icu-data-dir=file</code><span><a class="mark" href="#cli_icu_data_dir_file" id="cli_icu…
18693 <h4><code>--input-type=type</code><span><a class="mark" href="#cli_input_type_type" id="cli_input_t…
18698 module. String input is input via <code>--eval</code>, <code>--print</code>, or <code>STDIN</code>.…
18700 <h4><code>--inspect-brk[=[host:]port]</code><span><a class="mark" href="#cli_inspect_brk_host_port"…
18706 <h4><code>--inspect-port=[host:]port</code><span><a class="mark" href="#cli_inspect_port_host_port"…
18715 <h4><code>--inspect[=[host:]port]</code><span><a class="mark" href="#cli_inspect_host_port" id="cli…
18722 tcp port and communicate using the <a href="https://chromedevtools.github.io/devtools-protocol/">Ch…
18733 <p><strong>More specifically, <code>--inspect=0.0.0.0</code> is insecure if the port (<code>9229</c…
18734 default) is not firewall-protected.</strong></p>
18735 <p>See the <a href="https://nodejs.org/en/docs/guides/debugging-getting-started/#security-implicati…
18736 <h4><code>--inspect-publish-uid=stderr,http</code><span><a class="mark" href="#cli_inspect_publish_…
18740 <h4><code>--insecure-http-parser</code><span><a class="mark" href="#cli_insecure_http_parser" id="c…
18745 interoperability with non-conformant HTTP implementations. It may also allow
18748 <h4><code>--jitless</code><span><a class="mark" href="#cli_jitless" id="cli_jitless">#</a></span></…
18756 disappear in a non-semver-major release.</p>
18757 <h4><code>--max-http-header-size=size</code><span><a class="mark" href="#cli_max_http_header_size_s…
18770 <h4><code>--napi-modules</code><span><a class="mark" href="#cli_napi_modules" id="cli_napi_modules"…
18774 <p>This option is a no-op. It is kept for compatibility.</p>
18775 <h4><code>--no-addons</code><span><a class="mark" href="#cli_no_addons" id="cli_no_addons">#</a></s…
18779 <p>Disable the <code>node-addons</code> exports condition as well as disable loading
18780 native addons. When <code>--no-addons</code> is specified, calling <code>process.dlopen</code> or
18782 <h4><code>--no-deprecation</code><span><a class="mark" href="#cli_no_deprecation" id="cli_no_deprec…
18787 <h4><code>--no-force-async-hooks-checks</code><span><a class="mark" href="#cli_no_force_async_hooks…
18793 <h4><code>--no-warnings</code><span><a class="mark" href="#cli_no_warnings" id="cli_no_warnings">#<…
18798 <h4><code>--node-memory-debug</code><span><a class="mark" href="#cli_node_memory_debug" id="cli_nod…
18804 <h4><code>--openssl-config=file</code><span><a class="mark" href="#cli_openssl_config_file" id="cli…
18809 used to enable FIPS-compliant crypto if Node.js is built
18810 against FIPS-enabled OpenSSL.</p>
18811 <h4><code>--pending-deprecation</code><span><a class="mark" href="#cli_pending_deprecation" id="cli…
18818 unless either the <code>--pending-deprecation</code> command-line flag, or the
18822 <h4><code>--policy-integrity=sri</code><span><a class="mark" href="#cli_policy_integrity_sri" id="c…
18826 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
18828 the specified integrity. It expects a <a href="https://developer.mozilla.org/en-US/docs/Web/Securit…
18830 <h4><code>--preserve-symlinks</code><span><a class="mark" href="#cli_preserve_symlinks" id="cli_pre…
18837 to a different on-disk location, Node.js will dereference the link and use the
18838 actual on-disk "real path" of the module as both an identifier and as a root
18843 <pre><code class="language-text">{appDir}
18847 │ ├── moduleA -> {appDir}/moduleA
18854 <p>The <code>--preserve-symlinks</code> command-line flag instructs Node.js to use the
18857 <p>Note, however, that using <code>--preserve-symlinks</code> can have other side effects.
18862 <p>The <code>--preserve-symlinks</code> flag does not apply to the main module, which allows
18863 <code>node --preserve-symlinks node_module/.bin/&#x3C;foo></code> to work. To apply the same
18864 behavior for the main module, also use <code>--preserve-symlinks-main</code>.</p>
18865 <h4><code>--preserve-symlinks-main</code><span><a class="mark" href="#cli_preserve_symlinks_main" i…
18871 <p>This flag exists so that the main module can be opted-in to the same behavior
18872 that <code>--preserve-symlinks</code> gives to all other imports; they are separate flags,
18874 <p><code>--preserve-symlinks-main</code> does not imply <code>--preserve-symlinks</code>; use
18875 <code>--preserve-symlinks-main</code> in addition to
18876 <code>--preserve-symlinks</code> when it is not desirable to follow symlinks before
18878 <p>See <code>--preserve-symlinks</code> for more information.</p>
18879 <h4><code>--prof</code><span><a class="mark" href="#cli_prof" id="cli_prof">#</a></span></h4>
18884 <h4><code>--prof-process</code><span><a class="mark" href="#cli_prof_process" id="cli_prof_process"…
18888 <p>Process V8 profiler output generated using the V8 option <code>--prof</code>.</p>
18889 <h4><code>--redirect-warnings=file</code><span><a class="mark" href="#cli_redirect_warnings_file" i…
18899 <a href="#cli_diagnostic_dir_directory">--diagnostic-dir</a> command-line option.</p>
18900 <h4><code>--report-compact</code><span><a class="mark" href="#cli_report_compact" id="cli_report_co…
18904 <p>Write reports in a compact format, single-line JSON, more easily consumable
18905 by log processing systems than the default multi-line format designed for
18907 <h4><code>--report-dir=directory</code>, <code>report-directory=directory</code><span><a class="mar…
18915 <td><p>Changed from <code>--diagnostic-report-directory</code> to <code>--report-directory</code>.<…
18922 <h4><code>--report-filename=filename</code><span><a class="mark" href="#cli_report_filename_filenam…
18930 <td><p>changed from <code>--diagnostic-report-filename</code> to <code>--report-filename</code>.</p…
18937 <h4><code>--report-on-fatalerror</code><span><a class="mark" href="#cli_report_on_fatalerror" id="c…
18945 <td><p>changed from <code>--diagnostic-report-on-fatalerror</code> to <code>--report-on-fatalerror<…
18956 <h4><code>--report-on-signal</code><span><a class="mark" href="#cli_report_on_signal" id="cli_repor…
18964 <td><p>changed from <code>--diagnostic-report-on-signal</code> to <code>--report-on-signal</code>.<…
18972 specified through <code>--report-signal</code>.</p>
18973 <h4><code>--report-signal=signal</code><span><a class="mark" href="#cli_report_signal_signal" id="c…
18981 <td><p>changed from <code>--diagnostic-report-signal</code> to <code>--report-signal</code>.</p></t…
18989 <h4><code>--report-uncaught-exception</code><span><a class="mark" href="#cli_report_uncaught_except…
18997 <td><p>changed from <code>--diagnostic-report-uncaught-exception</code> to <code>--report-uncaught-
19006 <h4><code>--throw-deprecation</code><span><a class="mark" href="#cli_throw_deprecation" id="cli_thr…
19011 <h4><code>--title=title</code><span><a class="mark" href="#cli_title_title" id="cli_title_title">#<…
19016 <h4><code>--tls-cipher-list=list</code><span><a class="mark" href="#cli_tls_cipher_list_list" id="c…
19022 <h4><code>--tls-keylog=file</code><span><a class="mark" href="#cli_tls_keylog_file" id="cli_tls_key…
19029 <h4><code>--tls-max-v1.2</code><span><a class="mark" href="#cli_tls_max_v1_2" id="cli_tls_max_v1_2"…
19035 <h4><code>--tls-max-v1.3</code><span><a class="mark" href="#cli_tls_max_v1_3" id="cli_tls_max_v1_3"…
19041 <h4><code>--tls-min-v1.0</code><span><a class="mark" href="#cli_tls_min_v1_0" id="cli_tls_min_v1_0"…
19047 <h4><code>--tls-min-v1.1</code><span><a class="mark" href="#cli_tls_min_v1_1" id="cli_tls_min_v1_1"…
19053 <h4><code>--tls-min-v1.2</code><span><a class="mark" href="#cli_tls_min_v1_2" id="cli_tls_min_v1_2"…
19060 <h4><code>--tls-min-v1.3</code><span><a class="mark" href="#cli_tls_min_v1_3" id="cli_tls_min_v1_3"…
19066 <h4><code>--trace-atomics-wait</code><span><a class="mark" href="#cli_trace_atomics_wait" id="cli_t…
19070 <p>Print short summaries of calls to <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
19072 <pre><code class="language-text">(node:15701) [Thread 0] Atomics.wait(&#x26;lt;address> + 0, 1, inf…
19077 (node:15701) [Thread 1] Atomics.wait(&#x26;lt;address> + 4, -1, inf) started
19079 (node:15701) [Thread 1] Atomics.wait(&#x26;lt;address> + 4, -1, inf) was woken up by another thread…
19088 <h4><code>--trace-deprecation</code><span><a class="mark" href="#cli_trace_deprecation" id="cli_tra…
19093 <h4><code>--trace-event-categories</code><span><a class="mark" href="#cli_trace_event_categories" i…
19098 tracing is enabled using <code>--trace-events-enabled</code>.</p>
19099 <h4><code>--trace-event-file-pattern</code><span><a class="mark" href="#cli_trace_event_file_patter…
19105 <h4><code>--trace-events-enabled</code><span><a class="mark" href="#cli_trace_events_enabled" id="c…
19110 <h4><code>--trace-exit</code><span><a class="mark" href="#cli_trace_exit" id="cli_trace_exit">#</a>…
19116 <h4><code>--trace-sigint</code><span><a class="mark" href="#cli_trace_sigint" id="cli_trace_sigint"…
19121 <h4><code>--trace-sync-io</code><span><a class="mark" href="#cli_trace_sync_io" id="cli_trace_sync_…
19127 <h4><code>--trace-tls</code><span><a class="mark" href="#cli_trace_tls" id="cli_trace_tls">#</a></s…
19133 <h4><code>--trace-uncaught</code><span><a class="mark" href="#cli_trace_uncaught" id="cli_trace_unc…
19142 <h4><code>--trace-warnings</code><span><a class="mark" href="#cli_trace_warnings" id="cli_trace_war…
19147 <h4><code>--track-heap-objects</code><span><a class="mark" href="#cli_track_heap_objects" id="cli_t…
19152 <h4><code>--unhandled-rejections=mode</code><span><a class="mark" href="#cli_unhandled_rejections_m…
19167 <li><code>warn-with-error-code</code>: Emit <a href="#process_event_unhandledrejection"><code>unhan…
19171 <h4><code>--use-bundled-ca</code>, <code>--use-openssl-ca</code><span><a class="mark" href="#cli_us…
19177 at build-time.</p>
19186 <h4><code>--use-largepages=mode</code><span><a class="mark" href="#cli_use_largepages_mode" id="cli…
19190 <p>Re-map the Node.js static code to large memory pages at startup. If supported on
19191 the target system, this will cause the Node.js static code to be moved onto 2
19201 <h4><code>--v8-options</code><span><a class="mark" href="#cli_v8_options" id="cli_v8_options">#</a>…
19205 <p>Print V8 command-line options.</p>
19206 <h4><code>--v8-pool-size=num</code><span><a class="mark" href="#cli_v8_pool_size_num" id="cli_v8_po…
19215 <h4><code>--zero-fill-buffers</code><span><a class="mark" href="#cli_zero_fill_buffers" id="cli_zer…
19219 <p>Automatically zero-fills all newly allocated <a href="#buffer_class_buffer"><code>Buffer</code><…
19221 <h4><code>-c</code>, <code>--check</code><span><a class="mark" href="#cli_c_check" id="cli_c_check"…
19227 <td><p>The <code>--require</code> option is now supported when checking a file.</p></td></tr>
19234 <h4><code>-e</code>, <code>--eval "script"</code><span><a class="mark" href="#cli_e_eval_script" id…
19240 <td><p>Built-in libraries are now available as predefined variables.</p></td></tr>
19248 <p>On Windows, using <code>cmd.exe</code> a single quote will not work correctly because it
19251 <h4><code>-h</code>, <code>--help</code><span><a class="mark" href="#cli_h_help" id="cli_h_help">#<…
19255 <p>Print node command-line options.
19257 <h4><code>-i</code>, <code>--interactive</code><span><a class="mark" href="#cli_i_interactive" id="…
19262 <h4><code>-p</code>, <code>--print "script"</code><span><a class="mark" href="#cli_p_print_script" …
19268 <td><p>Built-in libraries are now available as predefined variables.</p></td></tr>
19274 <p>Identical to <code>-e</code> but prints the result.</p>
19275 <h4><code>-r</code>, <code>--require module</code><span><a class="mark" href="#cli_r_require_module…
19283 ES6 Module using <code>--require</code> will fail with an error.</p>
19284 <h4><code>-v</code>, <code>--version</code><span><a class="mark" href="#cli_v_version" id="cli_v_ve…
19294 <li><code>1</code>, <code>true</code>, or the empty string <code>''</code> indicate 16-color suppor…
19295 <li><code>2</code> to indicate 256-color support, or</li>
19296 <li><code>3</code> to indicate 16 million-color support.</li>
19305 <p><code>','</code>-separated list of core modules that should print debug information.</p>
19307 <p><code>','</code>-separated list of core C++ modules that should print debug information.</p>
19333 <p>Data path for ICU (<code>Intl</code> object) data. Will extend linked-in data when compiled
19334 with small-icu support.</p>
19344 <p>A space-separated list of command-line options. <code>options...</code> are interpreted
19345 before command-line options, so command-line options will override or
19347 an option that is not allowed in the environment is used, such as <code>-p</code> or a
19350 <pre><code class="language-bash">NODE_OPTIONS=<span class="hljs-string">'--require "./my path/file.…
19351 <p>A singleton flag passed as a command-line option will override the same flag
19353 <pre><code class="language-bash"><span class="hljs-comment"># The inspector will be available on po…
19354 NODE_OPTIONS=<span class="hljs-string">'--inspect=localhost:4444'</span> node --inspect=localhost:5…
19356 <code>NODE_OPTIONS</code> instances were passed first, and then its command-line
19358 …e><code class="language-bash">NODE_OPTIONS=<span class="hljs-string">'--require "./a.js"'</span> n…
19359 <span class="hljs-comment"># is equivalent to:</span>
19360 node --require <span class="hljs-string">"./a.js"</span> --require <span class="hljs-string">"./b.j…
19362 <!-- node-options-node start -->
19364 <li><code>--conditions</code>, <code>-C</code></li>
19365 <li><code>--diagnostic-dir</code></li>
19366 <li><code>--disable-proto</code></li>
19367 <li><code>--dns-result-order</code></li>
19368 <li><code>--enable-fips</code></li>
19369 <li><code>--enable-source-maps</code></li>
19370 <li><code>--experimental-abortcontroller</code></li>
19371 <li><code>--experimental-import-meta-resolve</code></li>
19372 <li><code>--experimental-json-modules</code></li>
19373 <li><code>--experimental-loader</code></li>
19374 <li><code>--experimental-modules</code></li>
19375 <li><code>--experimental-policy</code></li>
19376 <li><code>--experimental-repl-await</code></li>
19377 <li><code>--experimental-specifier-resolution</code></li>
19378 <li><code>--experimental-top-level-await</code></li>
19379 <li><code>--experimental-vm-modules</code></li>
19380 <li><code>--experimental-wasi-unstable-preview1</code></li>
19381 <li><code>--experimental-wasm-modules</code></li>
19382 <li><code>--force-context-aware</code></li>
19383 <li><code>--force-fips</code></li>
19384 <li><code>--frozen-intrinsics</code></li>
19385 <li><code>--heapsnapshot-near-heap-limit</code></li>
19386 <li><code>--heapsnapshot-signal</code></li>
19387 <li><code>--http-parser</code></li>
19388 <li><code>--icu-data-dir</code></li>
19389 <li><code>--input-type</code></li>
19390 <li><code>--insecure-http-parser</code></li>
19391 <li><code>--inspect-brk</code></li>
19392 <li><code>--inspect-port</code>, <code>--debug-port</code></li>
19393 <li><code>--inspect-publish-uid</code></li>
19394 <li><code>--inspect</code></li>
19395 <li><code>--max-http-header-size</code></li>
19396 <li><code>--napi-modules</code></li>
19397 <li><code>--no-addons</code></li>
19398 <li><code>--no-deprecation</code></li>
19399 <li><code>--no-force-async-hooks-checks</code></li>
19400 <li><code>--no-warnings</code></li>
19401 <li><code>--node-memory-debug</code></li>
19402 <li><code>--openssl-config</code></li>
19403 <li><code>--pending-deprecation</code></li>
19404 <li><code>--policy-integrity</code></li>
19405 <li><code>--preserve-symlinks-main</code></li>
19406 <li><code>--preserve-symlinks</code></li>
19407 <li><code>--prof-process</code></li>
19408 <li><code>--redirect-warnings</code></li>
19409 <li><code>--report-compact</code></li>
19410 <li><code>--report-dir</code>, <code>--report-directory</code></li>
19411 <li><code>--report-filename</code></li>
19412 <li><code>--report-on-fatalerror</code></li>
19413 <li><code>--report-on-signal</code></li>
19414 <li><code>--report-signal</code></li>
19415 <li><code>--report-uncaught-exception</code></li>
19416 <li><code>--require</code>, <code>-r</code></li>
19417 <li><code>--throw-deprecation</code></li>
19418 <li><code>--title</code></li>
19419 <li><code>--tls-cipher-list</code></li>
19420 <li><code>--tls-keylog</code></li>
19421 <li><code>--tls-max-v1.2</code></li>
19422 <li><code>--tls-max-v1.3</code></li>
19423 <li><code>--tls-min-v1.0</code></li>
19424 <li><code>--tls-min-v1.1</code></li>
19425 <li><code>--tls-min-v1.2</code></li>
19426 <li><code>--tls-min-v1.3</code></li>
19427 <li><code>--trace-atomics-wait</code></li>
19428 <li><code>--trace-deprecation</code></li>
19429 <li><code>--trace-event-categories</code></li>
19430 <li><code>--trace-event-file-pattern</code></li>
19431 <li><code>--trace-events-enabled</code></li>
19432 <li><code>--trace-exit</code></li>
19433 <li><code>--trace-sigint</code></li>
19434 <li><code>--trace-sync-io</code></li>
19435 <li><code>--trace-tls</code></li>
19436 <li><code>--trace-uncaught</code></li>
19437 <li><code>--trace-warnings</code></li>
19438 <li><code>--track-heap-objects</code></li>
19439 <li><code>--unhandled-rejections</code></li>
19440 <li><code>--use-bundled-ca</code></li>
19441 <li><code>--use-largepages</code></li>
19442 <li><code>--use-openssl-ca</code></li>
19443 <li><code>--v8-pool-size</code></li>
19444 <li><code>--zero-fill-buffers</code></li>
19446 <!-- node-options-node end -->
19448 <!-- node-options-v8 start -->
19450 <li><code>--abort-on-uncaught-exception</code></li>
19451 <li><code>--disallow-code-generation-from-strings</code></li>
19452 <li><code>--huge-max-old-generation-size</code></li>
19453 <li><code>--interpreted-frames-native-stack</code></li>
19454 <li><code>--jitless</code></li>
19455 <li><code>--max-old-space-size</code></li>
19456 <li><code>--perf-basic-prof-only-functions</code></li>
19457 <li><code>--perf-basic-prof</code></li>
19458 <li><code>--perf-prof-unwinding-info</code></li>
19459 <li><code>--perf-prof</code></li>
19460 <li><code>--stack-trace-limit</code></li>
19462 <!-- node-options-v8 end -->
19463 <p><code>--perf-basic-prof-only-functions</code>, <code>--perf-basic-prof</code>,
19464 <code>--perf-prof-unwinding-info</code>, and <code>--perf-prof</code> are only available on Linux.<…
19469 <p><code>':'</code>-separated list of directories prefixed to the module search path.</p>
19470 <p>On Windows, this is a <code>';'</code>-separated list instead.</p>
19478 unless either the <code>--pending-deprecation</code> command-line flag, or the
19499 equivalent to using the <code>--redirect-warnings=file</code> command-line flag.</p>
19511 <p>Path to a Node.js module which will be loaded in place of the built-in REPL.
19512 Overriding this value to an empty string (<code>''</code>) will use the built-in REPL.</p>
19525 …gin outputting <a href="https://v8project.blogspot.com/2017/12/javascript-code-coverage.html">V8 J…
19533 …//chromedevtools.github.io/devtools-protocol/tot/Profiler#type-ScriptCoverage">ScriptCoverage</a> …
19535 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
19536 …<span class="hljs-attr">"result"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
19537 <span class="hljs-punctuation">{</span>
19538 …<span class="hljs-attr">"scriptId"</span><span class="hljs-punctuation">:</span> <span class="hljs
19539 …n class="hljs-attr">"url"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
19540 …pan class="hljs-attr">"functions"</span><span class="hljs-punctuation">:</span> <span class="hljs-
19541 <span class="hljs-punctuation">}</span>
19542 <span class="hljs-punctuation">]</span>
19543 <span class="hljs-punctuation">}</span></code></pre>
19545 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
19546 <p>If found, source map data is appended to the top-level key <code>source-map-cache</code>
19548 <p><code>source-map-cache</code> is an object with keys representing the files source maps
19549 were extracted from, and values which include the raw source-map URL
19552 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
19553 …<span class="hljs-attr">"result"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
19554 <span class="hljs-punctuation">{</span>
19555 …<span class="hljs-attr">"scriptId"</span><span class="hljs-punctuation">:</span> <span class="hljs
19556 …ljs-attr">"url"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"file:///a…
19557 …pan class="hljs-attr">"functions"</span><span class="hljs-punctuation">:</span> <span class="hljs-
19558 <span class="hljs-punctuation">}</span>
19559 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
19560 …<span class="hljs-attr">"source-map-cache"</span><span class="hljs-punctuation">:</span> <span cla…
19561 …<span class="hljs-attr">"file:///absolute/path/to/source.js"</span><span class="hljs-punctuation">…
19562 …class="hljs-attr">"url"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">".…
19563 …<span class="hljs-attr">"data"</span><span class="hljs-punctuation">:</span> <span class="hljs-pun…
19564 …<span class="hljs-attr">"version"</span><span class="hljs-punctuation">:</span> <span class="hljs-
19565 …<span class="hljs-attr">"sources"</span><span class="hljs-punctuation">:</span> <span class="hljs-
19566 <span class="hljs-string">"file:///absolute/path/to/original.js"</span>
19567 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
19568 …<span class="hljs-attr">"names"</span><span class="hljs-punctuation">:</span> <span class="hljs-pu…
19569 <span class="hljs-string">"Foo"</span><span class="hljs-punctuation">,</span>
19570 <span class="hljs-string">"console"</span><span class="hljs-punctuation">,</span>
19571 <span class="hljs-string">"info"</span>
19572 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
19573 …ass="hljs-attr">"mappings"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
19574 …<span class="hljs-attr">"sourceRoot"</span><span class="hljs-punctuation">:</span> <span class="hl…
19575 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
19576 …<span class="hljs-attr">"lineLengths"</span><span class="hljs-punctuation">:</span> <span class="h…
19577 <span class="hljs-number">13</span><span class="hljs-punctuation">,</span>
19578 <span class="hljs-number">62</span><span class="hljs-punctuation">,</span>
19579 <span class="hljs-number">38</span><span class="hljs-punctuation">,</span>
19580 <span class="hljs-number">27</span>
19581 <span class="hljs-punctuation">]</span>
19582 <span class="hljs-punctuation">}</span>
19583 <span class="hljs-punctuation">}</span>
19584 <span class="hljs-punctuation">}</span></code></pre>
19586 <p><a href="https://no-color.org"><code>NO_COLOR</code></a> is an alias for <code>NODE_DISABLE_COL…
19593 used to enable FIPS-compliant crypto if Node.js is built with <code>./configure --openssl-fips</cod…
19594 <p>If the <a href="#cli_openssl_config_file"><code>--openssl-config</code></a> command-line option …
19600 <p>If <code>--use-openssl-ca</code> is enabled, this overrides and sets OpenSSL's directory
19609 <p>If <code>--use-openssl-ca</code> is enabled, this overrides and sets OpenSSL's file
19643 <h4><code>--max-old-space-size=SIZE</code> (in megabytes)<span><a class="mark" href="#cli_max_old_s…
19649 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
19653 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
19670 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
19671 <span class="hljs-comment">// Prints: hello world, to stdout</span>
19672 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class=…
19673 <span class="hljs-comment">// Prints: hello world, to stdout</span>
19674-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
19675 <span class="hljs-comment">// Prints error message and stack trace to stderr:</span>
19676 <span class="hljs-comment">// Error: Whoops, something bad happened</span>
19677 <span class="hljs-comment">// at [eval]:5:15</span>
19678 <span class="hljs-comment">// at Script.runInThisContext (node:vm:132:18)</span>
19679 <span class="hljs-comment">// at Object.runInThisContext (node:vm:309:38)</span>
19680 <span class="hljs-comment">// at node:internal/process/execution:77:19</span>
19681 <span class="hljs-comment">// at [eval]-wrapper:6:22</span>
19682 <span class="hljs-comment">// at evalScript (node:internal/process/execution:76:60)</span>
19683 <span class="hljs-comment">// at node:internal/main/eval_string:23:3</span>
19685 <span class="hljs-keyword">const</span> name = <span class="hljs-string">'Will Robinson'</span>;
19686 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span>(<span …
19687 <span class="hljs-comment">// Prints: Danger Will Robinson! Danger!, to stderr</span></code></pre>
19689 <pre><code class="language-js"><span class="hljs-keyword">const</span> out = <span class="hljs-titl…
19690 <span class="hljs-keyword">const</span> err = <span class="hljs-title function_">getStreamSomehow</…
19691 …hljs-keyword">const</span> myConsole = <span class="hljs-keyword">new</span> <span class="hljs-var…
19693 myConsole.<span class="hljs-title function_">log</span>(<span class="hljs-string">'hello world'</sp…
19694 <span class="hljs-comment">// Prints: hello world, to out</span>
19695 myConsole.<span class="hljs-title function_">log</span>(<span class="hljs-string">'hello %s'</span>…
19696 <span class="hljs-comment">// Prints: hello world, to out</span>
19697 …lass="hljs-title function_">error</span>(<span class="hljs-keyword">new</span> <span class="hljs-t…
19698 <span class="hljs-comment">// Prints: [Error: Whoops, something bad happened], to err</span>
19700 <span class="hljs-keyword">const</span> name = <span class="hljs-string">'Will Robinson'</span>;
19701 myConsole.<span class="hljs-title function_">warn</span>(<span class="hljs-string">`Danger <span cl…
19702 <span class="hljs-comment">// Prints: Danger Will Robinson! Danger!, to err</span></code></pre>
19717-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Console</span> } = …
19718 …de class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">C…
19737 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
19741 <li><code>ignoreErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
19743 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
19750 <li><code>inspectOptions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
19752 <li><code>groupIndentation</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
19760 …"language-js"><span class="hljs-keyword">const</span> output = fs.<span class="hljs-title function…
19761 <span class="hljs-keyword">const</span> errorOutput = fs.<span class="hljs-title function_">createW…
19762 <span class="hljs-comment">// Custom simple logger</span>
19763-keyword">const</span> logger = <span class="hljs-keyword">new</span> <span class="hljs-title clas…
19764 <span class="hljs-comment">// use it like console</span>
19765 <span class="hljs-keyword">const</span> count = <span class="hljs-number">5</span>;
19766 logger.<span class="hljs-title function_">log</span>(<span class="hljs-string">'count: %d'</span>, …
19767 <span class="hljs-comment">// In stdout.log: count 5</span></code></pre>
19770-js"><span class="hljs-keyword">new</span> <span class="hljs-title class_">Console</span>({ <span …
19784 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
19785 <li><code>...message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
19787 …a message if <code>value</code> is <a href="https://developer.mozilla.org/en-US/docs/Glossary/Fals…
19791 <p>If <code>value</code> is <a href="https://developer.mozilla.org/en-US/docs/Glossary/Truthy">trut…
19792-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">asser…
19794-variable language_">console</span>.<span class="hljs-title function_">assert</span>(<span class="…
19795 <span class="hljs-comment">// Assertion failed: Whoops didn't work</span>
19797 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">assert</spa…
19798 <span class="hljs-comment">// Assertion failed</span></code></pre>
19815 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
19819 <!-- eslint-skip -->
19820 <pre><code class="language-js">> <span class="hljs-variable language_">console</span>.<span class="…
19821 <span class="hljs-attr">default</span>: <span class="hljs-number">1</span>
19822 <span class="hljs-literal">undefined</span>
19823 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</sp…
19824 <span class="hljs-attr">default</span>: <span class="hljs-number">2</span>
19825 <span class="hljs-literal">undefined</span>
19826 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</sp…
19827 <span class="hljs-attr">abc</span>: <span class="hljs-number">1</span>
19828 <span class="hljs-literal">undefined</span>
19829 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</sp…
19830 <span class="hljs-attr">xyz</span>: <span class="hljs-number">1</span>
19831 <span class="hljs-literal">undefined</span>
19832 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</sp…
19833 <span class="hljs-attr">abc</span>: <span class="hljs-number">2</span>
19834 <span class="hljs-literal">undefined</span>
19835 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</sp…
19836 <span class="hljs-attr">default</span>: <span class="hljs-number">3</span>
19837 <span class="hljs-literal">undefined</span>
19844 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
19847 <!-- eslint-skip -->
19848 …ass="language-js">> <span class="hljs-variable language_">console</span>.<span class="hljs-title f…
19849 <span class="hljs-attr">abc</span>: <span class="hljs-number">1</span>
19850 <span class="hljs-literal">undefined</span>
19851 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">countRese…
19852 <span class="hljs-literal">undefined</span>
19853 > <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">count</sp…
19854 <span class="hljs-attr">abc</span>: <span class="hljs-number">1</span>
19855 <span class="hljs-literal">undefined</span>
19870 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
19871 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
19879 <li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
19880 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
19882 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> …
19884 <li><code>depth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
19887 <li><code>colors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
19908 <li><code>...data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
19917 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
19918 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
19922 values similar to <a href="http://man7.org/linux/man-pages/man3/printf.3.html"><code>printf(3)</cod…
19924 <pre><code class="language-js"><span class="hljs-keyword">const</span> code = <span class="hljs-num…
19925 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span…
19926 <span class="hljs-comment">// Prints: error #5, to stderr</span>
19927 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span…
19928 <span class="hljs-comment">// Prints: error 5, to stderr</span></code></pre>
19937 <li><code>...label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
19959 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
19960 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
19968 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
19969 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
19973 values similar to <a href="http://man7.org/linux/man-pages/man3/printf.3.html"><code>printf(3)</cod…
19975 <pre><code class="language-js"><span class="hljs-keyword">const</span> count = <span class="hljs-nu…
19976 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
19977 <span class="hljs-comment">// Prints: count: 5, to stdout</span>
19978 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
19979 <span class="hljs-comment">// Prints: count: 5, to stdout</span></code></pre>
19986 <li><code>tabularData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
19987 <li><code>properties</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
19992 <pre><code class="language-js"><span class="hljs-comment">// These can't be parsed as tabular data<…
19993 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">table</span…
19994 <span class="hljs-comment">// Symbol()</span>
19996 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">table</span…
19997 <span class="hljs-comment">// undefined</span>
19999-variable language_">console</span>.<span class="hljs-title function_">table</span>([{ <span class…
20000 <span class="hljs-comment">// ┌─────────┬─────┬─────┐</span>
20001 <span class="hljs-comment">// │ (index) │ a │ b │</span>
20002 <span class="hljs-comment">// ├─────────┼─────┼─────┤</span>
20003 <span class="hljs-comment">// │ 0 │ 1 │ 'Y' │</span>
20004 <span class="hljs-comment">// │ 1 │ 'Z' │ 2 │</span>
20005 <span class="hljs-comment">// └─────────┴─────┴─────┘</span>
20007-variable language_">console</span>.<span class="hljs-title function_">table</span>([{ <span class…
20008 <span class="hljs-comment">// ┌─────────┬─────┐</span>
20009 <span class="hljs-comment">// │ (index) │ a │</span>
20010 <span class="hljs-comment">// ├─────────┼─────┤</span>
20011 <span class="hljs-comment">// │ 0 │ 1 │</span>
20012 <span class="hljs-comment">// │ 1 │ 'Z' │</span>
20013 <span class="hljs-comment">// └─────────┴─────┘</span></code></pre>
20019 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20041 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20045 …language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function…
20046 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
20047 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">timeEnd</sp…
20048 <span class="hljs-comment">// prints 100-elements: 225.438ms</span></code></pre>
20054 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20055 <li><code>...data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
20059 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
20060 <span class="hljs-keyword">const</span> value = <span class="hljs-title function_">expensiveProcess…
20061 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">timeLog</sp…
20062 <span class="hljs-comment">// Prints "process: 365.227ms 42".</span>
20063 <span class="hljs-title function_">doExpensiveProcess2</span>(value);
20064 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">timeEnd</sp…
20070 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
20071 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
20075 …lass="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title fu…
20076 <span class="hljs-comment">// Prints: (stack trace will vary based on where trace is called)</span>
20077 <span class="hljs-comment">// Trace: Show me</span>
20078 <span class="hljs-comment">// at repl:2:9</span>
20079 <span class="hljs-comment">// at REPLServer.defaultEval (repl.js:248:27)</span>
20080 <span class="hljs-comment">// at bound (domain.js:287:14)</span>
20081 <span class="hljs-comment">// at REPLServer.runBound [as eval] (domain.js:300:12)</span>
20082 <span class="hljs-comment">// at REPLServer.&#x3C;anonymous> (repl.js:412:12)</span>
20083 <span class="hljs-comment">// at emitOne (events.js:82:20)</span>
20084 <span class="hljs-comment">// at REPLServer.emit (events.js:169:7)</span>
20085 <span class="hljs-comment">// at REPLServer.Interface._onLine (readline.js:210:10)</span>
20086 <span class="hljs-comment">// at REPLServer.Interface._line (readline.js:549:8)</span>
20087 <span class="hljs-comment">// at REPLServer.Interface._ttyWrite (readline.js:826:14)</span></cod…
20093 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
20094 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
20100 (<code>--inspect</code> flag).</p>
20106 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20112 …ass="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title fun…
20113 <span class="hljs-comment">// Some code</span>
20114 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">profileEnd<…
20115 <span class="hljs-comment">// Adds the profile 'MyLabel' to the Profiles panel of the inspector.</s…
20121 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20134 <li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20143 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
20153 system-specific installation processes anymore just to have the package
20165 … do that simply run <a href="https://github.com/nodejs/corepack#corepack-enable--name"><code>corep…
20170 <a href="https://github.com/nodejs/corepack#corepack-disable--name"><code>corepack disable</code></…
20183 be easily overriden by running the <a href="https://github.com/nodejs/corepack#corepack-prepare--na…
20185 <pre><code class="language-bash">corepack prepare yarn@x.y.z --activate</code></pre>
20190 <a href="https://github.com/nodejs/corepack#corepack-prepare--nameversion"><code>corepack prepare</…
20193 <p>The <code>prepare</code> command has <a href="https://github.com/nodejs/corepack#utility-command…
20227 <h4>Running <code>npm install -g yarn</code> doesn't work<span><a class="mark" href="#corepack_runn…
20236 <li>Add the <code>--force</code> flag to <code>npm install</code>; this will tell npm that it's fin…
20239 that happen, run <a href="https://github.com/nodejs/corepack#corepack-enable--name"><code>corepack …
20243 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
20248 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20250 <span class="hljs-keyword">const</span> secret = <span class="hljs-string">'abcdefg'</span>;
20251 <span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHmac…
20252 ….<span class="hljs-title function_">update</span>(<span class="hljs-string">'I love cupcakes'</spa…
20253 … .<span class="hljs-title function_">digest</span>(<span class="hljs-string">'hex'</span>);
20254 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
20255 <span class="hljs-comment">// Prints:</span>
20256 <span class="hljs-comment">// c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e</s…
20261 <pre><code class="language-js"><span class="hljs-keyword">let</span> crypto;
20262 <span class="hljs-keyword">try</span> {
20263 crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>);
20264 } <span class="hljs-keyword">catch</span> (err) {
20265 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
20272 Netscape and was specified formally as part of <a href="https://developer.mozilla.org/en-US/docs/We…
20273 …deprecated since <a href="https://www.w3.org/TR/html52/changes.html#features-removed">HTML 5.2</a>…
20277 …nt. Node.js uses <a href="https://www.openssl.org/docs/man1.1.0/apps/openssl-spkac.html">OpenSSL's…
20283-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20287-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> …
20288 <span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow<…
20289 <span class="hljs-keyword">const</span> challenge = <span class="hljs-title class_">Certificate</sp…
20290-variable language_">console</span>.<span class="hljs-title function_">log</span>(challenge.<span …
20291 <span class="hljs-comment">// Prints: the challenge as a UTF8 string</span></code></pre>
20297-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20298 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20302-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> …
20303 <span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow<…
20304 <span class="hljs-keyword">const</span> publicKey = <span class="hljs-title class_">Certificate</sp…
20305 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
20306 <span class="hljs-comment">// Prints: the public key as &#x3C;Buffer ...></span></code></pre>
20312 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
20313 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
20316-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Certificate</span> …
20317 <span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow<…
20318-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
20319 <span class="hljs-comment">// Prints: true or false</span></code></pre>
20321 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
20327 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20329 <span class="hljs-keyword">const</span> cert1 = <span class="hljs-keyword">new</span> crypto.<span …
20330 <span class="hljs-keyword">const</span> cert2 = crypto.<span class="hljs-title class_">Certificate<…
20336-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20340-js"><span class="hljs-keyword">const</span> cert = <span class="hljs-built_in">require</span>(<sp…
20341 <span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow<…
20342 <span class="hljs-keyword">const</span> challenge = cert.<span class="hljs-title function_">exportC…
20343-variable language_">console</span>.<span class="hljs-title function_">log</span>(challenge.<span …
20344 <span class="hljs-comment">// Prints: the challenge as a UTF8 string</span></code></pre>
20350-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20354-js"><span class="hljs-keyword">const</span> cert = <span class="hljs-built_in">require</span>(<sp…
20355 <span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow<…
20356 <span class="hljs-keyword">const</span> publicKey = cert.<span class="hljs-title function_">exportP…
20357 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
20358 <span class="hljs-comment">// Prints: the public key as &#x3C;Buffer ...></span></code></pre>
20364 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
20365 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
20368-js"><span class="hljs-keyword">const</span> cert = <span class="hljs-built_in">require</span>(<sp…
20369 <span class="hljs-keyword">const</span> spkac = <span class="hljs-title function_">getSpkacSomehow<…
20370-variable language_">console</span>.<span class="hljs-title function_">log</span>(cert.<span class…
20371 <span class="hljs-comment">// Prints: true or false</span></code></pre>
20391 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20393 <span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
20394 <span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to gene…
20395 <span class="hljs-comment">// Key length is dependent on the algorithm. In this case for aes192, it…
20396 <span class="hljs-comment">// 24 bytes (192 bits).</span>
20397 <span class="hljs-comment">// Use async `crypto.scrypt()` instead.</span>
20398-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password,…
20399 <span class="hljs-comment">// Use `crypto.randomBytes()` to generate a random iv instead of the sta…
20400 <span class="hljs-comment">// shown here.</span>
20401-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title …
20403 <span class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCi…
20405 <span class="hljs-keyword">let</span> encrypted = <span class="hljs-string">''</span>;
20406 cipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <s…
20407 <span class="hljs-keyword">let</span> chunk;
20408 …<span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = ciphe…
20409 …encrypted += chunk.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'h…
20412 cipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span c…
20413 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
20414 …<span class="hljs-comment">// Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda…
20417 cipher.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some clear text d…
20418 cipher.<span class="hljs-title function_">end</span>();</code></pre>
20420 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20421 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
20423 <span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
20424 <span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to gene…
20425 <span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
20426-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password,…
20427 <span class="hljs-comment">// Use `crypto.randomBytes()` to generate a random iv instead of the sta…
20428 <span class="hljs-comment">// shown here.</span>
20429-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title …
20431 <span class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCi…
20433 <span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStr…
20434 <span class="hljs-keyword">const</span> output = fs.<span class="hljs-title function_">createWriteS…
20436 input.<span class="hljs-title function_">pipe</span>(cipher).<span class="hljs-title function_">pip…
20438 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20440 <span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
20441 <span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to gene…
20442 <span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
20443-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password,…
20444 <span class="hljs-comment">// Use `crypto.randomBytes` to generate a random iv instead of the stati…
20445 <span class="hljs-comment">// shown here.</span>
20446-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title …
20448 <span class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCi…
20450-keyword">let</span> encrypted = cipher.<span class="hljs-title function_">update</span>(<span cla…
20451 encrypted += cipher.<span class="hljs-title function_">final</span>(<span class="hljs-string">'hex'…
20452 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
20453 <span class="hljs-comment">// Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0…
20459 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20460 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20484 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
20485 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
20487 <li><code>plaintextLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
20504 <li><code>autoPadding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
20512 Disabling automatic padding is useful for non-standard padding, for instance
20529-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20530 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
20531 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20532 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20566 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20568 <span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
20569 <span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to gene…
20570 <span class="hljs-comment">// Key length is dependent on the algorithm. In this case for aes192, it…
20571 <span class="hljs-comment">// 24 bytes (192 bits).</span>
20572 <span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
20573-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password,…
20574 <span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span>
20575-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title …
20577 <span class="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">create…
20579 <span class="hljs-keyword">let</span> decrypted = <span class="hljs-string">''</span>;
20580 decipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, …
20581 …<span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = decip…
20582 …decrypted += chunk.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'u…
20585 decipher.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span…
20586 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
20587 <span class="hljs-comment">// Prints: some clear text data</span>
20590 <span class="hljs-comment">// Encrypted with same algorithm, key and iv.</span>
20591 <span class="hljs-keyword">const</span> encrypted =
20592 …<span class="hljs-string">'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'</span…
20593 decipher.<span class="hljs-title function_">write</span>(encrypted, <span class="hljs-string">'hex'…
20594 decipher.<span class="hljs-title function_">end</span>();</code></pre>
20596 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20597 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
20599 <span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
20600 <span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to gene…
20601 <span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
20602-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password,…
20603 <span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span>
20604-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title …
20606 <span class="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">create…
20608 <span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStr…
20609 <span class="hljs-keyword">const</span> output = fs.<span class="hljs-title function_">createWriteS…
20611 input.<span class="hljs-title function_">pipe</span>(decipher).<span class="hljs-title function_">p…
20613 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20615 <span class="hljs-keyword">const</span> algorithm = <span class="hljs-string">'aes-192-cbc'</span>;
20616 <span class="hljs-keyword">const</span> password = <span class="hljs-string">'Password used to gene…
20617 <span class="hljs-comment">// Use the async `crypto.scrypt()` instead.</span>
20618-keyword">const</span> key = crypto.<span class="hljs-title function_">scryptSync</span>(password,…
20619 <span class="hljs-comment">// The IV is usually passed along with the ciphertext.</span>
20620-keyword">const</span> iv = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title …
20622 <span class="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">create…
20624 <span class="hljs-comment">// Encrypted using same algorithm, key and iv.</span>
20625 <span class="hljs-keyword">const</span> encrypted =
20626 …<span class="hljs-string">'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'</span…
20627-keyword">let</span> decrypted = decipher.<span class="hljs-title function_">update</span>(encrypt…
20628 decrypted += decipher.<span class="hljs-title function_">final</span>(<span class="hljs-string">'ut…
20629 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
20630 <span class="hljs-comment">// Prints: some clear text data</span></code></pre>
20636 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20637 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20657 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
20658 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
20660 <li><code>plaintextLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
20687 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
20695 …s://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf">NIST SP 800-38D</a> or …
20705 <li><code>autoPadding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
20728-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20729 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
20730 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20731 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20749 <p>The <code>DiffieHellman</code> class is a utility for creating Diffie-Hellman key
20753 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20754 <span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span c…
20756 <span class="hljs-comment">// Generate Alice's keys...</span>
20757 <span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">createDif…
20758 <span class="hljs-keyword">const</span> aliceKey = alice.<span class="hljs-title function_">generat…
20760 <span class="hljs-comment">// Generate Bob's keys...</span>
20761-keyword">const</span> bob = crypto.<span class="hljs-title function_">createDiffieHellman</span>(…
20762 <span class="hljs-keyword">const</span> bobKey = bob.<span class="hljs-title function_">generateKey…
20764 <span class="hljs-comment">// Exchange and generate the secret...</span>
20765 <span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">comp…
20766 <span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeS…
20768 <span class="hljs-comment">// OK</span>
20769-title function_">strictEqual</span>(aliceSecret.<span class="hljs-title function_">toString</span…
20775-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20776 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
20777 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20778 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20794 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20795 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20797 <p>Generates private and public Diffie-Hellman key values, and returns
20807 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20808 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20810 <p>Returns the Diffie-Hellman generator in the specified <code>encoding</code>.
20818 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20819 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20821 <p>Returns the Diffie-Hellman prime in the specified <code>encoding</code>.
20829 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20830 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20832 <p>Returns the Diffie-Hellman private key in the specified <code>encoding</code>.
20840 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20841 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20843 <p>Returns the Diffie-Hellman public key in the specified <code>encoding</code>.
20851-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20852 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20854 <p>Sets the Diffie-Hellman private key. If the <code>encoding</code> argument is provided,
20863-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20864 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
20866 <p>Sets the Diffie-Hellman public key. If the <code>encoding</code> argument is provided,
20888 <p>The <code>DiffieHellmanGroup</code> class takes a well-known modp group as its argument.
20892 <pre><code class="language-js"><span class="hljs-keyword">const</span> name = <span class="hljs-str…
20893 <span class="hljs-keyword">const</span> dh = crypto.<span class="hljs-title function_">createDiffie…
20894 …aken from <a href="https://www.rfc-editor.org/rfc/rfc2412.txt">RFC 2412</a> (modp1 and 2) and <a h…
20895 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">pe…
20908 <p>The <code>ECDH</code> class is a utility for creating Elliptic Curve Diffie-Hellman (ECDH)
20912 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
20913 <span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span c…
20915 <span class="hljs-comment">// Generate Alice's keys...</span>
20916 <span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">createECD…
20917 <span class="hljs-keyword">const</span> aliceKey = alice.<span class="hljs-title function_">generat…
20919 <span class="hljs-comment">// Generate Bob's keys...</span>
20920 <span class="hljs-keyword">const</span> bob = crypto.<span class="hljs-title function_">createECDH<…
20921 <span class="hljs-keyword">const</span> bobKey = bob.<span class="hljs-title function_">generateKey…
20923 <span class="hljs-comment">// Exchange and generate the secret...</span>
20924 <span class="hljs-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">comp…
20925 <span class="hljs-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeS…
20927-title function_">strictEqual</span>(aliceSecret.<span class="hljs-title function_">toString</span…
20928 <span class="hljs-comment">// OK</span></code></pre>
20934-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20935 <li><code>curve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
20936 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
20937 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20938 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
20939 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
20941 <p>Converts the EC Diffie-Hellman public key specified by <code>key</code> and <code>curve</code> t…
20947 On recent OpenSSL releases, <code>openssl ecparam -list_curves</code> will also display
20954-js"><span class="hljs-keyword">const</span> { createECDH, <span class="hljs-variable constant_">E…
20956 <span class="hljs-keyword">const</span> ecdh = <span class="hljs-title function_">createECDH</span>…
20957 ecdh.<span class="hljs-title function_">generateKeys</span>();
20959-keyword">const</span> compressedKey = ecdh.<span class="hljs-title function_">getPublicKey</span>…
20961 <span class="hljs-keyword">const</span> uncompressedKey = <span class="hljs-variable constant_">ECD…
20962 <span class="hljs-string">'secp256k1'</span>,
20963 <span class="hljs-string">'hex'</span>,
20964 <span class="hljs-string">'hex'</span>,
20965 <span class="hljs-string">'uncompressed'</span>);
20967 <span class="hljs-comment">// The converted key and the uncompressed public key should be the same<…
20968-variable language_">console</span>.<span class="hljs-title function_">log</span>(uncompressedKey …
20984-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
20985 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
20986 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
20987 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
21008 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21009 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
21010 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
21012 <p>Generates private and public EC Diffie-Hellman key values, and returns
21025 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21026 …://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C…
21035 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21036 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
21037 …://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C…
21050-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21051 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21053 <p>Sets the EC Diffie-Hellman private key.
21064 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
21066-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21067 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21069 <p>Sets the EC Diffie-Hellman public key.
21079 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21080 <span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">createECD…
21081 <span class="hljs-keyword">const</span> bob = crypto.<span class="hljs-title function_">createECDH<…
21083 <span class="hljs-comment">// This is a shortcut way of specifying one of Alice's previous private<…
21084 <span class="hljs-comment">// keys. It would be unwise to use such a predictable private key in a r…
21085 <span class="hljs-comment">// application.</span>
21086 alice.<span class="hljs-title function_">setPrivateKey</span>(
21087-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>).<span class="hljs-t…
21090 <span class="hljs-comment">// Bob uses a newly generated cryptographically strong</span>
21091 <span class="hljs-comment">// pseudorandom key pair</span>
21092 bob.<span class="hljs-title function_">generateKeys</span>();
21094-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>…
21095-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(ali…
21097 <span class="hljs-comment">// aliceSecret and bobSecret should be the same shared secret value</spa…
21098 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
21117 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21118 <span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash…
21120 hash.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <spa…
21121 <span class="hljs-comment">// Only one element is going to be produced by the</span>
21122 <span class="hljs-comment">// hash stream.</span>
21123 …<span class="hljs-keyword">const</span> data = hash.<span class="hljs-title function_">read</span>…
21124 <span class="hljs-keyword">if</span> (data) {
21125-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span class…
21126 <span class="hljs-comment">// Prints:</span>
21127 …<span class="hljs-comment">// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50</…
21131 hash.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to hash'<…
21132 hash.<span class="hljs-title function_">end</span>();</code></pre>
21134 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21135 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
21136 <span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash…
21138 <span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStr…
21139-title function_">pipe</span>(hash).<span class="hljs-title function_">setEncoding</span>(<span cl…
21141 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21142 <span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash…
21144 hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to hash'…
21145-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash.<span class…
21146 <span class="hljs-comment">// Prints:</span>
21147 <span class="hljs-comment">// 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50</s…
21153 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
21163 <pre><code class="language-js"><span class="hljs-comment">// Calculate a rolling hash.</span>
21164 <span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span c…
21165 <span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash…
21167 hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'one'</span>);
21168-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash.<span class…
21170 hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'two'</span>);
21171-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash.<span class…
21173 hash.<span class="hljs-title function_">update</span>(<span class="hljs-string">'three'</span>);
21174-variable language_">console</span>.<span class="hljs-title function_">log</span>(hash.<span class…
21176 <span class="hljs-comment">// Etc.</span></code></pre>
21182 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21183 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
21204-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21205 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
21231 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21232 …s-keyword">const</span> hmac = crypto.<span class="hljs-title function_">createHmac</span>(<span c…
21234 hmac.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <spa…
21235 <span class="hljs-comment">// Only one element is going to be produced by the</span>
21236 <span class="hljs-comment">// hash stream.</span>
21237 …<span class="hljs-keyword">const</span> data = hmac.<span class="hljs-title function_">read</span>…
21238 <span class="hljs-keyword">if</span> (data) {
21239-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span class…
21240 <span class="hljs-comment">// Prints:</span>
21241 …<span class="hljs-comment">// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e</…
21245 hmac.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to hash'<…
21246 hmac.<span class="hljs-title function_">end</span>();</code></pre>
21248 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21249 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
21250 …s-keyword">const</span> hmac = crypto.<span class="hljs-title function_">createHmac</span>(<span c…
21252 <span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStr…
21253 …nput.<span class="hljs-title function_">pipe</span>(hmac).<span class="hljs-title function_">pipe<…
21255 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21256 …s-keyword">const</span> hmac = crypto.<span class="hljs-title function_">createHmac</span>(<span c…
21258 hmac.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to hash'…
21259-variable language_">console</span>.<span class="hljs-title function_">log</span>(hmac.<span class…
21260 <span class="hljs-comment">// Prints:</span>
21261 <span class="hljs-comment">// 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e</s…
21267 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
21268 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
21288-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21289 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
21330 <td><p>Added support for <code>'rsa-pss'</code>.</p></td></tr>
21343 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
21349 <li><code>'rsa-pss'</code> (OID 1.2.840.113549.1.1.10)</li>
21365 <li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
21366 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
21374 <li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
21375 <li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
21379 <li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
21381 <li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
21382 <li><code>cipher</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
21385 <li><code>passphrase</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
21396 encryption mechanism, PEM-level encryption is not supported when encrypting
21397 …. See <a href="https://www.rfc-editor.org/rfc/rfc5208.txt">RFC 5208</a> for PKCS#8 encryption and …
21404 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
21413 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
21437 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21439 …s="hljs-keyword">const</span> { privateKey, publicKey } = crypto.<span class="hljs-title function_…
21440 <span class="hljs-attr">namedCurve</span>: <span class="hljs-string">'sect239k1'</span>
21443 <span class="hljs-keyword">const</span> sign = crypto.<span class="hljs-title function_">createSign…
21444 sign.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to sign'<…
21445 sign.<span class="hljs-title function_">end</span>();
21446 <span class="hljs-keyword">const</span> signature = sign.<span class="hljs-title function_">sign</s…
21448 <span class="hljs-keyword">const</span> verify = crypto.<span class="hljs-title function_">createVe…
21449 verify.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data to sign…
21450 verify.<span class="hljs-title function_">end</span>();
21451-variable language_">console</span>.<span class="hljs-title function_">log</span>(verify.<span cla…
21452 <span class="hljs-comment">// Prints: true</span></code></pre>
21454 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
21456 …s="hljs-keyword">const</span> { privateKey, publicKey } = crypto.<span class="hljs-title function_…
21457 <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">2048</span>,
21460 <span class="hljs-keyword">const</span> sign = crypto.<span class="hljs-title function_">createSign…
21461 sign.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to sign'…
21462 sign.<span class="hljs-title function_">end</span>();
21463 <span class="hljs-keyword">const</span> signature = sign.<span class="hljs-title function_">sign</s…
21465 <span class="hljs-keyword">const</span> verify = crypto.<span class="hljs-title function_">createVe…
21466 verify.<span class="hljs-title function_">update</span>(<span class="hljs-string">'some data to sig…
21467 verify.<span class="hljs-title function_">end</span>();
21468 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
21469 <span class="hljs-comment">// Prints: true</span></code></pre>
21476 <td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr>
21478 <td><p>This function now supports RSA-PSS keys.</p></td></tr>
21482 <td><p>Support for RSASSA-PSS and additional options was added.</p></td></tr>
21489 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
21491 <li><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
21492 <li><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
21493 <li><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
21496 <li><code>outputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
21497 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
21506 <p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
21509 <li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code…
21510 <li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li>
21514 <p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
21520 used to sign the message as specified in section 3.1 of <a href="https://www.rfc-editor.org/rfc/rfc…
21522 section 3.3 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>.</p>
21525 <p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21549-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21550 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
21589-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21590 <li><code>inputEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
21604 <td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr>
21606 <td><p>This function now supports RSA-PSS keys.</p></td></tr>
21610 <td><p>Support for RSASSA-PSS and additional options was added.</p></td></tr>
21617 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
21619 <li><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
21620 <li><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
21621 <li><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
21624-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21625 <li><code>signatureEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
21626 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
21635 <p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
21638 <li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code…
21639 <li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li>
21643 <p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
21649 used to verify the message as specified in section 3.1 of <a href="https://www.rfc-editor.org/rfc/r…
21651 section 3.3 of <a href="https://www.rfc-editor.org/rfc/rfc4055.txt">RFC 4055</a>.</p>
21654 <p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21677 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
21685 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
21697 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
21718 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
21720 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21721-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21722 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
21728 cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the
21734 recent OpenSSL releases, <code>openssl list -cipher-algorithms</code>
21735 (<code>openssl list-cipher-algorithms</code> for older versions of OpenSSL) will
21744 non-cryptographically secure hash algorithm allow passwords to be tested very
21752 …IV is reused in GCM, see <a href="https://github.com/nonce-disrespect/nonce-disrespect">Nonce-Disr…
21762 <td><p>The cipher <code>chacha20-poly1305</code> is now supported.</p></td></tr>
21775 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21776-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21777-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21778 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
21784 cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the
21790 recent OpenSSL releases, <code>openssl list -cipher-algorithms</code>
21791 (<code>openssl list-cipher-algorithms</code> for older versions of OpenSSL) will
21818 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
21820 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21821-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21822 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
21828 cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the
21835 non-cryptographically secure hash algorithm allow passwords to be tested very
21849 <td><p>The cipher <code>chacha20-poly1305</code> is now supported.</p></td></tr>
21862 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21863-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21864-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21865 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
21871 cipher in CCM or OCB mode is used (e.g. <code>'aes-128-ccm'</code>). In that case, the
21877 recent OpenSSL releases, <code>openssl list -cipher-algorithms</code>
21878 (<code>openssl list-cipher-algorithms</code> for older versions of OpenSSL) will
21908-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
21909 <li><code>primeEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
21910-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
21912 <li><code>generatorEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
21928 <li><code>primeLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
21929 <li><code>generator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21940 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
21949 <li><code>curveName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21952 <p>Creates an Elliptic Curve Diffie-Hellman (<code>ECDH</code>) key exchange object using a
21955 OpenSSL releases, <code>openssl ecparam -list_curves</code> will also display the name
21970 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
21971 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
21980 On recent releases of OpenSSL, <code>openssl list -digest-algorithms</code>
21981 (<code>openssl list-message-digest-algorithms</code> for older versions of OpenSSL) will
21984 …class="language-js"><span class="hljs-keyword">const</span> filename = process.<span class="hljs-p…
21985 <span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span c…
21986 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
21988 <span class="hljs-keyword">const</span> hash = crypto.<span class="hljs-title function_">createHash…
21990 <span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStr…
21991 input.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <sp…
21992 <span class="hljs-comment">// Only one element is going to be produced by the</span>
21993 <span class="hljs-comment">// hash stream.</span>
21994 …<span class="hljs-keyword">const</span> data = input.<span class="hljs-title function_">read</span…
21995 <span class="hljs-keyword">if</span> (data)
21996 hash.<span class="hljs-title function_">update</span>(data);
21997 <span class="hljs-keyword">else</span> {
21998-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22014 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22015-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22016 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22023 On recent releases of OpenSSL, <code>openssl list -digest-algorithms</code>
22024 (<code>openssl list-message-digest-algorithms</code> for older versions of OpenSSL) will
22029 …class="language-js"><span class="hljs-keyword">const</span> filename = process.<span class="hljs-p…
22030 <span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span c…
22031 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
22033 …s-keyword">const</span> hmac = crypto.<span class="hljs-title function_">createHmac</span>(<span c…
22035 <span class="hljs-keyword">const</span> input = fs.<span class="hljs-title function_">createReadStr…
22036 input.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <sp…
22037 <span class="hljs-comment">// Only one element is going to be produced by the</span>
22038 <span class="hljs-comment">// hash stream.</span>
22039 …<span class="hljs-keyword">const</span> data = input.<span class="hljs-title function_">read</span…
22040 <span class="hljs-keyword">if</span> (data)
22041 hmac.<span class="hljs-title function_">update</span>(data);
22042 <span class="hljs-keyword">else</span> {
22043-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22051 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
22053 <li><code>key</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
22054 <li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
22055 <li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
22057 <li><code>passphrase</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
22082 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
22084 <li><code>key</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
22085 <li><code>format</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
22086 <li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
22109 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22119 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22120 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22127 algorithm, such as <code>'RSA-SHA256'</code>, instead of a digest algorithm. This will use
22129 algorithms, such as <code>'ecdsa-with-SHA256'</code>, so it is best to always use digest
22136 <li><code>algorithm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22137 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22145 algorithm, such as <code>'RSA-SHA256'</code>, instead of a digest algorithm. This will use
22147 algorithms, such as <code>'ecdsa-with-SHA256'</code>, so it is best to always use digest
22154 <li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22162 <p>Computes the Diffie-Hellman secret based on a <code>privateKey</code> and a <code>publicKey</cod…
22164 (for Diffie-Hellman), <code>'ec'</code> (for ECDH), <code>'x448'</code>, or <code>'x25519'</code> (…
22171 <td><p>Add support for Diffie-Hellman.</p></td></tr>
22184 <li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
22186 <li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22188 <li><code>modulusLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
22189 <li><code>publicExponent</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
22190 <li><code>divisorLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
22191 <li><code>namedCurve</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
22193 <li><code>primeLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
22194 <li><code>generator</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22195 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&…
22197 <li><code>publicKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
22198 <li><code>privateKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
22201 <li><code>callback</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
22203 <li><code>err</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
22204 <li><code>publicKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22205 <li><code>privateKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
22215 <code>'pkcs8'</code> with encryption for long-term storage:</p>
22216 …ss="language-js"><span class="hljs-keyword">const</span> { generateKeyPair } = <span class="hljs-b…
22217 <span class="hljs-title function_">generateKeyPair</span>(<span class="hljs-string">'rsa'</span>, {
22218 <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">4096</span>,
22219 <span class="hljs-attr">publicKeyEncoding</span>: {
22220 <span class="hljs-attr">type</span>: <span class="hljs-string">'spki'</span>,
22221 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>
22223 <span class="hljs-attr">privateKeyEncoding</span>: {
22224 <span class="hljs-attr">type</span>: <span class="hljs-string">'pkcs8'</span>,
22225 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>,
22226 <span class="hljs-attr">cipher</span>: <span class="hljs-string">'aes-256-cbc'</span>,
22227 <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'top secret'</span>
22229 }, <span class="hljs-function">(<span class="hljs-params">err, publicKey, privateKey</span>) =></sp…
22230 <span class="hljs-comment">// Handle errors and use the generated key pair.</span>
22242 <td><p>Add support for Diffie-Hellman.</p></td></tr>
22253 <li><code>type</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
22255 <li><code>options</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22257 <li><code>modulusLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
22258 <li><code>publicExponent</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
22259 <li><code>divisorLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
22260 <li><code>namedCurve</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
22262 <li><code>primeLength</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
22263 <li><code>generator</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22264 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&…
22266 <li><code>publicKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
22267 <li><code>privateKeyEncoding</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
22270 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
22272 <li><code>publicKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22273 <li><code>privateKey</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
22285 …="language-js"><span class="hljs-keyword">const</span> { generateKeyPairSync } = <span class="hljs
22286 …class="hljs-keyword">const</span> { publicKey, privateKey } = <span class="hljs-title function_">g…
22287 <span class="hljs-attr">modulusLength</span>: <span class="hljs-number">4096</span>,
22288 <span class="hljs-attr">publicKeyEncoding</span>: {
22289 <span class="hljs-attr">type</span>: <span class="hljs-string">'spki'</span>,
22290 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>
22292 <span class="hljs-attr">privateKeyEncoding</span>: {
22293 <span class="hljs-attr">type</span>: <span class="hljs-string">'pkcs8'</span>,
22294 <span class="hljs-attr">format</span>: <span class="hljs-string">'pem'</span>,
22295 <span class="hljs-attr">cipher</span>: <span class="hljs-string">'aes-256-cbc'</span>,
22296 <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'top secret'</span>
22307 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
22310 <pre><code class="language-js"><span class="hljs-keyword">const</span> ciphers = crypto.<span class…
22311 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(ciphers); <sp…
22317 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
22319 <pre><code class="language-js"><span class="hljs-keyword">const</span> curves = crypto.<span class=…
22320-variable language_">console</span>.<span class="hljs-title function_">log</span>(curves); <span c…
22326 <li><code>groupName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22331 <a href="https://www.rfc-editor.org/rfc/rfc2412.txt">RFC 2412</a>, but see <a href="#crypto_support…
22332 …>'modp17'</code>, <code>'modp18'</code> (defined in <a href="https://www.rfc-editor.org/rfc/rfc352…
22340 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22341 <span class="hljs-keyword">const</span> alice = crypto.<span class="hljs-title function_">getDiffie…
22342 <span class="hljs-keyword">const</span> bob = crypto.<span class="hljs-title function_">getDiffieHe…
22344 alice.<span class="hljs-title function_">generateKeys</span>();
22345 bob.<span class="hljs-title function_">generateKeys</span>();
22347-keyword">const</span> aliceSecret = alice.<span class="hljs-title function_">computeSecret</span>…
22348-keyword">const</span> bobSecret = bob.<span class="hljs-title function_">computeSecret</span>(ali…
22350 <span class="hljs-comment">/* aliceSecret and bobSecret should be the same */</span>
22351 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
22357 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
22358 currently in use, <code>0</code> otherwise. A future semver-major release may change
22359 the return type of this API to a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
22366 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
22367 such as <code>'RSA-SHA256'</code>. Hash algorithms are also called "digest" algorithms.</li>
22369 <pre><code class="language-js"><span class="hljs-keyword">const</span> hashes = crypto.<span class=…
22370 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(hashes); <sp…
22390-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22391-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22392 <li><code>iterations</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22393 <li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22394 <li><code>digest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22395 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22397 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
22402 <p>Provides an asynchronous Password-Based Key Derivation Function 2 (PBKDF2)
22417 …s://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST SP 800-132</a> for…
22418 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22419-title function_">pbkdf2</span>(<span class="hljs-string">'secret'</span>, <span class="hljs-strin…
22420 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22421-variable language_">console</span>.<span class="hljs-title function_">log</span>(derivedKey.<span…
22426 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22427 crypto.<span class="hljs-property">DEFAULT_ENCODING</span> = <span class="hljs-string">'hex'</span>;
22428-title function_">pbkdf2</span>(<span class="hljs-string">'secret'</span>, <span class="hljs-strin…
22429 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22430 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
22454-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22455-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22456 <li><code>iterations</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22457 <li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22458 <li><code>digest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22461 <p>Provides a synchronous Password-Based Key Derivation Function 2 (PBKDF2)
22473 …s://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST SP 800-132</a> for…
22474 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22475-keyword">const</span> key = crypto.<span class="hljs-title function_">pbkdf2Sync</span>(<span cla…
22476-variable language_">console</span>.<span class="hljs-title function_">log</span>(key.<span class=…
22480 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22481 crypto.<span class="hljs-property">DEFAULT_ENCODING</span> = <span class="hljs-string">'hex'</span>;
22482-keyword">const</span> key = crypto.<span class="hljs-title function_">pbkdf2Sync</span>(<span cla…
22483 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
22503 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
22505 <li><code>oaepHash</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
22507 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22515 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22537 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
22539 <li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
22540 <li><code>passphrase</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22546 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22568 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
22570 <li><code>passphrase</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22576 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22604 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
22606 <li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
22607 <li><code>oaepHash</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
22609 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22611 <li><code>passphrase</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
22618 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22643 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
22644 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22646 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
22652 <p>Generates cryptographically strong pseudo-random data. The <code>size</code> argument
22658 <pre><code class="language-js"><span class="hljs-comment">// Asynchronous</span>
22659 <span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span c…
22660 …pan class="hljs-title function_">randomBytes</span>(<span class="hljs-number">256</span>, <span cl…
22661 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22662-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22667 <pre><code class="language-js"><span class="hljs-comment">// Synchronous</span>
22668 <span class="hljs-keyword">const</span> buf = crypto.<span class="hljs-title function_">randomBytes…
22669 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
22670 …ss="hljs-string">`<span class="hljs-subst">${buf.length}</span> bytes of random data: <span class=…
22679 <p>The asynchronous version of <code>crypto.randomBytes()</code> is carried out in a single
22696 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22697 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22698 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <st…
22699 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22702-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
22703-variable language_">console</span>.<span class="hljs-title function_">log</span>(crypto.<span cla…
22705 crypto.<span class="hljs-title function_">randomFillSync</span>(buf, <span class="hljs-number">5</s…
22706-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
22708 <span class="hljs-comment">// The above is equivalent to the following:</span>
22709 crypto.<span class="hljs-title function_">randomFillSync</span>(buf, <span class="hljs-number">5</s…
22710-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
22712-js"><span class="hljs-keyword">const</span> a = <span class="hljs-keyword">new</span> <span class…
22713-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22714 …js-property">byteOffset</span>, a.<span class="hljs-property">byteLength</span>).<span class="hljs
22716-keyword">const</span> b = <span class="hljs-keyword">new</span> <span class="hljs-title class_">F…
22717-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22718 …js-property">byteOffset</span>, b.<span class="hljs-property">byteLength</span>).<span class="hljs
22720-keyword">const</span> c = <span class="hljs-keyword">new</span> <span class="hljs-title class_">D…
22721-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22722 …js-property">byteOffset</span>, c.<span class="hljs-property">byteLength</span>).<span class="hljs
22736 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22737 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22738 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> <st…
22739 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22745-js"><span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<…
22746 crypto.<span class="hljs-title function_">randomFill</span>(buf, <span class="hljs-function">(<span…
22747 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22748-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
22751 …an class="hljs-title function_">randomFill</span>(buf, <span class="hljs-number">5</span>, <span c…
22752 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22753-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
22756 <span class="hljs-comment">// The above is equivalent to the following:</span>
22757-title function_">randomFill</span>(buf, <span class="hljs-number">5</span>, <span class="hljs-num…
22758 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22759-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
22762-js"><span class="hljs-keyword">const</span> a = <span class="hljs-keyword">new</span> <span class…
22763 crypto.<span class="hljs-title function_">randomFill</span>(a, <span class="hljs-function">(<span c…
22764 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22765-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22766 .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
22769-keyword">const</span> b = <span class="hljs-keyword">new</span> <span class="hljs-title class_">F…
22770 crypto.<span class="hljs-title function_">randomFill</span>(b, <span class="hljs-function">(<span c…
22771 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22772-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22773 .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
22776-keyword">const</span> c = <span class="hljs-keyword">new</span> <span class="hljs-title class_">D…
22777 crypto.<span class="hljs-title function_">randomFill</span>(c, <span class="hljs-function">(<span c…
22778 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22779-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22780 .<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
22785 <p>The asynchronous version of <code>crypto.randomFill()</code> is carried out in a single
22794 <li><code>min</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
22795 <li><code>max</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
22796 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22800 <p>The range (<code>max - min</code>) must be less than 2<sup>48</sup>. <code>min</code> and <code>…
22801 be <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number…
22804 <pre><code class="language-js"><span class="hljs-comment">// Asynchronous</span>
22805 …<span class="hljs-title function_">randomInt</span>(<span class="hljs-number">3</span>, <span clas…
22806 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22807-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22809 <pre><code class="language-js"><span class="hljs-comment">// Synchronous</span>
22810 <span class="hljs-keyword">const</span> n = crypto.<span class="hljs-title function_">randomInt</sp…
22811-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
22812 <pre><code class="language-js"><span class="hljs-comment">// With `min` argument</span>
22813 …="hljs-keyword">const</span> n = crypto.<span class="hljs-title function_">randomInt</span>(<span …
22814 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class=…
22820 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22822 <li><code>disableEntropyCache</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
22828 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
22830 <p>Generates a random <a href="https://www.rfc-editor.org/rfc/rfc4122.txt">RFC 4122</a> version 4 U…
22847-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22848-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22849 <li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22850 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22852 <li><code>cost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
22854 <li><code>blockSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22855 <li><code>parallelization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
22856 <li><code>N</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
22857 <li><code>r</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
22858 <li><code>p</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
22859 <li><code>maxmem</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22863 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
22865 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
22870 …a href="https://en.wikipedia.org/wiki/Scrypt">scrypt</a> implementation. Scrypt is a password-based
22872 memory-wise in order to make brute-force attacks unrewarding.</p>
22874 …s://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST SP 800-132</a> for…
22880 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22881 <span class="hljs-comment">// Using the factory defaults.</span>
22882-title function_">scrypt</span>(<span class="hljs-string">'password'</span>, <span class="hljs-str…
22883 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22884-variable language_">console</span>.<span class="hljs-title function_">log</span>(derivedKey.<span…
22886 <span class="hljs-comment">// Using a custom N parameter. Must be a power of two.</span>
22887-title function_">scrypt</span>(<span class="hljs-string">'password'</span>, <span class="hljs-str…
22888 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
22889-variable language_">console</span>.<span class="hljs-title function_">log</span>(derivedKey.<span…
22906-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22907-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
22908 <li><code>keylen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22909 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
22911 <li><code>cost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
22913 <li><code>blockSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
22914 <li><code>parallelization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
22915 <li><code>N</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
22916 <li><code>r</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
22917 <li><code>p</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
22918 <li><code>maxmem</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22924 …a href="https://en.wikipedia.org/wiki/Scrypt">scrypt</a> implementation. Scrypt is a password-based
22926 memory-wise in order to make brute-force attacks unrewarding.</p>
22928 …s://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST SP 800-132</a> for…
22933 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
22934 <span class="hljs-comment">// Using the factory defaults.</span>
22935-keyword">const</span> key1 = crypto.<span class="hljs-title function_">scryptSync</span>(<span cl…
22936-variable language_">console</span>.<span class="hljs-title function_">log</span>(key1.<span class…
22937 <span class="hljs-comment">// Using a custom N parameter. Must be a power of two.</span>
22938-keyword">const</span> key2 = crypto.<span class="hljs-title function_">scryptSync</span>(<span cl…
22939-variable language_">console</span>.<span class="hljs-title function_">log</span>(key2.<span class…
22945 <li><code>engine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
22966 <p>The flags below are deprecated in OpenSSL-1.1.0.</p>
22977 <li><code>bool</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
22979 <p>Enables the FIPS compliant crypto provider in a FIPS-enabled Node.js build.
22987 <td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr>
22994-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
22995 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
22996 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
23007 <p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
23010 <li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code…
23011 <li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li>
23015 <p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
23021 used to sign the message as specified in section 3.1 of <a href="https://www.rfc-editor.org/rfc/rfc…
23024 <p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
23036 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
23037 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
23038 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
23040 <p>This function is based on a constant-time algorithm.
23044 <a href="https://www.w3.org/TR/capability-urls/">capability urls</a>.</p>
23051 is timing-safe. Care should be taken to ensure that the surrounding code does
23059 <td><p>This function now supports IEEE-P1363 DSA and ECDSA signatures.</p></td></tr>
23066-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
23067 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
23068 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
23069 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
23070 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
23080 <p><code>dsaEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
23083 <li><code>'der'</code> (default): DER-encoded ASN.1 signature structure encoding <code>(r, s)</code…
23084 <li><code>'ieee-p1363'</code>: Signature format <code>r || s</code> as proposed in IEEE-P1363.</li>
23088 <p><code>padding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
23094 used to sign the message as specified in section 3.1 of <a href="https://www.rfc-editor.org/rfc/rfc…
23097 <p><code>saltLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
23118 <p>Usage of <code>ECDH</code> with non-dynamically generated key pairs has been simplified.
23136 …ttps://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar1.pdf">NIST SP 800-131A</a>:…
23138 <li>MD5 and SHA-1 are no longer acceptable where collision resistance is
23156 <li>The length of the plaintext is limited to <code>2 ** (8 * (15 - N))</code> bytes.</li>
23160 compliance with section 2.6 of <a href="https://www.rfc-editor.org/rfc/rfc3610.txt">RFC 3610</a>.</…
23177 …ode class="language-js"><span class="hljs-keyword">const</span> crypto = <span class="hljs-built_i…
23179 <span class="hljs-keyword">const</span> key = <span class="hljs-string">'keykeykeykeykeykeykeykey'<…
23180 <span class="hljs-keyword">const</span> nonce = crypto.<span class="hljs-title function_">randomByt…
23182-keyword">const</span> aad = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title…
23184 …class="hljs-keyword">const</span> cipher = crypto.<span class="hljs-title function_">createCipheri…
23185 <span class="hljs-attr">authTagLength</span>: <span class="hljs-number">16</span>
23187 <span class="hljs-keyword">const</span> plaintext = <span class="hljs-string">'Hello world'</span>;
23188 cipher.<span class="hljs-title function_">setAAD</span>(aad, {
23189 …<span class="hljs-attr">plaintextLength</span>: <span class="hljs-title class_">Buffer</span>.<spa…
23191 …span class="hljs-keyword">const</span> ciphertext = cipher.<span class="hljs-title function_">upda…
23192 cipher.<span class="hljs-title function_">final</span>();
23193 <span class="hljs-keyword">const</span> tag = cipher.<span class="hljs-title function_">getAuthTag<…
23195 <span class="hljs-comment">// Now transmit { ciphertext, nonce, tag }.</span>
23197 …ass="hljs-keyword">const</span> decipher = crypto.<span class="hljs-title function_">createDeciphe…
23198 <span class="hljs-attr">authTagLength</span>: <span class="hljs-number">16</span>
23200 decipher.<span class="hljs-title function_">setAuthTag</span>(tag);
23201 decipher.<span class="hljs-title function_">setAAD</span>(aad, {
23202 …<span class="hljs-attr">plaintextLength</span>: ciphertext.<span class="hljs-property">length</spa…
23204-keyword">const</span> receivedPlaintext = decipher.<span class="hljs-title function_">update</spa…
23206 <span class="hljs-keyword">try</span> {
23207 decipher.<span class="hljs-title function_">final</span>();
23208 } <span class="hljs-keyword">catch</span> (err) {
23209 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
23210 <span class="hljs-keyword">return</span>;
23213 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
23231 <td>Instructs OpenSSL to allow a non-[EC]DHE-based key exchange mode
23256 <td>Instructs OpenSSL to add server-hello extension from an early version
23283 <td>Instructs OpenSSL to disable the workaround for a man-in-the-middle
23284 protocol-version vulnerability in the SSL 2.0 server implementation.</td>
23308 <td>Instructs OpenSSL to disable encrypt-then-MAC.</td>
23534 <td>Specifies the built-in default cipher list used by Node.js.</td>
23545 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
23547 <p>Node.js includes a command-line debugging utility. To use it, start Node.js
23549 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
23550 &#x3C; Debugger listening on ws://127.0.0.1:9229/621111f9-ffcb-4e82-b718-48a145fa5db8
23558 <span class="hljs-meta">> </span><span class="language-bash">2 global.x = 5;</span>
23561 <span class="hljs-meta">debug></span></code></pre>
23562 <p>The Node.js debugger client is not a full-featured debugger, but simple step and
23566 <!-- eslint-disable no-debugger -->
23567 <pre><code class="language-js"><span class="hljs-comment">// myscript.js</span>
23568 <span class="hljs-variable language_">global</span>.<span class="hljs-property">x</span> = <span cl…
23569 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
23570 <span class="hljs-keyword">debugger</span>;
23571 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
23572 }, <span class="hljs-number">1000</span>);
23573 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
23575 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
23576 &#x3C; Debugger listening on ws://127.0.0.1:9229/621111f9-ffcb-4e82-b718-48a145fa5db8
23584 <span class="hljs-meta">> </span><span class="language-bash">2 global.x = 5;</span>
23587 <span class="hljs-meta">debug> </span><span class="language-bash">cont</span>
23593 <span class="hljs-meta">> </span><span class="language-bash">4 debugger;</span>
23596 <span class="hljs-meta">debug> </span><span class="language-bash">next</span>
23600 <span class="hljs-meta">> </span><span class="language-bash">5 console.log(<span class="hljs-stri…
23603 <span class="hljs-meta">debug> </span><span class="language-bash">repl</span>
23605 <span class="hljs-meta">> </span><span class="language-bash">x</span>
23607 <span class="hljs-meta">> </span><span class="language-bash">2 + 2</span>
23609 <span class="hljs-meta">debug> </span><span class="language-bash">next</span>
23615 <span class="hljs-meta">> </span><span class="language-bash">6 }, 1000);</span>
23618 <span class="hljs-meta">debug> </span><span class="language-bash">.<span class="hljs-built_in">exit…
23619 <span class="hljs-meta">$</span></code></pre>
23657 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
23658 &#x3C; Debugger listening on ws://127.0.0.1:9229/48a5b28a-550c-471b-b5e1-d13dd7165df9
23665 <span class="hljs-meta">> </span><span class="language-bash">1 const mod = require(<span class="hlj…
23668 <span class="hljs-meta">debug> </span><span class="language-bash">setBreakpoint(<span class="hljs-s…
23670 <span class="hljs-meta">debug> </span><span class="language-bash">c</span>
23674 <span class="hljs-meta">></span><span class="language-bash">22 exports.hello = <span class="hljs-fu…
23677 <span class="hljs-meta">debug></span></code></pre>
23680 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
23681 &#x3C; Debugger listening on ws://127.0.0.1:9229/ce24daa8-3816-44d4-b8ab-8273c8a66d35
23687 <span class="hljs-meta">> </span><span class="language-bash">7 addOne(10);</span>
23688 8 addOne(-1);
23690 … class="hljs-meta">debug> </span><span class="language-bash">setBreakpoint(<span class="hljs-strin…
23694 <span class="hljs-meta">> </span><span class="language-bash">4 <span class="hljs-built_in">return…
23698 8 addOne(-1);
23700 <span class="hljs-meta">debug> </span><span class="language-bash">cont</span>
23704 <span class="hljs-meta">> </span><span class="language-bash">4 <span class="hljs-built_in">return…
23707 <span class="hljs-meta">debug> </span><span class="language-bash"><span class="hljs-built_in">exec<…
23708 -1
23709 <span class="hljs-meta">debug></span></code></pre>
23737 <a href="https://chromedevtools.github.io/devtools-protocol/">Chrome DevTools Protocol</a>.</p>
23738 <p>V8 Inspector can be enabled by passing the <code>--inspect</code> flag when starting a
23740 e.g. <code>--inspect=9222</code> will accept DevTools connections on port 9222.</p>
23741 <p>To break on the first line of the application code, pass the <code>--inspect-brk</code>
23742 flag instead of <code>--inspect</code>.</p>
23743 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
23744 Debugger listening on ws://127.0.0.1:9229/dc9010dd-f8b8-4ac5-a510-c1a114ec7d29
23746 <p>(In the example above, the UUID dc9010dd-f8b8-4ac5-a510-c1a114ec7d29
23765 <li>Documentation-only</li>
23767 <li>End-of-Life</li>
23769 <p>A Documentation-only deprecation is one that is expressed only within the
23770 Node.js API docs. These generate no side-effects while running Node.js.
23771 Some Documentation-only deprecations trigger a runtime warning when launched
23772 with <a href="#cli_pending_deprecation"><code>--pending-deprecation</code></a> flag (or its alterna…
23774 deprecations below. Documentation-only deprecations that support that flag
23779 <a href="#cli_throw_deprecation"><code>--throw-deprecation</code></a> command-line flag is used, a …
23781 <p>An End-of-Life deprecation is used when functionality is or will soon be removed
23794 <td><p>End-of-Life.</p></td></tr>
23802 <p>Type: End-of-Life</p>
23811 <td><p>End-of-Life.</p></td></tr>
23819 <p>Type: End-of-Life</p>
23827 <td><p>End-of-Life.</p></td></tr>
23835 <p>Type: End-of-Life</p>
23844 <td><p>End-of-Life.</p></td></tr>
23848 <td><p>Documentation-only deprecation.</p></td></tr>
23852 <p>Type: End-of-Life</p>
23864 <td><p>Documentation-only deprecation.</p></td></tr>
23868 <p>Type: Runtime (supports <a href="#cli_pending_deprecation"><code>--pending-deprecation</code></a…
23881 …_arraybuffer_byteoffset_length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a> -
23887 <p>Without <code>--pending-deprecation</code>, runtime warnings occur only for code not in
23889 <code>Buffer()</code> usage in dependencies. With <code>--pending-deprecation</code>, a runtime
23897 <td><p>End-of-Life.</p></td></tr>
23903 <td><p>Documentation-only deprecation.</p></td></tr>
23907 <p>Type: End-of-Life</p>
23917 <td><p>End-of-Life.</p></td></tr>
23923 <td><p>Documentation-only deprecation.</p></td></tr>
23927 <p>Type: End-of-Life</p>
23933 precisely describe the actual semantics and was unnecessarily emotion-laden.</p>
23942 <td><p>Documentation-only deprecation.</p></td></tr>
23946 <p>Type: Documentation-only</p>
23957 <td><p>End-of-Life (for <code>digest === null</code>).</p></td></tr>
23961 <td><p>End-of-Life (for <code>digest === undefined</code>).</p></td></tr>
23969 <p>Type: End-of-Life</p>
23971 in Node.js 6.0 because the method defaulted to using the non-recommended
23985 <td><p>End-of-Life.</p></td></tr>
23993 <p>Type: End-of-Life</p>
24002 <td><p>End-of-Life.</p></td></tr>
24010 <p>Type: End-of-Life</p>
24019 <td><p>End-of-Life.</p></td></tr>
24027 <p>Type: End-of-Life</p>
24036 <td><p>End-of-Life.</p></td></tr>
24042 <p>Type: End-of-Life</p>
24051 <td><p>End-of-Life.</p></td></tr>
24057 <td><p>Documentation-only deprecation.</p></td></tr>
24061 <p>Type: End-of-Life</p>
24070 <td><p>End-of-Life.</p></td></tr>
24076 <td><p>Documentation-only deprecation.</p></td></tr>
24080 <p>Type: End-of-Life</p>
24089 <td><p>End-of-Life.</p></td></tr>
24097 <p>Type: End-of-Life</p>
24106 <td><p>End-of-Life.</p></td></tr>
24112 <p>Type: End-of-Life</p>
24113 <p><code>Intl.v8BreakIterator</code> was a non-standard extension and has been removed.
24114 See <a href="https://github.com/tc39/proposal-intl-segmenter"><code>Intl.Segmenter</code></a>.</p>
24127 that are not handled will terminate the Node.js process with a non-zero exit
24143 <p>Type: End-of-Life</p>
24167 <td><p>End-of-Life.</p></td></tr>
24175 <p>Type: End-of-Life</p>
24184 <td><p>End-of-Life.</p></td></tr>
24190 <p>Type: End-of-Life</p>
24199 <td><p>End-of-Life.</p></td></tr>
24207 <p>Type: End-of-Life</p>
24216 <td><p>End-of-Life.</p></td></tr>
24222 <p>Type: End-of-Life</p>
24244 <td><p>End-of-Life.</p></td></tr>
24252 <p>Type: End-of-Life</p>
24260 <td><p>End-of-Life.</p></td></tr>
24268 <p>Type: End-of-Life</p>
24276 <td><p>End-of-Life.</p></td></tr>
24284 <p>Type: End-of-Life</p>
24292 <td><p>End-of-Life.</p></td></tr>
24300 <p>Type: End-of-Life</p>
24310 <td><p>Documentation-only deprecation.</p></td></tr>
24314 <p>Type: Documentation-only</p>
24325 <td><p>Documentation-only deprecation.</p></td></tr>
24329 <p>Type: Documentation-only</p>
24340 <td><p>Documentation-only deprecation.</p></td></tr>
24344 <p>Type: Documentation-only</p>
24354 <td><p>Documentation-only deprecation.</p></td></tr>
24358 <p>Type: Documentation-only</p>
24369 <td><p>Documentation-only deprecation.</p></td></tr>
24373 <p>Type: Documentation-only</p>
24384 <td><p>Documentation-only deprecation.</p></td></tr>
24388 <p>Type: Documentation-only</p>
24398 <td><p>Documentation-only deprecation.</p></td></tr>
24402 <p>Type: Documentation-only</p>
24414 <td><p>Documentation-only deprecation.</p></td></tr>
24432 <td><p>Documentation-only deprecation.</p></td></tr>
24447 <td><p>Documentation-only deprecation.</p></td></tr>
24451 <p>Type: Documentation-only</p>
24459 <td><p>Documentation-only deprecation.</p></td></tr>
24463 <p>Type: Documentation-only</p>
24472 <td><p>End-of-Life.</p></td></tr>
24476 <td><p>Documentation-only deprecation.</p></td></tr>
24480 <p>Type: End-of-Life</p>
24489 <td><p>End-of-Life.</p></td></tr>
24493 <td><p>Documentation-only deprecation.</p></td></tr>
24497 <p>Type: End-of-Life</p>
24510 <td><p>Documentation-only deprecation.</p></td></tr>
24518 <p>Type: Documentation-only</p>
24529 <td><p>Documentation-only deprecation.</p></td></tr>
24533 <p>Type: Documentation-only</p>
24544 <td><p>Documentation-only deprecation.</p></td></tr>
24548 <p>Type: Documentation-only</p>
24558 <td><p>Documentation-only deprecation.</p></td></tr>
24562 <p>Type: Documentation-only</p>
24573 <td><p>Documentation-only deprecation.</p></td></tr>
24577 <p>Type: Documentation-only</p>
24587 <td><p>Documentation-only deprecation.</p></td></tr>
24591 <p>Type: Documentation-only</p>
24601 <td><p>Documentation-only deprecation.</p></td></tr>
24605 <p>Type: Documentation-only</p>
24615 <td><p>Documentation-only deprecation.</p></td></tr>
24619 <p>Type: Documentation-only</p>
24629 <td><p>Documentation-only deprecation.</p></td></tr>
24633 <p>Type: Documentation-only</p>
24643 <td><p>Documentation-only deprecation.</p></td></tr>
24647 <p>Type: Documentation-only</p>
24657 <td><p>Documentation-only deprecation.</p></td></tr>
24661 <p>Type: Documentation-only</p>
24671 <td><p>Documentation-only deprecation.</p></td></tr>
24675 <p>Type: Documentation-only</p>
24685 <td><p>Documentation-only deprecation.</p></td></tr>
24689 <p>Type: Documentation-only</p>
24699 <td><p>Documentation-only deprecation.</p></td></tr>
24703 <p>Type: Documentation-only</p>
24713 <td><p>Documentation-only deprecation.</p></td></tr>
24717 <p>Type: Documentation-only</p>
24727 <td><p>Documentation-only deprecation.</p></td></tr>
24731 <p>Type: Documentation-only</p>
24741 <td><p>Documentation-only deprecation.</p></td></tr>
24745 <p>Type: Documentation-only</p>
24755 <td><p>Documentation-only deprecation.</p></td></tr>
24759 <p>Type: Documentation-only</p>
24767 <td><p>End-of-Life.</p></td></tr>
24771 <td><p>Documentation-only deprecation.</p></td></tr>
24775 <p>Type: End-of-Life</p>
24779 <h4 id="DEP0062">DEP0062: <code>node --debug</code><span><a class="mark" href="#deprecations_dep006…
24785 <td><p>End-of-Life.</p></td></tr>
24791 <p>Type: End-of-Life</p>
24792 <p><code>--debug</code> activates the legacy V8 debugger interface, which was removed as
24793 of V8 5.8. It is replaced by Inspector which is activated with <code>--inspect</code>
24801 <td><p>Documentation-only deprecation.</p></td></tr>
24805 <p>Type: Documentation-only</p>
24820 <td><p>Documentation-only deprecation.</p></td></tr>
24837 <td><p>End-of-Life.</p></td></tr>
24839 <td><p>Documentation-only deprecation.</p></td></tr>
24843 <p>Type: End-of-Life</p>
24859 <td><p>Documentation-only deprecation.</p></td></tr>
24882 <td><p>Documentation-only deprecation.</p></td></tr>
24886 <p>Type: Documentation-only</p>
24903 a V8-inspector based CLI debugger available through <code>node inspect</code>.</p>
24910 <td><p>End-of-Life.</p></td></tr>
24914 <td><p>Documentation-only deprecation.</p></td></tr>
24918 <p>Type: End-of-Life</p>
24927 <td><p>End-of-Life.</p></td></tr>
24933 <p>Type: End-of-Life</p>
24943 <td><p>End-of-Life.</p></td></tr>
24949 <p>Type: End-of-Life</p>
24959 <td><p>End-of-Life.</p></td></tr>
24965 <p>Type: End-of-Life</p>
24975 <td><p>End-of-Life.</p></td></tr>
24981 <p>Type: End-of-Life</p>
24984 <p>As the original API was undocumented and not generally useful for non-internal
25019 <td><p>Documentation-only deprecation.</p></td></tr>
25026 … class="language-js"><span class="hljs-keyword">const</span> querystring = <span class="hljs-built…
25027 querystring.<span class="hljs-title function_">parse</span>(str, <span class="hljs-string">'\n'</sp…
25030-console"><span class="hljs-meta">> </span><span class="language-bash">querystring.parse(<span cla…
25032 <span class="hljs-meta">> </span><span class="language-bash">tls.parseCertString(<span class="hljs-
25066 <td><p>End-of-Life.</p></td></tr>
25070 <td><p>Documentation-only deprecation.</p></td></tr>
25074 <p>Type: End-of-Life</p>
25085 <td><p>Documentation-only deprecation.</p></td></tr>
25089 <p>Type: Documentation-only</p>
25126 <td><p>End-of-Life.</p></td></tr>
25132 <p>Type: End-of-Life.</p>
25149 <p>Type: End-of-Life</p>
25162 <li><code>v8/tools/tickprocessor-driver</code></li>
25164 <li><code>node-inspect/lib/_inspect</code> (from 7.6.0)</li>
25165 <li><code>node-inspect/lib/internal/inspect_client</code> (from 7.6.0)</li>
25166 <li><code>node-inspect/lib/internal/inspect_repl</code> (from 7.6.0)</li>
25171 <p>On the other hand, <code>node-inspect</code> can be installed locally through a package
25180 <td><p>End-of-Life.</p></td></tr>
25186 <p>Type: End-of-Life</p>
25196 <td><p>End-of-Life.</p></td></tr>
25202 <p>Type: End-of-Life</p>
25213 <td><p>Documentation-only deprecation.</p></td></tr>
25227 <td><p>End-of-Life.</p></td></tr>
25233 <p>Type: End-of-Life</p>
25238 …s://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf">NIST SP 800-38D</a>.</p>
25251 <h4 id="DEP0092">DEP0092: Top-level <code>this</code> bound to <code>module.exports</code><span><a …
25257 <td><p>Documentation-only deprecation.</p></td></tr>
25261 <p>Type: Documentation-only</p>
25262 <p>Assigning properties to the top-level <code>this</code> as an alternative
25271 <td><p>Documentation-only deprecation.</p></td></tr>
25275 <p>Type: Documentation-only</p>
25331 <code>CallbackScope</code>, or the high-level <code>AsyncResource</code> class.</p>
25338 <td><p>End-of-Life.</p></td></tr>
25344 <p>Type: End-of-Life</p>
25351 <h4 id="DEP0099">DEP0099: Async context-unaware <code>node::MakeCallback</code> C++ APIs<span><a cl…
25357 <td><p>Compile-time deprecation.</p></td></tr>
25361 <p>Type: Compile-time</p>
25373 <td><p>Documentation-only deprecation.</p></td></tr>
25380 <h4 id="DEP0101">DEP0101: <code>--with-lttng</code><span><a class="mark" href="#deprecations_dep010…
25386 <td><p>End-of-Life.</p></td></tr>
25390 <p>Type: End-of-Life</p>
25391 <p>The <code>--with-lttng</code> compile-time option has been removed.</p>
25398 <td><p>End-of-Life.</p></td></tr>
25402 <p>Type: End-of-Life</p>
25414 <td><p>Documentation-only deprecation.</p></td></tr>
25418 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25429 <td><p>Documentation-only deprecation.</p></td></tr>
25433 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25434 <p>When assigning a non-string property to <a href="#process_process_env"><code>process.env</code><…
25445 <td><p>End-of-Life.</p></td></tr>
25451 <p>Type: End-of-Life</p>
25463 <td><p>Documentation-only deprecation.</p></td></tr>
25480 <td><p>End-of-Life.</p></td></tr>
25486 <p>Type: End-of-Life</p>
25497 <td><p>Documentation-only deprecation.</p></td></tr>
25529 <td><p>Documentation-only deprecation.</p></td></tr>
25533 <p>Type: Documentation-only</p>
25542 <td><p>Added support for <code>--pending-deprecation</code>.</p></td></tr>
25544 <td><p>Documentation-only deprecation.</p></td></tr>
25548 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25573 <td><p>End-of-Life.</p></td></tr>
25579 <p>Type: End-of-Life</p>
25588 <td><p>End-of-Life.</p></td></tr>
25594 <p>Type: End-of-Life</p>
25598 <!--lint disable nodejs-yaml-comments -->
25604 <td><p>Added documentation-only deprecation with <code>--pending-deprecation</code> support.</p></t…
25608 <!--lint enable nodejs-yaml-comments -->
25609 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25623 <td><p>Documentation-only deprecation.</p></td></tr>
25637 <td><p>End-of-Life.</p></td></tr>
25643 <p>Type: End-of-Life</p>
25670 <td><p>Documentation-only deprecation.</p></td></tr>
25674 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25683 <td><p>End-of-Life.</p></td></tr>
25689 <p>Type: End-of-Life</p>
25735 <a href="https://tools.ietf.org/html/rfc6066#section-3">RFC 6066</a>. This will be ignored in a fut…
25773 If re-referencing the timeout is necessary, <a href="#timers_timeout_ref"><code>timeout.ref()</code…
25796 <td><p>Documentation-only.</p></td></tr>
25800 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25801 <p>Modules that have an invalid <code>main</code> entry (e.g., <code>./does-not-exist.js</code>) and
25813 <td><p>Documentation-only.</p></td></tr>
25829 <td><p>Documentation-only.</p></td></tr>
25844 <td><p>Documentation-only.</p></td></tr>
25848 <p>Type: End-of-Life</p>
25851 <code>--http-parser=legacy</code> command-line flag could be used to revert to using the
25872 <td><p>Documentation-only deprecation.</p></td></tr>
25876 <p>Type: Documentation-only</p>
25885 <td><p>Documentation-only deprecation.</p></td></tr>
25889 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
25913 <td><p>Documentation-only deprecation.</p></td></tr>
25917 <p>Type: Documentation-only</p>
25941-js"><span class="hljs-keyword">const</span> fsPromises = <span class="hljs-built_in">require</spa…
25942 …s="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title f…
25943 <span class="hljs-keyword">let</span> filehandle;
25944 <span class="hljs-keyword">try</span> {
25945 …ljs-keyword">await</span> fsPromises.<span class="hljs-title function_">open</span>(<span class="h…
25946 } <span class="hljs-keyword">finally</span> {
25947 … <span class="hljs-keyword">if</span> (filehandle !== <span class="hljs-literal">undefined</span>)
25948 …<span class="hljs-keyword">await</span> filehandle.<span class="hljs-title function_">close</span>…
25957 <td><p>Documentation-only deprecation.</p></td></tr>
25961 <p>Type: Documentation-only</p>
25962 <p><a href="#process_process_mainmodule"><code>process.mainModule</code></a> is a CommonJS-only fea…
25963 object is shared with non-CommonJS environment. Its use within ECMAScript
25973 <td><p>Documentation-only deprecation.</p></td></tr>
25977 <p>Type: Documentation-only</p>
25978 <p>Calling <code>process.umask()</code> with no argument causes the process-wide umask to be
25980 potential security vulnerability. There is no safe, cross-platform alternative
25988 <td><p>Documentation-only deprecation.</p></td></tr>
25992 <p>Type: Documentation-only</p>
26000 <td><p>Documentation-only (supports [<code>--pending-deprecation</code>][]).</p></td></tr>
26004 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
26013 <td><p>Documentation-only (supports [<code>--pending-deprecation</code>][]).</p></td></tr>
26017 <p>Type: Documentation-only</p>
26040 <td><p>Documentation-only deprecation.</p></td></tr>
26044 <p>Type: Documentation-only</p>
26051 …ge-js"><span class="hljs-keyword">if</span> (<span class="hljs-built_in">require</span>.<span clas…
26052 …<span class="hljs-comment">// Code section that will run only if current file is the entry point.<…
26056-js"><span class="hljs-keyword">const</span> moduleParents = <span class="hljs-title class_">Objec…
26057-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">m</span>) =…
26064 <td><p>Documentation-only deprecation.</p></td></tr>
26068 <p>Type: Documentation-only</p>
26076 <td><p>Documentation-only deprecation.</p></td></tr>
26080 <p>Type: Documentation-only</p>
26089 <td><p>Documentation-only deprecation.</p></td></tr>
26093 <p>Type: Documentation-only</p>
26095 on nonexistent paths, or when given a file as a target.
26103 <td><p>Documentation-only deprecation with <code>--pending-deprecation</code> support.</p></td></tr>
26107 <p>Type: Documentation-only (supports <a href="#cli_pending_deprecation"><code>--pending-deprecatio…
26115 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
26120 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26122 will create one or many top-level channels to report messages through.
26134 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26136 <span class="hljs-comment">// Get a reusable channel object</span>
26137 …lass="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">…
26139 <span class="hljs-comment">// Subscribe to the channel</span>
26140 channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-function">(<span clas…
26141 <span class="hljs-comment">// Received data</span>
26144 <span class="hljs-comment">// Check if the channel has an active subscriber</span>
26145 <span class="hljs-keyword">if</span> (channel.<span class="hljs-property">hasSubscribers</span>) {
26146 <span class="hljs-comment">// Publish data to the channel</span>
26147 channel.<span class="hljs-title function_">publish</span>({
26148 <span class="hljs-attr">some</span>: <span class="hljs-string">'data'</span>
26153 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
26154 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
26159 performance-sensitive code.</p>
26160 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26162 … class="hljs-keyword">if</span> (diagnostics_channel.<span class="hljs-title function_">hasSubscri…
26163 <span class="hljs-comment">// There are subscribers, prepare and publish message</span>
26167 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
26170 <p>This is the primary entry-point for anyone wanting to interact with a named
26173 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26175 …lass="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">…
26186 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
26191 performance-sensitive code.</p>
26192 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26194 …lass="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">…
26196 <span class="hljs-keyword">if</span> (channel.<span class="hljs-property">hasSubscribers</span>) {
26197 <span class="hljs-comment">// There are subscribers, prepare and publish message</span>
26201 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
26205 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26207 …lass="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">…
26209 channel.<span class="hljs-title function_">publish</span>({
26210 <span class="hljs-attr">some</span>: <span class="hljs-string">'message'</span>
26214 <li><code>onMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
26216 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
26217 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
26224 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26226 …lass="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">…
26228 channel.<span class="hljs-title function_">subscribe</span>(<span class="hljs-function">(<span clas…
26229 <span class="hljs-comment">// Received data</span>
26244 <li><code>onMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
26245 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
26249 …ss="language-js"><span class="hljs-keyword">const</span> diagnostics_channel = <span class="hljs-b…
26251 …lass="hljs-keyword">const</span> channel = diagnostics_channel.<span class="hljs-title function_">…
26253 <span class="hljs-keyword">function</span> <span class="hljs-title function_">onMessage</span>(<spa…
26254 <span class="hljs-comment">// Received data</span>
26257 channel.<span class="hljs-title function_">subscribe</span>(onMessage);
26259 channel.<span class="hljs-title function_">unsubscribe</span>(onMessage);</code></pre></section>
26263 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
26272 …code class="language-js"><span class="hljs-keyword">const</span> dns = <span class="hljs-built_in"…
26274 …n class="hljs-title function_">lookup</span>(<span class="hljs-string">'example.org'</span>, <span…
26275 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
26277 <span class="hljs-comment">// address: "93.184.216.34" family: IPv4</span></code></pre>
26282 DNS queries, bypassing other name-resolution facilities.</p>
26283 …code class="language-js"><span class="hljs-keyword">const</span> dns = <span class="hljs-built_in"…
26285 … class="hljs-title function_">resolve4</span>(<span class="hljs-string">'archive.org'</span>, <spa…
26286 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
26288-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
26290 …addresses.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span cla…
26291 …dns.<span class="hljs-title function_">reverse</span>(a, <span class="hljs-function">(<span class=…
26292 <span class="hljs-keyword">if</span> (err) {
26293 <span class="hljs-keyword">throw</span> err;
26295-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
26309-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Resolver</span> } =…
26310 <span class="hljs-keyword">const</span> resolver = <span class="hljs-keyword">new</span> <span clas…
26311 resolver.<span class="hljs-title function_">setServers</span>([<span class="hljs-string">'4.4.4.4'<…
26313 <span class="hljs-comment">// This request will use the server at 4.4.4.4, independent of global se…
26314 … class="hljs-title function_">resolve4</span>(<span class="hljs-string">'example.org'</span>, <spa…
26315 <span class="hljs-comment">// ...</span>
26344 <td><p>The constructor now accepts an <code>options</code> object. The single supported option is <…
26352 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
26354 …ozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> …
26356 <li><code>tries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
26367 <h4><code>resolver.setLocalAddress([ipv4][, ipv6])</code><span><a class="mark" href="#dns_resolver_…
26372 <li><code>ipv4</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
26374ipv6</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
26378 This allows programs to specify outbound interfaces when used on multi-homed
26383 servers, and the v6 local address when making requests to IPv6 DNS servers.
26390 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
26392 …rings, formatted according to <a href="https://tools.ietf.org/html/rfc5952#section-6">RFC 5952</a>,
26395 <!-- eslint-disable semi-->
26396 <pre><code class="language-js">[
26397 <span class="hljs-string">'4.4.4.4'</span>,
26398 <span class="hljs-string">'2001:4860:4860::8888'</span>,
26399 <span class="hljs-string">'4.4.4.4:1053'</span>,
26400 <span class="hljs-string">'[2001:4860:4860::8888]:1053'</span>,
26417 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26418 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
26420 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
26421 <code>0</code> indicates that IPv4 and IPv6 addresses are both returned. <strong>Default:</strong>
26423 <li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
26425 <li><code>all</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
26426 an array. Otherwise, returns a single address. <strong>Default:</strong> <code>false</code>.</li>
26427 …g/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <co…
26429 IPv4 addresses are placed before IPv6 addresses.
26433 <a href="#cli_dns_result_order_order"><code>--dns-result-order</code></a>. New code should use <cod…
26436 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26438 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26439 …ozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> A…
26440 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
26441 the address is not an IPv4 or IPv6 address. <code>0</code> is a likely indicator of a
26447 AAAA (IPv6) record. All <code>option</code> properties are optional. If <code>options</code> is an
26449 and IPv6 addresses are both returned if found.</p>
26464 …code class="language-js"><span class="hljs-keyword">const</span> dns = <span class="hljs-built_in"…
26465 <span class="hljs-keyword">const</span> options = {
26466 <span class="hljs-attr">family</span>: <span class="hljs-number">6</span>,
26467 …<span class="hljs-attr">hints</span>: dns.<span class="hljs-property">ADDRCONFIG</span> | dns.<spa…
26469 …ss="hljs-title function_">lookup</span>(<span class="hljs-string">'example.com'</span>, options, <…
26470 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
26471 <span class="hljs-comment">// address: "2606:2800:220:1:248:1893:25c8:1946" family: IPv6</span>
26473 <span class="hljs-comment">// When options.all is true, the result will be an Array.</span>
26474 options.<span class="hljs-property">all</span> = <span class="hljs-literal">true</span>;
26475 …ss="hljs-title function_">lookup</span>(<span class="hljs-string">'example.com'</span>, options, <…
26476 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
26477 <span class="hljs-comment">// addresses: [{"address":"2606:2800:220:1:248:1893:25c8:1946","family":…
26493 <li><code>dns.ADDRCONFIG</code>: Limits returned address types to the types of non-loopback
26496 <li><code>dns.V4MAPPED</code>: If the IPv6 family was specified, but no IPv6 addresses were
26497 found, then return IPv4 mapped IPv6 addresses. It is not supported
26499 <li><code>dns.ALL</code>: If <code>dns.V4MAPPED</code> is specified, return resolved IPv6 addresses…
26500 well as IPv4 mapped IPv6 addresses.</li>
26507 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
26508 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
26509 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26511 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26512 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26513 <li><code>service</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
26523 …code class="language-js"><span class="hljs-keyword">const</span> dns = <span class="hljs-built_in"…
26524-title function_">lookupService</span>(<span class="hljs-string">'127.0.0.1'</span>, <span class="…
26525 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
26526 <span class="hljs-comment">// Prints: localhost ssh</span>
26535 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26536 <li><code>rrtype</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
26537 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26539 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26540-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="h…
26631-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></td><td><a hre…
26647 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26648 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
26650 …eveloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;bo…
26656 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26658 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26659 …illa.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> |…
26680 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26681 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
26683 …eveloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;bo…
26689 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26691 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26692 …illa.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> |…
26696 <p>Uses the DNS protocol to resolve a IPv6 addresses (<code>AAAA</code> records) for the
26698 will contain an array of IPv6 addresses.</p>
26701 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26702 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26704 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26705 <li><code>ret</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26763 …stname_callback"><code>dns.resolveSoa()</code></a></td></tr><tr><td><code>'SRV'</code></td><td>Ref…
26765 <!-- eslint-disable semi -->
26766-js">[ { <span class="hljs-attr">type</span>: <span class="hljs-string">'A'</span>, <span class="h…
26767 …<span class="hljs-attr">type</span>: <span class="hljs-string">'CNAME'</span>, <span class="hljs-a…
26768-attr">type</span>: <span class="hljs-string">'MX'</span>, <span class="hljs-attr">exchange</span>…
26769 …{ <span class="hljs-attr">type</span>: <span class="hljs-string">'NS'</span>, <span class="hljs-at…
26770 …span class="hljs-attr">type</span>: <span class="hljs-string">'TXT'</span>, <span class="hljs-attr…
26771 { <span class="hljs-attr">type</span>: <span class="hljs-string">'SOA'</span>,
26772 <span class="hljs-attr">nsname</span>: <span class="hljs-string">'ns1.example.com'</span>,
26773 <span class="hljs-attr">hostmaster</span>: <span class="hljs-string">'admin.example.com'</span>,
26774 <span class="hljs-attr">serial</span>: <span class="hljs-number">156696742</span>,
26775 <span class="hljs-attr">refresh</span>: <span class="hljs-number">900</span>,
26776 <span class="hljs-attr">retry</span>: <span class="hljs-number">900</span>,
26777 <span class="hljs-attr">expire</span>: <span class="hljs-number">1800</span>,
26778 <span class="hljs-attr">minttl</span>: <span class="hljs-number">60</span> } ]</code></pre>
26787 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26788 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26790 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26791 <li><code>addresses</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
26804 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26805 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26807 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26808 <li><code>records</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
26821 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26822 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26824 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26825 <li><code>addresses</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
26838 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26839 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26841 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26842 <li><code>addresses</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
26857 <!-- eslint-skip -->
26858 <pre><code class="language-js">{
26859 <span class="hljs-attr">flags</span>: <span class="hljs-string">'s'</span>,
26860 <span class="hljs-attr">service</span>: <span class="hljs-string">'SIP+D2U'</span>,
26861 <span class="hljs-attr">regexp</span>: <span class="hljs-string">''</span>,
26862 …<span class="hljs-attr">replacement</span>: <span class="hljs-string">'_sip._udp.example.com'</spa…
26863 <span class="hljs-attr">order</span>: <span class="hljs-number">30</span>,
26864 <span class="hljs-attr">preference</span>: <span class="hljs-number">100</span>
26871 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26872 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26874 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26875 <li><code>addresses</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
26888 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26889 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26891 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26892 <li><code>addresses</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
26904 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26905 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26907 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26908 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
26924 <!-- eslint-skip -->
26925 <pre><code class="language-js">{
26926 <span class="hljs-attr">nsname</span>: <span class="hljs-string">'ns.example.com'</span>,
26927 <span class="hljs-attr">hostmaster</span>: <span class="hljs-string">'root.example.com'</span>,
26928 <span class="hljs-attr">serial</span>: <span class="hljs-number">2013101809</span>,
26929 <span class="hljs-attr">refresh</span>: <span class="hljs-number">10000</span>,
26930 <span class="hljs-attr">retry</span>: <span class="hljs-number">2400</span>,
26931 <span class="hljs-attr">expire</span>: <span class="hljs-number">604800</span>,
26932 <span class="hljs-attr">minttl</span>: <span class="hljs-number">3600</span>
26939 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26940 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26942 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26943 <li><code>addresses</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
26947 <p>Uses the DNS protocol to resolve service records (<code>SRV</code> records) for the
26956 <!-- eslint-skip -->
26957 <pre><code class="language-js">{
26958 <span class="hljs-attr">priority</span>: <span class="hljs-number">10</span>,
26959 <span class="hljs-attr">weight</span>: <span class="hljs-number">5</span>,
26960 <span class="hljs-attr">port</span>: <span class="hljs-number">21223</span>,
26961 <span class="hljs-attr">name</span>: <span class="hljs-string">'service.example.com'</span>
26967 <!--lint disable no-undefined-references list-item-bullet-indent-->
26969 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
26970 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26972 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26973 <li><code>records</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
26977 <!--lint enable no-undefined-references list-item-bullet-indent-->
26980 two-dimensional array of the text records available for <code>hostname</code> (e.g.
26981 <code>[ ['v=spf1 ip4:0.0.0.0 ', '~all' ] ]</code>). Each sub-array contains TXT chunks of
26989 <li><code>ip</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
26990 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
26992 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
26993 <li><code>hostnames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
26997 <p>Performs a reverse DNS query that resolves an IPv4 or IPv6 address to an
27006 <li><code>order</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
27015 priority than <a href="#cli_dns_result_order_order"><code>--dns-result-order</code></a>. When using…
27023 …en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> array of <…
27026 …argument is an array of <a href="https://tools.ietf.org/html/rfc5952#section-6">RFC 5952</a> forma…
27028 <pre><code class="language-js">dns.<span class="hljs-title function_">setServers</span>([
27029 <span class="hljs-string">'4.4.4.4'</span>,
27030 <span class="hljs-string">'[2001:4860:4860::8888]'</span>,
27031 <span class="hljs-string">'4.4.4.4:1053'</span>,
27032 <span class="hljs-string">'[2001:4860:4860::8888]:1053'</span>,
27041 <a href="https://man7.org/linux/man-pages/man5/resolv.conf.5.html">resolve.conf</a>.
27070-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Resolver</span> } =…
27071 <span class="hljs-keyword">const</span> resolver = <span class="hljs-keyword">new</span> <span clas…
27072 resolver.<span class="hljs-title function_">setServers</span>([<span class="hljs-string">'4.4.4.4'<…
27074 <span class="hljs-comment">// This request will use the server at 4.4.4.4, independent of global se…
27075-title function_">resolve4</span>(<span class="hljs-string">'example.org'</span>).<span class="hlj…
27076 <span class="hljs-comment">// ...</span>
27079 <span class="hljs-comment">// Alternatively, the same code can be written using async-await style.<…
27080 (<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hl…
27081-keyword">const</span> addresses = <span class="hljs-keyword">await</span> resolver.<span class="h…
27113 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
27115 …rings, formatted according to <a href="https://tools.ietf.org/html/rfc5952#section-6">RFC 5952</a>,
27118 <!-- eslint-disable semi-->
27119 <pre><code class="language-js">[
27120 <span class="hljs-string">'4.4.4.4'</span>,
27121 <span class="hljs-string">'2001:4860:4860::8888'</span>,
27122 <span class="hljs-string">'4.4.4.4:1053'</span>,
27123 <span class="hljs-string">'[2001:4860:4860::8888]:1053'</span>,
27130 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27131 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
27133 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
27134 <code>0</code> indicates that IPv4 and IPv6 addresses are both returned. <strong>Default:</strong>
27136 <li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
27138 <li><code>all</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
27139 an array. Otherwise, returns a single address. <strong>Default:</strong> <code>false</code>.</li>
27140 <li><code>verbatim</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27141 IPv6 addresses in the order the DNS resolver returned them. When <code>false</code>,
27142 IPv4 addresses are placed before IPv6 addresses.
27146 <a href="#cli_dns_result_order_order"><code>--dns-result-order</code></a>. New code should use <cod…
27151 AAAA (IPv6) record. All <code>option</code> properties are optional. If <code>options</code> is an
27153 and IPv6 addresses are both returned if found.</p>
27168 …code class="language-js"><span class="hljs-keyword">const</span> dns = <span class="hljs-built_in"…
27169 <span class="hljs-keyword">const</span> dnsPromises = dns.<span class="hljs-property">promises</spa…
27170 <span class="hljs-keyword">const</span> options = {
27171 <span class="hljs-attr">family</span>: <span class="hljs-number">6</span>,
27172 …<span class="hljs-attr">hints</span>: dns.<span class="hljs-property">ADDRCONFIG</span> | dns.<spa…
27175-title function_">lookup</span>(<span class="hljs-string">'example.com'</span>, options).<span cla…
27176-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
27177 <span class="hljs-comment">// address: "2606:2800:220:1:248:1893:25c8:1946" family: IPv6</span>
27180 <span class="hljs-comment">// When options.all is true, the result will be an Array.</span>
27181 options.<span class="hljs-property">all</span> = <span class="hljs-literal">true</span>;
27182-title function_">lookup</span>(<span class="hljs-string">'example.com'</span>, options).<span cla…
27183 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
27184 …<span class="hljs-comment">// addresses: [{"address":"2606:2800:220:1:248:1893:25c8:1946","family"…
27191 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
27192 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
27201-js"><span class="hljs-keyword">const</span> dnsPromises = <span class="hljs-built_in">require</sp…
27202-title function_">lookupService</span>(<span class="hljs-string">'127.0.0.1'</span>, <span class="…
27203-variable language_">console</span>.<span class="hljs-title function_">log</span>(result.<span cla…
27204 <span class="hljs-comment">// Prints: localhost ssh</span>
27211 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27212 <li><code>rrtype</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
27301-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></td><td><a hre…
27309 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27310 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
27312 …eveloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;bo…
27327 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27328 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
27330 …eveloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;bo…
27337 <p>Uses the DNS protocol to resolve IPv6 addresses (<code>AAAA</code> records) for the
27338 <code>hostname</code>. On success, the <code>Promise</code> is resolved with an array of IPv6
27345 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27401 …ostname"><code>dnsPromises.resolveSoa()</code></a></td></tr><tr><td><code>'SRV'</code></td><td>Ref…
27403 <!-- eslint-disable semi -->
27404-js">[ { <span class="hljs-attr">type</span>: <span class="hljs-string">'A'</span>, <span class="h…
27405 …<span class="hljs-attr">type</span>: <span class="hljs-string">'CNAME'</span>, <span class="hljs-a…
27406-attr">type</span>: <span class="hljs-string">'MX'</span>, <span class="hljs-attr">exchange</span>…
27407 …{ <span class="hljs-attr">type</span>: <span class="hljs-string">'NS'</span>, <span class="hljs-at…
27408 …span class="hljs-attr">type</span>: <span class="hljs-string">'TXT'</span>, <span class="hljs-attr…
27409 { <span class="hljs-attr">type</span>: <span class="hljs-string">'SOA'</span>,
27410 <span class="hljs-attr">nsname</span>: <span class="hljs-string">'ns1.example.com'</span>,
27411 <span class="hljs-attr">hostmaster</span>: <span class="hljs-string">'admin.example.com'</span>,
27412 <span class="hljs-attr">serial</span>: <span class="hljs-number">156696742</span>,
27413 <span class="hljs-attr">refresh</span>: <span class="hljs-number">900</span>,
27414 <span class="hljs-attr">retry</span>: <span class="hljs-number">900</span>,
27415 <span class="hljs-attr">expire</span>: <span class="hljs-number">1800</span>,
27416 <span class="hljs-attr">minttl</span>: <span class="hljs-number">60</span> } ]</code></pre>
27422 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27433 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27443 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27454 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27467 <!-- eslint-skip -->
27468 <pre><code class="language-js">{
27469 <span class="hljs-attr">flags</span>: <span class="hljs-string">'s'</span>,
27470 <span class="hljs-attr">service</span>: <span class="hljs-string">'SIP+D2U'</span>,
27471 <span class="hljs-attr">regexp</span>: <span class="hljs-string">''</span>,
27472 …<span class="hljs-attr">replacement</span>: <span class="hljs-string">'_sip._udp.example.com'</spa…
27473 <span class="hljs-attr">order</span>: <span class="hljs-number">30</span>,
27474 <span class="hljs-attr">preference</span>: <span class="hljs-number">100</span>
27481 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27492 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27502 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27516 <!-- eslint-skip -->
27517 <pre><code class="language-js">{
27518 <span class="hljs-attr">nsname</span>: <span class="hljs-string">'ns.example.com'</span>,
27519 <span class="hljs-attr">hostmaster</span>: <span class="hljs-string">'root.example.com'</span>,
27520 <span class="hljs-attr">serial</span>: <span class="hljs-number">2013101809</span>,
27521 <span class="hljs-attr">refresh</span>: <span class="hljs-number">10000</span>,
27522 <span class="hljs-attr">retry</span>: <span class="hljs-number">2400</span>,
27523 <span class="hljs-attr">expire</span>: <span class="hljs-number">604800</span>,
27524 <span class="hljs-attr">minttl</span>: <span class="hljs-number">3600</span>
27531 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27533 <p>Uses the DNS protocol to resolve service records (<code>SRV</code> records) for the
27542 <!-- eslint-skip -->
27543 <pre><code class="language-js">{
27544 <span class="hljs-attr">priority</span>: <span class="hljs-number">10</span>,
27545 <span class="hljs-attr">weight</span>: <span class="hljs-number">5</span>,
27546 <span class="hljs-attr">port</span>: <span class="hljs-number">21223</span>,
27547 <span class="hljs-attr">name</span>: <span class="hljs-string">'service.example.com'</span>
27554 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
27557 <code>hostname</code>. On success, the <code>Promise</code> is resolved with a two-dimensional array
27559 <code>[ ['v=spf1 ip4:0.0.0.0 ', '~all' ] ]</code>). Each sub-array contains TXT chunks of
27567 <li><code>ip</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
27569 <p>Performs a reverse DNS query that resolves an IPv4 or IPv6 address to an
27578 <li><code>order</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
27587 higher priority than <a href="#cli_dns_result_order_order"><code>--dns-result-order</code></a>. Whe…
27595 …en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> array of <…
27598 …argument is an array of <a href="https://tools.ietf.org/html/rfc5952#section-6">RFC 5952</a> forma…
27600 <pre><code class="language-js">dnsPromises.<span class="hljs-title function_">setServers</span>([
27601 <span class="hljs-string">'4.4.4.4'</span>,
27602 <span class="hljs-string">'[2001:4860:4860::8888]'</span>,
27603 <span class="hljs-string">'4.4.4.4:1053'</span>,
27604 <span class="hljs-string">'[2001:4860:4860::8888]:1053'</span>,
27610 <a href="https://man7.org/linux/man-pages/man5/resolv.conf.5.html">resolve.conf</a>.
27638 <li><code>dns.NOTINITIALIZED</code>: c-ares library initialization not yet performed.</li>
27652 resolve a given name the same way as the <code>ping</code> command. On most POSIX-like
27654 …://man7.org/linux/man-pages/man5/nsswitch.conf.5.html"><code>nsswitch.conf(5)</code></a> and/or <a…
27658 perspective, it is implemented as a synchronous call to <a href="http://man7.org/linux/man-pages/ma…
27669 do not use <a href="http://man7.org/linux/man-pages/man3/getaddrinfo.3.html"><code>getaddrinfo(3)</…
27692 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
27701 single group. If any of the event emitters or callbacks registered to a
27710 <p>By the very nature of how <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer…
27720 <p>In this way, <code>domain</code> usage goes hand-in-hand with the cluster module,
27726 <pre><code class="language-js"><span class="hljs-comment">// XXX WARNING! BAD IDEA!</span>
27728 …class="hljs-keyword">const</span> d = <span class="hljs-built_in">require</span>(<span class="hljs
27729 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
27730 <span class="hljs-comment">// The error won't crash the process, but what it does is worse!</span>
27731 …<span class="hljs-comment">// Though we've prevented abrupt process restarting, we are leaking</sp…
27732 <span class="hljs-comment">// a lot of resources if this ever happens.</span>
27733 <span class="hljs-comment">// This is no better than process.on('uncaughtException')!</span>
27734 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
27736 d.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
27737-built_in">require</span>(<span class="hljs-string">'http'</span>).<span class="hljs-title functio…
27738 <span class="hljs-title function_">handleRequest</span>(req, res);
27739 …}).<span class="hljs-title function_">listen</span>(<span class="hljs-variable constant_">PORT</sp…
27744 <pre><code class="language-js"><span class="hljs-comment">// Much better!</span>
27746 <span class="hljs-keyword">const</span> cluster = <span class="hljs-built_in">require</span>(<span …
27747-keyword">const</span> <span class="hljs-variable constant_">PORT</span> = +process.<span class="h…
27749 <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isMaster</span>) {
27750 <span class="hljs-comment">// A more realistic scenario would have more than 2 workers,</span>
27751 <span class="hljs-comment">// and perhaps not put the master and worker in the same file.</span>
27752 <span class="hljs-comment">//</span>
27753 <span class="hljs-comment">// It is also possible to get a bit fancier about logging, and</span>
27754 <span class="hljs-comment">// implement whatever custom logic is needed to prevent DoS</span>
27755 <span class="hljs-comment">// attacks and other bad behavior.</span>
27756 <span class="hljs-comment">//</span>
27757 <span class="hljs-comment">// See the options in the cluster documentation.</span>
27758 <span class="hljs-comment">//</span>
27759 <span class="hljs-comment">// The important thing is that the master does very little,</span>
27760 <span class="hljs-comment">// increasing our resilience to unexpected errors.</span>
27762 cluster.<span class="hljs-title function_">fork</span>();
27763 cluster.<span class="hljs-title function_">fork</span>();
27765 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'disconnect'</span>, <span cl…
27766 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
27767 cluster.<span class="hljs-title function_">fork</span>();
27770 } <span class="hljs-keyword">else</span> {
27771 <span class="hljs-comment">// the worker</span>
27772 <span class="hljs-comment">//</span>
27773 <span class="hljs-comment">// This is where we put our bugs!</span>
27775 …<span class="hljs-keyword">const</span> domain = <span class="hljs-built_in">require</span>(<span …
27777 <span class="hljs-comment">// See the cluster documentation for more details about using</span>
27778 …<span class="hljs-comment">// worker processes to serve requests. How it works, caveats, etc.</spa…
27780-keyword">const</span> server = <span class="hljs-built_in">require</span>(<span class="hljs-strin…
27781 …<span class="hljs-keyword">const</span> d = domain.<span class="hljs-title function_">create</span…
27782 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
27783 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span…
27785 <span class="hljs-comment">// We're in dangerous territory!</span>
27786 <span class="hljs-comment">// By definition, something unexpected occurred,</span>
27787 <span class="hljs-comment">// which we probably didn't want.</span>
27788 <span class="hljs-comment">// Anything can happen now! Be very careful!</span>
27790 <span class="hljs-keyword">try</span> {
27791 <span class="hljs-comment">// Make sure we close down within 30 seconds</span>
27792 …<span class="hljs-keyword">const</span> killtimer = <span class="hljs-built_in">setTimeout</span>(…
27793 … process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">1</span>);
27794 }, <span class="hljs-number">30000</span>);
27795 <span class="hljs-comment">// But don't keep the process open just for that!</span>
27796 killtimer.<span class="hljs-title function_">unref</span>();
27798 <span class="hljs-comment">// Stop taking new requests.</span>
27799 server.<span class="hljs-title function_">close</span>();
27801 <span class="hljs-comment">// Let the master know we're dead. This will trigger a</span>
27802 … <span class="hljs-comment">// 'disconnect' in the cluster master, and then it will fork</span>
27803 <span class="hljs-comment">// a new worker.</span>
27804 …cluster.<span class="hljs-property">worker</span>.<span class="hljs-title function_">disconnect</s…
27806 …<span class="hljs-comment">// Try to send an error to the request that triggered the problem</span>
27807 res.<span class="hljs-property">statusCode</span> = <span class="hljs-number">500</span>;
27808 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'content-type'</…
27809 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Oops, there was a pro…
27810 } <span class="hljs-keyword">catch</span> (er2) {
27811 <span class="hljs-comment">// Oh well, not much we can do at this point.</span>
27812 …js-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class=…
27816 … <span class="hljs-comment">// Because req and res were created before this domain existed,</span>
27817 <span class="hljs-comment">// we need to explicitly add them.</span>
27818 <span class="hljs-comment">// See the explanation of implicit vs explicit binding below.</span>
27819 d.<span class="hljs-title function_">add</span>(req);
27820 d.<span class="hljs-title function_">add</span>(res);
27822 <span class="hljs-comment">// Now run the handler function in the domain.</span>
27823 d.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
27824 <span class="hljs-title function_">handleRequest</span>(req, res);
27827 …server.<span class="hljs-title function_">listen</span>(<span class="hljs-variable constant_">PORT…
27830 <span class="hljs-comment">// This part is not important. Just an example routing thing.</span>
27831 <span class="hljs-comment">// Put fancy application logic here.</span>
27832 <span class="hljs-keyword">function</span> <span class="hljs-title function_">handleRequest</span>(…
27833 <span class="hljs-keyword">switch</span> (req.<span class="hljs-property">url</span>) {
27834 <span class="hljs-keyword">case</span> <span class="hljs-string">'/error'</span>:
27835 <span class="hljs-comment">// We do some async stuff, and then...</span>
27836 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
27837 <span class="hljs-comment">// Whoops!</span>
27838 flerb.<span class="hljs-title function_">bark</span>();
27840 <span class="hljs-keyword">break</span>;
27841 <span class="hljs-attr">default</span>:
27842 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
27864 to <code>fs.open()</code>, or other callback-taking methods) will automatically be
27886 <pre><code class="language-js"><span class="hljs-comment">// Create a top-level domain for the serv…
27887 <span class="hljs-keyword">const</span> domain = <span class="hljs-built_in">require</span>(<span c…
27888 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
27889 <span class="hljs-keyword">const</span> serverDomain = domain.<span class="hljs-title function_">cr…
27891 serverDomain.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span>…
27892 <span class="hljs-comment">// Server is created in the scope of serverDomain</span>
27893 …http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span cla…
27894 <span class="hljs-comment">// Req and res are also created in the scope of serverDomain</span>
27895 …<span class="hljs-comment">// however, we'd prefer to have a separate domain for each request.</sp…
27896 <span class="hljs-comment">// create it first thing, and add req and res to it.</span>
27897 …<span class="hljs-keyword">const</span> reqd = domain.<span class="hljs-title function_">create</s…
27898 reqd.<span class="hljs-title function_">add</span>(req);
27899 reqd.<span class="hljs-title function_">add</span>(res);
27900 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
27901-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
27902 <span class="hljs-keyword">try</span> {
27903 … res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">500</span>);
27904 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Error occurred, sorry…
27905 } <span class="hljs-keyword">catch</span> (er2) {
27906-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
27909 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);
27924 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array…
27943 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
27944 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
27949 <pre><code class="language-js"><span class="hljs-keyword">const</span> d = domain.<span class="hljs
27951 <span class="hljs-keyword">function</span> <span class="hljs-title function_">readSomeFile</span>(<…
27952-title function_">readFile</span>(filename, <span class="hljs-string">'utf8'</span>, d.<span class…
27953 <span class="hljs-comment">// If this throws, it will also be passed to the domain.</span>
27954-keyword">return</span> <span class="hljs-title function_">cb</span>(er, data ? <span class="hljs-
27958 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
27959 …<span class="hljs-comment">// An error occurred somewhere. If we throw it now, it will crash the p…
27960 <span class="hljs-comment">// with the normal line number and stack message.</span>
27971 single domain.</p>
27982 single domain.</p>
27985 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
27986 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
27992 with a single error handler in a single place.</p>
27993 <pre><code class="language-js"><span class="hljs-keyword">const</span> d = domain.<span class="hljs
27995 <span class="hljs-keyword">function</span> <span class="hljs-title function_">readSomeFile</span>(<…
27996-title function_">readFile</span>(filename, <span class="hljs-string">'utf8'</span>, d.<span class…
27997 <span class="hljs-comment">// Note, the first argument is never passed to the</span>
27998 <span class="hljs-comment">// callback since it is assumed to be the 'Error' argument</span>
27999 <span class="hljs-comment">// and thus intercepted by the domain.</span>
28001 <span class="hljs-comment">// If this throws, it will also be passed to the domain</span>
28002 <span class="hljs-comment">// so the error-handling logic can be moved to the 'error'</span>
28003 <span class="hljs-comment">// event on the domain instead of being repeated throughout</span>
28004 <span class="hljs-comment">// the program.</span>
28005-keyword">return</span> <span class="hljs-title function_">cb</span>(<span class="hljs-literal">nu…
28009 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
28010 …<span class="hljs-comment">// An error occurred somewhere. If we throw it now, it will crash the p…
28011 <span class="hljs-comment">// with the normal line number and stack message.</span>
28021 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
28022 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
28029 …ode class="language-js"><span class="hljs-keyword">const</span> domain = <span class="hljs-built_i…
28030 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
28031 <span class="hljs-keyword">const</span> d = domain.<span class="hljs-title function_">create</span>…
28032 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
28033 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
28035 d.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
28036 …process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span…
28037 …<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { <span cla…
28038-title function_">open</span>(<span class="hljs-string">'non-existent file'</span>, <span class="h…
28039 <span class="hljs-keyword">if</span> (er) <span class="hljs-keyword">throw</span> er;
28040 <span class="hljs-comment">// proceed...</span>
28042 }, <span class="hljs-number">100</span>);
28050 <pre><code class="language-js"><span class="hljs-keyword">const</span> d1 = domain.<span class="hlj…
28051 <span class="hljs-keyword">const</span> d2 = domain.<span class="hljs-title function_">create</span…
28053 <span class="hljs-keyword">let</span> p;
28054 d1.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
28055 …p = <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</spa…
28058 d2.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
28059 …p.<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-pa…
28060 <span class="hljs-comment">// running in d2</span>
28064 <pre><code class="language-js"><span class="hljs-keyword">const</span> d1 = domain.<span class="hlj…
28065 <span class="hljs-keyword">const</span> d2 = domain.<span class="hljs-title function_">create</span…
28067 <span class="hljs-keyword">let</span> p;
28068 d1.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
28069 …p = <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</spa…
28072 d2.<span class="hljs-title function_">run</span>(<span class="hljs-function">() =></span> {
28073-title function_">then</span>(p.<span class="hljs-property">domain</span>.<span class="hljs-title …
28074 <span class="hljs-comment">// running in d1</span>
28087-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError" class="type">&#x3C;EvalError></a>, <a …
28088-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError" class="type">&#x3C;ReferenceError…
28092 <li>User-specified errors triggered by application code.</li>
28098 instances of, the standard JavaScript <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
28108 are handled using the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/St…
28110 <pre><code class="language-js"><span class="hljs-comment">// Throws with a ReferenceError because z…
28111 <span class="hljs-keyword">try</span> {
28112 <span class="hljs-keyword">const</span> m = <span class="hljs-number">1</span>;
28113 <span class="hljs-keyword">const</span> n = m + z;
28114 } <span class="hljs-keyword">catch</span> (err) {
28115 <span class="hljs-comment">// Handle the error here.</span>
28130 <!-- eslint-disable no-useless-return -->
28131 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
28132 …hljs-title function_">readFile</span>(<span class="hljs-string">'a file that does not exist'</span…
28133 <span class="hljs-keyword">if</span> (err) {
28134 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
28135 <span class="hljs-keyword">return</span>;
28137 <span class="hljs-comment">// Otherwise handle the data</span>
28143 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
28144 <span class="hljs-keyword">const</span> connection = net.<span class="hljs-title function_">connect…
28146 <span class="hljs-comment">// Adding an 'error' event handler to a stream:</span>
28147 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
28148 <span class="hljs-comment">// If the connection is reset by the server, or if it can't</span>
28149 <span class="hljs-comment">// connect at all, or on any sort of error encountered by</span>
28150 <span class="hljs-comment">// the connection, the error will be sent here.</span>
28151 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
28154 connection.<span class="hljs-title function_">pipe</span>(process.<span class="hljs-property">stdou…
28164 … the <code>'error'</code> event mechanism is most common for <a href="stream.html">stream-based</a>
28165 and <a href="#events_class_eventemitter">event emitter-based</a> APIs, which themselves represent a…
28166 asynchronous operations over time (as opposed to a single operation that may
28173-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
28174 <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hlj…
28176 <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> {
28177 <span class="hljs-comment">// This will crash the process because no 'error' event</span>
28178 <span class="hljs-comment">// handler has been added.</span>
28179-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword"…
28185 <h4>Error-first callbacks<span><a class="mark" href="#errors_error_first_callbacks" id="errors_erro…
28188 pattern referred to as an <em>error-first callback</em>. With this pattern, a callback
28193 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
28195 <span class="hljs-keyword">function</span> <span class="hljs-title function_">errorFirstCallback</s…
28196 <span class="hljs-keyword">if</span> (err) {
28197 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
28198 <span class="hljs-keyword">return</span>;
28200 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
28203 fs.<span class="hljs-title function_">readFile</span>(<span class="hljs-string">'/some/file/that/do…
28204 fs.<span class="hljs-title function_">readFile</span>(<span class="hljs-string">'/some/file/that/do…
28207 use <code>throw</code> inside an error-first callback:</p>
28208 <pre><code class="language-js"><span class="hljs-comment">// THIS WILL NOT WORK:</span>
28209 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
28211 <span class="hljs-keyword">try</span> {
28212 …js-title function_">readFile</span>(<span class="hljs-string">'/some/file/that/does-not-exist'</sp…
28213 <span class="hljs-comment">// Mistaken assumption: throwing here...</span>
28214 <span class="hljs-keyword">if</span> (err) {
28215 <span class="hljs-keyword">throw</span> err;
28218 } <span class="hljs-keyword">catch</span> (err) {
28219 <span class="hljs-comment">// This will not catch the throw!</span>
28220 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
28230 <p>A generic JavaScript <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
28238 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
28244 are dependent on <a href="https://github.com/v8/v8/wiki/Stack-Trace-API">V8's stack trace API</a>. …
28249 <li><code>targetObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
28250 <li><code>constructorOpt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
28255 <pre><code class="language-js"><span class="hljs-keyword">const</span> myObject = {};
28256 <span class="hljs-title class_">Error</span>.<span class="hljs-title function_">captureStackTrace</…
28257 myObject.<span class="hljs-property">stack</span>; <span class="hljs-comment">// Similar to `new E…
28265 …de class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function…
28266-title class_">Error</span>.<span class="hljs-title function_">captureStackTrace</span>(<span clas…
28269 <span class="hljs-comment">// Without passing MyError to captureStackTrace, the MyError</span>
28270 <span class="hljs-comment">// frame would show up in the .stack property. By passing</span>
28271 <span class="hljs-comment">// the constructor, we omit that frame, and retain all frames below it.<…
28272 <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyError</span>().<span class=…
28275 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
28282 <p>If set to a non-number value, or set to a negative number, stack traces will
28286 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28291 change between any versions of Node.js. See <a href="#nodejs-error-codes">Node.js error codes</a> f…
28295 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28303-js"><span class="hljs-keyword">const</span> err = <span class="hljs-keyword">new</span> <span cla…
28304 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span…
28305 <span class="hljs-comment">// Prints: The message</span></code></pre>
28308 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28312 <pre><code class="language-console">Error: Things keep happening!
28314 at Frobnicator.refrobulate (/home/gbusey/business-logic.js:424:21)
28330 …language-js"><span class="hljs-keyword">const</span> cheetahify = <span class="hljs-built_in">requ…
28332 <span class="hljs-keyword">function</span> <span class="hljs-title function_">makeFaster</span>(<sp…
28333 <span class="hljs-comment">// `cheetahify()` *synchronously* calls speedy.</span>
28334 …ljs-title function_">cheetahify</span>(<span class="hljs-keyword">function</span> <span class="hlj…
28335 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
28339 <span class="hljs-title function_">makeFaster</span>();
28340 <span class="hljs-comment">// will throw:</span>
28341 <span class="hljs-comment">// /home/gbusey/file.js:6</span>
28342 <span class="hljs-comment">// throw new Error('oh no!');</span>
28343 <span class="hljs-comment">// ^</span>
28344 <span class="hljs-comment">// Error: oh no!</span>
28345 <span class="hljs-comment">// at speedy (/home/gbusey/file.js:6:11)</span>
28346 <span class="hljs-comment">// at makeFaster (/home/gbusey/file.js:5:3)</span>
28347 <span class="hljs-comment">// at Object.&#x3C;anonymous> (/home/gbusey/file.js:10:1)</span>
28348 <span class="hljs-comment">// at Module._compile (module.js:456:26)</span>
28349 <span class="hljs-comment">// at Object.Module._extensions..js (module.js:474:10)</span>
28350 <span class="hljs-comment">// at Module.load (module.js:356:32)</span>
28351 <span class="hljs-comment">// at Function.Module._load (module.js:312:12)</span>
28352 <span class="hljs-comment">// at Function.Module.runMain (module.js:497:10)</span>
28353 <span class="hljs-comment">// at startup (node.js:119:16)</span>
28354 <span class="hljs-comment">// at node.js:906:3</span></code></pre>
28358 <li><code>plain-filename.js:line:column</code>, if the frame represents a call internal
28381-js"><span class="hljs-built_in">require</span>(<span class="hljs-string">'net'</span>).<span clas…
28382 <span class="hljs-comment">// Throws "RangeError: "port" option should be >= 0 and &#x3C; 65536: -1…
28394 <pre><code class="language-js">doesNotExist;
28395 <span class="hljs-comment">// Throws ReferenceError, doesNotExist is not a variable in this program…
28406 <pre><code class="language-js"><span class="hljs-keyword">try</span> {
28407 …"hljs-built_in">require</span>(<span class="hljs-string">'vm'</span>).<span class="hljs-title func…
28408 } <span class="hljs-keyword">catch</span> (err) {
28409 <span class="hljs-comment">// 'err' will be a SyntaxError.</span>
28422 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
28424 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
28425 <li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
28427 …ps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x…
28428 <li><code>info</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
28429 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;stri…
28430 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
28431 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
28432 <li><code>syscall</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
28436 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28442 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28447 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28453 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
28462 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
28467 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28469 <p><code>error.message</code> is a system-provided human-readable description of the error.</p>
28472 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28477 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
28482 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
28484 …ode> property is a string describing the <a href="https://man7.org/linux/man-pages/man2/syscalls.2…
28486 <p>This is a list of system errors commonly-encountered when writing a Node.js
28487 program. For a comprehensive list, see the <a href="https://man7.org/linux/man-pages/man3/errno.3.h…
28500 target machine actively refused it. This usually results from trying to
28510 <p><code>EEXIST</code> (File exists): An existing file was the target of an operation that
28511 required that the target not exist.</p>
28524 <code>ulimit -n 2048</code> in the same shell that will run the Node.js process.</p>
28536 <p><code>ENOTEMPTY</code> (Directory not empty): A directory with entries was the target
28566-js"><span class="hljs-built_in">require</span>(<span class="hljs-string">'url'</span>).<span clas…
28567 <span class="hljs-comment">// Throws TypeError, since it expected a string.</span></code></pre>
28573 operation or as the target of a <code>throw</code> statement. While it is not required
28583 OpenSSL-specific properties.</p>
28592 <p>A human-readable string describing the reason for the error.</p>
28593 <p><a id="nodejs-error-codes"></a></p>
28667 <!--
28670 - version: 11.12.0
28671 pr-url: https://github.com/nodejs/node/pull/26487
28673 - version: v14.17.1
28674 pr-url: https://github.com/nodejs/node/pull/38510
28676 -->
28682 non-writable <code>stdout</code> or <code>stderr</code> stream.</p>
28685 <!--
28687 -->
28720 <p>The <a href="#cli_force_fips"><code>--force-fips</code></a> command-line argument was used but t…
28773 <p>An unknown Diffie-Hellman group name was given. See
28780 <p>Loading native addons has been disabled using <a href="#cli_no_addons"><code>--no-addons</code><…
28811 <p><code>c-ares</code> failed to set the DNS server.</p>
28834 <p><code>--print</code> cannot be used with ESM input.</p>
28873 <p>Status code was outside the regular status code range (100-999).</p>
28886 <p>For HTTP/2 requests using the <code>CONNECT</code> method, the <code>:authority</code> pseudo-he…
28890 <p>For HTTP/2 requests using the <code>CONNECT</code> method, the <code>:path</code> pseudo-header …
28894 <p>For HTTP/2 requests using the <code>CONNECT</code> method, the <code>:scheme</code> pseudo-heade…
28898 <p>A non-specific HTTP/2 error has occurred.</p>
28906 have only a single value.</p>
28973 <p>The number of streams created on a single HTTP/2 session reached the maximum
28987 <p>An HTTP/2 pseudo-header has been used inappropriately. Pseudo-headers are header
29004 <p>The <code>Http2Session</code> closed with a non-zero error code.</p>
29029 <p>A non-zero error code was been specified in an <code>RST_STREAM</code> frame.</p>
29054 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
29055 <p>The <code>--input-type</code> flag was used to attempt to execute a file. This flag can
29056 only be used with input via <code>--eval</code>, <code>--print</code> or <code>STDIN</code>.</p>
29103 less than -1 should never happen.</p>
29128 situation can only occur on Unix-like systems where only <code>localhost</code> or an empty
29134 semantics for determining whether a path can be used is platform-dependent.</p>
29168 …de> <a href="#packages_exports"><code>"exports"</code></a> field contains an invalid target mapping
29205-js"><span class="hljs-keyword">const</span> urlSearchParams = <span class="hljs-keyword">new</spa…
29207 …="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs
29208 …SearchParams.<span class="hljs-property">has</span>.<span class="hljs-title function_">call</span>…
29209 <span class="hljs-comment">// Throws a TypeError with code 'ERR_INVALID_THIS'</span></code></pre>
29299 …ker_threads_class_messageport"><code>MessagePort</code></a> could not be deserialized in the target
29332 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
29342 <p>While using <code>Node-API</code>, a constructor passed was not a function.</p>
29356 <p>An error occurred while invoking the JavaScript portion of the thread-safe
29365 thread-safe function in an idle loop. This error indicates that an error
29381 <p>A non-context-aware native addon was loaded in a process that disallows them.</p>
29401 … href="#cli_disable_proto_mode"><code>--disable-proto=throw</code></a>. <a href="https://developer…
29402 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/se…
29406 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
29460 <a href="https://www.w3.org/TR/SRI/#the-integrity-attribute">Subresource Integrity specification</a…
29491-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">Socket</span> = <span…
29492 <span class="hljs-keyword">const</span> instance = <span class="hljs-keyword">new</span> <span clas…
29494 instance.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</sp…
29509 <p>An unspecified or non-specific system error has occurred within the Node.js
29514 <p>This error is thrown by <code>checkServerIdentity</code> if a user-supplied
29524 <p>While using TLS, the parameter offered for the Diffie-Hellman (<code>DH</code>)
29525 key-agreement protocol is too small. By default, the key length must be greater
29569 vector for denial-of-service attacks.</p>
29572 <p>An attempt was made to issue Server Name Indication from a TLS server-side
29581 the <code>--without-v8-platform</code> flag.</p>
29621 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
29626 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
29635 <a href="#packages_self_referencing_a_package_using_its_name">self-reference a package using its na…
29637 <!-- eslint-skip -->
29638 …pre><code class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-string">'…
29639 <span class="hljs-keyword">import</span> <span class="hljs-string">'./index.js'</span>; <span class…
29640 <span class="hljs-keyword">import</span> <span class="hljs-string">'package-name'</span>; <span cla…
29736 <p>Server is sending both a <code>Content-Length</code> header and <code>Transfer-Encoding: chunked…
29737 <p><code>Transfer-Encoding: chunked</code> allows the server to maintain an HTTP persistent
29739 In this case, the <code>Content-Length</code> HTTP header cannot be used.</p>
29740 <p>Use <code>Content-Length</code> or <code>Transfer-Encoding: chunked</code>.</p>
29755 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. These er…
29759 <!--
29762 -->
29770 <p>The UTF-16 encoding was used with <a href="#crypto_hash_digest_encoding"><code>hash.digest()</co…
29772 causing the method to return a string rather than a <code>Buffer</code>, the UTF-16
29828 <p>Used by the <code>Node-API</code> when <code>Constructor.prototype</code> is not an object.</p>
29894 <p>Used when a TLS renegotiation request has failed in a non-specific way.</p>
29943 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
29947 event-driven architecture in which certain kinds of objects (called "emitters")
29955 event names are camel-cased strings but any valid JavaScript property key
29960 <p>The following example shows a simple <code>EventEmitter</code> instance with a single
29963-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
29965 …lass="hljs-keyword">class</span> <span class="hljs-title class_">MyEmitter</span> <span class="hlj…
29967 <span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span cla…
29968 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <s…
29969 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
29971 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);…
29978 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
29979 …class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs
29980-variable language_">console</span>.<span class="hljs-title function_">log</span>(a, b, <span clas…
29981 <span class="hljs-comment">// Prints:</span>
29982 <span class="hljs-comment">// a b MyEmitter {</span>
29983 <span class="hljs-comment">// domain: null,</span>
29984 <span class="hljs-comment">// _events: { event: [Function] },</span>
29985 <span class="hljs-comment">// _eventsCount: 1,</span>
29986 <span class="hljs-comment">// _maxListeners: undefined } true</span>
29988 … class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>, <span class="h…
29991 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
29992 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span clas…
29993 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
29994 <span class="hljs-comment">// Prints: a b {}</span>
29996 … class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>, <span class="h…
30003 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30004 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span clas…
30005 <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> {
30006 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30009 … class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>, <span class="h…
30013 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30014 <span class="hljs-keyword">let</span> m = <span class="hljs-number">0</span>;
30015 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <s…
30016 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30018 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30019 <span class="hljs-comment">// Prints: 1</span>
30020 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30021 <span class="hljs-comment">// Prints: 2</span></code></pre>
30025 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30026 <span class="hljs-keyword">let</span> m = <span class="hljs-number">0</span>;
30027 myEmitter.<span class="hljs-title function_">once</span>(<span class="hljs-string">'event'</span>, …
30028 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30030 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30031 <span class="hljs-comment">// Prints: 1</span>
30032 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30033 <span class="hljs-comment">// Ignored</span></code></pre>
30041 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30042-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword"…
30043 <span class="hljs-comment">// Throws and crashes Node.js</span></code></pre>
30047 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30048 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
30049 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
30051-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword"…
30052 <span class="hljs-comment">// Prints: whoops! there was an error</span></code></pre>
30055-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30057 <span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span cla…
30058 myEmitter.<span class="hljs-title function_">on</span>(errorMonitor, <span class="hljs-function">(<…
30059 …<span class="hljs-title class_">MyMonitoringTool</span>.<span class="hljs-title function_">log</sp…
30061-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword"…
30062 <span class="hljs-comment">// Still throws and crashes Node.js</span></code></pre>
30064 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - captureRejections is…
30067 …e><code class="language-js"><span class="hljs-keyword">const</span> ee = <span class="hljs-keyword…
30068 ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span…
30069 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
30076-js"><span class="hljs-keyword">const</span> ee1 = <span class="hljs-keyword">new</span> <span cla…
30077 ee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <spa…
30078 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
30081 …="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-vari…
30083-keyword">const</span> ee2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
30084 ee2.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <spa…
30085 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
30088-title class_">Symbol</span>.<span class="hljs-title function_">for</span>(<span class="hljs-strin…
30091 …ode class="language-js"><span class="hljs-keyword">const</span> events = <span class="hljs-built_i…
30092 events.<span class="hljs-property">captureRejections</span> = <span class="hljs-literal">true</span…
30093 <span class="hljs-keyword">const</span> ee1 = <span class="hljs-keyword">new</span> events.<span cl…
30094 ee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <spa…
30095 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
30098 …="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-vari…
30115-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
30120 <li><code>captureRejections</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
30129 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30130 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30140-js"><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyEmitter</span> <spa…
30142 <span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span cla…
30143 <span class="hljs-comment">// Only do this once so we don't loop forever</span>
30144 …an class="hljs-title function_">once</span>(<span class="hljs-string">'newListener'</span>, <span …
30145 <span class="hljs-keyword">if</span> (event === <span class="hljs-string">'event'</span>) {
30146 <span class="hljs-comment">// Insert a new listener in front</span>
30147 …myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <…
30148 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30152 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <s…
30153 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30155 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30156 <span class="hljs-comment">// Prints:</span>
30157 <span class="hljs-comment">// B</span>
30158 <span class="hljs-comment">// A</span></code></pre>
30172 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30173 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30181 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30182 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30190 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30191 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
30192 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
30198-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
30199 <span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span cla…
30201 <span class="hljs-comment">// First listener</span>
30202-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-keyword">f…
30203 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30205 <span class="hljs-comment">// Second listener</span>
30206-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-keyword">f…
30207-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
30209 <span class="hljs-comment">// Third listener</span>
30210-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-keyword">f…
30211 …<span class="hljs-keyword">const</span> parameters = args.<span class="hljs-title function_">join<…
30212 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
30215-variable language_">console</span>.<span class="hljs-title function_">log</span>(myEmitter.<span …
30217-title function_">emit</span>(<span class="hljs-string">'event'</span>, <span class="hljs-number">…
30219 <span class="hljs-comment">// Prints:</span>
30220 <span class="hljs-comment">// [</span>
30221 <span class="hljs-comment">// [Function: firstListener],</span>
30222 <span class="hljs-comment">// [Function: secondListener],</span>
30223 <span class="hljs-comment">// [Function: thirdListener]</span>
30224 <span class="hljs-comment">// ]</span>
30225 <span class="hljs-comment">// Helloooo! first listener</span>
30226 <span class="hljs-comment">// event with parameters 1, 2 in second listener</span>
30227 <span class="hljs-comment">// event with parameters 1, 2, 3, 4, 5 in third listener</span></code></…
30233 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
30237-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
30238 <span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="h…
30239 myEE.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span cla…
30240 myEE.<span class="hljs-title function_">on</span>(<span class="hljs-string">'bar'</span>, <span cla…
30242 <span class="hljs-keyword">const</span> sym = <span class="hljs-title class_">Symbol</span>(<span c…
30243 myEE.<span class="hljs-title function_">on</span>(sym, <span class="hljs-function">() =></span> {});
30245 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
30246 <span class="hljs-comment">// Prints: [ 'foo', 'bar', Symbol(symbol) ]</span></code></pre>
30252 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
30262 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30263 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
30279 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30280 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
30283 …e-js">server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</…
30284 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30286-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class…
30287 <span class="hljs-comment">// Prints: [ [Function] ]</span></code></pre>
30293 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30294 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30303 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30304 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30312 …e-js">server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</…
30313 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30319 …><code class="language-js"><span class="hljs-keyword">const</span> myEE = <span class="hljs-keywor…
30320-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">()…
30321-title function_">prependListener</span>(<span class="hljs-string">'foo'</span>, <span class="hljs
30322 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>);
30323 <span class="hljs-comment">// Prints:</span>
30324 <span class="hljs-comment">// b</span>
30325 <span class="hljs-comment">// a</span></code></pre>
30331 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30332 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30335 <p>Adds a <strong>one-time</strong> <code>listener</code> function for the event named <code>eventN…
30337-js">server.<span class="hljs-title function_">once</span>(<span class="hljs-string">'connection'<…
30338 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30344 …><code class="language-js"><span class="hljs-keyword">const</span> myEE = <span class="hljs-keywor…
30345-title function_">once</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">…
30346-title function_">prependOnceListener</span>(<span class="hljs-string">'foo'</span>, <span class="…
30347 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>);
30348 <span class="hljs-comment">// Prints:</span>
30349 <span class="hljs-comment">// b</span>
30350 <span class="hljs-comment">// a</span></code></pre>
30356 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30357 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30365-js">server.<span class="hljs-title function_">prependListener</span>(<span class="hljs-string">'c…
30366 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30374 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30375 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30378 <p>Adds a <strong>one-time</strong> <code>listener</code> function for the event named <code>eventN…
30381-js">server.<span class="hljs-title function_">prependOnceListener</span>(<span class="hljs-string…
30382 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30390 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30403 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30404 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
30409 …de class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-title function_">…
30410 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30412 server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, …
30413 <span class="hljs-comment">// ...</span>
30414 server.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'connecti…
30416 listener array. If any single listener has been added multiple times to the
30424 …code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-key…
30426 <span class="hljs-keyword">const</span> <span class="hljs-title function_">callbackA</span> = (<spa…
30427 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30428 …myEmitter.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'even…
30431 <span class="hljs-keyword">const</span> <span class="hljs-title function_">callbackB</span> = (<spa…
30432 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30435 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, ca…
30437 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, ca…
30439 <span class="hljs-comment">// callbackA removes listener callbackB but it will still be called.</sp…
30440 <span class="hljs-comment">// Internal listener array at time of emit [callbackA, callbackB]</span>
30441 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30442 <span class="hljs-comment">// Prints:</span>
30443 <span class="hljs-comment">// A</span>
30444 <span class="hljs-comment">// B</span>
30446 <span class="hljs-comment">// callbackB is now removed.</span>
30447 <span class="hljs-comment">// Internal listener array [callbackA]</span>
30448 myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
30449 <span class="hljs-comment">// Prints:</span>
30450 <span class="hljs-comment">// A</span></code></pre>
30456 <p>When a single function has been added as a handler multiple times for a single
30460 …e><code class="language-js"><span class="hljs-keyword">const</span> ee = <span class="hljs-keyword…
30462 <span class="hljs-keyword">function</span> <span class="hljs-title function_">pong</span>(<span cla…
30463 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30466 ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'ping'</span>, pong);
30467 ee.<span class="hljs-title function_">once</span>(<span class="hljs-string">'ping'</span>, pong);
30468 ee.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'ping'</span>…
30470 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'ping'</span>);
30471 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'ping'</span>);</code><…
30478 <li><code>n</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
30492 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30493 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
30497 …<code class="language-js"><span class="hljs-keyword">const</span> emitter = <span class="hljs-keyw…
30498-title function_">once</span>(<span class="hljs-string">'log'</span>, <span class="hljs-function">…
30500 <span class="hljs-comment">// Returns a new Array with a function `onceWrapper` which has a propert…
30501 <span class="hljs-comment">// `listener` which contains the original listener bound above</span>
30502 <span class="hljs-keyword">const</span> listeners = emitter.<span class="hljs-title function_">rawL…
30503 <span class="hljs-keyword">const</span> logFnWrapper = listeners[<span class="hljs-number">0</span>…
30505 <span class="hljs-comment">// Logs "log once" to the console and does not unbind the `once` event</…
30506 logFnWrapper.<span class="hljs-title function_">listener</span>();
30508 <span class="hljs-comment">// Logs "log once" to the console and removes the listener</span>
30509 <span class="hljs-title function_">logFnWrapper</span>();
30511-title function_">on</span>(<span class="hljs-string">'log'</span>, <span class="hljs-function">()…
30512 <span class="hljs-comment">// Will return a new Array with a single function bound by `.on()` above…
30513 <span class="hljs-keyword">const</span> newListeners = emitter.<span class="hljs-title function_">r…
30515 <span class="hljs-comment">// Logs "log persistently" twice</span>
30516 newListeners[<span class="hljs-number">0</span>]();
30517 emitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'log'</span>);</co…
30522 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - captureRejections is…
30525 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30526 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
30533-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30535 …class="hljs-keyword">class</span> <span class="hljs-title class_">MyClass</span> <span class="hljs
30536 <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) {
30537 …<span class="hljs-variable language_">super</span>({ <span class="hljs-attr">captureRejections</sp…
30541-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
30542 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">destroy</span…
30545 <span class="hljs-title function_">destroy</span>(<span class="hljs-params">err</span>) {
30546 <span class="hljs-comment">// Tear the resource down here.</span>
30553 <p>By default, a maximum of <code>10</code> listeners can be registered for any single
30565 that a "possible EventEmitter memory leak" has been detected. For any single
30568-js">emitter.<span class="hljs-title function_">setMaxListeners</span>(emitter.<span class="hljs-t…
30569 emitter.<span class="hljs-title function_">once</span>(<span class="hljs-string">'event'</span>, <s…
30570 <span class="hljs-comment">// do stuff</span>
30571-title function_">setMaxListeners</span>(<span class="hljs-title class_">Math</span>.<span class="…
30573 <p>The <a href="#cli_trace_warnings"><code>--trace-warnings</code></a> command-line flag can be use…
30596 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30597 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
30603 event target. This is useful for debugging and diagnostic purposes.</p>
30604-js"><span class="hljs-keyword">const</span> { getEventListeners, <span class="hljs-title class_">…
30607 …<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hl…
30608-keyword">const</span> <span class="hljs-title function_">listener</span> = (<span class="hljs-par…
30609 ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, listener);
30610 …<span class="hljs-title function_">getEventListeners</span>(ee, <span class="hljs-string">'foo'</s…
30613 …<span class="hljs-keyword">const</span> et = <span class="hljs-keyword">new</span> <span class="hl…
30614-keyword">const</span> <span class="hljs-title function_">listener</span> = (<span class="hljs-par…
30615 …et.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</spa…
30616 …<span class="hljs-title function_">getEventListeners</span>(et, <span class="hljs-string">'foo'</s…
30632 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
30633 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
30638 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
30645 <a href="https://dom.spec.whatwg.org/#interface-eventtarget">EventTarget</a> interface, which has n…
30647-js"><span class="hljs-keyword">const</span> { once, <span class="hljs-title class_">EventEmitter<…
30649 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
30650 …<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hl…
30652 …process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span…
30653 …ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'myevent'</span>, <spa…
30656 …"hljs-keyword">const</span> [value] = <span class="hljs-keyword">await</span> <span class="hljs-ti…
30657 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30659 …class="hljs-keyword">const</span> err = <span class="hljs-keyword">new</span> <span class="hljs-ti…
30660 …process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span…
30661 … ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'error'</span>, err);
30664 <span class="hljs-keyword">try</span> {
30665 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(ee, <span c…
30666 } <span class="hljs-keyword">catch</span> (err) {
30667 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30671 <span class="hljs-title function_">run</span>();</code></pre>
30676-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30678 <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hlj…
30680 <span class="hljs-title function_">once</span>(ee, <span class="hljs-string">'error'</span>)
30681-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">[err]</span>)…
30682-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-params">err</span>) …
30684-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword"…
30686 <span class="hljs-comment">// Prints: ok boom</span></code></pre>
30688-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30690 <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hlj…
30691 <span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hlj…
30693 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
30694 <span class="hljs-keyword">try</span> {
30695 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(emitter, ev…
30696 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30697 } <span class="hljs-keyword">catch</span> (error) {
30698 …<span class="hljs-keyword">if</span> (error.<span class="hljs-property">name</span> === <span clas…
30699 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
30700 } <span class="hljs-keyword">else</span> {
30701-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
30706 <span class="hljs-title function_">foo</span>(ee, <span class="hljs-string">'foo'</span>, ac.<span …
30707 ac.<span class="hljs-title function_">abort</span>(); <span class="hljs-comment">// Abort waiting f…
30708 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); <span cl…
30716-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30718 <span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="h…
30720 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
30721 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(myEE, <span…
30722 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30724 <span class="hljs-comment">// This Promise will never resolve because the 'foo' event will</span>
30725 <span class="hljs-comment">// have already been emitted before the Promise is created.</span>
30726 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(myEE, <span…
30727 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30730 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span>…
30731 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'bar'</span>);
30732 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>);
30735-title function_">foo</span>().<span class="hljs-title function_">then</span>(<span class="hljs-fu…
30739-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30741 <span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="h…
30743 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
30744-keyword">await</span> <span class="hljs-title class_">Promise</span>.<span class="hljs-title func…
30745 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
30748 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span>…
30749 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'bar'</span>);
30750 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>);
30753-title function_">foo</span>().<span class="hljs-title function_">then</span>(<span class="hljs-fu…
30758 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - captureRejections is…
30759 <p>Value: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
30765 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - captureRejections is…
30772 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
30775 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30779-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>…
30780 <span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span cla…
30781 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <s…
30782 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <s…
30783-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
30784 <span class="hljs-comment">// Prints: 2</span></code></pre>
30791 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
30792 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
30797 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x…
30799-js"><span class="hljs-keyword">const</span> { on, <span class="hljs-title class_">EventEmitter</s…
30801 (<span class="hljs-keyword">async</span> () => {
30802 …<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hl…
30804 <span class="hljs-comment">// Emit later on</span>
30805 …process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span…
30806 …ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span cl…
30807 …ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span cl…
30810-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</sp…
30811 <span class="hljs-comment">// The execution of this inner block is synchronous and it</span>
30812 <span class="hljs-comment">// processes one event at a time (even with await). Do not use</span>
30813 <span class="hljs-comment">// if concurrent execution is required.</span>
30814 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30816 <span class="hljs-comment">// Unreachable here</span>
30823-js"><span class="hljs-keyword">const</span> { on, <span class="hljs-title class_">EventEmitter</s…
30824 <span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hlj…
30826 (<span class="hljs-keyword">async</span> () => {
30827 …<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hl…
30829 <span class="hljs-comment">// Emit later on</span>
30830 …process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span…
30831 …ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span cl…
30832 …ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span cl…
30835-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</sp…
30836 <span class="hljs-comment">// The execution of this inner block is synchronous and it</span>
30837 <span class="hljs-comment">// processes one event at a time (even with await). Do not use</span>
30838 <span class="hljs-comment">// if concurrent execution is required.</span>
30839 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30841 <span class="hljs-comment">// Unreachable here</span>
30844 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span>…
30850 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&…
30856 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
30858 <span class="hljs-title class_">EventEmitter</span>
30859 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
30861 <span class="hljs-keyword">const</span> target = <span class="hljs-keyword">new</span> <span class=…
30862 <span class="hljs-keyword">const</span> emitter = <span class="hljs-keyword">new</span> <span class…
30864 <span class="hljs-title function_">setMaxListeners</span>(<span class="hljs-number">5</span>, targe…
30865 <p><a id="event-target-and-event-api"></a></p>
30870 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
30871 <p>The <code>EventTarget</code> and <code>Event</code> objects are a Node.js-specific implementation
30875 <pre><code class="language-js"><span class="hljs-keyword">const</span> target = <span class="hljs-t…
30877 target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</…
30878 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
30887 nested target objects that may each have their own set of handlers for the
30926 …de class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function…
30927 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span cl…
30928 event.<span class="hljs-property">a</span> = <span class="hljs-number">1</span>;
30931 …ass="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title…
30932 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span cl…
30933 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span c…
30936 <span class="hljs-keyword">const</span> handler3 = {
30937 <span class="hljs-title function_">handleEvent</span>(<span class="hljs-params">event</span>) {
30938 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span cl…
30942 <span class="hljs-keyword">const</span> handler4 = {
30943 …<span class="hljs-keyword">async</span> <span class="hljs-title function_">handleEvent</span>(<spa…
30944 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span cl…
30948 <span class="hljs-keyword">const</span> target = <span class="hljs-title function_">getEventTargetS…
30950 target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</…
30951 target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</…
30952 target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</…
30953 target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</…
30979 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
30993 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
31000 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
31017 <p>Alias for <code>event.target</code>.</p>
31023 <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#Number_t…
31041 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
31055 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
31065 <p>Alias for <code>event.target</code>.</p>
31076 <h5><code>event.target</code><span><a class="mark" href="#events_event_target" id="events_event_tar…
31088 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_t…
31096 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
31108 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31109 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
31110 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31112 <li><code>once</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31114 <li><code>passive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
31117 <li><code>capture</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
31131 …de class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function…
31133 <span class="hljs-keyword">const</span> target = <span class="hljs-title function_">getEventTargetS…
31134 target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</…
31135 target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</…
31137 <span class="hljs-comment">// Removes the second instance of handler</span>
31138 target.<span class="hljs-title function_">removeEventListener</span>(<span class="hljs-string">'foo…
31140 <span class="hljs-comment">// Removes the first instance of handler</span>
31141 target.<span class="hljs-title function_">removeEventListener</span>(<span class="hljs-string">'foo…
31148 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
31159 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31160 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
31161 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31163 <li><code>capture</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
31175 <p>The <code>NodeEventTarget</code> is a Node.js-specific extension to <code>EventTarget</code>
31183 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31186 <p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31189 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
31191 <li><code>once</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31198 <p>Node.js-specific extension to the <code>EventTarget</code> class that emulates the
31207 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
31209 <p>Node.js-specific extension to the <code>EventTarget</code> class that returns an array
31217 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31220 <p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number…
31223 <p>Node.js-specific extension to the <code>EventTarget</code> class that returns the number
31231 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31234 <p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31240 <p>Node.js-specific alias for <code>eventTarget.removeListener()</code>.</p>
31247 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31250 <p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31253 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
31255 <li><code>once</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31262 <p>Node.js-specific alias for <code>eventTarget.addListener()</code>.</p>
31269 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31272 <p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31275 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
31281 <p>Node.js-specific extension to the <code>EventTarget</code> class that adds a <code>once</code>
31290 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31296 <p>Node.js-specific extension to the <code>EventTarget</code> class. If <code>type</code> is specif…
31305 <p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31308 <p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31314 <p>Node.js-specific extension to the <code>EventTarget</code> class that removes the
31321 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
31326 <p>To use the promise-based APIs:</p>
31328-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
31331-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
31332 <p>All file system operations have synchronous, callback, and promise-based
31335 <p>Promise-based operations return a promise that is fulfilled when the
31338-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
31340 <span class="hljs-keyword">try</span> {
31341 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">unlink</span>(<span cla…
31342 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31343 } <span class="hljs-keyword">catch</span> (error) {
31344-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
31345 …class="language-js cjs"><span class="hljs-keyword">const</span> { unlink } = <span class="hljs-bui…
31347 (<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hl…
31348 <span class="hljs-keyword">try</span> {
31349 <span class="hljs-keyword">await</span> <span class="hljs-title function_">unlink</span>(path);
31350 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
31351 } <span class="hljs-keyword">catch</span> (error) {
31352-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
31354 })(<span class="hljs-string">'/tmp/hello'</span>);</code></pre>
31362-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
31364 …n class="hljs-title function_">unlink</span>(<span class="hljs-string">'/tmp/hello'</span>, <span …
31365 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
31366 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31367 …class="language-js cjs"><span class="hljs-keyword">const</span> { unlink } = <span class="hljs-bui…
31369 …n class="hljs-title function_">unlink</span>(<span class="hljs-string">'/tmp/hello'</span>, <span …
31370 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
31371 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31373 <p>The callback-based versions of the <code>fs</code> module APIs are preferable over
31381-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
31383 <span class="hljs-keyword">try</span> {
31384 …<span class="hljs-title function_">unlinkSync</span>(<span class="hljs-string">'/tmp/hello'</span>…
31385 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31386 } <span class="hljs-keyword">catch</span> (err) {
31387 <span class="hljs-comment">// handle the error</span>
31388 …ass="language-js cjs"><span class="hljs-keyword">const</span> { unlinkSync } = <span class="hljs-b…
31390 <span class="hljs-keyword">try</span> {
31391 …<span class="hljs-title function_">unlinkSync</span>(<span class="hljs-string">'/tmp/hello'</span>…
31392 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31393 } <span class="hljs-keyword">catch</span> (err) {
31394 <span class="hljs-comment">// handle the error</span>
31436-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
31437 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
31439 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
31442 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31453 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31454 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31456 <p>Modifies the permissions on the file. See <a href="http://man7.org/linux/man-pages/man2/chmod.2.…
31462 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31463 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31464 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31466 <p>Changes the ownership of the file. A wrapper for <a href="http://man7.org/linux/man-pages/man2/c…
31472 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31476 …de class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-key…
31478 <span class="hljs-keyword">let</span> filehandle;
31479 <span class="hljs-keyword">try</span> {
31480 …ss="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-st…
31481 } <span class="hljs-keyword">finally</span> {
31482 …<span class="hljs-keyword">await</span> filehandle?.<span class="hljs-title function_">close</span…
31489 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31493 <a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html"><code>fdatasync(2)</code></a> docum…
31500 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
31507 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
31509 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31511 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31513 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31517 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31519 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
31520 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
31526 <p>If the file is not modified concurrently, the end-of-file is reached when the
31533 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31535 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
31537 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31539 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31541 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31547 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31549 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
31550 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
31556 <p>If the file is not modified concurrently, the end-of-file is reached when the
31563 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
31565 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
31566 …class_abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress readFile</li>
31569 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31585 …rg/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]>…
31586 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31589 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31591 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
31592 …rg/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]>…
31597 <p>Read from a file and write to an array of <a href="https://developer.mozilla.org/en-US/docs/Web/…
31611 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
31613 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31617 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31624 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31628 Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/fsync.2.html"><code>fsync(2)</code…
31634 <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31635 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31641 …de class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-key…
31643 <span class="hljs-keyword">let</span> filehandle = <span class="hljs-literal">null</span>;
31644 <span class="hljs-keyword">try</span> {
31645 …lass="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-
31646 …<span class="hljs-keyword">await</span> filehandle.<span class="hljs-title function_">truncate</sp…
31647 } <span class="hljs-keyword">finally</span> {
31648 …<span class="hljs-keyword">await</span> filehandle?.<span class="hljs-title function_">close</span…
31658-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
31659-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
31660 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31681-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
31682 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31684 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
31686 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31688 the data will be written at the current position. See the POSIX <a href="http://man7.org/linux/man-
31690 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31697 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
31698-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
31722 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
31723 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31725 data will be written at the current position. See the POSIX <a href="http://man7.org/linux/man-page…
31727 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31728 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31734 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
31735 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
31760-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
31761 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
31763 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
31767 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31770 …github.io/ecma262/#sec-asynciterable-interface" class="type">&#x3C;AsyncIterable></a> or <a href="…
31786 …rg/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]>…
31787 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
31790 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31792 <p>Write an array of <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" cla…
31795 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
31796 …rg/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]>…
31809 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31810 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31811 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31820 with an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/E…
31822 …e class="language-js mjs"><span class="hljs-keyword">import</span> { access } <span class="hljs-ke…
31823 <span class="hljs-keyword">import</span> { constants } <span class="hljs-keyword">from</span> <span…
31825 <span class="hljs-keyword">try</span> {
31826-keyword">await</span> <span class="hljs-title function_">access</span>(<span class="hljs-string">…
31827 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31828 } <span class="hljs-keyword">catch</span> {
31829 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
31841 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31842 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31843 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
31845 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
31846 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31847 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31850 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31864 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31865 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
31866 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31874 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31875 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31876 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31877 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31893 <li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31894 <li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31895 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31904 a copy-on-write reflink. If the platform does not support copy-on-write,
31907 create a copy-on-write reflink. If the platform does not support
31908 copy-on-write, then the operation will fail.</li>
31911 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31918 … class="language-js mjs"><span class="hljs-keyword">import</span> { constants } <span class="hljs-
31919 <span class="hljs-keyword">import</span> { copyFile } <span class="hljs-keyword">from</span> <span …
31921 <span class="hljs-keyword">try</span> {
31922 …s="hljs-keyword">await</span> <span class="hljs-title function_">copyFile</span>(<span class="hljs
31923 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31924 } <span class="hljs-keyword">catch</span> {
31925 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31928 <span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt ex…
31929 <span class="hljs-keyword">try</span> {
31930-keyword">await</span> <span class="hljs-title function_">copyFile</span>(<span class="hljs-string…
31931 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31932 } <span class="hljs-keyword">catch</span> {
31933 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
31940 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31941 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31942 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31959 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31960 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31961 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
31962 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31970 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
31971-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
31972-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
31973 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31984 <li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
31985 <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
31986 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
31989 <a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a> documentation f…
32003 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32004 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
32006 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
32010 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32014 in which case the link itself is stat-ed, not the file that it refers to.
32015 Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/lstat.2.html"><code>lstat(2)</code…
32021 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32022 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
32024 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
32025 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32028 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32050 <li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
32051 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32053 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32056 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32066 …e class="language-js mjs"><span class="hljs-keyword">import</span> { mkdtemp } <span class="hljs-k…
32068 <span class="hljs-keyword">try</span> {
32069-keyword">await</span> <span class="hljs-title function_">mkdtemp</span>(path.<span class="hljs-ti…
32070 } <span class="hljs-keyword">catch</span> (err) {
32071 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32076 <code>prefix</code> must end with a trailing platform-specific path separator
32091 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32092 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32094 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32096 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32099 <p>Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/open.2.html"><code>open(2)</cod…
32101 by <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file">Naming Files, Pa…
32103 <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams">this MSDN page</a>.…
32117 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32118 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
32120 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32121 <li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
32126 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32129 <a href="http://man7.org/linux/man-pages/man3/opendir.3.html"><code>opendir(3)</code></a> documenta…
32135 …e class="language-js mjs"><span class="hljs-keyword">import</span> { opendir } <span class="hljs-k…
32137 <span class="hljs-keyword">try</span> {
32138 …ss="hljs-keyword">const</span> dir = <span class="hljs-keyword">await</span> <span class="hljs-tit…
32139 …n class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keywo…
32140 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
32141 } <span class="hljs-keyword">catch</span> (err) {
32142 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32159 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32160 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32162 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32163 <li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
32166 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32176 …e class="language-js mjs"><span class="hljs-keyword">import</span> { readdir } <span class="hljs-k…
32178 <span class="hljs-keyword">try</span> {
32179 …<span class="hljs-keyword">const</span> files = <span class="hljs-keyword">await</span> <span clas…
32180 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> file <span class="h…
32181 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
32182 } <span class="hljs-keyword">catch</span> (err) {
32183 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32198 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32199 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
32201 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32202 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32203 …class_abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress readFile</li>
32206 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32213 platform-specific. On macOS, Linux, and Windows, the promise will be rejected
32218 … class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-k…
32220 <span class="hljs-keyword">try</span> {
32221 …<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span c…
32222 <span class="hljs-keyword">const</span> { signal } = controller;
32223 …<span class="hljs-keyword">const</span> promise = <span class="hljs-title function_">readFile</spa…
32225 <span class="hljs-comment">// Abort the request before the promise settles.</span>
32226 controller.<span class="hljs-title function_">abort</span>();
32228 <span class="hljs-keyword">await</span> promise;
32229 } <span class="hljs-keyword">catch</span> (err) {
32230 <span class="hljs-comment">// When a request is aborted - err is an AbortError</span>
32231 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32241 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32242 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32244 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32247 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32250 <a href="http://man7.org/linux/man-pages/man2/readlink.2.html"><code>readlink(2)</code></a> documen…
32261 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32262 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32264 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32267 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32284 <li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
32285 <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
32286 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32306 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32307 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
32309 <li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
32314 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
32317 <li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
32322 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32329 <code>rm -rf</code>: an error will not be raised for paths that do not exist, and paths
32338 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32339 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
32341 <li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
32343 <li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
32348 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
32350 <li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
32355 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32371 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32372 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
32374 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
32378 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32381 <h4><code>fsPromises.symlink(target, path[, type])</code><span><a class="mark" href="#fs_fspromises…
32386 <li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
32387 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32388 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32389 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32394 to be absolute. When using <code>'junction'</code>, the <code>target</code> argument will
32401 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32402 <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
32403 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32412 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32413 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32417 … link, the file is deleted. See the POSIX <a href="http://man7.org/linux/man-pages/man2/unlink.2.h…
32424 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32425-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
32426-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
32427 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32435 <code>-Infinity</code>, an <code>Error</code> will be thrown.</li>
32442 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32443 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32445 <li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
32447 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
32451 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32457 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x…
32459 <li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
32460 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
32466 …e class="language-js"><span class="hljs-keyword">const</span> { watch } = <span class="hljs-built_…
32468 <span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hlj…
32469 <span class="hljs-keyword">const</span> { signal } = ac;
32470 …"hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> ac.<span class="hljs-ti…
32472 (<span class="hljs-keyword">async</span> () => {
32473 <span class="hljs-keyword">try</span> {
32474 …<span class="hljs-keyword">const</span> watcher = <span class="hljs-title function_">watch</span>(…
32475 …n class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keywo…
32476 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
32477 } <span class="hljs-keyword">catch</span> (err) {
32478 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">name</span> === <span class=…
32479 <span class="hljs-keyword">return</span>;
32480 <span class="hljs-keyword">throw</span> err;
32505 <li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32506-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
32507 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
32509 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32510 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32511 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32512 …class_abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress writeFile</l…
32515 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
32527 <p>Similarly to <code>fsPromises.readFile</code> - <code>fsPromises.writeFile</code> is a convenien…
32534 … class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-
32536 <span class="hljs-keyword">try</span> {
32537 …<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span c…
32538 <span class="hljs-keyword">const</span> { signal } = controller;
32539-keyword">const</span> data = <span class="hljs-keyword">new</span> <span class="hljs-title class_…
32540 …<span class="hljs-keyword">const</span> promise = <span class="hljs-title function_">writeFile</sp…
32542 <span class="hljs-comment">// Abort the request before the promise settles.</span>
32543 controller.<span class="hljs-title function_">abort</span>();
32545 <span class="hljs-keyword">await</span> promise;
32546 } <span class="hljs-keyword">catch</span> (err) {
32547 <span class="hljs-comment">// When a request is aborted - err is an AbortError</span>
32548 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32574 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32575 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32576 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
32578 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
32591 …ss="language-js mjs"><span class="hljs-keyword">import</span> { access, constants } <span class="h…
32593 <span class="hljs-keyword">const</span> file = <span class="hljs-string">'package.json'</span>;
32595 <span class="hljs-comment">// Check if the file exists in the current directory.</span>
32596 …ss="hljs-title function_">access</span>(file, constants.<span class="hljs-property">F_OK</span>, <…
32597-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
32600 <span class="hljs-comment">// Check if the file is readable.</span>
32601 …ss="hljs-title function_">access</span>(file, constants.<span class="hljs-property">R_OK</span>, <…
32602-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
32605 <span class="hljs-comment">// Check if the file is writable.</span>
32606 …ss="hljs-title function_">access</span>(file, constants.<span class="hljs-property">W_OK</span>, <…
32607-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
32610 <span class="hljs-comment">// Check if the file exists in the current directory, and if it is writa…
32611-title function_">access</span>(file, constants.<span class="hljs-property">F_OK</span> | constant…
32612 <span class="hljs-keyword">if</span> (err) {
32613 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32614-string">`<span class="hljs-subst">${file}</span> <span class="hljs-subst">${err.code === <span cl…
32615 } <span class="hljs-keyword">else</span> {
32616 …n class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<sp…
32625 …s="language-js mjs"><span class="hljs-keyword">import</span> { access, open, close } <span class="…
32627 …pan class="hljs-title function_">access</span>(<span class="hljs-string">'myfile'</span>, <span cl…
32628 <span class="hljs-keyword">if</span> (!err) {
32629 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32630 <span class="hljs-keyword">return</span>;
32633-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
32634 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32636 <span class="hljs-keyword">try</span> {
32637 <span class="hljs-title function_">writeMyData</span>(fd);
32638 } <span class="hljs-keyword">finally</span> {
32639 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
32640 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32646 …class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs
32648-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
32649 <span class="hljs-keyword">if</span> (err) {
32650 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class=…
32651 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32652 <span class="hljs-keyword">return</span>;
32655 <span class="hljs-keyword">throw</span> err;
32658 <span class="hljs-keyword">try</span> {
32659 <span class="hljs-title function_">writeMyData</span>(fd);
32660 } <span class="hljs-keyword">finally</span> {
32661 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
32662 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32667 …s="language-js mjs"><span class="hljs-keyword">import</span> { access, open, close } <span class="…
32668 …pan class="hljs-title function_">access</span>(<span class="hljs-string">'myfile'</span>, <span cl…
32669 <span class="hljs-keyword">if</span> (err) {
32670 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class=…
32671 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32672 <span class="hljs-keyword">return</span>;
32675 <span class="hljs-keyword">throw</span> err;
32678-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
32679 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32681 <span class="hljs-keyword">try</span> {
32682 <span class="hljs-title function_">readMyData</span>(fd);
32683 } <span class="hljs-keyword">finally</span> {
32684 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
32685 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32691 …class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs
32693-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
32694 <span class="hljs-keyword">if</span> (err) {
32695 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class=…
32696 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
32697 <span class="hljs-keyword">return</span>;
32700 <span class="hljs-keyword">throw</span> err;
32703 <span class="hljs-keyword">try</span> {
32704 <span class="hljs-title function_">readMyData</span>(fd);
32705 } <span class="hljs-keyword">finally</span> {
32706 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
32707 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32717 <p>On Windows, access-control policies (ACLs) on a directory may limit access to
32740-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
32741 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32742 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
32744 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32745 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32746 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32749 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
32751 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
32759 …class="language-js mjs"><span class="hljs-keyword">import</span> { appendFile } <span class="hljs-
32761-title function_">appendFile</span>(<span class="hljs-string">'message.txt'</span>, <span class="h…
32762 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32763 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
32766 …class="language-js mjs"><span class="hljs-keyword">import</span> { appendFile } <span class="hljs-
32768-title function_">appendFile</span>(<span class="hljs-string">'message.txt'</span>, <span class="h…
32772 …"language-js mjs"><span class="hljs-keyword">import</span> { open, close, appendFile } <span class…
32774 <span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span …
32775 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
32776 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32780-title function_">open</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-st…
32781 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32783 <span class="hljs-keyword">try</span> {
32784-title function_">appendFile</span>(fd, <span class="hljs-string">'data to append'</span>, <span c…
32785 <span class="hljs-title function_">closeFd</span>(fd);
32786 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32788 } <span class="hljs-keyword">catch</span> (err) {
32789 <span class="hljs-title function_">closeFd</span>(fd);
32790 <span class="hljs-keyword">throw</span> err;
32810 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32811 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
32812 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
32814 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
32820 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/chmod.2.html"><code>chmod(2)</code><…
32821 …de class="language-js mjs"><span class="hljs-keyword">import</span> { chmod } <span class="hljs-ke…
32823-title function_">chmod</span>(<span class="hljs-string">'my_file.txt'</span>, <span class="hljs-n…
32824 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
32825 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
32888 octal digits (e.g. <code>765</code>). The left-most digit (<code>7</code> in the example), specifies
32890 specifies permissions for the group. The right-most digit (<code>5</code> in the example),
32941 <code>0o777</code> may result in platform-specific behaviors that are not supported to work
32964 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
32965 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
32966 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
32967 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
32969 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
32975 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/chown.2.html"><code>chown(2)</code><…
32993 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
32994 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
32996 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33004 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/close.2.html"><code>close(2)</code><…
33018 <li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33019 <li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33020 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33021 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33036 copy-on-write reflink. If the platform does not support copy-on-write, then a
33039 create a copy-on-write reflink. If the platform does not support
33040 copy-on-write, then the operation will fail.</li>
33042 …s="language-js mjs"><span class="hljs-keyword">import</span> { copyFile, constants } <span class="…
33044 <span class="hljs-keyword">function</span> <span class="hljs-title function_">callback</span>(<span…
33045 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33046 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
33049 <span class="hljs-comment">// destination.txt will be created or overwritten by default.</span>
33050 <span class="hljs-title function_">copyFile</span>(<span class="hljs-string">'source.txt'</span>, <…
33052 <span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt ex…
33053-title function_">copyFile</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs
33079 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33080 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33082 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
33084 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
33085 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33086 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33087 <li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33088 <li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33089 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
33090 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33091 <li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
33092 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
33102 [0, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Numbe…
33108 emitted. <code>fd</code> should be blocking; non-blocking <code>fd</code>s should be passed to
33120 …ss="language-js mjs"><span class="hljs-keyword">import</span> { createReadStream } <span class="hl…
33122 <span class="hljs-comment">// Create a stream from some character device.</span>
33123 <span class="hljs-keyword">const</span> stream = <span class="hljs-title function_">createReadStrea…
33124 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
33125 …stream.<span class="hljs-title function_">close</span>(); <span class="hljs-comment">// This may n…
33126 …<span class="hljs-comment">// Artificially marking end-of-stream, as if the underlying resource ha…
33127 <span class="hljs-comment">// indicated end-of-file by itself, allows the stream to close.</span>
33128 …<span class="hljs-comment">// This does not cancel pending read operations, and if there is such a…
33129 …<span class="hljs-comment">// operation, the process may still not be able to exit successfully</s…
33130 <span class="hljs-comment">// until it finishes.</span>
33131 stream.<span class="hljs-title function_">push</span>(<span class="hljs-literal">null</span>);
33132 stream.<span class="hljs-title function_">read</span>(<span class="hljs-number">0</span>);
33133 }, <span class="hljs-number">100</span>);</code></pre>
33142 …ss="language-js mjs"><span class="hljs-keyword">import</span> { createReadStream } <span class="hl…
33144-title function_">createReadStream</span>(<span class="hljs-string">'sample.txt'</span>, { <span c…
33171 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33172 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33174 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
33176 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
33177 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33178 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33179 <li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33180 <li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33181 <li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
33182 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
33189 [0, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Numbe…
33207 <code>'open'</code> event will be emitted. <code>fd</code> should be blocking; non-blocking <code>f…
33224 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
33226 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33227 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33229 <li><code>exists</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33235 …e class="language-js mjs"><span class="hljs-keyword">import</span> { exists } <span class="hljs-ke…
33237 …n class="hljs-title function_">exists</span>(<span class="hljs-string">'/etc/passwd'</span>, <span…
33238 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(e ? <span class…
33251 …s="language-js mjs"><span class="hljs-keyword">import</span> { exists, open, close } <span class="…
33253 …pan class="hljs-title function_">exists</span>(<span class="hljs-string">'myfile'</span>, <span cl…
33254 <span class="hljs-keyword">if</span> (e) {
33255 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
33256 } <span class="hljs-keyword">else</span> {
33257-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
33258 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33260 <span class="hljs-keyword">try</span> {
33261 <span class="hljs-title function_">writeMyData</span>(fd);
33262 } <span class="hljs-keyword">finally</span> {
33263 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
33264 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33271 …class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs
33272-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
33273 <span class="hljs-keyword">if</span> (err) {
33274 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class=…
33275 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
33276 <span class="hljs-keyword">return</span>;
33279 <span class="hljs-keyword">throw</span> err;
33282 <span class="hljs-keyword">try</span> {
33283 <span class="hljs-title function_">writeMyData</span>(fd);
33284 } <span class="hljs-keyword">finally</span> {
33285 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
33286 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33291 …s="language-js mjs"><span class="hljs-keyword">import</span> { open, close, exists } <span class="…
33293 …pan class="hljs-title function_">exists</span>(<span class="hljs-string">'myfile'</span>, <span cl…
33294 <span class="hljs-keyword">if</span> (e) {
33295-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
33296 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33298 <span class="hljs-keyword">try</span> {
33299 <span class="hljs-title function_">readMyData</span>(fd);
33300 } <span class="hljs-keyword">finally</span> {
33301 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
33302 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33306 } <span class="hljs-keyword">else</span> {
33307 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
33311 …class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs
33313-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string"…
33314 <span class="hljs-keyword">if</span> (err) {
33315 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class=…
33316 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
33317 <span class="hljs-keyword">return</span>;
33320 <span class="hljs-keyword">throw</span> err;
33323 <span class="hljs-keyword">try</span> {
33324 <span class="hljs-title function_">readMyData</span>(fd);
33325 } <span class="hljs-keyword">finally</span> {
33326 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
33327 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33352 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33353 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33354 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33356 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33362 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fchmod.2.html"><code>fchmod(2)</code…
33378 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33379 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33380 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33381 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33383 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33389 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fchown.2.html"><code>fchown(2)</code…
33405 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33406 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33408 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33414 <a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html"><code>fdatasync(2)</code></a> docum…
33433 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33434 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
33436 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33440 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33442 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33448 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fstat.2.html"><code>fstat(2)</code><…
33464 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33465 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33467 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33473 Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/fsync.2.html"><code>fsync(2)</code…
33490 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33491 <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33492 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33494 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33500 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/ftruncate.2.html"><code>ftruncate(2)…
33505 …"language-js mjs"><span class="hljs-keyword">import</span> { open, close, ftruncate } <span class=…
33507 <span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span …
33508 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
33509 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33513-title function_">open</span>(<span class="hljs-string">'temp.txt'</span>, <span class="hljs-strin…
33514 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33516 <span class="hljs-keyword">try</span> {
33517 …pan class="hljs-title function_">ftruncate</span>(fd, <span class="hljs-number">4</span>, <span cl…
33518 <span class="hljs-title function_">closeFd</span>(fd);
33519 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33521 } <span class="hljs-keyword">catch</span> (err) {
33522 <span class="hljs-title function_">closeFd</span>(fd);
33523 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33546 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33547-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
33548-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
33549 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33551 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33574 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33575 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33576 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33578 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33598 <td><p>Documentation-only deprecation.</p></td></tr>
33603 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33604 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33605 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
33606 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33608 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33614 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/lchown.2.html"><code>lchown(2)</code…
33620 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33621-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
33622-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
33623 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33625 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33652 <li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
33653 <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
33654 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33656 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33661 <a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a> documentation f…
33682 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33683 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
33685 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33689 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33691 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33699 link, then the link itself is stat-ed, not the file that it refers to.</p>
33700 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/lstat.2.html"><code>lstat(2)</code><…
33722 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33723 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
33725 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33726 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33729 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33731 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33745 …de class="language-js mjs"><span class="hljs-keyword">import</span> { mkdir } <span class="hljs-ke…
33747 <span class="hljs-comment">// Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist.<…
33748-title function_">mkdir</span>(<span class="hljs-string">'/tmp/a/apple'</span>, { <span class="hlj…
33749 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33753 …de class="language-js mjs"><span class="hljs-keyword">import</span> { mkdir } <span class="hljs-ke…
33755-title function_">mkdir</span>(<span class="hljs-string">'/'</span>, { <span class="hljs-attr">rec…
33756 <span class="hljs-comment">// => [Error: EPERM: operation not permitted, mkdir 'C:\']</span>
33758 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/mkdir.2.html"><code>mkdir(2)</code><…
33778 <li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33779 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33781 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
33784 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33786 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33787 <li><code>directory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33801 …e class="language-js mjs"><span class="hljs-keyword">import</span> { mkdtemp } <span class="hljs-k…
33803-title function_">mkdtemp</span>(path.<span class="hljs-title function_">join</span>(os.<span clas…
33804 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33805 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
33806 …<span class="hljs-comment">// Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde…
33811 must end with a trailing platform-specific path separator
33813 …e class="language-js mjs"><span class="hljs-keyword">import</span> { tmpdir } <span class="hljs-ke…
33814 <span class="hljs-keyword">import</span> { mkdtemp } <span class="hljs-keyword">from</span> <span c…
33816 <span class="hljs-comment">// The parent directory for the new temporary directory</span>
33817 <span class="hljs-keyword">const</span> tmpDir = <span class="hljs-title function_">tmpdir</span>();
33819 <span class="hljs-comment">// This method is *INCORRECT*:</span>
33820 <span class="hljs-title function_">mkdtemp</span>(tmpDir, <span class="hljs-function">(<span class=…
33821 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33822 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
33823 <span class="hljs-comment">// Will print something similar to `/tmpabc123`.</span>
33824 <span class="hljs-comment">// A new temporary directory is created at the file system root</span>
33825 <span class="hljs-comment">// rather than *within* the /tmp directory.</span>
33828 <span class="hljs-comment">// This method is *CORRECT*:</span>
33829 <span class="hljs-keyword">import</span> { sep } <span class="hljs-keyword">from</span> <span class…
33830-title function_">mkdtemp</span>(<span class="hljs-string">`<span class="hljs-subst">${tmpDir}</sp…
33831 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
33832 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
33833 <span class="hljs-comment">// Will print something similar to `/tmp/abc123`.</span>
33834 <span class="hljs-comment">// A new temporary directory is created within</span>
33835 <span class="hljs-comment">// the /tmp directory.</span>
33854 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33855 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33857 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33858 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33860 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33861 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33865 <p>Asynchronous file open. See the POSIX <a href="http://man7.org/linux/man-pages/man2/open.2.html"…
33871 by <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file">Naming Files, Pa…
33873 <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams">this MSDN page</a>.…
33889 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
33890 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
33892 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
33893 <li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
33898 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33900 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33905 <p>Asynchronously open a directory. See the POSIX <a href="http://man7.org/linux/man-pages/man3/ope…
33928 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33929 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
33931 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33933 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33935 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
33936 file. If <code>position</code> is <code>null</code> or <code>-1 </code>, data will be read from the…
33939 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33941 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33942 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
33949 <p>If the file is not modified concurrently, the end-of-file is reached when the
33966 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
33967 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
33969 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
33970 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
33971 <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> |…
33975 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
33977 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
33978 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
34007 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34008 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34010 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34011 <li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
34014 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34016 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34017 <li><code>files</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
34024 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</co…
34054-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
34055 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
34057 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34058 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34059 …class_abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress readFile</li>
34062 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34064 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34065 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34070 … class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-k…
34072 … class="hljs-title function_">readFile</span>(<span class="hljs-string">'/etc/passwd'</span>, <spa…
34073 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
34074 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34080 … class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-k…
34082 <span class="hljs-title function_">readFile</span>(<span class="hljs-string">'/etc/passwd'</span>, …
34084 <a href="#fs_fs_readfilesync_path_options"><code>fs.readFileSync()</code></a> is platform-specific.…
34087 … class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-k…
34089 <span class="hljs-comment">// macOS, Linux, and Windows</span>
34090 …ass="hljs-title function_">readFile</span>(<span class="hljs-string">'&#x3C;directory>'</span>, <s…
34091 …<span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, read &#x3C;dire…
34094 <span class="hljs-comment">// FreeBSD</span>
34095 …ass="hljs-title function_">readFile</span>(<span class="hljs-string">'&#x3C;directory>'</span>, <s…
34096 <span class="hljs-comment">// => null, &#x3C;data></span>
34100 … class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-k…
34102 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span cl…
34103 <span class="hljs-keyword">const</span> signal = controller.<span class="hljs-property">signal</spa…
34104-title function_">readFile</span>(fileInfo[<span class="hljs-number">0</span>].<span class="hljs-p…
34105 <span class="hljs-comment">// ...</span>
34107 <span class="hljs-comment">// When you want to abort the request</span>
34108 controller.<span class="hljs-title function_">abort</span>();</code></pre>
34134 <p>For applications that require as-fast-as-possible reading of file contents, it
34157 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34158 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34160 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34163 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34165 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34166 <li><code>linkString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34172 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/readlink.2.html"><code>readlink(2)</…
34182 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
34183 <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"…
34184 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34185 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34187 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34188 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
34189 <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"…
34225 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34226 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34228 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34231 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34233 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34234 <li><code>resolvedPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
34242 <p>This function behaves like <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>…
34245 <p>No case conversion is performed on case-insensitive file systems.</p>
34248 <p>The maximum number of symbolic links is platform-independent and generally
34249 (much) higher than what the native <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><…
34266 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34267 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34269 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34272 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34274 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34275 <li><code>resolvedPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
34279 <p>Asynchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</c…
34306 <li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
34307 <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
34308 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34310 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34319 <p>See also: <a href="http://man7.org/linux/man-pages/man2/rename.2.html"><code>rename(2)</code></a…
34320 …e class="language-js mjs"><span class="hljs-keyword">import</span> { rename } <span class="hljs-ke…
34322-title function_">rename</span>(<span class="hljs-string">'oldFile.txt'</span>, <span class="hljs-
34323 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
34324 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34349 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34350 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
34352 <li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34357 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
34360 <li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34365 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34367 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34371 <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/rmdir.2.html"><code>rmdir(2)</code></…
34376 <code>rm -rf</code>: an error will not be raised for paths that do not exist, and paths
34385 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34386 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
34388 <li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
34390 <li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34395 <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…
34430 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34431 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
34433 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34437 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34439 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34444 <p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/stat.2.html"><code>stat(2)</code></a>…
34454 <pre><code class="language-text">- txtDir
34455 -- file.txt
34456 - app.js</code></pre>
34458 …de class="language-js mjs"><span class="hljs-keyword">import</span> { stat } <span class="hljs-key…
34460 <span class="hljs-keyword">const</span> pathsToCheck = [<span class="hljs-string">'./txtDir'</span>…
34462 …="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0<…
34463 …<span class="hljs-title function_">stat</span>(pathsToCheck[i], <span class="hljs-function">(<span…
34464 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34465 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34469 <pre><code class="language-console">true
34485 atime: 2019-06-22T03:37:33.072Z,
34486 mtime: 2019-06-22T03:36:54.583Z,
34487 ctime: 2019-06-22T03:37:06.624Z,
34488 birthtime: 2019-06-22T03:28:46.937Z
34506 atime: 2019-06-22T03:36:56.619Z,
34507 mtime: 2019-06-22T03:36:54.584Z,
34508 ctime: 2019-06-22T03:36:54.584Z,
34509 birthtime: 2019-06-22T03:26:47.711Z
34511 <h4><code>fs.symlink(target, path[, type], callback)</code><span><a class="mark" href="#fs_fs_symli…
34517 <td><p>If the <code>type</code> argument is left undefined, Node will autodetect <code>target</code…
34519 <td><p>The <code>target</code> and <code>path</code> parameters can be WHATWG <code>URL</code> obje…
34526 <li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34527 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34528 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34529 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34531 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34535 <p>Creates the link called <code>path</code> pointing to <code>target</code>. No arguments other th…
34537 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/symlink.2.html"><code>symlink(2)</co…
34540 not set, Node.js will autodetect <code>target</code> type and use <code>'file'</code> or <code>'dir…
34541 the <code>target</code> does not exist, <code>'file'</code> will be used. Windows junction points
34543 <code>target</code> argument will automatically be normalized to absolute path.</p>
34545 …e class="language-js mjs"><span class="hljs-keyword">import</span> { symlink } <span class="hljs-k…
34547 <span class="hljs-title function_">symlink</span>(<span class="hljs-string">'./mew'</span>, <span c…
34550 <pre><code class="language-bash">$ tree example/
34553 └── mewtwo -> ./mew</code></pre>
34569 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34570 <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
34571 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34573 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34581-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
34582 <span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span>
34583 …class="hljs-title function_">truncate</span>(<span class="hljs-string">'path/file.txt'</span>, <sp…
34584 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
34585 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34586 …lass="language-js cjs"><span class="hljs-keyword">const</span> { truncate } = <span class="hljs-bu…
34587 <span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span>
34588 …class="hljs-title function_">truncate</span>(<span class="hljs-string">'path/file.txt'</span>, <sp…
34589 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
34590 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34594 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/truncate.2.html"><code>truncate(2)</…
34612 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34613 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34615 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34621 …e class="language-js mjs"><span class="hljs-keyword">import</span> { unlink } <span class="hljs-ke…
34622 <span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span>
34623 … class="hljs-title function_">unlink</span>(<span class="hljs-string">'path/file.txt'</span>, <spa…
34624 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
34625 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34629 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/unlink.2.html"><code>unlink(2)</code…
34635 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34636 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34643 no-op, not an error.</p>
34655 <td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time…
34668 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
34669-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
34670-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
34671 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34673 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34683 <code>-Infinity</code>, an <code>Error</code> will be thrown.</li>
34702 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34703 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34705 <li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34707 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
34711 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34716 …a.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a…
34718 <li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
34719 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34752 <li>On Linux systems, this uses <a href="https://man7.org/linux/man-pages/man7/inotify.7.html"><cod…
34757 …ature depends on <a href="https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-
34758 <li>On AIX systems, this feature depends on <a href="https://developer.ibm.com/articles/au-aix_even…
34784 …de class="language-js mjs"><span class="hljs-keyword">import</span> { watch } <span class="hljs-ke…
34785 …pan class="hljs-title function_">watch</span>(<span class="hljs-string">'somedir'</span>, <span cl…
34786 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
34787 <span class="hljs-keyword">if</span> (filename) {
34788 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
34789 } <span class="hljs-keyword">else</span> {
34790 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
34808 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34809 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
34811 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34812 <li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
34813 <li><code>interval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34816 <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34830 target should be polled in milliseconds.</p>
34833 … class="language-js mjs"><span class="hljs-keyword">import</span> { watchFile } <span class="hljs-
34835 …class="hljs-title function_">watchFile</span>(<span class="hljs-string">'message.text'</span>, <sp…
34836 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
34837 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hl…
34885 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
34886-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
34887 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34888 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34889 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34890 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34892 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34893 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
34894 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
34904 at the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwr…
34936 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
34937 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
34938 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34939 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
34940 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
34942 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
34943 <li><code>written</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
34944 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
34952 the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite…
34965 or <code>stdout</code>) a string containing non-ASCII characters will not be rendered
34967 It is possible to configure the console to render UTF-8 properly by changing the
34997-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
34998-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
34999 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
35001 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35002 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35003 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35004 …class_abortsignal" class="type">&#x3C;AbortSignal></a> allows aborting an in-progress writeFile</l…
35007 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
35009 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
35023 … class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-
35025-keyword">const</span> data = <span class="hljs-keyword">new</span> <span class="hljs-title class_…
35026 …ss="hljs-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, data, <…
35027 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
35028 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
35031 … class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-
35033 …s-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, <span class="h…
35037 <p>Similarly to <code>fs.readFile</code> - <code>fs.writeFile</code> is a convenience method that
35043 … class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-
35045 <span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span cl…
35046 <span class="hljs-keyword">const</span> { signal } = controller;
35047-keyword">const</span> data = <span class="hljs-keyword">new</span> <span class="hljs-title class_…
35048 …js-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, data, { signa…
35049 …<span class="hljs-comment">// When a request is aborted - the callback is called with an AbortErro…
35051 <span class="hljs-comment">// When the request should be aborted</span>
35052 controller.<span class="hljs-title function_">abort</span>();</code></pre>
35058 …de class="language-js mjs"><span class="hljs-keyword">import</span> { write } <span class="hljs-ke…
35060-title function_">write</span>(fd, <span class="hljs-title class_">Buffer</span>.<span class="hljs
35080 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35081 <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"…
35082 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35083 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
35085 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
35086 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
35087 <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"…
35121 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35122 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35132 …="language-js mjs"><span class="hljs-keyword">import</span> { accessSync, constants } <span class=…
35134 <span class="hljs-keyword">try</span> {
35135-title function_">accessSync</span>(<span class="hljs-string">'etc/passwd'</span>, constants.<span…
35136 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
35137 } <span class="hljs-keyword">catch</span> (err) {
35138 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
35155-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
35156 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35157 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
35159 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35160 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35161 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35169 …ass="language-js mjs"><span class="hljs-keyword">import</span> { appendFileSync } <span class="hlj…
35171 <span class="hljs-keyword">try</span> {
35172 …<span class="hljs-title function_">appendFileSync</span>(<span class="hljs-string">'message.txt'</…
35173 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
35174 } <span class="hljs-keyword">catch</span> (err) {
35175 <span class="hljs-comment">/* Handle the error */</span>
35178 …ass="language-js mjs"><span class="hljs-keyword">import</span> { appendFileSync } <span class="hlj…
35180-title function_">appendFileSync</span>(<span class="hljs-string">'message.txt'</span>, <span clas…
35184 …age-js mjs"><span class="hljs-keyword">import</span> { openSync, closeSync, appendFileSync } <span…
35186 <span class="hljs-keyword">let</span> fd;
35188 <span class="hljs-keyword">try</span> {
35189 …fd = <span class="hljs-title function_">openSync</span>(<span class="hljs-string">'message.txt'</s…
35190 …<span class="hljs-title function_">appendFileSync</span>(fd, <span class="hljs-string">'data to ap…
35191 } <span class="hljs-keyword">catch</span> (err) {
35192 <span class="hljs-comment">/* Handle the error */</span>
35193 } <span class="hljs-keyword">finally</span> {
35194 <span class="hljs-keyword">if</span> (fd !== <span class="hljs-literal">undefined</span>)
35195 <span class="hljs-title function_">closeSync</span>(fd);
35210 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35211 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35215 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/chmod.2.html"><code>chmod(2)</code><…
35229 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35230 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35231 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35235 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/chown.2.html"><code>chown(2)</code><…
35241 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35246 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/close.2.html"><code>close(2)</code><…
35260 <li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35261 <li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35262 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35276 copy-on-write reflink. If the platform does not support copy-on-write, then a
35279 create a copy-on-write reflink. If the platform does not support
35280 copy-on-write, then the operation will fail.</li>
35282 …"language-js mjs"><span class="hljs-keyword">import</span> { copyFileSync, constants } <span class…
35284 <span class="hljs-comment">// destination.txt will be created or overwritten by default.</span>
35285 <span class="hljs-title function_">copyFileSync</span>(<span class="hljs-string">'source.txt'</span…
35286 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
35288 <span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt ex…
35289-title function_">copyFileSync</span>(<span class="hljs-string">'source.txt'</span>, <span class="…
35303 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35304 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
35312 …class="language-js mjs"><span class="hljs-keyword">import</span> { existsSync } <span class="hljs-
35314 <span class="hljs-keyword">if</span> (<span class="hljs-title function_">existsSync</span>(<span cl…
35315 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
35321 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35322 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35325 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fchmod.2.html"><code>fchmod(2)</code…
35331 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35332 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35333 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35336 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fchown.2.html"><code>fchown(2)</code…
35342 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35346 <a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html"><code>fdatasync(2)</code></a> docum…
35360 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35361 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35363 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35370 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fstat.2.html"><code>fstat(2)</code><…
35376 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35380 Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/fsync.2.html"><code>fsync(2)</code…
35386 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35387 <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35405 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35406-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
35407-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
35415 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35416 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35429 <td><p>Documentation-only deprecation.</p></td></tr>
35434 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35435 <li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35436 <li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35439 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/lchown.2.html"><code>lchown(2)</code…
35445 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35446-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
35447-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
35465 <li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
35466 <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
35469 <a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a> documentation f…
35487 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35488 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35490 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35492 <li><code>throwIfNoEntry</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
35500 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/lstat.2.html"><code>lstat(2)</code><…
35518 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35519 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
35521 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
35522 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35525 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35530 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/mkdir.2.html"><code>mkdir(2)</code><…
35544 <li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35545 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35547 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35550 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
35570 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35571 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35573 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35574 <li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
35581 <p>Synchronously open a directory. See <a href="http://man7.org/linux/man-pages/man3/opendir.3.html…
35603 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35604 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35606 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35607 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
35627 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35628 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35630 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35631 <li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
35634 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
35637 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</co…
35659-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
35660 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
35662 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35663 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35666 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
35674 <code>fs.readFileSync()</code> is platform-specific.</p>
35675 …lass="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs
35677 <span class="hljs-comment">// macOS, Linux, and Windows</span>
35678 <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'&#x3C;directory>'…
35679 <span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, read &#x3C;direc…
35681 <span class="hljs-comment">// FreeBSD</span>
35682 <span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'&#x3C;directory>'…
35696 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35697 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35699 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35702 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
35705 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/readlink.2.html"><code>readlink(2)</…
35725 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35726 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
35727 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35728 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35729 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
35730 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
35748 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35749 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
35750 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35752 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35753 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35754 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
35757 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
35769 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
35770 <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"…
35771 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35772 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
35795 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35796 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35798 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35801 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
35811 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35812 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
35814 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
35817 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
35819 <p>Synchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</co…
35841 <li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
35842 <li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
35845 <p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/rename.2.html"><code>rename(2)</code…
35865 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35866 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35868 <li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
35873 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
35876 <li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
35882 <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/rmdir.2.html"><code>rmdir(2)</code></a…
35886 <code>rm -rf</code>: an error will not be raised for paths that do not exist, and paths
35895 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35896 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35898 <li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
35900 <li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
35905 <li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
35907 <li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
35932 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35933 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
35935 <li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35937 <li><code>throwIfNoEntry</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
35945 <h4><code>fs.symlinkSync(target, path[, type])</code><span><a class="mark" href="#fs_fs_symlinksync…
35951 <td><p>If the <code>type</code> argument is left undefined, Node will autodetect <code>target</code…
35953 <td><p>The <code>target</code> and <code>path</code> parameters can be WHATWG <code>URL</code> obje…
35960 <li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
35961 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35962 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35972 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35973 <li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
35992 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
35994 <p>Synchronous <a href="http://man7.org/linux/man-pages/man2/unlink.2.html"><code>unlink(2)</code><…
36001 <td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time…
36012 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
36013-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
36014-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
36040-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
36041-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
36042 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
36044 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
36045 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
36046 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
36078 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
36079-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
36080 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
36081 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
36082 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
36083 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
36106 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
36107 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
36108 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
36109 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
36110 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
36121 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
36122 <li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView"…
36123 <li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
36124 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
36138 …e class="language-js mjs"><span class="hljs-keyword">import</span> { opendir } <span class="hljs-k…
36140 <span class="hljs-keyword">try</span> {
36141 …ss="hljs-keyword">const</span> dir = <span class="hljs-keyword">await</span> <span class="hljs-tit…
36142 …n class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keywo…
36143 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
36144 } <span class="hljs-keyword">catch</span> (err) {
36145 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
36154 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
36165 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
36167 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
36185 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
36187 <p>The read-only path of this directory as was provided to <a href="#fs_fs_opendir_path_options_cal…
36194-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a> containi…
36196 <p>Asynchronously read the next directory entry via <a href="http://man7.org/linux/man-pages/man3/r…
36209 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
36211 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
36212 …lass="type">&#x3C;fs.Dirent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
36216 <p>Asynchronously read the next directory entry via <a href="http://man7.org/linux/man-pages/man3/r…
36229 …lass="type">&#x3C;fs.Dirent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
36232 POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</code></a> doc…
36243 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x…
36246 been read. Refer to the POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>r…
36269 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36277 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36285 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36294 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36296 … href="#fs_class_fs_dirent" class="type">&#x3C;fs.Dirent></a> object describes a first-in-first-out
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…
36319 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36327 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
36348 <li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
36349 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
36356 <code>filename</code> will be a UTF-8 string.</p>
36357 …de class="language-js mjs"><span class="hljs-keyword">import</span> { watch } <span class="hljs-ke…
36358 <span class="hljs-comment">// Example when handled through fs.watch() listener</span>
36359-title function_">watch</span>(<span class="hljs-string">'./tmp'</span>, { <span class="hljs-attr"…
36360 <span class="hljs-keyword">if</span> (filename) {
36361 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
36362 <span class="hljs-comment">// Prints: &#x3C;Buffer ...></span>
36376 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
36464 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
36478 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
36486 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
36497 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
36518 nanosecond-precision properties suffixed with <code>Ns</code>.</p>
36519 <pre><code class="language-console">Stats {
36539 <pre><code class="language-console">BigIntStats {
36567 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36575 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36583 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36594 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36596 …="#fs_class_fs_stats" class="type">&#x3C;fs.Stats></a> object describes a first-in-first-out (FIFO)
36603 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36611 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36619 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
36625 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36630 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36635 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36637 <p>A bit-field describing the file type and mode.</p>
36640 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36642 <p>The number of hard-links that exist for the file.</p>
36645 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36650 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36655 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36660 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36665 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36670 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36678 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36687 …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> | …
36705 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
36714 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigIn…
36725 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigIn…
36736 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigIn…
36747 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigIn…
36758 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"…
36766 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"…
36774 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"…
36782 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"…
36789 method that generates the object, the properties will be <a href="https://tc39.github.io/proposal-b…
36790 otherwise they will be <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
36792 <a href="https://tc39.github.io/proposal-bigint">bigints</a> that hold the corresponding times in n…
36796 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"><co…
36803-pages/man2/mknod.2.html"><code>mknod(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2…
36805-pages/man2/mknod.2.html"><code>mknod(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2…
36807 … href="http://man7.org/linux/man-pages/man2/chmod.2.html"><code>chmod(2)</code></a>, <a href="http…
36808-pages/man2/link.2.html"><code>link(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/m…
36809 …href="http://man7.org/linux/man-pages/man2/read.2.html"><code>read(2)</code></a>, and <a href="htt…
36813 <code>1970-01-01T00:00Z</code> (ie, Unix epoch timestamp <code>0</code>). This value may be greater
36816 <code>birthtime</code> using the <a href="http://man7.org/linux/man-pages/man2/utimes.2.html"><code…
36839 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
36859 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
36861 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
36881 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
36887 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
36896 …ass="language-js mjs"><span class="hljs-keyword">import</span> { open, constants } <span class="hl…
36898 <span class="hljs-keyword">const</span> {
36899 <span class="hljs-variable constant_">O_RDWR</span>,
36900 <span class="hljs-variable constant_">O_CREAT</span>,
36901 <span class="hljs-variable constant_">O_EXCL</span>
36904-title function_">open</span>(<span class="hljs-string">'/path/to/my/file'</span>, <span class="hl…
36905 <span class="hljs-comment">// ...</span>
36951 copy-on-write reflink. If the underlying platform does not support
36952 copy-on-write, then a fallback copy mechanism is used.</td>
36957 copy-on-write reflink. If the underlying platform does not support
36958 copy-on-write, then the operation will fail with an error.</td>
36970 <td>Flag indicating to open a file for read-only access.</td>
36974 <td>Flag indicating to open a file for write-only access.</td>
36978 <td>Flag indicating to open a file for read-write access.</td>
37074 <td>File type constant for a character-oriented device file.</td>
37078 <td>File type constant for a block-oriented device file.</td>
37151 <h4>Ordering of callback and promise-based operations<span><a class="mark" href="#fs_ordering_of_ca…
37154 promise-based methods.</p>
37157-js">fs.<span class="hljs-title function_">rename</span>(<span class="hljs-string">'/tmp/hello'</s…
37158 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37159 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
37161 …an class="hljs-title function_">stat</span>(<span class="hljs-string">'/tmp/world'</span>, <span c…
37162 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37163-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
37168-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
37170 <span class="hljs-keyword">const</span> <span class="hljs-keyword">from</span> = <span class="hljs-
37171 <span class="hljs-keyword">const</span> to = <span class="hljs-string">'/tmp/world'</span>;
37173 <span class="hljs-keyword">try</span> {
37174 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">rename</span>(<span cla…
37175 …<span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span clas…
37176-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
37177 } <span class="hljs-keyword">catch</span> (error) {
37178-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
37179 …ss="language-js cjs"><span class="hljs-keyword">const</span> { rename, stat } = <span class="hljs-
37181 (<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hl…
37182 <span class="hljs-keyword">try</span> {
37183 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">rename</span>(<span cla…
37184 …<span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span clas…
37185-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
37186 } <span class="hljs-keyword">catch</span> (error) {
37187-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="h…
37189 })(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-string">'/tmp/world'</span>);</…
37193-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
37195-title function_">rename</span>(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-s…
37196 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37197 …an class="hljs-title function_">stat</span>(<span class="hljs-string">'/tmp/world'</span>, <span c…
37198 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37199-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
37201 …ss="language-js cjs"><span class="hljs-keyword">const</span> { rename, stat } = <span class="hljs-
37203-title function_">rename</span>(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-s…
37204 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37205 …an class="hljs-title function_">stat</span>(<span class="hljs-string">'/tmp/world'</span>, <span c…
37206 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37207-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
37214 <p>String form paths are interpreted as UTF-8 character sequences identifying
37218 …de class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-key…
37220 <span class="hljs-keyword">let</span> fd;
37221 <span class="hljs-keyword">try</span> {
37222 …hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string…
37223 <span class="hljs-comment">// Do something with the file</span>
37224 } <span class="hljs-keyword">finally</span> {
37225 <span class="hljs-keyword">await</span> fd.<span class="hljs-title function_">close</span>();
37228 …de class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-key…
37230 <span class="hljs-keyword">let</span> fd;
37231 <span class="hljs-keyword">try</span> {
37232 …lass="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-
37233 <span class="hljs-comment">// Do something with the file</span>
37234 } <span class="hljs-keyword">finally</span> {
37235 <span class="hljs-keyword">await</span> fd.<span class="hljs-title function_">close</span>();
37243 …lass="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs
37245 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37247 <h6>Platform-specific considerations<span><a class="mark" href="#fs_platform_specific_consideration…
37251 …lass="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs
37252 <span class="hljs-comment">// On Windows :</span>
37254 <span class="hljs-comment">// - WHATWG file URLs with hostname convert to UNC path</span>
37255 <span class="hljs-comment">// file://hostname/p/a/t/h/file => \\hostname\p\a\t\h\file</span>
37256 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37258 <span class="hljs-comment">// - WHATWG file URLs with drive letters convert to absolute path</span>
37259 <span class="hljs-comment">// file:///C:/tmp/hello => C:\tmp\hello</span>
37260 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37262 <span class="hljs-comment">// - WHATWG file URLs without hostname must have a drive letters</span>
37263 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37264 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37265 <span class="hljs-comment">// TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute…
37270 …lass="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs
37271 <span class="hljs-comment">// On other platforms:</span>
37273 <span class="hljs-comment">// - WHATWG file URLs with hostname are unsupported</span>
37274 <span class="hljs-comment">// file://hostname/p/a/t/h/file => throw!</span>
37275 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37276 <span class="hljs-comment">// TypeError [ERR_INVALID_FILE_URL_PATH]: must be absolute</span>
37278 <span class="hljs-comment">// - WHATWG file URLs convert to absolute path</span>
37279 <span class="hljs-comment">// file:///tmp/hello => /tmp/hello</span>
37280 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37283 …lass="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs
37285 <span class="hljs-comment">// On Windows</span>
37286 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37287 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37288 <span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include…
37291 <span class="hljs-comment">// On POSIX</span>
37292 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37293 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
37294 <span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include…
37297 …lass="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs
37299 <span class="hljs-comment">// On Windows</span>
37300 …="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hlj…
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]: File URL path must not include…
37307 systems, it is possible for a single file path to contain sub-sequences that
37311 …de class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-key…
37313 <span class="hljs-keyword">let</span> fd;
37314 <span class="hljs-keyword">try</span> {
37315-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-title clas…
37316 <span class="hljs-comment">// Do something with the file</span>
37317 } <span class="hljs-keyword">finally</span> {
37318 <span class="hljs-keyword">await</span> fd.<span class="hljs-title function_">close</span>();
37320 <h5>Per-drive working directories on Windows<span><a class="mark" href="#fs_per_drive_working_direc…
37321 <p>On Windows, Node.js follows the concept of per-drive working directory. This
37325 <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-r…
37329 identifier called a <em>file descriptor</em>. At the system-level, all file system
37335 <p>The callback-based <code>fs.open()</code>, and synchronous <code>fs.openSync()</code> methods op…
37342 …s="language-js mjs"><span class="hljs-keyword">import</span> { open, close, fstat } <span class="h…
37344 <span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span …
37345 …<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs
37346 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37350-title function_">open</span>(<span class="hljs-string">'/open/some/file.txt'</span>, <span class=…
37351 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
37352 <span class="hljs-keyword">try</span> {
37353 …<span class="hljs-title function_">fstat</span>(fd, <span class="hljs-function">(<span class="hljs
37354 <span class="hljs-keyword">if</span> (err) {
37355 <span class="hljs-title function_">closeFd</span>(fd);
37356 <span class="hljs-keyword">throw</span> err;
37359 <span class="hljs-comment">// use stat</span>
37361 <span class="hljs-title function_">closeFd</span>(fd);
37363 } <span class="hljs-keyword">catch</span> (err) {
37364 <span class="hljs-title function_">closeFd</span>(fd);
37365 <span class="hljs-keyword">throw</span> err;
37368 <p>The promise-based APIs use a <a href="#fs_class_filehandle" class="type">&#x3C;FileHandle></a> o…
37372 …de class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-key…
37374 <span class="hljs-keyword">let</span> file;
37375 <span class="hljs-keyword">try</span> {
37376 …hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string…
37377 …<span class="hljs-keyword">const</span> stat = <span class="hljs-keyword">await</span> file.<span …
37378 <span class="hljs-comment">// use stat</span>
37379 } <span class="hljs-keyword">finally</span> {
37380 <span class="hljs-keyword">await</span> file.<span class="hljs-title function_">close</span>();
37383 <p>All callback and promise-based file system APIs ( with the exception of
37446 <p><code>flag</code> can also be a number as documented by <a href="http://man7.org/linux/man-pages…
37450 …e>'x'</code> (<code>O_EXCL</code> flag in <a href="http://man7.org/linux/man-pages/man2/open.2.htm…
37459 <p>The behavior of some flags are platform-specific. As such, opening a directory
37463 <pre><code class="language-js"><span class="hljs-comment">// macOS and Linux</span>
37464-title function_">open</span>(<span class="hljs-string">'&#x3C;directory>'</span>, <span class="hl…
37465 …<span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, open &#x3C;dire…
37468 <span class="hljs-comment">// Windows and FreeBSD</span>
37469-title function_">open</span>(<span class="hljs-string">'&#x3C;directory>'</span>, <span class="hl…
37470 <span class="hljs-comment">// => null, &#x3C;fd></span>
37491 …re are <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects">…
37498 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
37500 <p>A utility class used to signal cancelation in selected <code>Promise</code>-based APIs.
37501 The API is based on the Web API <a href="https://developer.mozilla.org/en-US/docs/Web/API/AbortCont…
37502 <p>To use, launch Node.js using the <code>--experimental-abortcontroller</code> flag.</p>
37503 …e><code class="language-js"><span class="hljs-keyword">const</span> ac = <span class="hljs-keyword…
37505-property">signal</span>.<span class="hljs-title function_">addEventListener</span>(<span class="h…
37506 … { <span class="hljs-attr">once</span>: <span class="hljs-literal">true</span> });
37508 ac.<span class="hljs-title function_">abort</span>();
37510-variable language_">console</span>.<span class="hljs-title function_">log</span>(ac.<span class="…
37546 is called. The callback is invoked with a single object argument with a
37547 single <code>type</code> property set to <code>'abort'</code>:</p>
37548 …e><code class="language-js"><span class="hljs-keyword">const</span> ac = <span class="hljs-keyword…
37550 <span class="hljs-comment">// Use either the onabort property...</span>
37551-property">signal</span>.<span class="hljs-property">onabort</span> = <span class="hljs-function">…
37553 <span class="hljs-comment">// Or the EventTarget API...</span>
37554-property">signal</span>.<span class="hljs-title function_">addEventListener</span>(<span class="h…
37555 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span cl…
37556 }, { <span class="hljs-attr">once</span>: <span class="hljs-literal">true</span> });
37558 ac.<span class="hljs-title function_">abort</span>();</code></pre>
37573 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
37580 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects…
37590 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Funct…
37621 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
37632 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
37634 <p>In browsers, the top-level scope is the global scope. This means that
37636 Node.js this is different. The top-level scope is not the global scope;
37646 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
37655 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
37664 <pre><code class="language-js"><span class="hljs-comment">// Here, `queueMicrotask()` is used to en…
37665 <span class="hljs-comment">// emitted asynchronously, and therefore consistently. Using</span>
37666 <span class="hljs-comment">// `process.nextTick()` here would result in the 'load' event always emi…
37667 <span class="hljs-comment">// before any other promise jobs.</span>
37669-title class_">DataHandler</span>.<span class="hljs-property"><span class="hljs-keyword">prototype…
37670 …s="hljs-keyword">const</span> hit = <span class="hljs-variable language_">this</span>.<span class=…
37671 <span class="hljs-keyword">if</span> (hit !== <span class="hljs-literal">undefined</span>) {
37672 …<span class="hljs-title function_">queueMicrotask</span>(<span class="hljs-function">() =></span> {
37673 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<…
37675 <span class="hljs-keyword">return</span>;
37678 …<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> <span class…
37679 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">_cache</span>.<span …
37680 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<…
37732 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
37736 <a href="https://developer.mozilla.org/en-US/docs/WebAssembly">Mozilla Developer Network</a> for us…
37740 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
37745 In particular, large, possibly chunk-encoded, messages. The interface is
37749 <!-- eslint-skip -->
37750 <pre><code class="language-js">{ <span class="hljs-string">'content-length'</span>: <span class="hl…
37751 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain'</span>,
37752 <span class="hljs-string">'connection'</span>: <span class="hljs-string">'keep-alive'</span>,
37753 <span class="hljs-string">'host'</span>: <span class="hljs-string">'mysite.com'</span>,
37754 <span class="hljs-string">'accept'</span>: <span class="hljs-string">'*/*'</span> }</code></pre>
37757 HTTP API is very low-level. It deals with stream handling and message
37765 <!-- eslint-disable semi -->
37766 <pre><code class="language-js">[ <span class="hljs-string">'ConTent-Length'</span>, <span class="hl…
37767 <span class="hljs-string">'content-LENGTH'</span>, <span class="hljs-string">'123'</span>,
37768 <span class="hljs-string">'content-type'</span>, <span class="hljs-string">'text/plain'</span>,
37769 <span class="hljs-string">'CONNECTION'</span>, <span class="hljs-string">'keep-alive'</span>,
37770 <span class="hljs-string">'Host'</span>, <span class="hljs-string">'mysite.com'</span>,
37771 <span class="hljs-string">'accepT'</span>, <span class="hljs-string">'*/*'</span> ]</code></pre>
37778 for a given host and port, reusing a single socket connection for each
37783 <p>Pooled connections have TCP Keep-Alive enabled for them, but servers may
37800 …code class="language-js">http.<span class="hljs-title function_">get</span>(options, <span class="…
37801 <span class="hljs-comment">// Do stuff</span>
37802 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'socket'</span>, <span clas…
37803 …socket.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'agentRemove'</spa…
37807 functions, a one-time use <code>Agent</code> with default options will be used
37810 <pre><code class="language-js">http.<span class="hljs-title function_">get</span>({
37811 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'localhost'</span>,
37812 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
37813 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
37814 …<span class="hljs-attr">agent</span>: <span class="hljs-literal">false</span> <span class="hljs-c…
37815 }, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
37816 <span class="hljs-comment">// Do stuff with response</span>
37835 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
37838 <li><code>keepAlive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
37841 <code>keep-alive</code> value of the <code>Connection</code> header. The <code>Connection: keep-ali…
37846 <li><code>keepAliveMsecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
37848 for TCP Keep-Alive packets. Ignored when the
37850 <li><code>maxSockets</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
37859 <li><code>maxTotalSockets</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
37863 <li><code>maxFreeSockets</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
37866 <li><code>scheduling</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
37878 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
37887 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
37888-keyword">const</span> keepAliveAgent = <span class="hljs-keyword">new</span> http.<span class="hl…
37889 options.<span class="hljs-property">agent</span> = keepAliveAgent;
37890 http.<span class="hljs-title function_">request</span>(options, onResponseCallback);</code></pre>
37896 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
37898 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
37919-js">socket.<span class="hljs-title function_">setKeepAlive</span>(<span class="hljs-literal">true…
37920 socket.<span class="hljs-title function_">unref</span>();
37921 <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;</code></pre>
37936 the keep-alive options. Default behavior is to:</p>
37937 <pre><code class="language-js">socket.<span class="hljs-title function_">ref</span>();</code></pre>
37956 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
37967 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
37969 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
37971 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
37972 <li><code>localAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
37974 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
37977 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
37982 the name includes the CA, cert, ciphers, and other HTTPS/TLS-specific options
37989 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
37999 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38008 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38017 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
38026 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
38038 represents an <em>in-progress</em> request whose header has already been queued. The
38059 <p>Node.js does not check whether Content-Length and the length of the
38083 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38084 <span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span clas…
38085 …s="hljs-keyword">const</span> { <span class="hljs-variable constant_">URL</span> } = <span class="…
38087 <span class="hljs-comment">// Create an HTTP tunneling proxy</span>
38088 …s="hljs-keyword">const</span> proxy = http.<span class="hljs-title function_">createServer</span>(…
38089 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
38090 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'okay'</span>);
38092 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'connect'</span>, <span cla…
38093 <span class="hljs-comment">// Connect to an origin server</span>
38094-keyword">const</span> { port, hostname } = <span class="hljs-keyword">new</span> <span class="hlj…
38095-keyword">const</span> serverSocket = net.<span class="hljs-title function_">connect</span>(port |…
38096 …clientSocket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'HTTP/1.1 2…
38097 <span class="hljs-string">'Proxy-agent: Node.js-Proxy\r\n'</span> +
38098 <span class="hljs-string">'\r\n'</span>);
38099 serverSocket.<span class="hljs-title function_">write</span>(head);
38100 serverSocket.<span class="hljs-title function_">pipe</span>(clientSocket);
38101 clientSocket.<span class="hljs-title function_">pipe</span>(serverSocket);
38105 <span class="hljs-comment">// Now that proxy is running</span>
38106 …ass="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>, <span class="hljs-
38108 <span class="hljs-comment">// Make a request to a tunneling proxy</span>
38109 <span class="hljs-keyword">const</span> options = {
38110 <span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>,
38111 <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
38112 <span class="hljs-attr">method</span>: <span class="hljs-string">'CONNECT'</span>,
38113 <span class="hljs-attr">path</span>: <span class="hljs-string">'www.google.com:80'</span>
38116 …<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</spa…
38117 req.<span class="hljs-title function_">end</span>();
38119 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'connect'</span>, <span cla…
38120 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
38122 <span class="hljs-comment">// Make a request over an HTTP tunnel</span>
38123 …socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'GET / HTTP/1.1\r…
38124 <span class="hljs-string">'Host: www.google.com:80\r\n'</span> +
38125 <span class="hljs-string">'Connection: close\r\n'</span> +
38126 <span class="hljs-string">'\r\n'</span>);
38127 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
38128 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
38130 …socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span …
38131 proxy.<span class="hljs-title function_">close</span>();
38140 the request contained 'Expect: 100-continue'. This is an instruction that
38147 <li><code>info</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
38149 <li><code>httpVersion</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
38150 <li><code>httpVersionMajor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
38151 <li><code>httpVersionMinor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
38152 <li><code>statusCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
38153 <li><code>statusMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
38154 <li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
38155 <li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
38161 HTTP version, status code, status message, key-value headers object,
38163 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38165 <span class="hljs-keyword">const</span> options = {
38166 <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
38167 <span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>,
38168 <span class="hljs-attr">path</span>: <span class="hljs-string">'/length_request'</span>
38171 <span class="hljs-comment">// Make a request</span>
38172 <span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span…
38173 req.<span class="hljs-title function_">end</span>();
38175 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'information'</span>, <span c…
38176-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
38179 traditional HTTP request/response chain, such as web sockets, in-place TLS
38225 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38227 <span class="hljs-comment">// Create an HTTP server</span>
38228 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
38229 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
38230 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'okay'</span>);
38232 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'upgrade'</span>, <span cla…
38233 …socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'HTTP/1.1 101 Web…
38234 <span class="hljs-string">'Upgrade: WebSocket\r\n'</span> +
38235 <span class="hljs-string">'Connection: Upgrade\r\n'</span> +
38236 <span class="hljs-string">'\r\n'</span>);
38238 …socket.<span class="hljs-title function_">pipe</span>(socket); <span class="hljs-comment">// echo …
38241 <span class="hljs-comment">// Now that server is running</span>
38242 …ass="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>, <span class="hljs-
38244 <span class="hljs-comment">// make a request</span>
38245 <span class="hljs-keyword">const</span> options = {
38246 <span class="hljs-attr">port</span>: <span class="hljs-number">1337</span>,
38247 <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
38248 <span class="hljs-attr">headers</span>: {
38249 <span class="hljs-string">'Connection'</span>: <span class="hljs-string">'Upgrade'</span>,
38250 <span class="hljs-string">'Upgrade'</span>: <span class="hljs-string">'websocket'</span>
38254 …<span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</spa…
38255 req.<span class="hljs-title function_">end</span>();
38257 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'upgrade'</span>, <span cla…
38258 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
38259 socket.<span class="hljs-title function_">end</span>();
38260 process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>);
38267 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
38283 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38291 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
38309 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
38310 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
38311 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
38312 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
38334 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
38335 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
38346 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38354 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
38356 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38368 then tries to pack the request headers and data into a single TCP packet.</p>
38369 <p>That's usually desired (it saves a TCP round-trip), but not when the first
38377 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
38378 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
38380 <p>Reads out a header on the request. The name is case-insensitive.
38383 …="language-js">request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-strin…
38384 …s-title function_">setHeader</span>(<span class="hljs-string">'Content-Length'</span>, <span class…
38385 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Cookie'</span>, [<span class="hl…
38386 …lass="hljs-keyword">const</span> contentType = request.<span class="hljs-title function_">getHeade…
38387 <span class="hljs-comment">// 'contentType' is 'text/html'</span>
38388 …ass="hljs-keyword">const</span> contentLength = request.<span class="hljs-title function_">getHead…
38389 <span class="hljs-comment">// 'contentLength' is of type number</span>
38390 <span class="hljs-keyword">const</span> cookie = request.<span class="hljs-title function_">getHead…
38391 <span class="hljs-comment">// 'cookie' is of type string[]</span></code></pre>
38397 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
38401 …class="language-js">request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-
38402 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class…
38404 <span class="hljs-keyword">const</span> headerNames = request.<span class="hljs-title function_">ge…
38405 <span class="hljs-comment">// headerNames === ['Foo', 'Set-Cookie']</span></code></pre>
38408 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38416 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
38423 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
38430 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
38437 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
38444 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
38447 …pre><code class="language-js">request.<span class="hljs-title function_">removeHeader</span>(<span…
38453 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38455 <p>When sending request through a keep-alive enabled agent, the underlying socket
38458 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38460 <span class="hljs-comment">// Server has a 5 seconds keep-alive timeout by default</span>
38462 ….<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="…
38463 res.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello\n'</span>);
38464 res.<span class="hljs-title function_">end</span>();
38466 .<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);
38468 <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> {
38469 <span class="hljs-comment">// Adapting a keep-alive agent</span>
38470 …hljs-title function_">get</span>(<span class="hljs-string">'http://localhost:3000'</span>, { agent…
38471 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
38472 <span class="hljs-comment">// Do nothing</span>
38475 }, <span class="hljs-number">5000</span>); <span class="hljs-comment">// Sending request on 5s inte…
38478 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38479-keyword">const</span> agent = <span class="hljs-keyword">new</span> http.<span class="hljs-title …
38481 <span class="hljs-keyword">function</span> <span class="hljs-title function_">retriableRequest</spa…
38482 <span class="hljs-keyword">const</span> req = http
38483 …hljs-title function_">get</span>(<span class="hljs-string">'http://localhost:3000'</span>, { agent…
38484 <span class="hljs-comment">// ...</span>
38486 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
38487 <span class="hljs-comment">// Check if retry is needed</span>
38488-keyword">if</span> (req.<span class="hljs-property">reusedSocket</span> &#x26;&#x26; err.<span cl…
38489 <span class="hljs-title function_">retriableRequest</span>();
38494 <span class="hljs-title function_">retriableRequest</span>();</code></pre>
38500 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
38501 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
38503 <p>Sets a single header value for headers object. If this header already exists in
38504 the to-be-sent headers, its value will be replaced. Use an array of strings
38505 here to send multiple headers with the same name. Non-string values will be
38507 non-string values. However, the non-string values will be converted to strings
38509 …="language-js">request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-strin…
38511-js">request.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Cookie…
38517 <li><code>noDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
38526 <li><code>enable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
38527 <li><code>initialDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
38544 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
38545 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
38561 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38562 <span class="hljs-keyword">const</span> options = {
38563 <span class="hljs-attr">host</span>: <span class="hljs-string">'www.google.com'</span>,
38565 <span class="hljs-keyword">const</span> req = http.<span class="hljs-title function_">get</span>(op…
38566 req.<span class="hljs-title function_">end</span>();
38567 …pan class="hljs-title function_">once</span>(<span class="hljs-string">'response'</span>, <span cl…
38568 …<span class="hljs-keyword">const</span> ip = req.<span class="hljs-property">socket</span>.<span c…
38569 …<span class="hljs-keyword">const</span> port = req.<span class="hljs-property">socket</span>.<span…
38570-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
38571 <span class="hljs-comment">// Consume response object</span>
38581 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38591 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38600 <li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
38601 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
38602 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
38603 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
38606 <code>Content-Length</code> is set, data will automatically be encoded in HTTP Chunked
38608 <code>Transfer-Encoding: chunked</code> header is added. Calling <a href="#http_request_end_data_en…
38613 is flushed, but only if the chunk is non-empty.</p>
38634 <p>Emitted each time a request with an HTTP <code>Expect: 100-continue</code> is received.
38652 value is not <code>100-continue</code>. If this event is not listened for, the server will
38673 <li><code>exception</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
38688 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
38690 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
38691 res.<span class="hljs-title function_">end</span>();
38693 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'clientError'</span>, <span c…
38694 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'HTTP/1.1 400 Bad R…
38696 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</co…
38711-js">server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'clientError'</…
38712 …hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string"…
38713 <span class="hljs-keyword">return</span>;
38716 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'HTTP/1.1 400 Bad R…
38758 <code>server.keepAliveTimeout</code> is non-zero).</p>
38771 per connection (in the case of HTTP Keep-Alive connections).</p>
38803 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
38811 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38832 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
38839 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38847 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38853 <p>It must be set to a non-zero value (e.g. 120 seconds) to protect against
38854 potential Denial-of-Service attacks in case the server is deployed without a
38869 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
38870 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
38877 will be called with the timed-out socket as an argument.</p>
38894 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38906 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
38910 will be destroyed. If the server receives new data before the keep-alive
38913 <p>A value of <code>0</code> will disable the keep-alive timeout behavior on incoming
38916 to 8.0.0, which did not have a keep-alive timeout.</p>
38947 <li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
38956-js">response.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</s…
38957 … <span class="hljs-string">'Trailer'</span>: <span class="hljs-string">'Content-MD5'</span> });
38958 response.<span class="hljs-title function_">write</span>(fileData);
38959 …sponse.<span class="hljs-title function_">addTrailers</span>({ <span class="hljs-string">'Content-
38960 response.<span class="hljs-title function_">end</span>();</code></pre>
38967 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
38990 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
38991 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
38992 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
38993 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
39006 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
39008 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39022 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39023 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
39026 The name is case-insensitive. The type of the return value depends
39028 …="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-stri…
39029 …s-title function_">setHeader</span>(<span class="hljs-string">'Content-Length'</span>, <span class…
39030 …js-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class="…
39031 …lass="hljs-keyword">const</span> contentType = response.<span class="hljs-title function_">getHead…
39032 <span class="hljs-comment">// contentType is 'text/html'</span>
39033 …ass="hljs-keyword">const</span> contentLength = response.<span class="hljs-title function_">getHea…
39034 <span class="hljs-comment">// contentLength is of type number</span>
39035 …n class="hljs-keyword">const</span> setCookie = response.<span class="hljs-title function_">getHea…
39036 <span class="hljs-comment">// setCookie is of type string[]</span></code></pre>
39042 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
39046 …lass="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-
39047 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class…
39049 <span class="hljs-keyword">const</span> headerNames = response.<span class="hljs-title function_">g…
39050 <span class="hljs-comment">// headerNames === ['foo', 'set-cookie']</span></code></pre>
39056 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
39060 header-related http module methods. The keys of the returned object are the
39067 …lass="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-
39068 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class…
39070 <span class="hljs-keyword">const</span> headers = response.<span class="hljs-title function_">getHe…
39071 <span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</spa…
39077 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39078 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
39081 outgoing headers. The header name matching is case-insensitive.</p>
39082-js"><span class="hljs-keyword">const</span> hasContentType = response.<span class="hljs-title fun…
39088 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39090 <p>Boolean (read-only). True if headers were sent, false otherwise.</p>
39096 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39099 …re><code class="language-js">response.<span class="hljs-title function_">removeHeader</span>(<span…
39105 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39116 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39117 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39121 <p>Sets a single header value for implicit headers. If this header already exists
39122 in the to-be-sent headers, its value will be replaced. Use an array of strings
39123 here to send multiple headers with the same name. Non-string values will be
39125 non-string values. However, the non-string values will be converted to strings
39128 …="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-stri…
39130-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-C…
39136 <pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</spa…
39137 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
39138 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</…
39139 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, …
39140 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
39141 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
39154 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39155 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39176 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
39177 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
39178 …<span class="hljs-keyword">const</span> ip = res.<span class="hljs-property">socket</span>.<span c…
39179 …<span class="hljs-keyword">const</span> port = res.<span class="hljs-property">socket</span>.<span…
39180-title function_">end</span>(<span class="hljs-string">`Your IP address is <span class="hljs-subst…
39181 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code><…
39190 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
39195 <pre><code class="language-js">response.<span class="hljs-property">statusCode</span> = <span class…
39203 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39209 <pre><code class="language-js">response.<span class="hljs-property">statusMessage</span> = <span cl…
39222 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39232 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39241 <li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39242 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
39243 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39244 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
39256 <p>This is the raw HTTP body and has nothing to do with higher-level multi-part
39290 <li><code>statusCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
39291 <li><code>statusMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
39292 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
39295 <p>Sends a response header to the request. The status code is a 3-digit HTTP
39297 Optionally one can give a human-readable <code>statusMessage</code> as the second
39300 It is <em>not</em> a list of tuples. So, the even-numbered offsets are key values,
39301 and the odd-numbered offsets are the associated values. The array is in the same
39304 <pre><code class="language-js"><span class="hljs-keyword">const</span> body = <span class="hljs-str…
39306 .<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, {
39307 …<span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<…
39308 <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain'</span>
39310 .<span class="hljs-title function_">end</span>(body);</code></pre>
39324 <pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</spa…
39325 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
39326 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</…
39327 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, …
39328 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
39329 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
39331 <p><code>Content-Length</code> is given in bytes, not characters. Use
39333 does not check whether <code>Content-Length</code> and the length of the body which has
39377 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39386 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39392 <pre><code class="language-js"><span class="hljs-keyword">const</span> req = http.<span class="hljs
39393 <span class="hljs-attr">host</span>: <span class="hljs-string">'127.0.0.1'</span>,
39394 <span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>,
39395 <span class="hljs-attr">method</span>: <span class="hljs-string">'POST'</span>
39396 }, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
39397 res.<span class="hljs-title function_">resume</span>();
39398 …res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span cla…
39399 <span class="hljs-keyword">if</span> (!res.<span class="hljs-property">complete</span>)
39400 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
39401 …<span class="hljs-string">'The connection was terminated while the message was still being sent'</…
39417 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
39418 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
39428 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
39431 <p>Key-value pairs of header names and values. Header names are lower-cased.</p>
39432 <pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
39433 <span class="hljs-comment">//</span>
39434 <span class="hljs-comment">// { 'user-agent': 'curl/7.22.0',</span>
39435 <span class="hljs-comment">// host: '127.0.0.1:8000',</span>
39436 <span class="hljs-comment">// accept: '*/*' }</span>
39437 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
39441 …f <code>age</code>, <code>authorization</code>, <code>content-length</code>, <code>content-type</c…
39442 …/code>, <code>from</code>, <code>host</code>, <code>if-modified-since</code>, <code>if-unmodified-
39443 <code>last-modified</code>, <code>location</code>, <code>max-forwards</code>, <code>proxy-authoriza…
39444 <code>retry-after</code>, <code>server</code>, or <code>user-agent</code> are discarded.</li>
39445 <li><code>set-cookie</code> is always an array. Duplicates are added to the array.</li>
39454 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39457 client response, the HTTP version of the connected-to server.
39466 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39475 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39479 list of tuples. So, the even-numbered offsets are key values, and the
39480 odd-numbered offsets are the associated values.</p>
39482 <pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
39483 <span class="hljs-comment">//</span>
39484 <span class="hljs-comment">// [ 'user-agent',</span>
39485 <span class="hljs-comment">// 'this is invalid because there can be only one',</span>
39486 <span class="hljs-comment">// 'User-Agent',</span>
39487 <span class="hljs-comment">// 'curl/7.22.0',</span>
39488 <span class="hljs-comment">// 'Host',</span>
39489 <span class="hljs-comment">// '127.0.0.1:8000',</span>
39490 <span class="hljs-comment">// 'ACCEPT',</span>
39491 <span class="hljs-comment">// '*/*' ]</span>
39492 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
39498 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39507 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39508 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39530 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
39533 <p>The 3-digit HTTP response status code. E.G. <code>404</code>.</p>
39539 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39548 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
39556 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39561 …ode class="language-http"><span class="hljs-keyword">GET</span> <span class="hljs-string">/status?…
39562 <span class="hljs-attribute">Accept</span><span class="hljs-punctuation">: </span>text/plain</code>…
39564-js"><span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(request.<…
39567 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
39568 <span class="hljs-meta">> </span><span class="language-bash">new URL(request.url, `http://<span cla…
39615 <li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
39622-js">message.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</sp…
39623 … <span class="hljs-string">'Trailer'</span>: <span class="hljs-string">'Content-MD5'</span> });
39624 message.<span class="hljs-title function_">write</span>(fileData);
39625 …essage.<span class="hljs-title function_">addTrailers</span>({ <span class="hljs-string">'Content-
39626 message.<span class="hljs-title function_">end</span>();</code></pre>
39633 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
39645 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
39646 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
39663 <li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39664 …illa.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Opt…
39665 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39666 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
39683 is written. It then tries to pack the headers and data into a single TCP
39685 <p>It is usually desired (it saves a TCP round-trip), but not when the first
39693 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39694 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
39703 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String…
39712 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
39716 various header-related HTTP module methods. The keys of the returned
39723 …s="language-js">outgoingMessage.<span class="hljs-title function_">setHeader</span>(<span class="h…
39724 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class…
39726 <span class="hljs-keyword">const</span> headers = outgoingMessage.<span class="hljs-title function_…
39727 <span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</spa…
39733 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39734 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolea…
39737 outgoing headers. The header name is case-insensitive.</p>
39738-js"><span class="hljs-keyword">const</span> hasContentType = outgoingMessage.<span class="hljs-ti…
39744 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39746 <p>Read-only. <code>true</code> if the headers were sent, otherwise <code>false</code>.</p>
39753 <p>Since <code>OutgoingMessage</code> should be a write-only stream,
39762 …<code class="language-js">outgoingMessage.<span class="hljs-title function_">removeHeader</span>(<…
39768 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
39769 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39770 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
39772 <p>Sets a single header value for the header object.</p>
39778 <li><code>msesc</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39779 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39783 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
39807 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
39816 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39826 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39834 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
39846 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
39855 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
39871 <li><code>chunk</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
39872 …r.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a…
39873 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
39874 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolea…
39890 with higher-level multi-part body encodings that may be used.</p>
39904 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
39912 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
39934 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
39942 <li><code>insecureHTTPParser</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
39944 avoided. See <a href="#cli_insecure_http_parser"><code>--insecure-http-parser</code></a> for more i…
39946 <li><code>maxHeaderSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
39947 <a href="#cli_max_http_header_size_size"><code>--max-http-header-size</code></a> for requests recei…
39953 <p><code>requestListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
39963-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><pre><code cl…
39965 <span class="hljs-comment">// Create a local server to receive data from</span>
39966 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
39967 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
39968 …res.<span class="hljs-title function_">end</span>(<span class="hljs-variable constant_">JSON</span…
39969 <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>
39973-title function_">listen</span>(<span class="hljs-number">8000</span>);</code></pre><code class="l…
39975 <span class="hljs-comment">// Create a local server to receive data from</span>
39976 <span class="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServ…
39978 <span class="hljs-comment">// Listen to the request event</span>
39979 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'request'</span>, <span cla…
39980 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
39981 …res.<span class="hljs-title function_">end</span>(<span class="hljs-variable constant_">JSON</span…
39982 <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>
39986 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</co…
40003 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
40004 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
40007 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
40015 <p>The <code>callback</code> is invoked with a single argument that is an instance of
40018-js">http.<span class="hljs-title function_">get</span>(<span class="hljs-string">'http://localhos…
40019 <span class="hljs-keyword">const</span> { statusCode } = res;
40020 …<span class="hljs-keyword">const</span> contentType = res.<span class="hljs-property">headers</spa…
40022 <span class="hljs-keyword">let</span> error;
40023 <span class="hljs-comment">// Any 2xx status code signals a successful response but</span>
40024 <span class="hljs-comment">// here we're only checking for 200.</span>
40025 <span class="hljs-keyword">if</span> (statusCode !== <span class="hljs-number">200</span>) {
40026 …error = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span c…
40027 … <span class="hljs-string">`Status Code: <span class="hljs-subst">${statusCode}</span>`</span>);
40028 …lass="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!<span class="hljs-regexp">/…
40029 …or = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span clas…
40030 …<span class="hljs-string">`Expected application/json but received <span class="hljs-subst">${conte…
40032 <span class="hljs-keyword">if</span> (error) {
40033 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
40034 <span class="hljs-comment">// Consume response data to free up memory</span>
40035 res.<span class="hljs-title function_">resume</span>();
40036 <span class="hljs-keyword">return</span>;
40039 …res.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
40040 <span class="hljs-keyword">let</span> rawData = <span class="hljs-string">''</span>;
40041 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
40042 …res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span cla…
40043 <span class="hljs-keyword">try</span> {
40044 …<span class="hljs-keyword">const</span> parsedData = <span class="hljs-variable constant_">JSON</s…
40045 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
40046 } <span class="hljs-keyword">catch</span> (e) {
40047 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
40050 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
40051 …="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span cl…
40054 <span class="hljs-comment">// Create a local server to receive data from</span>
40055 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
40056 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
40057 …res.<span class="hljs-title function_">end</span>(<span class="hljs-variable constant_">JSON</span…
40058 <span class="hljs-attr">data</span>: <span class="hljs-string">'Hello World!'</span>
40062 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</co…
40077 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
40079 <p>Read-only property specifying the maximum allowed size of HTTP headers in bytes.
40080 …figurable using the <a href="#cli_max_http_header_size_size"><code>--max-http-header-size</code></…
40107 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
40108 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
40110 …ass="type">&#x3C;http.Agent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
40118 <li><code>auth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40120 <li><code>createConnection</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
40125 <li><code>defaultPort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
40127 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
40130 <li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
40131 <li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
40132 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40134 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
40136 <li><code>insecureHTTPParser</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
40138 avoided. See <a href="#cli_insecure_http_parser"><code>--insecure-http-parser</code></a> for more i…
40140 <li><code>localAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
40141 <li><code>localPort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
40142 <li><code>lookup</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
40143 <li><code>maxHeaderSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
40144 <a href="#cli_max_http_header_size_size"><code>--max-http-header-size</code></a> for requests recei…
40147 <li><code>method</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
40149 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40153 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40155 <li><code>protocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
40156 <li><code>setHost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
40158 <li><code>socketPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
40160 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
40166 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
40176 <p>The optional <code>callback</code> parameter will be added as a one-time listener for
40181 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
40183 <span class="hljs-keyword">const</span> postData = <span class="hljs-variable constant_">JSON</span…
40184 <span class="hljs-string">'msg'</span>: <span class="hljs-string">'Hello World!'</span>
40187 <span class="hljs-keyword">const</span> options = {
40188 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'www.google.com'</span>,
40189 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
40190 <span class="hljs-attr">path</span>: <span class="hljs-string">'/upload'</span>,
40191 <span class="hljs-attr">method</span>: <span class="hljs-string">'POST'</span>,
40192 <span class="hljs-attr">headers</span>: {
40193 …<span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'application/json'</spa…
40194 …<span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<…
40198 …="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options,…
40199 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
40200-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
40201 …res.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
40202 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
40203 …lass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span …
40205 …res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span cla…
40206 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
40210 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
40211 …s-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="…
40214 <span class="hljs-comment">// Write data to request body</span>
40215 req.<span class="hljs-title function_">write</span>(postData);
40216 req.<span class="hljs-title function_">end</span>();</code></pre>
40218 must always call <code>req.end()</code> to signify the end of the request -
40227 <p>Sending a 'Connection: keep-alive' will notify Node.js that the connection to
40231 <p>Sending a 'Content-Length' header will disable the default chunked encoding.</p>
40235 Usually, when sending 'Expect: 100-continue', both a timeout and a listener
40245-js"><span class="hljs-keyword">const</span> options = <span class="hljs-keyword">new</span> <span…
40247 …="hljs-keyword">const</span> req = http.<span class="hljs-title function_">request</span>(options,…
40248 <span class="hljs-comment">// ...</span>
40365 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40367 <p>Performs the low-level validations on the provided <code>name</code> that are done when
40375 …s="language-js"><span class="hljs-keyword">const</span> { validateHeaderName } = <span class="hljs
40377 <span class="hljs-keyword">try</span> {
40378 <span class="hljs-title function_">validateHeaderName</span>(<span class="hljs-string">''</span>);
40379 } <span class="hljs-keyword">catch</span> (err) {
40380 …err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">TypeError</span>;…
40381 …err.<span class="hljs-property">code</span>; <span class="hljs-comment">// --> 'ERR_INVALID_HTTP_T…
40382 …err.<span class="hljs-property">message</span>; <span class="hljs-comment">// --> 'Header name mus…
40389 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40390 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
40392 <p>Performs the low-level validations on the provided <code>value</code> that are done when
40402 …="language-js"><span class="hljs-keyword">const</span> { validateHeaderValue } = <span class="hljs
40404 <span class="hljs-keyword">try</span> {
40405 …<span class="hljs-title function_">validateHeaderValue</span>(<span class="hljs-string">'x-my-head…
40406 } <span class="hljs-keyword">catch</span> (err) {
40407 …err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">TypeError</span>;…
40408 …an class="hljs-property">code</span> === <span class="hljs-string">'ERR_HTTP_INVALID_HEADER_VALUE'…
40409 …err.<span class="hljs-property">message</span>; <span class="hljs-comment">// --> 'Invalid value "…
40412 <span class="hljs-keyword">try</span> {
40413 …<span class="hljs-title function_">validateHeaderValue</span>(<span class="hljs-string">'x-my-head…
40414 } <span class="hljs-keyword">catch</span> (err) {
40415 …err <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">TypeError</span>;…
40416 …rr.<span class="hljs-property">code</span> === <span class="hljs-string">'ERR_INVALID_CHAR'</span>…
40417 …err.<span class="hljs-property">message</span>; <span class="hljs-comment">// --> 'Invalid charact…
40435 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
40439 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40441 <p>The Core API provides a low-level interface designed specifically around
40447 <code>'stream'</code>, can be emitted either by client-side code or server-side code.</p>
40448 <h4>Server-side example<span><a class="mark" href="#http2_server_side_example" id="http2_server_sid…
40451 <a href="https://http2.github.io/faq/#does-http2-require-encryption">unencrypted HTTP/2</a>, the us…
40454 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40455 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
40457 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSec…
40458 …<span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<spa…
40459 …<span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<sp…
40461-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">…
40463 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
40464 <span class="hljs-comment">// stream is a Duplex</span>
40465 stream.<span class="hljs-title function_">respond</span>({
40466 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf
40467 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
40469 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello Wor…
40472 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8443</span>);</co…
40474 <pre><code class="language-bash">openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj <span clas…
40475 -keyout localhost-privkey.pem -out localhost-cert.pem</code></pre>
40476 <h4>Client-side example<span><a class="mark" href="#http2_client_side_example" id="http2_client_sid…
40478 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40479 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
40480 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
40481 …<span class="hljs-attr">ca</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span…
40483-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">…
40485 …js-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span cl…
40487 …span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span cla…
40488 …<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="h…
40489-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
40493 req.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
40494 <span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
40495 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
40496 req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span clas…
40497 … class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<spa…
40498 client.<span class="hljs-title function_">close</span>();
40500 req.<span class="hljs-title function_">end</span>();</code></pre>
40518 <p>User code will not create <code>Http2Session</code> instances directly. Server-side
40520 new HTTP/2 connection is received. Client-side <code>Http2Session</code> instances are
40555 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
40564 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40565 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40566 <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
40582 <li><code>errorCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
40583 <li><code>lastStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
40602 …ass="language-js">session.<span class="hljs-title function_">settings</span>({ <span class="hljs-a…
40604 …an class="hljs-title function_">on</span>(<span class="hljs-string">'localSettings'</span>, <span …
40605 <span class="hljs-comment">/* Use the new settings */</span>
40612 …buffer_class_buffer" class="type">&#x3C;Buffer></a> The <code>PING</code> frame 8-byte payload</li>
40625-js">session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'remoteSetting…
40626 <span class="hljs-comment">/* Use the new settings */</span>
40635 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
40636 <li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere…
40640 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40641 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
40642 …<span class="hljs-keyword">const</span> method = headers[<span class="hljs-string">':method'</span…
40643 <span class="hljs-keyword">const</span> path = headers[<span class="hljs-string">':path'</span>];
40644 <span class="hljs-comment">// ...</span>
40645 stream.<span class="hljs-title function_">respond</span>({
40646 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>,
40647 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=ut…
40649 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
40650 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
40656 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40658 <span class="hljs-comment">// Create an unencrypted HTTP/2 server</span>
40659 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
40661 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
40662 stream.<span class="hljs-title function_">respond</span>({
40663 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf
40664 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
40666-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">…
40667 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello Wor…
40670 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">80</span>);</code…
40682 <pre><code class="language-js">session.<span class="hljs-built_in">setTimeout</span>(<span class="h…
40683 …lass="hljs-title function_">on</span>(<span class="hljs-string">'timeout'</span>, <span class="hlj…
40689 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
40700 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
40713 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
40722 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
40732 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
40734 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40750 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
40759 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
40770 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
40771 <li><code>lastStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
40772 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
40784 <p>A prototype-less object describing the current local settings of this
40791 …illa.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> |…
40802 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
40813 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
40814 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
40815 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
40828 acknowledgment was received, and a <code>Buffer</code> containing the 8-byte <code>PING</code>
40830-js">session.<span class="hljs-title function_">ping</span>(<span class="hljs-title class_">Buffer…
40831 <span class="hljs-keyword">if</span> (!err) {
40832 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
40833 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
40837 64-bit timestamp (little endian) marking the start of the <code>PING</code> duration.</p>
40851 <p>A prototype-less object describing the current remote settings of this
40858 <li><code>windowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
40863 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40865 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
40866 <span class="hljs-keyword">const</span> expectedWindowSize = <span class="hljs-number">2</span> ** …
40867 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'connect'</span>, <span cla…
40869 <span class="hljs-comment">// Set local window size to be 2 ** 20</span>
40870 session.<span class="hljs-title function_">setLocalWindowSize</span>(expectedWindowSize);
40877 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
40878 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
40904 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
40906 <li><code>effectiveLocalWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
40908 <li><code>effectiveRecvDataLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
40910 <li><code>nextStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
40912 <li><code>localWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
40914 <li><code>lastProcStreamID</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
40916 <li><code>remoteWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
40918 <li><code>outboundQueueSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
40920 <li><code>deflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
40922 <li><code>inflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
40934 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
40937 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> |…
40939 <li><code>duration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
40956 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
40980 <li><code>alt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
40982-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="htt…
40988 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
40990 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
40991 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'session'</span>, <span cla…
40992 <span class="hljs-comment">// Set altsvc for origin https://example.org:80</span>
40993 …session.<span class="hljs-title function_">altsvc</span>(<span class="hljs-string">'h2=":8000"'</s…
40996 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
40997 <span class="hljs-comment">// Set altsvc for a specific stream</span>
40998 …ljs-property">session</span>.<span class="hljs-title function_">altsvc</span>(<span class="hljs-st…
41017 ASCII string containing a comma-delimited list of "alternative" protocols
41024 … href="https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#al…
41032-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#ur…
41038 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41039 <span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getSecureOptio…
41040 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSec…
41041 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41042 stream.<span class="hljs-title function_">respond</span>();
41043 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
41045 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'session'</span>, <span cla…
41046 …ssion.<span class="hljs-title function_">origin</span>(<span class="hljs-string">'https://example.…
41059 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41060 <span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getSecureOptio…
41061 options.<span class="hljs-property">origins</span> = [<span class="hljs-string">'https://example.co…
41062 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSec…
41063 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41064 stream.<span class="hljs-title function_">respond</span>();
41065 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
41079 <li><code>alt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
41080 <li><code>origin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41081 <li><code>streamId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
41087 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41088 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
41090 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'altsvc'</span>, <span clas…
41091 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41092 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41093 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41100 <li><code>origins</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
41106 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41107 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
41109 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'origin'</span>, <span clas…
41110 …ass="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-number"…
41111 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41123 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
41125 <li><code>endStream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
41128 <li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
41132 <li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41134 <li><code>weight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41137 <li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
41152 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41153 <span class="hljs-keyword">const</span> clientSession = http2.<span class="hljs-title function_">co…
41154 <span class="hljs-keyword">const</span> {
41157 } = http2.<span class="hljs-property">constants</span>;
41159 …ass="hljs-keyword">const</span> req = clientSession.<span class="hljs-title function_">request</sp…
41160 …span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span cla…
41161 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41162-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(…
41163 … class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-
41176 <p>The <code>:method</code> and <code>:path</code> pseudo-headers are not specified within <code>he…
41190 communications stream over an <code>Http2Session</code> instance. Any single <code>Http2Session</co…
41191 may have up to 2<sup>31</sup>-1 <code>Http2Stream</code> instances over its lifetime.</p>
41206 <p>The default text character encoding for all <code>Http2Stream</code>s is UTF-8. As a best
41208 the <code>'content-type'</code> header should be set and should identify the character
41210 <pre><code class="language-js">stream.<span class="hljs-title function_">respond</span>({
41211 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf
41212 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
41256 abnormally aborted in mid-communication.
41274 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
41283 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
41284 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
41285 <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
41314 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
41322 …ge-js">stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'trailers'</s…
41323 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41338 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41347 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
41356 …s://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3…
41358 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
41368 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41376 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41385 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41395 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
41404 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41413 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
41415 <li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
41419 <li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41421 <li><code>weight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41424 <li><code>silent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41435 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
41480 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
41481 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
41483 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41484 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
41485 <span class="hljs-keyword">const</span> { NGHTTP2_CANCEL } = http2.<span class="hljs-property">cons…
41486 …js-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span cl…
41488 <span class="hljs-comment">// Cancel the stream if there's no activity after 5 seconds</span>
41489 …class="hljs-built_in">setTimeout</span>(<span class="hljs-number">5000</span>, <span class="hljs-f…
41497 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
41499 <li><code>localWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
41501 …eloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;numbe…
41503 <li><code>localClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
41504 <li><code>remoteClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
41506 <li><code>sumDependencyWeight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
41509 <li><code>weight</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41527 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41528 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41529 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41530 …ljs-title function_">respond</span>(<span class="hljs-literal">undefined</span>, { <span class="hl…
41531 …stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span…
41532 …stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">xyz</span>…
41534 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Hello World'</span…
41536 <p>The HTTP/1 specification forbids trailers from containing HTTP/2 pseudo-header
41554 the request contained <code>Expect: 100-continue</code>. This is an instruction that
41564 …ge-js">stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'headers'</sp…
41565 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41574 …uage-js">stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'push'</spa…
41575 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41585 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41586 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
41587 …js-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span cl…
41588 …span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span cla…
41589 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41615 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41617 <p>True if headers were sent, false otherwise (read-only).</p>
41623 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
41625 <p>Read-only property mapped to the <code>SETTINGS_ENABLE_PUSH</code> flag of the remote
41635 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
41637 <li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
41641 <li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41645 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
41648 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
41658 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41659 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41660 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41661 …stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</sp…
41662-title function_">pushStream</span>({ <span class="hljs-string">':path'</span>: <span class="hljs-
41663 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
41664 …pushStream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'…
41665 …pushStream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some pushed da…
41667 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
41671 <code>true</code> to enable server-side bandwidth balancing between concurrent streams.</p>
41688 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
41690 <li><code>endStream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
41692 <li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
41697 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41698 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41699 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41700 …stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</sp…
41701 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
41711 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41712 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41713 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41714-title function_">respond</span>({ <span class="hljs-string">':status'</span>: <span class="hljs-n…
41715 …stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span…
41716 …stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>…
41718 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'some data'</span>);
41737 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
41739 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
41741 <li><code>statCheck</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
41742 <li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
41744 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41745 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41755 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41756 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
41758 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41759 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41760 …ljs-keyword">const</span> fd = fs.<span class="hljs-title function_">openSync</span>(<span class="…
41762 …<span class="hljs-keyword">const</span> stat = fs.<span class="hljs-title function_">fstatSync</sp…
41763 <span class="hljs-keyword">const</span> headers = {
41764 <span class="hljs-string">'content-length'</span>: stat.<span class="hljs-property">size</span>,
41765 …<span class="hljs-string">'last-modified'</span>: stat.<span class="hljs-property">mtime</span>.<s…
41766 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=ut…
41768 stream.<span class="hljs-title function_">respondWithFD</span>(fd, headers);
41769 …lass="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-
41782 is not supported and may result in data loss. Re-using a file descriptor
41792 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41793 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
41795 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41796 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41797 …ljs-keyword">const</span> fd = fs.<span class="hljs-title function_">openSync</span>(<span class="…
41799 …<span class="hljs-keyword">const</span> stat = fs.<span class="hljs-title function_">fstatSync</sp…
41800 <span class="hljs-keyword">const</span> headers = {
41801 <span class="hljs-string">'content-length'</span>: stat.<span class="hljs-property">size</span>,
41802 …<span class="hljs-string">'last-modified'</span>: stat.<span class="hljs-property">mtime</span>.<s…
41803 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=ut…
41805 …span class="hljs-title function_">respondWithFD</span>(fd, headers, { <span class="hljs-attr">wait…
41806 …stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span…
41807 …stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>…
41810 …lass="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-
41827 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
41829 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
41831 <li><code>statCheck</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
41832 <li><code>onError</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
41834 <li><code>waitForTrailers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
41836 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41837 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
41853 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41854 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41855 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41856 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">statCheck</span>(<sp…
41857 …headers[<span class="hljs-string">'last-modified'</span>] = stat.<span class="hljs-property">mtime…
41860 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">onError</span>(<span…
41861 …<span class="hljs-comment">// stream.respond() can throw if the stream has been destroyed by</span>
41862 <span class="hljs-comment">// the other side.</span>
41863 <span class="hljs-keyword">try</span> {
41864 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class=…
41865 …stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</sp…
41866 } <span class="hljs-keyword">else</span> {
41867 …stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</sp…
41869 } <span class="hljs-keyword">catch</span> (err) {
41870 <span class="hljs-comment">// Perform actual error handling.</span>
41871 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
41873 stream.<span class="hljs-title function_">end</span>();
41876 …stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/…
41877 …{ <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=…
41884 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41885 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41886 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41887 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">statCheck</span>(<sp…
41888 <span class="hljs-comment">// Check the stat here...</span>
41889 …stream.<span class="hljs-title function_">respond</span>({ <span class="hljs-string">':status'</sp…
41890 …<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; <span class="hlj…
41892 …stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/…
41893 …{ <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=…
41896 <p>The <code>content-length</code> header field will be automatically set.</p>
41911 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41912 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
41913 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
41914 …stream.<span class="hljs-title function_">respondWithFile</span>(<span class="hljs-string">'/some/…
41915 …{ <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/plain; charset=…
41916 … { <span class="hljs-attr">waitForTrailers</span>: <span class="hljs-literal">true</span> });
41917 …stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'wantTrailers'</span…
41918 …stream.<span class="hljs-title function_">sendTrailers</span>({ <span class="hljs-attr">ABC</span>…
41941 a request with an HTTP <code>Expect: 100-continue</code> is received. If this event is
41991 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
41992 <li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere…
41998 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
41999 <span class="hljs-keyword">const</span> {
42004 } = http2.<span class="hljs-property">constants</span>;
42006 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
42007 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42008 <span class="hljs-keyword">const</span> method = headers[HTTP2_HEADER_METHOD];
42009 <span class="hljs-keyword">const</span> path = headers[HTTP2_HEADER_PATH];
42010 <span class="hljs-comment">// ...</span>
42011 stream.<span class="hljs-title function_">respond</span>({
42012 [HTTP2_HEADER_STATUS]: <span class="hljs-number">200</span>,
42013 [HTTP2_HEADER_CONTENT_TYPE]: <span class="hljs-string">'text/plain; charset=utf-8'</span>
42015 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
42016 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
42038 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
42060 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
42061 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
42083 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
42120 time a request with an HTTP <code>Expect: 100-continue</code> is received. If this event
42170 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
42171 <li><code>rawHeaders</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere…
42177 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42178 <span class="hljs-keyword">const</span> {
42183 } = http2.<span class="hljs-property">constants</span>;
42185 <span class="hljs-keyword">const</span> options = <span class="hljs-title function_">getOptionsSome…
42187 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSec…
42188 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42189 <span class="hljs-keyword">const</span> method = headers[HTTP2_HEADER_METHOD];
42190 <span class="hljs-keyword">const</span> path = headers[HTTP2_HEADER_PATH];
42191 <span class="hljs-comment">// ...</span>
42192 stream.<span class="hljs-title function_">respond</span>({
42193 [HTTP2_HEADER_STATUS]: <span class="hljs-number">200</span>,
42194 [HTTP2_HEADER_CONTENT_TYPE]: <span class="hljs-string">'text/plain; charset=utf-8'</span>
42196 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello '</span>);
42197 stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world'</span>);
42221 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
42235 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
42236 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
42258 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
42304 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
42306 <li><code>maxDeflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/J…
42308 <li><code>maxSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
42310 <li><code>maxSessionMemory</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42319 <li><code>maxHeaderListPairs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
42323 <li><code>maxOutstandingPings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
42325 <li><code>maxSendHeaderBlockLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42329 <li><code>paddingStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42337 padding to ensure that the total frame length, including the 9-byte
42345 <li><code>peerMaxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42349 <li><code>maxSessionInvalidFrames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
42352 <li><code>maxSessionRejectedStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Ja…
42374 <li><code>unknownProtocolTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
42381 <li><code>onRequestHandler</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
42387 <a href="https://http2.github.io/faq/#does-http2-require-encryption">unencrypted HTTP/2</a>, the us…
42390 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42392 <span class="hljs-comment">// Create an unencrypted HTTP/2 server.</span>
42393 <span class="hljs-comment">// Since there are no browsers known that support</span>
42394 <span class="hljs-comment">// unencrypted HTTP/2, the use of `http2.createSecureServer()`</span>
42395 <span class="hljs-comment">// is necessary when communicating with browser clients.</span>
42396 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
42398 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42399 stream.<span class="hljs-title function_">respond</span>({
42400 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf
42401 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
42403 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello Wor…
42406 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">80</span>);</code…
42434 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
42436 <li><code>allowHTTP1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
42440 <li><code>maxDeflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/J…
42442 <li><code>maxSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
42444 <li><code>maxSessionMemory</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42453 <li><code>maxHeaderListPairs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
42457 <li><code>maxOutstandingPings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
42459 <li><code>maxSendHeaderBlockLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42463 <li><code>paddingStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42472 9-byte header, is a multiple of 8. For each frame, there is a maximum
42479 <li><code>peerMaxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42483 <li><code>maxSessionInvalidFrames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
42486 <li><code>maxSessionRejectedStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Ja…
42496 <li><code>origins</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
42498 <li><code>unknownProtocolTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
42504 <li><code>onRequestHandler</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
42509 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42510 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
42512 <span class="hljs-keyword">const</span> options = {
42513 …<span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<spa…
42514 …<span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<sp…
42517 <span class="hljs-comment">// Create a secure HTTP/2 server</span>
42518 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSec…
42520 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42521 stream.<span class="hljs-title function_">respond</span>({
42522 …<span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text/html; charset=utf
42523 <span class="hljs-string">':status'</span>: <span class="hljs-number">200</span>
42525 …stream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'&#x3C;h1>Hello Wor…
42528 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">80</span>);</code…
42550 <li><code>authority</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
42552 prefix, host name, and IP port (if a non-default port is used). Userinfo
42555 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
42557 <li><code>maxDeflateDynamicTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/J…
42559 <li><code>maxSettings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
42561 <li><code>maxSessionMemory</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42570 <li><code>maxHeaderListPairs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
42574 <li><code>maxOutstandingPings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
42576 <li><code>maxReservedRemoteStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42580 is 0. The maximum allowed value is 22<sup>32</sup>-1. A negative value sets
42582 <li><code>maxSendHeaderBlockLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42586 <li><code>paddingStrategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42595 9-byte header, is a multiple of 8. For each frame, there is a maximum
42602 <li><code>peerMaxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
42606 <li><code>protocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
42611 <li><code>createConnection</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
42615 <li><code>unknownProtocolTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
42621 …r.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Func…
42626 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42627 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
42629 <span class="hljs-comment">/* Use the client */</span>
42631 client.<span class="hljs-title function_">close</span>();</code></pre>
42740 for use with the <code>HTTP2-Settings</code> header field.</p>
42741 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42743-keyword">const</span> packed = http2.<span class="hljs-title function_">getPackedSettings</span>(…
42745-variable language_">console</span>.<span class="hljs-title function_">log</span>(packed.<span cla…
42746 <span class="hljs-comment">// Prints: AAIAAAAA</span></code></pre>
42752 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
42762 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" c…
42768 <p>Headers are represented as own-properties on JavaScript objects. The property
42769 keys will be serialized to lower-case. Property values should be strings (if
42772 <pre><code class="language-js"><span class="hljs-keyword">const</span> headers = {
42773 <span class="hljs-string">':status'</span>: <span class="hljs-string">'200'</span>,
42774 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text-plain'</span>,
42775-string">'ABC'</span>: [<span class="hljs-string">'has'</span>, <span class="hljs-string">'more'</…
42778 stream.<span class="hljs-title function_">respond</span>(headers);</code></pre>
42787 <code>:protocol</code>, <code>age</code>, <code>authorization</code>, <code>access-control-allow-cr…
42788 <code>access-control-max-age</code>, <code>access-control-request-method</code>, <code>content-enco…
42789 <code>content-language</code>, <code>content-length</code>, <code>content-location</code>, <code>co…
42790 <code>content-range</code>, <code>content-type</code>, <code>date</code>, <code>dnt</code>, <code>e…
42791 <code>if-match</code>, <code>if-modified-since</code>, <code>if-none-match</code>, <code>if-range</…
42792 <code>if-unmodified-since</code>, <code>last-modified</code>, <code>location</code>, <code>max-forw…
42793 <code>proxy-authorization</code>, <code>range</code>, <code>referer</code>,<code>retry-after</code>…
42794 <code>upgrade-insecure-requests</code>, <code>user-agent</code> or <code>x-content-type-options</co…
42796 <li><code>set-cookie</code> is always an array. Duplicates are added to the array.</li>
42800 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42801 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
42802 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42803 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
42804 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
42812 <pre><code class="language-js"><span class="hljs-keyword">const</span> headers = {
42813 <span class="hljs-string">':status'</span>: <span class="hljs-string">'200'</span>,
42814 <span class="hljs-string">'content-type'</span>: <span class="hljs-string">'text-plain'</span>,
42815 <span class="hljs-string">'cookie'</span>: <span class="hljs-string">'some-cookie'</span>,
42816 …<span class="hljs-string">'other-sensitive-header'</span>: <span class="hljs-string">'very secret …
42817 …an class="hljs-property">sensitiveHeaders</span>]: [<span class="hljs-string">'cookie'</span>, <sp…
42820 stream.<span class="hljs-title function_">respond</span>(headers);</code></pre>
42847 <li><code>headerTableSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
42849 is 22<sup>32</sup>-1. <strong>Default:</strong> <code>4096</code>.</li>
42850 <li><code>enablePush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
42852 <li><code>initialWindowSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
42853 bytes for stream-level flow control. The minimum allowed value is 0. The
42854 maximum allowed value is 2<sup>32</sup>-1. <strong>Default:</strong> <code>65535</code>.</li>
42855 <li><code>maxFrameSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
42857 22<sup>24</sup>-1. <strong>Default:</strong> <code>16384</code>.</li>
42858 <li><code>maxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
42860 implies, at least theoretically, 2<sup>32</sup>-1 streams may be open
42862 is 0. The maximum allowed value is 2<sup>32</sup>-1. <strong>Default:</strong>
42864 <li><code>maxHeaderListSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
42866 maximum allowed value is 22<sup>32</sup>-1. <strong>Default:</strong> <code>65535</code>.</li>
42867 <li><code>maxHeaderSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
42868 <li><code>enableConnectProtocol</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
42894 <p>Header field names are <em>case-insensitive</em> and are transmitted over the wire
42895 strictly as lower-case strings. The API provided by Node.js allows header
42896 names to be set as mixed-case strings (e.g. <code>Content-Type</code>) but will convert
42897 those to lower-case (e.g. <code>content-type</code>) upon transmission.</p>
42898 <p>Header field-names <em>must only</em> contain one or more of the following ASCII
42899 characters: <code>a</code>-<code>z</code>, <code>A</code>-<code>Z</code>, <code>0</code>-<code>9</c…
42900 <code>-</code>, <code>.</code>, <code>^</code>, <code>_</code>, <code>`</code> (backtick), <code>|<…
42904 new-line or carriage return characters and <em>should</em> be limited to US-ASCII
42909 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42911 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
42913 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42914 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'push'</span>, <span class…
42915 <span class="hljs-comment">// Process response headers</span>
42917-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(…
42920 …js-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span cl…
42925 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
42927 …s="hljs-keyword">const</span> server = net.<span class="hljs-title function_">createServer</span>(…
42928 <span class="hljs-keyword">let</span> name = <span class="hljs-string">''</span>;
42929 …socket.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</spa…
42930 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
42931-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">()…
42934 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</co…
42936 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42937 <span class="hljs-keyword">const</span> { NGHTTP2_REFUSED_STREAM } = http2.<span class="hljs-proper…
42938 <span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span clas…
42940 <span class="hljs-keyword">const</span> proxy = http2.<span class="hljs-title function_">createServ…
42941 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'stream'</span>, <span clas…
42942 …<span class="hljs-keyword">if</span> (headers[<span class="hljs-string">':method'</span>] !== <spa…
42943 <span class="hljs-comment">// Only accept CONNECT requests</span>
42944 stream.<span class="hljs-title function_">close</span>(NGHTTP2_REFUSED_STREAM);
42945 <span class="hljs-keyword">return</span>;
42947-keyword">const</span> auth = <span class="hljs-keyword">new</span> <span class="hljs-title functi…
42948 <span class="hljs-comment">// It's a very good idea to verify that hostname and port are</span>
42949 <span class="hljs-comment">// things this proxy should be connecting to.</span>
42950-keyword">const</span> socket = net.<span class="hljs-title function_">connect</span>(auth.<span c…
42951 stream.<span class="hljs-title function_">respond</span>();
42952 socket.<span class="hljs-title function_">pipe</span>(stream);
42953 stream.<span class="hljs-title function_">pipe</span>(socket);
42955 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
42956 …stream.<span class="hljs-title function_">close</span>(http2.<span class="hljs-property">constants…
42960 proxy.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8001</span>);</cod…
42962 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42964 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
42966 <span class="hljs-comment">// Must not specify the ':path' and ':scheme' headers</span>
42967 <span class="hljs-comment">// for CONNECT requests or an error will be thrown.</span>
42968 <span class="hljs-keyword">const</span> req = client.<span class="hljs-title function_">request</sp…
42969 <span class="hljs-string">':method'</span>: <span class="hljs-string">'CONNECT'</span>,
42970 …<span class="hljs-string">':authority'</span>: <span class="hljs-string">`localhost:<span class="h…
42973 …span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span cla…
42974-variable language_">console</span>.<span class="hljs-title function_">log</span>(headers[http2.<s…
42976 <span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
42977 req.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
42978 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
42979 req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span clas…
42980 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class=…
42981 client.<span class="hljs-title function_">close</span>();
42983 req.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Jane'</span>);</code><…
42990 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42991 <span class="hljs-keyword">const</span> settings = { <span class="hljs-attr">enableConnectProtocol<…
42992 <span class="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createSer…
42995 <code>':protocol'</code> HTTP/2 pseudo-header:</p>
42996 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
42997 <span class="hljs-keyword">const</span> client = http2.<span class="hljs-title function_">connect</…
42998 …n class="hljs-title function_">on</span>(<span class="hljs-string">'remoteSettings'</span>, <span …
42999 …<span class="hljs-keyword">if</span> (settings.<span class="hljs-property">enableConnectProtocol</…
43000-keyword">const</span> req = client.<span class="hljs-title function_">request</span>({ <span clas…
43001 <span class="hljs-comment">// ...</span>
43013 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
43014 …="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>…
43015 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</…
43016 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, …
43017-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-strin…
43018 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
43022 Upgrading from non-tls HTTP/1 servers is not supported.</p>
43034 …s="language-js"><span class="hljs-keyword">const</span> { createSecureServer } = <span class="hljs
43035 <span class="hljs-keyword">const</span> { readFileSync } = <span class="hljs-built_in">require</spa…
43037 <span class="hljs-keyword">const</span> cert = <span class="hljs-title function_">readFileSync</spa…
43038 <span class="hljs-keyword">const</span> key = <span class="hljs-title function_">readFileSync</span…
43040 <span class="hljs-keyword">const</span> server = <span class="hljs-title function_">createSecureSer…
43041 { cert, key, <span class="hljs-attr">allowHTTP1</span>: <span class="hljs-literal">true</span> },
43043 ).<span class="hljs-title function_">listen</span>(<span class="hljs-number">4443</span>);
43045 <span class="hljs-keyword">function</span> <span class="hljs-title function_">onRequest</span>(<spa…
43046 <span class="hljs-comment">// Detects if it is a HTTPS request or HTTP/2</span>
43047-keyword">const</span> { <span class="hljs-attr">socket</span>: { alpnProtocol } } = req.<span cla…
43048 req.<span class="hljs-property">stream</span>.<span class="hljs-property">session</span> : req;
43049 …"hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-
43050 …res.<span class="hljs-title function_">end</span>(<span class="hljs-variable constant_">JSON</span…
43052 <span class="hljs-attr">httpVersion</span>: req.<span class="hljs-property">httpVersion</span>
43073 abnormally aborted in mid-communication.</p>
43087 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
43096 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43105 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
43113 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
43123 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
43134 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
43137 <p>Key-value pairs of header names and values. Header names are lower-cased.</p>
43138 <pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
43139 <span class="hljs-comment">//</span>
43140 <span class="hljs-comment">// { 'user-agent': 'curl/7.22.0',</span>
43141 <span class="hljs-comment">// host: '127.0.0.1:8000',</span>
43142 <span class="hljs-comment">// accept: '*/*' }</span>
43143 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
43150 <pre><code class="language-js"><span class="hljs-title function_">removeAllHeaders</span>(request.<…
43151 <span class="hljs-title function_">assert</span>(request.<span class="hljs-property">url</span>); …
43157 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43160 client response, the HTTP version of the connected-to server. Returns
43169 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43171 <p>The request method as a string. Read-only. Examples: <code>'GET'</code>, <code>'DELETE'</code>.<…
43177 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43181 list of tuples. So, the even-numbered offsets are key values, and the
43182 odd-numbered offsets are the associated values.</p>
43184 <pre><code class="language-js"><span class="hljs-comment">// Prints something like:</span>
43185 <span class="hljs-comment">//</span>
43186 <span class="hljs-comment">// [ 'user-agent',</span>
43187 <span class="hljs-comment">// 'this is invalid because there can be only one',</span>
43188 <span class="hljs-comment">// 'User-Agent',</span>
43189 <span class="hljs-comment">// 'curl/7.22.0',</span>
43190 <span class="hljs-comment">// 'Host',</span>
43191 <span class="hljs-comment">// '127.0.0.1:8000',</span>
43192 <span class="hljs-comment">// 'ACCEPT',</span>
43193 <span class="hljs-comment">// '*/*' ]</span>
43194 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
43200 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43209 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43212 portion of the target URL.</p>
43218 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
43219 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43262 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
43270 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43274 …ode class="language-http"><span class="hljs-keyword">GET</span> <span class="hljs-string">/status?…
43275 <span class="hljs-attribute">Accept</span><span class="hljs-punctuation">: </span>text/plain</code>…
43277 <!-- eslint-disable semi -->
43278 <pre><code class="language-js"><span class="hljs-string">'/status?name=ryan'</span></code></pre>
43280 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
43281 …an class="hljs-meta">> </span><span class="language-bash">new URL(<span class="hljs-string">'/stat…
43325 <li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
43335 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
43346 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43351 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
43352 …2serverresponse" class="type">&#x3C;http2.Http2ServerResponse></a> The newly-created <code>Http2Se…
43374-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
43375 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
43376 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43377 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
43390 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Use <a h…
43392 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
43401 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
43402 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
43405 The name is case-insensitive.</p>
43406-js"><span class="hljs-keyword">const</span> contentType = response.<span class="hljs-title functi…
43412 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
43416 …lass="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-
43417 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class…
43419 <span class="hljs-keyword">const</span> headerNames = response.<span class="hljs-title function_">g…
43420 <span class="hljs-comment">// headerNames === ['foo', 'set-cookie']</span></code></pre>
43426 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
43430 header-related http module methods. The keys of the returned object are the
43437 …lass="language-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-
43438 …hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-Cookie'</span>, [<span class…
43440 <span class="hljs-keyword">const</span> headers = response.<span class="hljs-title function_">getHe…
43441 <span class="hljs-comment">// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }</spa…
43447 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
43448 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
43451 outgoing headers. The header name matching is case-insensitive.</p>
43452-js"><span class="hljs-keyword">const</span> hasContentType = response.<span class="hljs-title fun…
43458 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
43460 <p>True if headers were sent, false otherwise (read-only).</p>
43466 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
43469 …re><code class="language-js">response.<span class="hljs-title function_">removeHeader</span>(<span…
43475 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
43486 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
43487 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
43489 <p>Sets a single header value for implicit headers. If this header already exists
43490 in the to-be-sent headers, its value will be replaced. Use an array of strings
43492-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Conte…
43494-js">response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Set-C…
43500 <pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</spa…
43501 …="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>…
43502 …ass="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span…
43503 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, …
43504-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-strin…
43505 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
43512 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
43513 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43541 …ode class="language-js"><span class="hljs-keyword">const</span> http2 = <span class="hljs-built_in…
43542 …="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>…
43543 …<span class="hljs-keyword">const</span> ip = req.<span class="hljs-property">socket</span>.<span c…
43544 …<span class="hljs-keyword">const</span> port = req.<span class="hljs-property">socket</span>.<span…
43545-title function_">end</span>(<span class="hljs-string">`Your IP address is <span class="hljs-subst…
43546 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">3000</span>);</code><…
43552 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
43557 <pre><code class="language-js">response.<span class="hljs-property">statusCode</span> = <span class…
43565 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
43582 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
43592-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
43593 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
43594 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43595 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
43608 <p>This is the raw HTTP body and has nothing to do with higher-level multi-part
43638 <li><code>statusCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
43639 <li><code>statusMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
43640 <li><code>headers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
43643 <p>Sends a response header to the request. The status code is a 3-digit HTTP
43646 <p>For compatibility with <a href="http.html">HTTP/1</a>, a human-readable <code>statusMessage</cod…
43650 <pre><code class="language-js"><span class="hljs-keyword">const</span> body = <span class="hljs-str…
43651 response.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, {
43652 …<span class="hljs-string">'Content-Length'</span>: <span class="hljs-title class_">Buffer</span>.<…
43653 …<span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain; charset=ut…
43655 <p><code>Content-Length</code> is given in bytes not characters. The
43657 given encoding. On outbound messages, Node.js does not check if Content-Length
43660 <code>Content-Length</code> does not match the actual payload size.</p>
43668 <pre><code class="language-js"><span class="hljs-comment">// Returns content-type = text/plain</spa…
43669 …="hljs-keyword">const</span> server = http2.<span class="hljs-title function_">createServer</span>…
43670 …ass="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span…
43671 …res.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'X-Foo'</span>, …
43672-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-strin…
43673 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'ok'</span>);
43680-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">PerformanceObserver…
43682-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
43683 …<span class="hljs-keyword">const</span> entry = items.<span class="hljs-title function_">getEntrie…
43684-variable language_">console</span>.<span class="hljs-title function_">log</span>(entry.<span clas…
43685 …<span class="hljs-keyword">if</span> (entry.<span class="hljs-property">name</span> === <span clas…
43686 <span class="hljs-comment">// Entry contains statistics about the Http2Session</span>
43687 … class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (entry.<span class="hljs-pr…
43688 <span class="hljs-comment">// Entry contains statistics about the Http2Stream</span>
43691 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: …
43698 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
43700 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
43702 <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
43703 <li><code>timeToFirstByte</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
43705 <li><code>timeToFirstByteSent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
43707 <li><code>timeToFirstHeader</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
43713 <li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
43714 <li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
43715 <li><code>framesReceived</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
43717 <li><code>framesSent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
43718 <li><code>maxConcurrentStreams</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
43720 <li><code>pingRTT</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
43723 <li><code>streamAverageDuration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
43725 <li><code>streamCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
43727 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
43733 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
43759 <td><p>do not automatically set servername if the target host was specified using an IP address.</p…
43764 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
43768 <p><code>maxCachedSessions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
43772 <p><code>servername</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
43775 <strong>Default:</strong> host name of the target server, unless the target server
43799 <pre><code class="language-js"><span class="hljs-comment">// ...</span>
43800-property">globalAgent</span>.<span class="hljs-title function_">on</span>(<span class="hljs-strin…
43801-title function_">appendFileSync</span>(<span class="hljs-string">'/tmp/ssl-keys.log'</span>, line…
43816 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43825 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
43833 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
43841 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
43849 <li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
43850 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43867 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
43875 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
43883 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
43885 <li><code>requestListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
43888 <pre><code class="language-js"><span class="hljs-comment">// curl -k https://localhost:8000/</span>
43889 <span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span cl…
43890 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
43892 <span class="hljs-keyword">const</span> options = {
43893 …class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span clas…
43894 …class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span cla…
43897 https.<span class="hljs-title function_">createServer</span>(options, <span class="hljs-function">(…
43898 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>);
43899 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>…
43900 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code><…
43902 …ode class="language-js"><span class="hljs-keyword">const</span> https = <span class="hljs-built_in…
43903 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
43905 <span class="hljs-keyword">const</span> options = {
43906 …<span class="hljs-attr">pfx</span>: fs.<span class="hljs-title function_">readFileSync</span>(<spa…
43907 <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'sample'</span>
43910 https.<span class="hljs-title function_">createServer</span>(options, <span class="hljs-function">(…
43911 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>);
43912 …res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>…
43913 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code><…
43930 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
43931 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
43933 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
43939 …ode class="language-js"><span class="hljs-keyword">const</span> https = <span class="hljs-built_in…
43941 …"hljs-title function_">get</span>(<span class="hljs-string">'https://encrypted.google.com/'</span>…
43942-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
43943 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
43945 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
43946 …process.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">write</span>(…
43949 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
43950 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
43979 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
43980 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
43988 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
44003 …ode class="language-js"><span class="hljs-keyword">const</span> https = <span class="hljs-built_in…
44005 <span class="hljs-keyword">const</span> options = {
44006 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'encrypted.google.com'</span>,
44007 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>,
44008 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
44009 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>
44012 …="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options…
44013-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44014 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
44016 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
44017 …process.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">write</span>(…
44021 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
44022 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
44024 req.<span class="hljs-title function_">end</span>();</code></pre>
44026 <pre><code class="language-js"><span class="hljs-keyword">const</span> options = {
44027 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'encrypted.google.com'</span>,
44028 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>,
44029 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
44030 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>,
44031 …class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span clas…
44032 …class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span cla…
44034 options.<span class="hljs-property">agent</span> = <span class="hljs-keyword">new</span> https.<spa…
44036 …="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options…
44037 <span class="hljs-comment">// ...</span>
44040 <pre><code class="language-js"><span class="hljs-keyword">const</span> options = {
44041 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'encrypted.google.com'</span>,
44042 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>,
44043 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
44044 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>,
44045 …class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span clas…
44046 …class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span cla…
44047 <span class="hljs-attr">agent</span>: <span class="hljs-literal">false</span>
44050 …="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options…
44051 <span class="hljs-comment">// ...</span>
44054-js"><span class="hljs-keyword">const</span> options = <span class="hljs-keyword">new</span> <span…
44056 …="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options…
44057 <span class="hljs-comment">// ...</span>
44060 <code>pin-sha256</code>):</p>
44061 …code class="language-js"><span class="hljs-keyword">const</span> tls = <span class="hljs-built_in"…
44062 <span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span cl…
44063 <span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span c…
44065 <span class="hljs-keyword">function</span> <span class="hljs-title function_">sha256</span>(<span c…
44066-keyword">return</span> crypto.<span class="hljs-title function_">createHash</span>(<span class="h…
44068 <span class="hljs-keyword">const</span> options = {
44069 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'github.com'</span>,
44070 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>,
44071 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
44072 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>,
44073 …<span class="hljs-attr">checkServerIdentity</span>: <span class="hljs-keyword">function</span>(<sp…
44074 …<span class="hljs-comment">// Make sure the certificate is issued to the host we are connected to<…
44075 …<span class="hljs-keyword">const</span> err = tls.<span class="hljs-title function_">checkServerId…
44076 <span class="hljs-keyword">if</span> (err) {
44077 <span class="hljs-keyword">return</span> err;
44080 <span class="hljs-comment">// Pin the public key, similar to HPKP pin-sha25 pinning</span>
44081 …<span class="hljs-keyword">const</span> pubkey256 = <span class="hljs-string">'pL1+qb9HTMRZJmuC/bB…
44082 …<span class="hljs-keyword">if</span> (<span class="hljs-title function_">sha256</span>(cert.<span …
44083 …<span class="hljs-keyword">const</span> msg = <span class="hljs-string">'Certificate verification …
44084 …<span class="hljs-string">`The public key of '<span class="hljs-subst">${cert.subject.CN}</span>' …
44085 <span class="hljs-string">'does not match our pinned fingerprint'</span>;
44086 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-t…
44089 <span class="hljs-comment">// Pin the exact certificate, rather than the pub key</span>
44090 …<span class="hljs-keyword">const</span> cert256 = <span class="hljs-string">'25:FE:39:32:D9:63:8C:…
44091 <span class="hljs-string">'D8:3E:4C:1D:98:DB:71:E4:1A:48:03:98:EA:22:6A:BD:8B:93:16'</span>;
44092 …<span class="hljs-keyword">if</span> (cert.<span class="hljs-property">fingerprint256</span> !== c…
44093 …<span class="hljs-keyword">const</span> msg = <span class="hljs-string">'Certificate verification …
44094 …<span class="hljs-string">`The certificate of '<span class="hljs-subst">${cert.subject.CN}</span>'…
44095 <span class="hljs-string">'does not match our pinned fingerprint'</span>;
44096 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-t…
44099 <span class="hljs-comment">// This loop is informational only.</span>
44100 …<span class="hljs-comment">// Print the certificate and public key fingerprints of all certs in th…
44101 …<span class="hljs-comment">// chain. Its common to pin the public key of the issuer on the public<…
44102 …<span class="hljs-comment">// internet, while pinning the public key of the service in sensitive</…
44103 <span class="hljs-comment">// environments.</span>
44104 <span class="hljs-keyword">do</span> {
44105-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44106-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44108 …hash = crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha2…
44109-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44111 lastprint256 = cert.<span class="hljs-property">fingerprint256</span>;
44112 cert = cert.<span class="hljs-property">issuerCertificate</span>;
44113 …} <span class="hljs-keyword">while</span> (cert.<span class="hljs-property">fingerprint256</span> …
44118 options.<span class="hljs-property">agent</span> = <span class="hljs-keyword">new</span> https.<spa…
44119 …="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options…
44120 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
44121-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44122 <span class="hljs-comment">// Print the HPKP values</span>
44123-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44125 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
44128 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
44129 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
44131 req.<span class="hljs-title function_">end</span>();</code></pre>
44133 <pre><code class="language-text">Subject Common Name: github.com
44135 Public key ping-sha256: pL1+qb9HTMRZJmuC/bB/ZI9d302BYrrqiVuRyW+DGrU=
44138 Public key ping-sha256: RRM1dGqnDFsCJXBTHky16vi1obOlCgFFn/yOhI/y+ho=
44141 Public key ping-sha256: WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18=
44144-age=0; pin-sha256="WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18="; pin-sha256="RRM1dGqnDFsCJXBTHky…
44148 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
44152 …e class="language-js"><span class="hljs-keyword">const</span> inspector = <span class="hljs-built_…
44157 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
44159-js"><span class="hljs-built_in">require</span>(<span class="hljs-string">'inspector'</span>).<spa…
44164 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
44166 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
44168 <li><code>wait</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
44171 <p>Activate inspector on host and port. Equivalent to <code>node --inspect=[[host:]port]</code>, bu…
44179 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
44182 …e><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node…
44183 Debugger listening on ws://127.0.0.1:9229/166e272e-7a30-4d09-97ce-f1c012b43c34
44185 ws://127.0.0.1:9229/166e272e-7a30-4d09-97ce-f1c012b43c34
44186 <span class="hljs-meta">
44187 $ </span><span class="language-bash">node --inspect=localhost:3000 -p <span class="hljs-string">'in…
44188 Debugger listening on ws://localhost:3000/51cf8d0e-3c36-4c59-8efd-54519839e56a
44190 ws://localhost:3000/51cf8d0e-3c36-4c59-8efd-54519839e56a
44191 <span class="hljs-meta">
44192 $ </span><span class="language-bash">node -p <span class="hljs-string">'inspector.url()'</span></sp…
44206 back-end and receiving message responses and notifications.</p>
44219 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
44222-js">session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'inspectorNoti…
44223 <span class="hljs-comment">// Debugger.paused</span>
44224 <span class="hljs-comment">// Debugger.resumed</span></code></pre>
44226 <h4>Event: <code>&#x3C;inspector-protocol-method></code>;<span><a class="mark" href="#inspector_eve…
44231 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
44234 to the <code>&#x3C;inspector-protocol-method></code> value.</p>
44235 …ener on the <a href="https://chromedevtools.github.io/devtools-protocol/v8/Debugger#event-paused">…
44238-js">session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'Debugger.paus…
44239 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
44241 <span class="hljs-comment">// [ '/the/file/that/has/the/breakpoint.js:11:0' ]</span></code></pre>
44246 <p>Connects a session to the inspector back-end.</p>
44251 <p>Connects a session to the main thread inspector back-end. An exception will
44266 <li><code>method</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
44267 <li><code>params</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
44268 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
44270 <p>Posts a message to the inspector back-end. <code>callback</code> will be notified when
44272 arguments: error and message-specific result.</p>
44273-js">session.<span class="hljs-title function_">post</span>(<span class="hljs-string">'Runtime.eva…
44274 …hljs-function">(<span class="hljs-params">error, { result }</span>) =></span> <span class="hljs-va…
44275 <span class="hljs-comment">// Output: { type: 'number', value: 4, description: '4' }</span></code><…
44277 <a href="https://chromedevtools.github.io/devtools-protocol/v8/">Chrome DevTools Protocol Viewer</a…
44281 to the run-time events.</p>
44286 <p>Here's an example showing how to use the <a href="https://chromedevtools.github.io/devtools-prot…
44287 …e class="language-js"><span class="hljs-keyword">const</span> inspector = <span class="hljs-built_…
44288 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
44289 <span class="hljs-keyword">const</span> session = <span class="hljs-keyword">new</span> inspector.<…
44290 session.<span class="hljs-title function_">connect</span>();
44292 session.<span class="hljs-title function_">post</span>(<span class="hljs-string">'Profiler.enable'<…
44293 …session.<span class="hljs-title function_">post</span>(<span class="hljs-string">'Profiler.start'<…
44294 <span class="hljs-comment">// Invoke business logic under measurement here...</span>
44296 <span class="hljs-comment">// some time later...</span>
44297 …n class="hljs-title function_">post</span>(<span class="hljs-string">'Profiler.stop'</span>, <span…
44298 <span class="hljs-comment">// Write profile to disk, upload, etc.</span>
44299 <span class="hljs-keyword">if</span> (!err) {
44300-title function_">writeFileSync</span>(<span class="hljs-string">'./profile.cpuprofile'</span>, <s…
44306 <p>Here's an example showing how to use the <a href="https://chromedevtools.github.io/devtools-prot…
44307 …e class="language-js"><span class="hljs-keyword">const</span> inspector = <span class="hljs-built_…
44308 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
44309 <span class="hljs-keyword">const</span> session = <span class="hljs-keyword">new</span> inspector.<…
44311-keyword">const</span> fd = fs.<span class="hljs-title function_">openSync</span>(<span class="hlj…
44313 session.<span class="hljs-title function_">connect</span>();
44315 …hljs-title function_">on</span>(<span class="hljs-string">'HeapProfiler.addHeapSnapshotChunk'</spa…
44316 …fs.<span class="hljs-title function_">writeSync</span>(fd, m.<span class="hljs-property">params</s…
44319-title function_">post</span>(<span class="hljs-string">'HeapProfiler.takeHeapSnapshot'</span>, <s…
44320 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
44321 session.<span class="hljs-title function_">disconnect</span>();
44322 fs.<span class="hljs-title function_">closeSync</span>(fd);
44331 <li>Locale-sensitive or Unicode-aware functions in the <a href="https://tc39.github.io/ecma262/">EC…
44334 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Strin…
44335 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Strin…
44336 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Strin…
44340 Specification</a> (aka ECMA-402):
44342 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl"…
44343 <li>Locale-sensitive methods like <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
44344 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLo…
44351 <li><a href="https://github.com/tc39/proposal-regexp-unicode-property-escapes"><code>RegExp</code> …
44354 <a href="http://site.icu-project.org/">International Components for Unicode (ICU)</a> to implement …
44364 <li><code>--with-intl=none</code>/<code>--without-intl</code></li>
44365 <li><code>--with-intl=system-icu</code></li>
44366 <li><code>--with-intl=small-icu</code></li>
44367 <li><code>--with-intl=full-icu</code> (default)</li>
44467-icu</code></th><th><code>small-icu</code></th><th><code>full-icu</code></th></tr></thead><tbody><…
44468 <p>The "(not locale-aware)" designation denotes that the function carries out its
44469 operation just like the non-<code>Locale</code> version of the function, if one
44475 <h4>Build with a pre-installed ICU (<code>system-icu</code>)<span><a class="mark" href="#intl_build…
44481 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/no…
44482 supported under <code>system-icu</code>. Features that require ICU locale data in
44483 addition, such as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global…
44486 <h4>Embed a limited set of ICU data (<code>small-icu</code>)<span><a class="mark" href="#intl_embed…
44491 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/no…
44492 supported under <code>small-icu</code>. Features that require ICU locale data in addition,
44493 such as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/D…
44494-js"><span class="hljs-keyword">const</span> january = <span class="hljs-keyword">new</span> <span…
44495-keyword">const</span> english = <span class="hljs-keyword">new</span> <span class="hljs-title cla…
44496-keyword">const</span> spanish = <span class="hljs-keyword">new</span> <span class="hljs-title cla…
44498 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
44499 <span class="hljs-comment">// Prints "January"</span>
44500 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
44501 <span class="hljs-comment">// Prints "M01" on small-icu</span>
44502 <span class="hljs-comment">// Should print "enero"</span></code></pre>
44505 <p>If the <code>small-icu</code> option is used, one can still provide additional locale data
44512 <pre><code class="language-bash">env NODE_ICU_DATA=/some/directory node</code></pre>
44515 <p>The <a href="#cli_icu_data_dir_file"><code>--icu-data-dir</code></a> CLI parameter:</p>
44516 <pre><code class="language-bash">node --icu-data-dir=/some/directory</code></pre>
44519 <p>(If both are specified, the <code>--icu-data-dir</code> CLI parameter takes precedence.)</p>
44524 Check <a href="http://userguide.icu-project.org/icudata">"ICU Data"</a> article in the ICU User Gui…
44526 <p>The <a href="https://www.npmjs.com/package/full-icu">full-icu</a> npm module can greatly simplif…
44528 appropriate data file. After installing the module through <code>npm i full-icu</code>,
44529 the data file will be available at <code>./node_modules/full-icu</code>. This path can be
44530 then passed either to <code>NODE_ICU_DATA</code> or <code>--icu-data-dir</code> as shown above to
44532 <h4>Embed the entire ICU (<code>full-icu</code>)<span><a class="mark" href="#intl_embed_the_entire_…
44536 the default behavior if no <code>--with-intl</code> flag is passed. The official binaries
44539 <p>To verify that ICU is enabled at all (<code>system-icu</code>, <code>small-icu</code>, or
44540 <code>full-icu</code>), simply checking the existence of <code>Intl</code> should suffice:</p>
44541-js"><span class="hljs-keyword">const</span> hasICU = <span class="hljs-keyword">typeof</span> <sp…
44544-js"><span class="hljs-keyword">const</span> hasICU = <span class="hljs-keyword">typeof</span> pro…
44545 <p>To check for support for a non-English locale (i.e. <code>full-icu</code> or
44546 <code>system-icu</code>), <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
44547 <pre><code class="language-js"><span class="hljs-keyword">const</span> hasFullICU = (<span class="h…
44548 <span class="hljs-keyword">try</span> {
44549 …lass="hljs-keyword">const</span> january = <span class="hljs-keyword">new</span> <span class="hljs
44550-keyword">const</span> spanish = <span class="hljs-keyword">new</span> <span class="hljs-title cla…
44551 …<span class="hljs-keyword">return</span> spanish.<span class="hljs-title function_">format</span>(…
44552 } <span class="hljs-keyword">catch</span> (err) {
44553 <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
44562 dedicated to ECMA-402.</li>
44567 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
44571 …ode class="language-js"><span class="hljs-keyword">const</span> circle = <span class="hljs-built_i…
44572-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44576 …de class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-variable consta…
44578-built_in">exports</span>.<span class="hljs-property">area</span> = <span class="hljs-function">(<…
44580-built_in">exports</span>.<span class="hljs-property">circumference</span> = <span class="hljs-fun…
44590-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">Square</span> = <span…
44591 …ss="hljs-keyword">const</span> mySquare = <span class="hljs-keyword">new</span> <span class="hljs-
44592 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hl…
44594 <pre><code class="language-js"><span class="hljs-comment">// Assigning to exports will not modify m…
44595 …s="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class…
44596 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">width</span>) {
44597 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">width</span> = width;
44600 <span class="hljs-title function_">area</span>(<span class="hljs-params"></span>) {
44601 …ss="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hlj…
44623 <code>/usr/lib/node/&#x3C;some-package>/&#x3C;some-version></code> hold the contents of a
44667 <p>Putting together all of the above, here is the high-level algorithm
44716 2. let I = count of PARTS - 1
44722 d. let I = I - 1
44730 …["node", "require"]) <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver…
44741 …`package.json` "exports", ["node", "require"]) <a href="esm.md#resolver-algorithm-specification">d…
44751 <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver</a>.
44782 <p>Additionally, on case-insensitive file systems or operating systems, different
44815 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
44816 <span class="hljs-built_in">exports</span>.<span class="hljs-property">done</span> = <span class="h…
44817 <span class="hljs-keyword">const</span> b = <span class="hljs-built_in">require</span>(<span class=…
44818-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44819 <span class="hljs-built_in">exports</span>.<span class="hljs-property">done</span> = <span class="h…
44820 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
44822 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
44823 <span class="hljs-built_in">exports</span>.<span class="hljs-property">done</span> = <span class="h…
44824 <span class="hljs-keyword">const</span> a = <span class="hljs-built_in">require</span>(<span class=…
44825-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44826 <span class="hljs-built_in">exports</span>.<span class="hljs-property">done</span> = <span class="h…
44827 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
44829 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
44830 <span class="hljs-keyword">const</span> a = <span class="hljs-built_in">require</span>(<span class=…
44831 <span class="hljs-keyword">const</span> b = <span class="hljs-built_in">require</span>(<span class=…
44832-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
44840 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
44871 <p>It is convenient to organize programs and libraries into self-contained
44872 directories, and then provide a single entry point to those directories.
44878-json"><span class="hljs-punctuation">{</span> <span class="hljs-attr">"name"</span> <span class="…
44879 …ss="hljs-attr">"main"</span> <span class="hljs-punctuation">:</span> <span class="hljs-string">"./…
44880 <p>If this was in a folder at <code>./some-library</code>, then
44881 <code>require('./some-library')</code> would attempt to load
44882 <code>./some-library/lib/some-library.js</code>.</p>
44888 example, then <code>require('./some-library')</code> would attempt to load:</p>
44890 <li><code>./some-library/index.js</code></li>
44891 <li><code>./some-library/index.node</code></li>
44895 <pre><code class="language-console">Error: Cannot find module 'some-library'</code></pre>
44919 <code>require('example-module/path/to/file')</code> would resolve <code>path/to/file</code>
44920 relative to where <code>example-module</code> is located. The suffixed path follows the
44924 <p>If the <code>NODE_PATH</code> environment variable is set to a colon-delimited list
44951-js">(<span class="hljs-keyword">function</span>(<span class="hljs-params"><span class="hljs-built…
44952 <span class="hljs-comment">// Module code actually lives in here</span>
44956 <li>It keeps top-level variables (defined with <code>var</code>, <code>const</code> or <code>let</c…
44958 <li>It helps to provide some global-looking variables that are actually specific
44975 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
44980 <pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hl…
44981 <span class="hljs-comment">// Prints: /Users/mjr</span>
44982 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
44983 <span class="hljs-comment">// Prints: /Users/mjr</span></code></pre>
44990 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
44999 <pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hl…
45000 <span class="hljs-comment">// Prints: /Users/mjr/example.js</span>
45001 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45002 <span class="hljs-comment">// Prints: /Users/mjr</span></code></pre>
45018 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
45040 <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
45041 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
45050 <pre><code class="language-js"><span class="hljs-comment">// Importing a local module with a path r…
45051 <span class="hljs-comment">// working directory. (On Windows, this would resolve to .\path\myLocalM…
45052 <span class="hljs-keyword">const</span> myLocalModule = <span class="hljs-built_in">require</span>(…
45054 <span class="hljs-comment">// Importing a JSON file:</span>
45055 <span class="hljs-keyword">const</span> jsonData = <span class="hljs-built_in">require</span>(<span…
45057 <span class="hljs-comment">// Importing a module from node_modules or Node.js built-in module:</spa…
45058 <span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span c…
45064 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
45072 only <code>node:</code>-prefixed require calls are going to receive the native module.
45074 <!-- eslint-disable node-core/no-duplicate-requires -->
45075 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
45076 <span class="hljs-keyword">const</span> realFs = <span class="hljs-built_in">require</span>(<span c…
45078 <span class="hljs-keyword">const</span> fakeFs = {};
45079 …n class="hljs-built_in">require</span>.<span class="hljs-property">cache</span>.<span class="hljs-
45081 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-built_in">require</s…
45082 assert.<span class="hljs-title function_">strictEqual</span>(<span class="hljs-built_in">require</s…
45087 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
45089 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
45093-js"><span class="hljs-built_in">require</span>.<span class="hljs-property">extensions</span>[<spa…
45094 <p><strong>Deprecated.</strong> In the past, this list has been used to load non-JavaScript
45095 modules into Node.js by compiling them on-demand. However, in practice, there
45111-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
45112 <pre><code class="language-bash">node entry.js</code></pre>
45113 <!-- eslint-skip -->
45114 <pre><code class="language-js"><span class="hljs-title class_">Module</span> {
45115 <span class="hljs-attr">id</span>: <span class="hljs-string">'.'</span>,
45116 <span class="hljs-attr">path</span>: <span class="hljs-string">'/absolute/path/to'</span>,
45117 <span class="hljs-attr">exports</span>: {},
45118 <span class="hljs-attr">parent</span>: <span class="hljs-literal">null</span>,
45119 …<span class="hljs-attr">filename</span>: <span class="hljs-string">'/absolute/path/to/entry.js'</s…
45120 <span class="hljs-attr">loaded</span>: <span class="hljs-literal">false</span>,
45121 <span class="hljs-attr">children</span>: [],
45122 <span class="hljs-attr">paths</span>:
45123 [ <span class="hljs-string">'/absolute/path/to/node_modules'</span>,
45124 <span class="hljs-string">'/absolute/path/node_modules'</span>,
45125 <span class="hljs-string">'/absolute/node_modules'</span>,
45126 <span class="hljs-string">'/node_modules'</span> ] }</code></pre>
45140 <li><code>request</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
45141 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
45143 <li><code>paths</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
45151 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
45161 <li><code>request</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
45162 …illa.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> |…
45174 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
45178 also accessible via the <code>exports</code> module-global. <code>module</code> is not actually
45193 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
45201-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
45203 …ss="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span clas…
45205 <span class="hljs-comment">// Do some work, and after some time emit</span>
45206 <span class="hljs-comment">// the 'ready' event from the module itself.</span>
45207 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
45208 …"hljs-variable language_">module</span>.<span class="hljs-property">exports</span>.<span class="hl…
45209 }, <span class="hljs-number">1000</span>);</code></pre>
45211 …<code class="language-js"><span class="hljs-keyword">const</span> a = <span class="hljs-built_in">…
45212 a.<span class="hljs-title function_">on</span>(<span class="hljs-string">'ready'</span>, <span clas…
45213 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
45218 <pre><code class="language-js"><span class="hljs-built_in">setTimeout</span>(<span class="hljs-func…
45219 …ass="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = { <span c…
45220 }, <span class="hljs-number">0</span>);</code></pre>
45222 …<code class="language-js"><span class="hljs-keyword">const</span> x = <span class="hljs-built_in">…
45223 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45228 <p>The <code>exports</code> variable is available within a module's file-level scope, and is
45233-js"><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span…
45234 …class="hljs-built_in">exports</span> = { <span class="hljs-attr">hello</span>: <span class="hljs-l…
45237 <!-- eslint-disable func-name-matching -->
45238-js"><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span…
45239 <span class="hljs-comment">// ... etc.</span>
45243 …guage-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">require</s…
45244 …<span class="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = { <s…
45245 …(<span class="hljs-function">(<span class="hljs-params"><span class="hljs-variable language_">modu…
45246 <span class="hljs-comment">// Module code here. In this example, define a function.</span>
45247 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">someFunc</span>(<spa…
45248 <span class="hljs-built_in">exports</span> = someFunc;
45249 …<span class="hljs-comment">// At this point, exports is no longer a shortcut to module.exports, an…
45250 <span class="hljs-comment">// this module will still export an empty default object.</span>
45251 …<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = s…
45252 …<span class="hljs-comment">// At this point, the module will now export someFunc, instead of the</…
45253 <span class="hljs-comment">// default object.</span>
45254 …})(<span class="hljs-variable language_">module</span>, <span class="hljs-variable language_">modu…
45255 …<span class="hljs-keyword">return</span> <span class="hljs-variable language_">module</span>.<span…
45262 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
45270 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
45279 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_…
45287 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
45295 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Please u…
45298 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> | <a…
45308 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
45317 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
45325 <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
45326 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
45337 <!-- Anchors to make sure old links find a target -->
45347 <!-- Anchors to make sure old links find a target -->
45371 <td><p>Unflag Top-Level Await.</p></td></tr>
45375 <td><p>Loading ECMAScript modules no longer requires a command-line flag.</p></td></tr>
45383 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
45386 <p>ECMAScript modules are <a href="https://tc39.github.io/ecma262/#sec-modules">the official standa…
45387 code for reuse. Modules are defined using a variety of <a href="https://developer.mozilla.org/en-US…
45388 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export"><code…
45390 <pre><code class="language-js"><span class="hljs-comment">// addTwo.mjs</span>
45391 <span class="hljs-keyword">function</span> <span class="hljs-title function_">addTwo</span>(<span c…
45392 <span class="hljs-keyword">return</span> num + <span class="hljs-number">2</span>;
45395 <span class="hljs-keyword">export</span> { addTwo };</code></pre>
45397 <pre><code class="language-js"><span class="hljs-comment">// app.mjs</span>
45398 <span class="hljs-keyword">import</span> { addTwo } <span class="hljs-keyword">from</span> <span cl…
45400 <span class="hljs-comment">// Prints: 6</span>
45401 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
45405 <!-- Anchors to make sure old links find a target -->
45414 <code>--input-type</code> flag. See
45417 <!-- Anchors to make sure old links find a target -->
45447 <p><em>Bare specifiers</em> like <code>'some-package'</code> or <code>'some-package/shuffle'</code>…
45482 …e class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-string">'./foo.mj…
45483 <span class="hljs-keyword">import</span> <span class="hljs-string">'./foo.mjs?query=2'</span>; <spa…
45491 <p><a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs"><code>data:…
45500 <a href="https://url.spec.whatwg.org/#special-scheme">special scheme</a>. For example, attempting t…
45504 <pre><code class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-string">'…
45505 <span class="hljs-keyword">import</span> _ <span class="hljs-keyword">from</span> <span class="hljs
45521 …e><code class="language-js"><span class="hljs-keyword">import</span> fs <span class="hljs-keyword"…
45528-js"><span class="hljs-keyword">import</span> <span class="hljs-title class_">EventEmitter</span> …
45529 <span class="hljs-keyword">const</span> e = <span class="hljs-keyword">new</span> <span class="hljs
45530 …de class="language-js"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-key…
45531 …n class="hljs-title function_">readFile</span>(<span class="hljs-string">'./foo.txt'</span>, <span…
45532 <span class="hljs-keyword">if</span> (err) {
45533 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
45534 } <span class="hljs-keyword">else</span> {
45535 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
45538 …lass="language-js"><span class="hljs-keyword">import</span> fs, { readFileSync } <span class="hljs
45539 <span class="hljs-keyword">import</span> { syncBuiltinESMExports } <span class="hljs-keyword">from<…
45541-property">readFileSync</span> = <span class="hljs-function">() =></span> <span class="hljs-title …
45542 <span class="hljs-title function_">syncBuiltinESMExports</span>();
45544 fs.<span class="hljs-property">readFileSync</span> === readFileSync;</code></pre>
45546 <p><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/impor…
45550 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
45556 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
45561 … class="language-js"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-k…
45562-keyword">const</span> buffer = <span class="hljs-title function_">readFileSync</span>(<span class…
45564 <!--
45566 - v13.9.0
45567 - v12.16.2
45569 - version: v14.18.0
45570 pr-url: https://github.com/nodejs/node/pull/38587
45572 -->
45573 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
45574 <p>This feature is only available with the <code>--experimental-import-meta-resolve</code>
45577 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
45578 <li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
45580 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
45582 <p>Provides a module-relative resolution function scoped to each module, returning
45584 <!-- eslint-skip -->
45585-js"><span class="hljs-keyword">const</span> dependencyAsset = <span class="hljs-keyword">await</s…
45588 <!-- eslint-skip -->
45589-js"><span class="hljs-keyword">await</span> <span class="hljs-keyword">import</span>.<span class=…
45610 <!-- eslint-disable no-duplicate-imports -->
45611-js"><span class="hljs-keyword">import</span> { <span class="hljs-keyword">default</span> <span cl…
45613 <span class="hljs-comment">// The following import statement is "syntax sugar" (equivalent but swee…
45614 <span class="hljs-comment">// for `{ default as cjsSugar }` in the above import statement:</span>
45615 <span class="hljs-keyword">import</span> cjsSugar <span class="hljs-keyword">from</span> <span clas…
45617 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45618 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45619 <span class="hljs-comment">// Prints:</span>
45620 <span class="hljs-comment">// &#x3C;module.exports></span>
45621 <span class="hljs-comment">// true</span></code></pre>
45627 <!-- eslint-skip -->
45628 …uage-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> m <span c…
45629 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45630-variable language_">console</span>.<span class="hljs-title function_">log</span>(m === <span clas…
45631 <span class="hljs-comment">// Prints:</span>
45632 <span class="hljs-comment">// [Module] { default: &#x3C;module.exports> }</span>
45633 <span class="hljs-comment">// true</span></code></pre>
45639 <pre><code class="language-js cjs"><span class="hljs-comment">// cjs.cjs</span>
45640 <span class="hljs-built_in">exports</span>.<span class="hljs-property">name</span> = <span class="h…
45642 <!-- eslint-disable no-duplicate-imports -->
45643 …code class="language-js"><span class="hljs-keyword">import</span> { name } <span class="hljs-keywo…
45644 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45645 <span class="hljs-comment">// Prints: 'exported'</span>
45647 <span class="hljs-keyword">import</span> cjs <span class="hljs-keyword">from</span> <span class="hl…
45648 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45649 <span class="hljs-comment">// Prints: { name: 'exported' }</span>
45651 …pan class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> m <span class="hljs-
45652 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45653 <span class="hljs-comment">// Prints: [Module] { default: { name: 'exported' }, name: 'exported' }<…
45663 …er outputs. See <a href="https://github.com/guybedford/cjs-module-lexer/tree/1.2.2">cjs-module-lex…
45676 <code>--experimental-json-modules</code> flag.</p>
45678 <!-- eslint-skip -->
45679 …de class="language-js"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-key…
45680-keyword">const</span> json = <span class="hljs-variable constant_">JSON</span>.<span class="hljs-
45702 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
45704 and are loaded using the CJS loader. <a href="https://html.spec.whatwg.org/#creating-a-json-module-
45706 additional flag <code>--experimental-json-modules</code> when running Node.js.</p>
45707 <p>When the <code>--experimental-json-modules</code> flag is included, both the
45715 <!-- eslint-skip -->
45716 …de class="language-js"><span class="hljs-keyword">import</span> packageConfig <span class="hljs-ke…
45717 <p>The <code>--experimental-json-modules</code> flag is needed for the module
45719 <pre><code class="language-bash">node index.mjs <span class="hljs-comment"># fails</span>
45720 node --experimental-json-modules index.mjs <span class="hljs-comment"># works</span></code></pre>
45723 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
45725 <code>--experimental-wasm-modules</code> flag, allowing any <code>.wasm</code> files to be
45728 <a href="https://github.com/webassembly/esm-integration">ES Module Integration Proposal for Web Ass…
45730 …uage-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> M <span c…
45731 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45733 <pre><code class="language-bash">node --experimental-wasm-modules index.mjs</code></pre>
45736 </section><section><h3>Top-level <code>await</code><span><a class="mark" href="#esm_top_level_await…
45740 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
45743-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> five = <span…
45745 …code class="language-js"><span class="hljs-keyword">import</span> { five } <span class="hljs-keywo…
45747 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
45748 <pre><code class="language-bash">node b.mjs <span class="hljs-comment"># works</span></code></pre>
45751 …code class="language-js"><span class="hljs-keyword">import</span> { spawn } <span class="hljs-keyw…
45752 <span class="hljs-keyword">import</span> { execPath } <span class="hljs-keyword">from</span> <span …
45754 <span class="hljs-title function_">spawn</span>(execPath, [
45755 <span class="hljs-string">'--input-type=module'</span>,
45756 <span class="hljs-string">'--eval'</span>,
45757 <span class="hljs-comment">// Never-resolving Promise:</span>
45758 <span class="hljs-string">'await new Promise(() => {})'</span>,
45759 …<span class="hljs-title function_">once</span>(<span class="hljs-string">'exit'</span>, <span clas…
45760 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
45764 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
45768 provided via a <code>--experimental-loader ./loader-name.mjs</code> argument to Node.js.</p>
45778 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
45779 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
45781 <li><code>conditions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
45782 <li><code>parentURL</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
45785 <li><code>defaultResolve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
45786 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
45788 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
45805 <pre><code class="language-js"><span class="hljs-comment">/**
45806 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">string</span>} <span class="hlj…
45807 * <span class="hljs-doctag">@param</span> {<span class="hljs-type">{
45811 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">Function</span>} <span class="h…
45812 …* <span class="hljs-doctag">@returns</span> {<span class="hljs-type">Promise&#x3C;{ url: string </…
45814-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">functio…
45815 …<span class="hljs-keyword">const</span> { parentURL = <span class="hljs-literal">null</span> } = c…
45816-keyword">if</span> (<span class="hljs-title class_">Math</span>.<span class="hljs-title function_…
45817 …<span class="hljs-comment">// For some or all specifiers, do some custom logic for resolving.</spa…
45818 <span class="hljs-comment">// Always return an object of the form {url: &#x3C;string>}.</span>
45819 <span class="hljs-keyword">return</span> {
45820 <span class="hljs-attr">url</span>: parentURL ?
45821 …<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(specifier, par…
45822 …<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(specifier).<sp…
45825-keyword">if</span> (<span class="hljs-title class_">Math</span>.<span class="hljs-title function_…
45826 …<span class="hljs-comment">// When calling `defaultResolve`, the arguments can be modified. In thi…
45827 …<span class="hljs-comment">// case it's adding another value for matching conditional exports.</sp…
45828 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultResolve</span>(…
45830 …span class="hljs-attr">conditions</span>: [...context.<span class="hljs-property">conditions</span…
45833 <span class="hljs-comment">// Defer to Node.js for all other specifiers.</span>
45834 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultResolve</span>(…
45842 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
45843 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
45844 <li><code>defaultGetFormat</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
45845 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
45847 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
45890-US/docs/Web/JavaScript/Reference/Global_Objects/String"><code>string</code></a>, <a href="https:/…
45893 …g/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> obj…
45894 …rg/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> obje…
45896 <p>Note: If the source value of a text-based format (i.e., <code>'json'</code>, <code>'module'</cod…
45898 <pre><code class="language-js"><span class="hljs-comment">/**
45899 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">string</span>} <span class="hlj…
45900 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">Object</span>} context (current…
45901 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">Function</span>} <span class="h…
45902 …* <span class="hljs-doctag">@returns</span> {<span class="hljs-type">Promise&#x3C;{ format: string…
45904-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">functio…
45905-keyword">if</span> (<span class="hljs-title class_">Math</span>.<span class="hljs-title function_…
45906 …<span class="hljs-comment">// For some or all URLs, do some custom logic for determining format.</…
45907 …<span class="hljs-comment">// Always return an object of the form {format: &#x3C;string>}, where t…
45908 <span class="hljs-comment">// format is one of the strings in the preceding table.</span>
45909 <span class="hljs-keyword">return</span> {
45910 <span class="hljs-attr">format</span>: <span class="hljs-string">'module'</span>,
45913 <span class="hljs-comment">// Defer to Node.js for all other URLs.</span>
45914 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultGetFormat</span…
45922 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
45923 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
45925 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
45928 <li><code>defaultGetSource</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
45929 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
45931-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
45938 <pre><code class="language-js"><span class="hljs-comment">/**
45939 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">string</span>} <span class="hlj…
45940 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">{ format: string </span>}} cont…
45941 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">Function</span>} <span class="h…
45942 …* <span class="hljs-doctag">@returns</span> {<span class="hljs-type">Promise&#x3C;{ source: !(stri…
45944-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">functio…
45945 <span class="hljs-keyword">const</span> { format } = context;
45946-keyword">if</span> (<span class="hljs-title class_">Math</span>.<span class="hljs-title function_…
45947 …<span class="hljs-comment">// For some or all URLs, do some custom logic for retrieving the source…
45948 …<span class="hljs-comment">// Always return an object of the form {source: &#x3C;string|buffer>}.<…
45949 <span class="hljs-keyword">return</span> {
45950 <span class="hljs-attr">source</span>: <span class="hljs-string">'...'</span>,
45953 <span class="hljs-comment">// Defer to Node.js for all other URLs.</span>
45954 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultGetSource</span…
45962-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
45963 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
45965 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
45966 <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
45969 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
45971-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
45978 <p>If this hook is used to convert unknown-to-Node.js file types into executable
45980 unknown-to-Node.js file extensions. See the <a href="#esm_transpiler_loader">transpiler loader exam…
45981 <pre><code class="language-js"><span class="hljs-comment">/**
45982 … <span class="hljs-doctag">@param</span> {<span class="hljs-type">!(string | SharedArrayBuffer | U…
45983 * <span class="hljs-doctag">@param</span> {<span class="hljs-type">{
45987 …* <span class="hljs-doctag">@param</span> {<span class="hljs-type">Function</span>} <span class="h…
45988 …* <span class="hljs-doctag">@returns</span> {<span class="hljs-type">Promise&#x3C;{ source: !(stri…
45990-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">functio…
45991 <span class="hljs-keyword">const</span> { url, format } = context;
45992-keyword">if</span> (<span class="hljs-title class_">Math</span>.<span class="hljs-title function_…
45993 …<span class="hljs-comment">// For some or all URLs, do some custom logic for modifying the source.…
45994 …<span class="hljs-comment">// Always return an object of the form {source: &#x3C;string|buffer>}.<…
45995 <span class="hljs-keyword">return</span> {
45996 <span class="hljs-attr">source</span>: <span class="hljs-string">'...'</span>,
45999 <span class="hljs-comment">// Defer to Node.js for all other sources.</span>
46000 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultTransformSource…
46008 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
46012 run as sloppy-mode script on startup.</p>
46014 scope. The only argument is a <code>require</code>-like function that can be used to load
46018 <pre><code class="language-js"><span class="hljs-comment">/**
46019 …* <span class="hljs-doctag">@returns</span> {<span class="hljs-type">string</span>} Code to run be…
46021 …js-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title funct…
46022 <span class="hljs-keyword">return</span> <span class="hljs-string">`\
46033 <p>The various loader hooks can be used together to accomplish wide-ranging
46042 <pre><code class="language-js"><span class="hljs-comment">// https-loader.mjs</span>
46043 <span class="hljs-keyword">import</span> { get } <span class="hljs-keyword">from</span> <span class…
46045 …ass="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-titl…
46046 …<span class="hljs-keyword">const</span> { parentURL = <span class="hljs-literal">null</span> } = c…
46048 …<span class="hljs-comment">// Normally Node.js would error on specifiers starting with 'https://',…
46049 …<span class="hljs-comment">// this hook intercepts them and converts them into absolute URLs to be…
46050 <span class="hljs-comment">// passed along to the later hooks below.</span>
46051 …<span class="hljs-keyword">if</span> (specifier.<span class="hljs-title function_">startsWith</spa…
46052 <span class="hljs-keyword">return</span> {
46053 <span class="hljs-attr">url</span>: specifier
46055-keyword">else</span> <span class="hljs-keyword">if</span> (parentURL &#x26;&#x26; parentURL.<span…
46056 <span class="hljs-keyword">return</span> {
46057 …"hljs-attr">url</span>: <span class="hljs-keyword">new</span> <span class="hljs-title function_">U…
46061 <span class="hljs-comment">// Let Node.js handle all other specifiers.</span>
46062 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultResolve</span>(…
46065 …ss="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title…
46066 …<span class="hljs-comment">// This loader assumes all network-provided JavaScript is ES module cod…
46067 …<span class="hljs-keyword">if</span> (url.<span class="hljs-title function_">startsWith</span>(<sp…
46068 <span class="hljs-keyword">return</span> {
46069 <span class="hljs-attr">format</span>: <span class="hljs-string">'module'</span>
46073 <span class="hljs-comment">// Let Node.js handle all other URLs.</span>
46074 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultGetFormat</span…
46077 …ss="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title…
46078 …<span class="hljs-comment">// For JavaScript to be loaded over the network, we need to fetch and</…
46079 <span class="hljs-comment">// return it.</span>
46080 …<span class="hljs-keyword">if</span> (url.<span class="hljs-title function_">startsWith</span>(<sp…
46081-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Prom…
46082 …<span class="hljs-title function_">get</span>(url, <span class="hljs-function">(<span class="hljs-
46083 <span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
46084 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
46085-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class="hljs-function">()…
46086-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">…
46090 <span class="hljs-comment">// Let Node.js handle all other URLs.</span>
46091 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultGetSource</span…
46093 <pre><code class="language-js"><span class="hljs-comment">// main.mjs</span>
46094-keyword">import</span> { <span class="hljs-variable constant_">VERSION</span> } <span class="hljs
46096 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
46098 <code>node --experimental-loader ./https-loader.mjs ./main.mjs</code>
46109 <pre><code class="language-js"><span class="hljs-comment">// coffeescript-loader.mjs</span>
46110 …js-keyword">import</span> { <span class="hljs-variable constant_">URL</span>, pathToFileURL } <spa…
46111 …lass="hljs-keyword">import</span> <span class="hljs-title class_">CoffeeScript</span> <span class=…
46113-keyword">const</span> baseURL = <span class="hljs-title function_">pathToFileURL</span>(<span cla…
46115 <span class="hljs-comment">// CoffeeScript files end in .coffee, .litcoffee or .coffee.md.</span>
46116 <span class="hljs-keyword">const</span> extensionsRegex = <span class="hljs-regexp">/\.coffee$|\.li…
46118 …ass="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-titl…
46119 <span class="hljs-keyword">const</span> { parentURL = baseURL } = context;
46121 …<span class="hljs-comment">// Node.js normally errors on unknown file extensions, so return a URL …
46122 <span class="hljs-comment">// specifiers ending in the CoffeeScript file extensions.</span>
46123 …<span class="hljs-keyword">if</span> (extensionsRegex.<span class="hljs-title function_">test</spa…
46124 <span class="hljs-keyword">return</span> {
46125 …"hljs-attr">url</span>: <span class="hljs-keyword">new</span> <span class="hljs-title function_">U…
46129 <span class="hljs-comment">// Let Node.js handle all other specifiers.</span>
46130 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultResolve</span>(…
46133 …ss="hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title…
46134 …<span class="hljs-comment">// Now that we patched resolve to let CoffeeScript URLs through, we nee…
46135 …<span class="hljs-comment">// tell Node.js what format such URLs should be interpreted as. For the…
46136 …<span class="hljs-comment">// purposes of this loader, all CoffeeScript URLs are ES modules.</span>
46137 …<span class="hljs-keyword">if</span> (extensionsRegex.<span class="hljs-title function_">test</spa…
46138 <span class="hljs-keyword">return</span> {
46139 <span class="hljs-attr">format</span>: <span class="hljs-string">'module'</span>
46143 <span class="hljs-comment">// Let Node.js handle all other URLs.</span>
46144 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultGetFormat</span…
46147 …"hljs-keyword">export</span> <span class="hljs-keyword">function</span> <span class="hljs-title fu…
46148 <span class="hljs-keyword">const</span> { url, format } = context;
46150 …<span class="hljs-keyword">if</span> (extensionsRegex.<span class="hljs-title function_">test</spa…
46151 <span class="hljs-keyword">return</span> {
46152-attr">source</span>: <span class="hljs-title class_">CoffeeScript</span>.<span class="hljs-title …
46156 <span class="hljs-comment">// Let Node.js handle all other sources.</span>
46157 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">defaultTransformSource…
46159 <pre><code class="language-coffee"><span class="hljs-comment"># main.coffee</span>
46160 <span class="hljs-keyword">import</span> { scream } <span class="hljs-keyword">from</span> <span cl…
46161 console.log scream <span class="hljs-string">'hello, world'</span>
46163 <span class="hljs-keyword">import</span> { version } <span class="hljs-keyword">from</span> <span c…
46164 console.log <span class="hljs-string">"Brought to you by Node.js version <span class="hljs-subst">#…
46165 <pre><code class="language-coffee"><span class="hljs-comment"># scream.coffee</span>
46166 <span class="hljs-keyword">export</span> scream = <span class="hljs-function"><span class="hljs-par…
46168 <code>node --experimental-loader ./coffeescript-loader.mjs main.coffee</code>
46177 <li>FileURL-based resolution as is used by ES modules</li>
46195 of these top-level routines unless stated otherwise.</p>
46204 <li><em>Invalid Package Target</em>: Package exports or imports define a target module
46205 for the package that is an invalid type or string target.</li>
46206 <li><em>Package Path Not Exported</em>: Package exports do not define or permit a target
46211 which is not a supported target for module imports.</li>
46434 <li>If <em>pjson.imports</em> is a non-null Object, then
46458 <li>Let <em>target</em> be the value of <em>matchObj</em>[<em>matchKey</em>].</li>
46460 <em>packageURL</em>, <em>target</em>, <em>""</em>, <strong>false</strong>, <em>isImports</em>, <em>…
46465 <em>"/"</em> or containing only a single <em>"*"</em>, sorted by the sorting function
46481 <li>Let <em>target</em> be the value of <em>matchObj</em>[<em>expansionKey</em>].</li>
46486 <em>packageURL</em>, <em>target</em>, <em>subpath</em>, <strong>true</strong>, <em>isImports</em>,
46496 <li>Let <em>target</em> be the value of <em>matchObj</em>[<em>expansionKey</em>].</li>
46500 <em>packageURL</em>, <em>target</em>, <em>subpath</em>, <strong>false</strong>, <em>isImports</em>,
46513 <li>Assert: <em>keyA</em> ends with <em>"/"</em> or contains only a single <em>"*"</em>.</li>
46514 <li>Assert: <em>keyB</em> ends with <em>"/"</em> or contains only a single <em>"*"</em>.</li>
46519 <li>If <em>baseLengthA</em> is greater than <em>baseLengthB</em>, return -1.</li>
46522 <li>If <em>keyB</em> does not contain <em>"*"</em>, return -1.</li>
46523 <li>If the length of <em>keyA</em> is greater than the length of <em>keyB</em>, return -1.</li>
46528 <p><strong>PACKAGE_TARGET_RESOLVE</strong>(<em>packageURL</em>, <em>target</em>, <em>subpath</em>, …
46532 <li>If <em>target</em> is a String, then
46534 …em>pattern</em> is <strong>false</strong>, <em>subpath</em> has non-zero length and <em>target</em>
46536 <li>If <em>target</em> does not start with <em>"./"</em>, then
46538 <li>If <em>internal</em> is <strong>true</strong> and <em>target</em> does not start with <em>"../"…
46543 <li>Return <strong>PACKAGE_RESOLVE</strong>(<em>target</em> with every instance of
46547 <li>Return <strong>PACKAGE_RESOLVE</strong>(<em>target</em> + <em>subpath</em>,
46551 <li>Otherwise, throw an <em>Invalid Package Target</em> error.</li>
46554 <li>If <em>target</em> split on <em>"/"</em> or <em>"\"</em> contains any <em>"."</em>, <em>".."</e…
46556 <em>Invalid Package Target</em> error.</li>
46558 <em>packageURL</em> and <em>target</em>.</li>
46576 <li>Otherwise, if <em>target</em> is a non-null Object, then
46578 <li>If <em>exports</em> contains any index property keys, as defined in ECMA-262
46579 <a href="https://tc39.es/ecma262/#integer-index">6.1.7 Array Index</a>, throw an <em>Invalid Packag…
46580 <li>For each property <em>p</em> of <em>target</em>, in object insertion order as,
46585 <li>Let <em>targetValue</em> be the value of the <em>p</em> property in <em>target</em>.</li>
46598 <li>Otherwise, if <em>target</em> is an Array, then
46601 <li>For each item <em>targetValue</em> in <em>target</em>, do
46605 <em>conditions</em>), continuing the loop on any <em>Invalid Package Target</em>
46614 <li>Otherwise, if <em>target</em> is <em>null</em>, return <strong>null</strong>.</li>
46615 <li>Otherwise throw an <em>Invalid Package Target</em> error.</li>
46687 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
46692 <p>The <code>--experimental-specifier-resolution=[mode]</code> flag can be used to customize
46697 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
46699 <span class="hljs-meta">$ </span><span class="language-bash">node index <span class="hljs-comment">…
46701 <span class="hljs-meta">$ </span><span class="language-bash">node --experimental-specifier-resoluti…
46703 <!-- Note: The cjs-module-lexer link should be kept in-sync with the deps version --></section>
46712 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
46722 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
46729 … class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><co…
46730 <span class="hljs-comment">// In an ECMAScript module</span>
46731-keyword">import</span> { builtinModules <span class="hljs-keyword">as</span> builtin } <span clas…
46732 <span class="hljs-comment">// In a CommonJS module</span>
46733 …ss="hljs-keyword">const</span> builtin = <span class="hljs-built_in">require</span>(<span class="h…
46739 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
46744 …lass="language-js mjs"><span class="hljs-keyword">import</span> { createRequire } <span class="hlj…
46745-keyword">const</span> <span class="hljs-built_in">require</span> = <span class="hljs-title functi…
46747 <span class="hljs-comment">// sibling-module.js is a CommonJS module.</span>
46748 <span class="hljs-keyword">const</span> siblingModule = <span class="hljs-built_in">require</span>(…
46753 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Please u…
46755 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
46759 …"language-js"><span class="hljs-keyword">const</span> { createRequireFromPath } = <span class="hlj…
46760 <span class="hljs-keyword">const</span> requireUtil = <span class="hljs-title function_">createRequ…
46762 <span class="hljs-comment">// Require `../src/utils/some-tool`</span>
46763 requireUtil(<span class="hljs-string">'./some-tool'</span>);</code></pre>
46771 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
46772 <span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span c…
46773 <span class="hljs-keyword">const</span> { syncBuiltinESMExports } = <span class="hljs-built_in">req…
46775 fs.<span class="hljs-property">readFile</span> = newAPI;
46777 <span class="hljs-keyword">delete</span> fs.<span class="hljs-property">readFileSync</span>;
46779 <span class="hljs-keyword">function</span> <span class="hljs-title function_">newAPI</span>(<span c…
46780 <span class="hljs-comment">// ...</span>
46783 fs.<span class="hljs-property">newAPI</span> = newAPI;
46785 <span class="hljs-title function_">syncBuiltinESMExports</span>();
46787-title function_">import</span>(<span class="hljs-string">'fs'</span>).<span class="hljs-title fun…
46788 <span class="hljs-comment">// It syncs the existing readFile property with the new value</span>
46789 …assert.<span class="hljs-title function_">strictEqual</span>(esmFS.<span class="hljs-property">rea…
46790 <span class="hljs-comment">// readFileSync has been deleted from the required fs</span>
46791 …hljs-title function_">strictEqual</span>(<span class="hljs-string">'readFileSync'</span> <span cla…
46792 …<span class="hljs-comment">// syncBuiltinESMExports() does not remove readFileSync from esmFS</spa…
46793 …ljs-title function_">strictEqual</span>(<span class="hljs-string">'readFileSync'</span> <span clas…
46794 <span class="hljs-comment">// syncBuiltinESMExports() does not add names</span>
46795 …assert.<span class="hljs-title function_">strictEqual</span>(esmFS.<span class="hljs-property">new…
46801 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
46806 <a href="#cli_enable_source_maps"><code>--enable-source-maps</code></a>, or with code coverage enab…
46809 … class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><co…
46810 <span class="hljs-comment">// In an ECMAScript module</span>
46811-keyword">import</span> { findSourceMap, <span class="hljs-title class_">SourceMap</span> } <span …
46812 <span class="hljs-comment">// In a CommonJS module</span>
46813-keyword">const</span> { findSourceMap, <span class="hljs-title class_">SourceMap</span> } = <span…
46814 <!-- Anchors to make sure old links find a target -->
46821 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
46832 <li><code>payload</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
46837 <li><code>file</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
46838 <li><code>version</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
46839 <li><code>sources</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
46840 <li><code>sourcesContent</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
46841 <li><code>names</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
46842 <li><code>mappings</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
46843 <li><code>sourceRoot</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
46847 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
46852 <li><code>lineNumber</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
46853 <li><code>columnNumber</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
46854 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
46860 <li>generatedLine: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
46861 <li>generatedColumn: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
46862 <li>originalSource: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
46863 <li>originalLine: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#…
46864 <li>originalColumn: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
46865 <li>name: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
46882 <td><p>Unflag self-referencing a package using its name.</p></td></tr>
46906 top-level <a href="#packages_type"><code>"type"</code></a> field with a value of <code>"module"</co…
46909 <p>Strings passed in as an argument to <code>--eval</code>, or piped to <code>node</code> via <code…
46910 with the flag <code>--input-type=module</code>.</p>
46914 where the nearest parent <code>package.json</code> file contains no top-level <code>"type"</code>
46915 field, or string input without the flag <code>--input-type</code>. This behavior is to
46926 top-level field <a href="#packages_type"><code>"type"</code></a> with a value of <code>"commonjs"</…
46929 <p>Strings passed in as an argument to <code>--eval</code> or <code>--print</code>, or piped to <co…
46930 via <code>STDIN</code>, with the flag <code>--input-type=commonjs</code>.</p>
46935 future-proof the package in case the default type of Node.js ever changes, and
46944 <p>The <code>"type"</code> field applies not only to initial entry points (<code>node my-app.js</co…
46946 <pre><code class="language-js"><span class="hljs-comment">// my-app.js, treated as an ES module bec…
46947 <span class="hljs-comment">// file in the same folder with "type": "module".</span>
46949 <span class="hljs-keyword">import</span> <span class="hljs-string">'./startup/init.js'</span>;
46950 <span class="hljs-comment">// Loaded as ES module since ./startup contains no package.json file,</s…
46951 <span class="hljs-comment">// and therefore inherits the "type" value from one level up.</span>
46953 <span class="hljs-keyword">import</span> <span class="hljs-string">'commonjs-package'</span>;
46954 <span class="hljs-comment">// Loaded as CommonJS since ./node_modules/commonjs-package/package.json…
46955 <span class="hljs-comment">// lacks a "type" field or contains "type": "commonjs".</span>
46957 <span class="hljs-keyword">import</span> <span class="hljs-string">'./node_modules/commonjs-package…
46958 <span class="hljs-comment">// Loaded as CommonJS since ./node_modules/commonjs-package/package.json…
46959 <span class="hljs-comment">// lacks a "type" field or contains "type": "commonjs".</span></code></p…
46964 <pre><code class="language-js"><span class="hljs-keyword">import</span> <span class="hljs-string">'…
46965 <span class="hljs-comment">// Loaded as CommonJS since .cjs is always loaded as CommonJS.</span>
46967 <span class="hljs-keyword">import</span> <span class="hljs-string">'commonjs-package/src/index.mjs'…
46968 <span class="hljs-comment">// Loaded as ES module since .mjs is always loaded as ES module.</span><…
46985 <h4><code>--input-type</code> flag<span><a class="mark" href="#packages_input_type_flag" id="packag…
46989 <p>Strings passed in as an argument to <code>--eval</code> (or <code>-e</code>), or piped to <code>…
46990 <code>STDIN</code>, are treated as <a href="esm.html">ES modules</a> when the <code>--input-type=mo…
46992 <pre><code class="language-bash">node --input-type=module --<span class="hljs-built_in">eval</span>…
46994 <span class="hljs-built_in">echo</span> <span class="hljs-string">"import { sep } from 'path'; cons…
46995 <p>For completeness there is also <code>--input-type=commonjs</code>, for explicitly running
46996 string input as CommonJS. This is the default behavior if <code>--input-type</code> is
46999 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
47004 available in your environment - provided you have Node.js installed.</p>
47028 both CommonJS and ES Modules in a single package please consult
47032 …n_field_definitions"><code>package.json</code></a> (e.g. <code>require('your-package/package.json'…
47034 <p>To make the introduction of <a href="#packages_exports"><code>"exports"</code></a> non-breaking,…
47036 entry points so that the package’s public API is well-defined. For example,
47039 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47040 …<span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47041 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47042 …an class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
47043 … class="hljs-attr">"./lib"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47044 …ass="hljs-attr">"./lib/index"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
47045 …ss="hljs-attr">"./lib/index.js"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
47046 …ss="hljs-attr">"./feature"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47047 …hljs-attr">"./feature/index.js"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
47048 …<span class="hljs-attr">"./package.json"</span><span class="hljs-punctuation">:</span> <span class…
47049 <span class="hljs-punctuation">}</span>
47050 <span class="hljs-punctuation">}</span></code></pre>
47052 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47053 …<span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47054 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47055 …an class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
47056 … class="hljs-attr">"./lib"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47057 …n class="hljs-attr">"./lib/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
47058 …ss="hljs-attr">"./feature"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47059 …ass="hljs-attr">"./feature/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
47060 …<span class="hljs-attr">"./package.json"</span><span class="hljs-punctuation">:</span> <span class…
47061 <span class="hljs-punctuation">}</span>
47062 <span class="hljs-punctuation">}</span></code></pre>
47070 <code>import feature from 'my-mod/feature'</code> as they need to provide the full
47071 path <code>import feature from 'my-mod/feature/index.js</code>.</p>
47075 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47076 …pan class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47077 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47078 <span class="hljs-punctuation">}</span></code></pre>
47095 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47096 …pan class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47097 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47098 …<span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47099 …<span class="hljs-attr">"./submodule"</span><span class="hljs-punctuation">:</span> <span class="h…
47100 <span class="hljs-punctuation">}</span>
47101 <span class="hljs-punctuation">}</span></code></pre>
47103 …="language-js"><span class="hljs-keyword">import</span> submodule <span class="hljs-keyword">from<…
47104 <span class="hljs-comment">// Loads ./node_modules/es-module-package/src/submodule.js</span></code>…
47106 …ge-js"><span class="hljs-keyword">import</span> submodule <span class="hljs-keyword">from</span> <…
47107 <span class="hljs-comment">// Throws ERR_PACKAGE_PATH_NOT_EXPORTED</span></code></pre>
47119 <pre><code class="language-json"><span class="hljs-comment">// package.json</span>
47120 <span class="hljs-punctuation">{</span>
47121 …<span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47122 …<span class="hljs-attr">"#dep"</span><span class="hljs-punctuation">:</span> <span class="hljs-pun…
47123 … class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
47124 …<span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47125 <span class="hljs-punctuation">}</span>
47126 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
47127 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
47128 …<span class="hljs-attr">"dep-node-native"</span><span class="hljs-punctuation">:</span> <span clas…
47129 <span class="hljs-punctuation">}</span>
47130 <span class="hljs-punctuation">}</span></code></pre>
47132 <code>dep-node-native</code> (including its exports in turn), and instead gets the local
47133 file <code>./dep-polyfill.js</code> relative to the package in other environments.</p>
47147 <pre><code class="language-json"><span class="hljs-comment">// ./node_modules/es-module-package/pac…
47148 <span class="hljs-punctuation">{</span>
47149 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47150 …<span class="hljs-attr">"./features/*"</span><span class="hljs-punctuation">:</span> <span class="…
47151 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
47152 …<span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47153 …<span class="hljs-attr">"#internal/*"</span><span class="hljs-punctuation">:</span> <span class="h…
47154 <span class="hljs-punctuation">}</span>
47155 <span class="hljs-punctuation">}</span></code></pre>
47160 …s="language-js"><span class="hljs-keyword">import</span> featureX <span class="hljs-keyword">from<…
47161 <span class="hljs-comment">// Loads ./node_modules/es-module-package/src/features/x.js</span>
47163 <span class="hljs-keyword">import</span> featureY <span class="hljs-keyword">from</span> <span clas…
47164 <span class="hljs-comment">// Loads ./node_modules/es-module-package/src/features/y/y.js</span>
47166 <span class="hljs-keyword">import</span> internalZ <span class="hljs-keyword">from</span> <span cla…
47167 <span class="hljs-comment">// Loads ./node_modules/es-module-package/src/internal/z.js</span></code…
47173 treating the right hand side target pattern as a <code>**</code> glob against the list of
47177 <pre><code class="language-json"><span class="hljs-comment">// ./node_modules/es-module-package/pac…
47178 <span class="hljs-punctuation">{</span>
47179 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47180 …="hljs-attr">"./features/*"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47181 …<span class="hljs-attr">"./features/private-internal/*"</span><span class="hljs-punctuation">:</sp…
47182 <span class="hljs-punctuation">}</span>
47183 <span class="hljs-punctuation">}</span></code></pre>
47184-js"><span class="hljs-keyword">import</span> featureInternal <span class="hljs-keyword">from</spa…
47185 <span class="hljs-comment">// Throws: ERR_PACKAGE_PATH_NOT_EXPORTED</span>
47187 <span class="hljs-keyword">import</span> featureX <span class="hljs-keyword">from</span> <span clas…
47188 <span class="hljs-comment">// Loads ./node_modules/es-module-package/src/features/x.js</span></code…
47197 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47198 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47199 …<span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47200 <span class="hljs-punctuation">}</span>
47201 <span class="hljs-punctuation">}</span></code></pre>
47203 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47204 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47205 <span class="hljs-punctuation">}</span></code></pre>
47222 <pre><code class="language-json"><span class="hljs-comment">// package.json</span>
47223 <span class="hljs-punctuation">{</span>
47224 …class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
47225 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47226 …class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47227 …<span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47228 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
47229 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47230 <span class="hljs-punctuation">}</span></code></pre>
47233 <li><code>"import"</code> - matches when the package is loaded via <code>import</code> or
47234 <code>import()</code>, or via any top-level import or resolve operation by the
47236 target file. <em>Always mutually exclusive with <code>"require"</code>.</em></li>
47237 <li><code>"require"</code> - matches when the package is loaded via <code>require()</code>. The
47239 matches regardless of the module format of the target file. Expected
47243 <li><code>"node"</code> - matches for any Node.js environment. Can be a CommonJS or ES
47246 <li><code>"node-addons"</code> - similar to <code>"node"</code> and matches for any Node.js environ…
47250 <a href="#cli_no_addons"><code>--no-addons</code> flag</a>.</li>
47251 <li><code>"default"</code> - the generic fallback that always matches. Can be a CommonJS
47261 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47262 …pan class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47263 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47264 …<span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47265 …<span class="hljs-attr">"./feature"</span><span class="hljs-punctuation">:</span> <span class="hlj…
47266 …class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
47267 …<span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47268 <span class="hljs-punctuation">}</span>
47269 <span class="hljs-punctuation">}</span>
47270 <span class="hljs-punctuation">}</span></code></pre>
47285 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47286 …pan class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47287 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47288 …<span class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-pun…
47289 …lass="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
47290 …<span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47291 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
47292 … class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47293 <span class="hljs-punctuation">}</span>
47294 <span class="hljs-punctuation">}</span></code></pre>
47304 <code>--conditions</code> flag:</p>
47305 <pre><code class="language-bash">node --conditions=development main.js</code></pre>
47307 exports, while resolving the existing <code>"node"</code>, <code>"node-addons"</code>, <code>"defau…
47311 <p>The <code>"import"</code>, <code>"require"</code>, <code>"node"</code>, <code>"node-addons"</cod…
47314 <p>The <code>"node-addons"</code> condition can be used to provide an entry point which
47316 <a href="#cli_no_addons"><code>--no-addons</code> flag</a>. When using <code>"node-addons"</code>, …
47321 … enabled in Node.js via the <a href="#packages_resolving_user_conditions"><code>--conditions</code>
47325 <li><code>"browser"</code> - any environment which implements a standard subset of global
47328 <li><code>"development"</code> - can be used to define a development-only environment
47330 <li><code>"production"</code> - can be used to define a production environment entry
47333 … enabled in Node.js via the <a href="#packages_resolving_user_conditions"><code>--conditions</code>
47335 …fic conditions such as <code>"deno"</code>, <code>"electron"</code>, or <code>"react-native"</code>
47339 to the <a href="https://github.com/nodejs/node/blob/HEAD/doc/api/packages.md#conditions-definitions…
47349 this would not necessarily be the case for company-specific or
47350 application-specific conditions.</li>
47354 <h4>Self-referencing a package using its name<span><a class="mark" href="#packages_self_referencing…
47362 <td><p>Unflag self-referencing a package using its name.</p></td></tr>
47369 <pre><code class="language-json"><span class="hljs-comment">// package.json</span>
47370 <span class="hljs-punctuation">{</span>
47371 …pan class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47372 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47373 …span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47374 …<span class="hljs-attr">"./foo"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
47375 <span class="hljs-punctuation">}</span>
47376 <span class="hljs-punctuation">}</span></code></pre>
47378 <pre><code class="language-js"><span class="hljs-comment">// ./a-module.mjs</span>
47379 …="hljs-keyword">import</span> { something } <span class="hljs-keyword">from</span> <span class="hl…
47380 <p>Self-referencing is available only if <code>package.json</code> has <a href="#packages_exports">…
47384 <pre><code class="language-js"><span class="hljs-comment">// ./another-module.mjs</span>
47386 <span class="hljs-comment">// Imports "another" from ./m.mjs. Fails because</span>
47387 <span class="hljs-comment">// the "package.json" "exports" field</span>
47388 <span class="hljs-comment">// does not provide an export named "./m.mjs".</span>
47389 <span class="hljs-keyword">import</span> { another } <span class="hljs-keyword">from</span> <span c…
47390 <p>Self-referencing is also available when using <code>require</code>, both in an ES module,
47392 <pre><code class="language-js cjs"><span class="hljs-comment">// ./a-module.js</span>
47393 …s-keyword">const</span> { something } = <span class="hljs-built_in">require</span>(<span class="hl…
47394 <p>Finally, self-referencing also works with scoped packages. For example, this
47396 <pre><code class="language-json"><span class="hljs-comment">// package.json</span>
47397 <span class="hljs-punctuation">{</span>
47398 …an class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47399 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47400 <span class="hljs-punctuation">}</span></code></pre>
47402 …ass="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><pre><…
47403-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-
47404 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
47405 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
47415 ignores) the top-level <code>"module"</code> field.</p>
47418 <code>'pkg'</code> and <code>'pkg/es-module'</code>, or both at the same specifier via <a href="#pa…
47440 all-CommonJS or all-ES module environments, respectively, and therefore is
47442 …href="https://babeljs.io/">Babel</a> or <a href="https://github.com/standard-things/esm#readme"><c…
47454 newest version of the package would only be usable in ES module-supporting
47476 <pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</spa…
47477 <span class="hljs-punctuation">{</span>
47478 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47479 …an class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47480 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47481 … class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47482 …<span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47483 <span class="hljs-punctuation">}</span>
47484 <span class="hljs-punctuation">}</span></code></pre>
47490 <pre><code class="language-js cjs"><span class="hljs-comment">// ./node_modules/pkg/index.cjs</span>
47491 <span class="hljs-built_in">exports</span>.<span class="hljs-property">name</span> = <span class="h…
47492 <pre><code class="language-js"><span class="hljs-comment">// ./node_modules/pkg/wrapper.mjs</span>
47493 <span class="hljs-keyword">import</span> cjsModule <span class="hljs-keyword">from</span> <span cla…
47494 <span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> name = cjsModule.<…
47504 …code class="language-js"><span class="hljs-keyword">import</span> cjsModule <span class="hljs-keyw…
47505 <span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> name = cjsModule.<…
47506 <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> cjsModule;</code…
47514 package is used directly in an application, and a <code>utilities-plus</code> package
47516 underlying CommonJS files, it doesn’t matter if <code>utilities-plus</code> is written in
47522 be to add an export, e.g. <code>"./module"</code>, to point to an all-ES module-syntax
47528 <pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</spa…
47529 <span class="hljs-punctuation">{</span>
47530 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47531 …an class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47532 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47533 …span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47534 …<span class="hljs-attr">"./module"</span><span class="hljs-punctuation">:</span> <span class="hljs
47535 <span class="hljs-punctuation">}</span>
47536 <span class="hljs-punctuation">}</span></code></pre>
47540 <pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</spa…
47541 <span class="hljs-punctuation">{</span>
47542 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47543 …an class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47544 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47545 …n class="hljs-attr">"import"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
47546 …<span class="hljs-attr">"require"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47547 <span class="hljs-punctuation">}</span>
47548 <span class="hljs-punctuation">}</span></code></pre>
47558 present. This would likely cause hard-to-troubleshoot bugs.</p>
47568 …age-js"><span class="hljs-keyword">import</span> <span class="hljs-title class_">Date</span> <span…
47569 <span class="hljs-keyword">const</span> someDate = <span class="hljs-keyword">new</span> <span clas…
47570 <span class="hljs-comment">// someDate contains state; Date does not</span></code></pre>
47572 object, or modify a passed-in object, to keep the state external to the
47579 <pre><code class="language-js cjs"><span class="hljs-comment">// ./node_modules/pkg/index.cjs</span>
47580 <span class="hljs-keyword">const</span> state = <span class="hljs-built_in">require</span>(<span cl…
47581 <span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span>.<spa…
47582 <pre><code class="language-js"><span class="hljs-comment">// ./node_modules/pkg/index.mjs</span>
47583 <span class="hljs-keyword">import</span> state <span class="hljs-keyword">from</span> <span class="…
47584 <span class="hljs-keyword">export</span> {
47609 <code>"./module"</code>, to point to an all-ES module-syntax version of the package:</p>
47610 <pre><code class="language-json"><span class="hljs-comment">// ./node_modules/pkg/package.json</spa…
47611 <span class="hljs-punctuation">{</span>
47612 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47613 …an class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
47614 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47615 …span class="hljs-attr">"."</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
47616 …<span class="hljs-attr">"./module"</span><span class="hljs-punctuation">:</span> <span class="hljs
47617 <span class="hljs-punctuation">}</span>
47618 <span class="hljs-punctuation">}</span></code></pre>
47621 as <a href="https://docs.npmjs.com/creating-a-package-json-file">npm</a>) use
47625 <li><a href="#packages_name"><code>"name"</code></a> - Relevant when using named imports within a p…
47627 <li><a href="#packages_main"><code>"main"</code></a> - The default module when loading the package,…
47629 <li><a href="#packages_packagemanager"><code>"packageManager"</code></a> - The package manager reco…
47631 <li><a href="#packages_type"><code>"type"</code></a> - The package type determining whether to load…
47633 <li><a href="#packages_exports"><code>"exports"</code></a> - Package exports and conditional export…
47635 <li><a href="#packages_imports"><code>"imports"</code></a> - Package imports, for use by modules wi…
47646 <td><p>Remove the <code>--experimental-resolve-self</code> option.</p></td></tr>
47651 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
47653 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47654 …<span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47655 <span class="hljs-punctuation">}</span></code></pre>
47660 <a href="#packages_self_referencing_a_package_using_its_name">self-reference</a> a package using it…
47666 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
47668 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47669 …<span class="hljs-attr">"main"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47670 <span class="hljs-punctuation">}</span></code></pre>
47674 …ass="language-js cjs"><span class="hljs-built_in">require</span>(<span class="hljs-string">'./path…
47681 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
47683 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
47685 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47686 …<span class="hljs-attr">"packageManager"</span><span class="hljs-punctuation">:</span> <span class…
47687 <span class="hljs-punctuation">}</span></code></pre>
47693 <p>This field is currently experimental and needs to be opted-in; check the
47701 <td><p>Unflag <code>--experimental-modules</code>.</p></td></tr>
47708 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
47713 <code>package.json</code> file contains a top-level field <code>"type"</code> with a value of
47718 <pre><code class="language-json"><span class="hljs-comment">// package.json</span>
47719 <span class="hljs-punctuation">{</span>
47720 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
47721 <span class="hljs-punctuation">}</span></code></pre>
47722 <pre><code class="language-bash"><span class="hljs-comment"># In same folder as preceding package.j…
47723 node my-app.js <span class="hljs-comment"># Runs as ES module</span></code></pre>
47730 <pre><code class="language-js"><span class="hljs-comment">// my-app.js, part of the same example as…
47731 <span class="hljs-keyword">import</span> <span class="hljs-string">'./startup.js'</span>; <span cla…
47744 <td><p>Remove the <code>--experimental-conditional-exports</code> option.</p></td></tr>
47753-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href=…
47755 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
47756 …<span class="hljs-attr">"exports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47757 <span class="hljs-punctuation">}</span></code></pre>
47760 <a href="#packages_self_referencing_a_package_using_its_name">self-reference</a> to its own name. I…
47773 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects…
47775 <pre><code class="language-json"><span class="hljs-comment">// package.json</span>
47776 <span class="hljs-punctuation">{</span>
47777 …<span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47778 …<span class="hljs-attr">"#dep"</span><span class="hljs-punctuation">:</span> <span class="hljs-pun…
47779 … class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
47780 …<span class="hljs-attr">"default"</span><span class="hljs-punctuation">:</span> <span class="hljs-
47781 <span class="hljs-punctuation">}</span>
47782 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
47783 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
47784 …<span class="hljs-attr">"dep-node-native"</span><span class="hljs-punctuation">:</span> <span clas…
47785 <span class="hljs-punctuation">}</span>
47786 <span class="hljs-punctuation">}</span></code></pre>
47793 <!--lint disable maximum-line-length-->
47794 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
47796 <p>The <code>net</code> module provides an asynchronous network API for creating stream-based
47800 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
47808 filesystem pathname. It gets truncated to an OS-dependent length of
47809 <code>sizeof(sockaddr_un.sun_path) - 1</code>. Typical values are 107 bytes on Linux and
47827 <pre><code class="language-js">net.<span class="hljs-title function_">createServer</span>().<span c…
47828-title function_">join</span>(<span class="hljs-string">'\\\\?\\pipe'</span>, process.<span class=…
47841-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#ne…
47842 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> …
47850-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#ne…
47852-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#ne…
47853 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> …
47862-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#ne…
47863 <li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
47864 must be a value between <code>0</code> and <code>32</code>. For IPv6, this must be between
47866 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> …
47874 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
47875 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> …
47876 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
47880 …de class="language-js"><span class="hljs-keyword">const</span> blockList = <span class="hljs-keywo…
47881 blockList.<span class="hljs-title function_">addAddress</span>(<span class="hljs-string">'123.123.1…
47882 blockList.<span class="hljs-title function_">addRange</span>(<span class="hljs-string">'10.0.0.1'</…
47883-title function_">addSubnet</span>(<span class="hljs-string">'8592:757c:efae:4e45::'</span>, <span…
47885-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span …
47886-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span …
47887-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span …
47889 <span class="hljs-comment">// IPv6 notation for IPv4 addresses works:</span>
47890-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span …
47891-variable language_">console</span>.<span class="hljs-title function_">log</span>(blockList.<span …
47897 <li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
47909 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
47911 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Th…
47913 <code>'ipv6'</code>.</li>
47914 …ozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> O…
47915 …n-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> An IPv6 flow-
47916 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
47925 <li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_ty…
47932 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> …
47939 <li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_ty…
47946 <li>Type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_ty…
47958 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
47960 <li><code>connectionListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
47985 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error…
48001-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href=…
48005 (useful to find which port was assigned when getting an OS-assigned address):
48009-js"><span class="hljs-keyword">const</span> server = net.<span class="hljs-title function_">creat…
48010 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'goodbye\n'</span>);
48011 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
48012 <span class="hljs-comment">// Handle errors here.</span>
48013 <span class="hljs-keyword">throw</span> err;
48016 <span class="hljs-comment">// Grab an arbitrary unused port.</span>
48017 server.<span class="hljs-title function_">listen</span>(<span class="hljs-function">() =></span> {
48018-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
48027 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48040 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
48042 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
48053 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48063 <!--lint disable no-undefined-references-->
48075 <!--lint enable no-undefined-references-->
48083 …re set to <code>SO_REUSEADDR</code> (see <a href="https://man7.org/linux/man-pages/man7/socket.7.h…
48092 …age-js">server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</spa…
48093 …<span class="hljs-keyword">if</span> (e.<span class="hljs-property">code</span> === <span class="h…
48094 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48095 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
48096 server.<span class="hljs-title function_">close</span>();
48097 …server.<span class="hljs-title function_">listen</span>(<span class="hljs-variable constant_">PORT…
48098 }, <span class="hljs-number">1000</span>);
48106 <li><code>handle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
48107 <li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48108 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48130 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
48132 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48133 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48134 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48136 <li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48138 <li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
48139 <li><code>readableAll</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
48141 <li><code>writableAll</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
48143 <li><code>ipv6Only</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48144 disable dual-stack support, i.e., binding to host <code>::</code> won't make
48148 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48152 <!--lint disable no-undefined-references-->
48159 <!--lint enable no-undefined-references-->
48165 <pre><code class="language-js">server.<span class="hljs-title function_">listen</span>({
48166 <span class="hljs-attr">host</span>: <span class="hljs-string">'localhost'</span>,
48167 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
48168 <span class="hljs-attr">exclusive</span>: <span class="hljs-literal">true</span>
48178 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48180 <li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48181 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48190 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48191 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48192 <li><code>backlog</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48193 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48201 …rg/wiki/IPv6_address#Unspecified_address">unspecified IPv6 address</a> (<code>::</code>) when IPv6
48203 …tps://en.wikipedia.org/wiki/IPv6_address#Unspecified_address">unspecified IPv6 address</a> (<code>…
48211 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
48218 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
48266 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
48268 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
48270 <li><code>allowHalfOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
48274 <li><code>readable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48276 <li><code>writable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48290 <li><code>hadError</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48305 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
48335 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error…
48354 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> |…
48355 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48356 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
48357 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48377 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
48386 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
48388 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
48407 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
48415 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
48455 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
48456 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48465 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48466 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48467 <li><code>localAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
48468 <li><code>localPort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
48469 <li><code>family</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
48470 <code>0</code> indicates that both IPv4 and IPv6 addresses are allowed. <strong>Default:</strong> <…
48471 <li><code>hints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
48472 <li><code>lookup</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
48476 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48477 …paths_for_ipc_connections">Identifying paths for IPC connections</a>. If provided, the TCP-specific
48482 …a.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> If…
48489 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array><…
48491 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48500 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
48501 net.<span class="hljs-title function_">connect</span>({
48502 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
48503 <span class="hljs-attr">onread</span>: {
48504 <span class="hljs-comment">// Reuses a 4KiB Buffer for every read from the socket.</span>
48505-attr">buffer</span>: <span class="hljs-title class_">Buffer</span>.<span class="hljs-title functi…
48506 …<span class="hljs-attr">callback</span>: <span class="hljs-keyword">function</span>(<span class="h…
48507 <span class="hljs-comment">// Received data is available in `buf` from 0 to `nread`.</span>
48508-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class=…
48514 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48516 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48529 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48530 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48531 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48544 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
48558 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
48566 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
48575-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
48576 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48577 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48580 <p>Half-closes the socket. i.e., it sends a FIN packet. It is possible the
48588 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
48599 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
48613 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
48633 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
48643 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
48645 <p>The string representation of the remote IP family. <code>'IPv4'</code> or <code>'IPv6'</code>.</…
48651 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
48664 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48682 <li><code>enable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
48683 <li><code>initialDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
48686 <p>Enable/disable keep-alive functionality, and optionally set the initial
48692 <p>Enabling the keep-alive functionality will set the following socket options:</p>
48704 <li><code>noDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48719 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48720 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48728 <pre><code class="language-js">socket.<span class="hljs-built_in">setTimeout</span>(<span class="hl…
48729 socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'timeout'</span>, <sp…
48730 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48731 socket.<span class="hljs-title function_">end</span>();
48734 <p>The optional <code>callback</code> parameter will be added as a one-time listener for the
48741 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
48760-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
48761 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
48762 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
48763 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
48779 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
48804 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
48805 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48815 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48816 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48826 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48827 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48828 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48854 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
48858 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48868 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
48874 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
48875-keyword">const</span> client = net.<span class="hljs-title function_">createConnection</span>({ <…
48876 <span class="hljs-comment">// 'connect' listener.</span>
48877 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48878 …client.<span class="hljs-title function_">write</span>(<span class="hljs-string">'world!\r\n'</spa…
48880 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
48881 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48882 client.<span class="hljs-title function_">end</span>();
48884 client.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span c…
48885 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48888-js"><span class="hljs-keyword">const</span> client = net.<span class="hljs-title function_">creat…
48894 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48897 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48913 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48915 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
48918 <li><code>connectListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/R…
48934 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
48936 <li><code>allowHalfOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
48939 <li><code>pauseOnConnect</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
48943 <li><code>connectionListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
48953 connection is half-closed (non-readable but still writable). See <a href="#net_event_end"><code>'en…
48964 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
48965 …s="hljs-keyword">const</span> server = net.<span class="hljs-title function_">createServer</span>(…
48966 <span class="hljs-comment">// 'connection' listener.</span>
48967 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48968 …c.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class…
48969 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48971 c.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello\r\n'</span>);
48972 c.<span class="hljs-title function_">pipe</span>(c);
48974 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
48975 <span class="hljs-keyword">throw</span> err;
48977 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8124</span>, <spa…
48978 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48981 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">te…
48983 …s="language-js">server.<span class="hljs-title function_">listen</span>(<span class="hljs-string">…
48984 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
48987 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">nc…
48993 <li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
48994 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
49004 <li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
49005 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
49013 <li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
49014 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
49020 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
49022 <p>The <code>os</code> module provides operating system-related utility methods and
49024 …<code class="language-js"><span class="hljs-keyword">const</span> os = <span class="hljs-built_in"…
49030 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
49032 <p>The operating system-specific end-of-line marker.</p>
49042 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49053 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
49055 <p>Contains commonly used operating system-specific constants for error codes,
49063 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
49068 <li><code>model</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
49069 <li><code>speed</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
49070 <li><code>times</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
49072 <li><code>user</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
49073 <li><code>nice</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
49074 <li><code>sys</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
49075 <li><code>idle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
49076 <li><code>irq</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
49080 <!-- eslint-disable semi -->
49081 <pre><code class="language-js">[
49083 …<span class="hljs-attr">model</span>: <span class="hljs-string">'Intel(R) Core(TM) i7 CPU …
49084 <span class="hljs-attr">speed</span>: <span class="hljs-number">2926</span>,
49085 <span class="hljs-attr">times</span>: {
49086 <span class="hljs-attr">user</span>: <span class="hljs-number">252020</span>,
49087 <span class="hljs-attr">nice</span>: <span class="hljs-number">0</span>,
49088 <span class="hljs-attr">sys</span>: <span class="hljs-number">30340</span>,
49089 <span class="hljs-attr">idle</span>: <span class="hljs-number">1070356870</span>,
49090 <span class="hljs-attr">irq</span>: <span class="hljs-number">0</span>
49094 …<span class="hljs-attr">model</span>: <span class="hljs-string">'Intel(R) Core(TM) i7 CPU …
49095 <span class="hljs-attr">speed</span>: <span class="hljs-number">2926</span>,
49096 <span class="hljs-attr">times</span>: {
49097 <span class="hljs-attr">user</span>: <span class="hljs-number">306960</span>,
49098 <span class="hljs-attr">nice</span>: <span class="hljs-number">0</span>,
49099 <span class="hljs-attr">sys</span>: <span class="hljs-number">26980</span>,
49100 <span class="hljs-attr">idle</span>: <span class="hljs-number">1071569080</span>,
49101 <span class="hljs-attr">irq</span>: <span class="hljs-number">0</span>
49105 …<span class="hljs-attr">model</span>: <span class="hljs-string">'Intel(R) Core(TM) i7 CPU …
49106 <span class="hljs-attr">speed</span>: <span class="hljs-number">2926</span>,
49107 <span class="hljs-attr">times</span>: {
49108 <span class="hljs-attr">user</span>: <span class="hljs-number">248450</span>,
49109 <span class="hljs-attr">nice</span>: <span class="hljs-number">0</span>,
49110 <span class="hljs-attr">sys</span>: <span class="hljs-number">21750</span>,
49111 <span class="hljs-attr">idle</span>: <span class="hljs-number">1070919370</span>,
49112 <span class="hljs-attr">irq</span>: <span class="hljs-number">0</span>
49116 …<span class="hljs-attr">model</span>: <span class="hljs-string">'Intel(R) Core(TM) i7 CPU …
49117 <span class="hljs-attr">speed</span>: <span class="hljs-number">2926</span>,
49118 <span class="hljs-attr">times</span>: {
49119 <span class="hljs-attr">user</span>: <span class="hljs-number">256880</span>,
49120 <span class="hljs-attr">nice</span>: <span class="hljs-number">0</span>,
49121 <span class="hljs-attr">sys</span>: <span class="hljs-number">19430</span>,
49122 <span class="hljs-attr">idle</span>: <span class="hljs-number">1070905480</span>,
49123 <span class="hljs-attr">irq</span>: <span class="hljs-number">20</span>
49127 <p><code>nice</code> values are POSIX-only. On Windows, the <code>nice</code> values of all process…
49134 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
49136 <p>The platform-specific file path of the null device.</p>
49146 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49156 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
49164 <li><code>pid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
49166 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
49175 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49187 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49195 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
49200 <p>The load average is a Unix-specific concept. On Windows, the return value is
49207 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
49215 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;stri…
49216 …//developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;…
49217 ….mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>…
49218 <li><code>mac</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
49219 <li><code>internal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
49221 …//developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;…
49222 is <code>IPv6</code>)</li>
49223 …veloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;stri…
49227 <!-- eslint-skip -->
49228 <pre><code class="language-js">{
49229 <span class="hljs-attr">lo</span>: [
49231 <span class="hljs-attr">address</span>: <span class="hljs-string">'127.0.0.1'</span>,
49232 <span class="hljs-attr">netmask</span>: <span class="hljs-string">'255.0.0.0'</span>,
49233 <span class="hljs-attr">family</span>: <span class="hljs-string">'IPv4'</span>,
49234 <span class="hljs-attr">mac</span>: <span class="hljs-string">'00:00:00:00:00:00'</span>,
49235 <span class="hljs-attr">internal</span>: <span class="hljs-literal">true</span>,
49236 <span class="hljs-attr">cidr</span>: <span class="hljs-string">'127.0.0.1/8'</span>
49239 <span class="hljs-attr">address</span>: <span class="hljs-string">'::1'</span>,
49240 …<span class="hljs-attr">netmask</span>: <span class="hljs-string">'ffff:ffff:ffff:ffff:ffff:ffff:f…
49241 <span class="hljs-attr">family</span>: <span class="hljs-string">'IPv6'</span>,
49242 <span class="hljs-attr">mac</span>: <span class="hljs-string">'00:00:00:00:00:00'</span>,
49243 <span class="hljs-attr">scopeid</span>: <span class="hljs-number">0</span>,
49244 <span class="hljs-attr">internal</span>: <span class="hljs-literal">true</span>,
49245 <span class="hljs-attr">cidr</span>: <span class="hljs-string">'::1/128'</span>
49248 <span class="hljs-attr">eth0</span>: [
49250 <span class="hljs-attr">address</span>: <span class="hljs-string">'192.168.1.108'</span>,
49251 <span class="hljs-attr">netmask</span>: <span class="hljs-string">'255.255.255.0'</span>,
49252 <span class="hljs-attr">family</span>: <span class="hljs-string">'IPv4'</span>,
49253 <span class="hljs-attr">mac</span>: <span class="hljs-string">'01:02:03:0a:0b:0c'</span>,
49254 <span class="hljs-attr">internal</span>: <span class="hljs-literal">false</span>,
49255 <span class="hljs-attr">cidr</span>: <span class="hljs-string">'192.168.1.108/24'</span>
49258 …<span class="hljs-attr">address</span>: <span class="hljs-string">'fe80::a00:27ff:fe4e:66a1'</span…
49259 … <span class="hljs-attr">netmask</span>: <span class="hljs-string">'ffff:ffff:ffff:ffff::'</span>,
49260 <span class="hljs-attr">family</span>: <span class="hljs-string">'IPv6'</span>,
49261 <span class="hljs-attr">mac</span>: <span class="hljs-string">'01:02:03:0a:0b:0c'</span>,
49262 <span class="hljs-attr">scopeid</span>: <span class="hljs-number">1</span>,
49263 <span class="hljs-attr">internal</span>: <span class="hljs-literal">false</span>,
49264 …<span class="hljs-attr">cidr</span>: <span class="hljs-string">'fe80::a00:27ff:fe4e:66a1/64'</span>
49273 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49280 …s://github.com/nodejs/node/blob/HEAD/BUILDING.md#androidandroid-based-devices-eg-firefox-os">Andro…
49286 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49297 <li><code>pid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
49299 <li><code>priority</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
49303 <p>The <code>priority</code> input must be an integer between <code>-20</code> (high priority) and …
49318 <td><p>This function is now cross-platform consistent and no longer returns a path with a trailing …
49325 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49334 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
49342 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
49361 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
49369 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
49371 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
49376 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
49381 <code>gid</code> fields are <code>-1</code>, and <code>shell</code> is <code>null</code>.</p>
49392 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String…
49465 <td>Sent to a process to identify user-defined conditions.</td>
49905 <h5>Windows-specific error constants<span><a class="mark" href="#os_windows_specific_error_constant…
50149 are exported in <code>os.constants.dlopen</code>. See <a href="http://man7.org/linux/man-pages/man3…
50177 <td>Make a self-contained library use its own symbols in preference to
50216 <code>-7</code> on all other platforms.</td>
50223 <code>-14</code> on all other platforms.</td>
50229 <code>-20</code> on all other platforms.</td>
50246 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
50250 …code class="language-js"><span class="hljs-keyword">const</span> path = <span class="hljs-built_in…
50254 Windows operating system, the <code>path</code> module will assume that Windows-style
50258 <pre><code class="language-js">path.<span class="hljs-title function_">basename</span>(<span class=…
50259 <span class="hljs-comment">// Returns: 'C:\\temp\\myfile.html'</span></code></pre>
50261 <pre><code class="language-js">path.<span class="hljs-title function_">basename</span>(<span class=…
50262 <span class="hljs-comment">// Returns: 'myfile.html'</span></code></pre>
50266 … class="language-js">path.<span class="hljs-property">win32</span>.<span class="hljs-title functio…
50267 <span class="hljs-comment">// Returns: 'myfile.html'</span></code></pre>
50271 … class="language-js">path.<span class="hljs-property">posix</span>.<span class="hljs-title functio…
50272 <span class="hljs-comment">// Returns: 'myfile.html'</span></code></pre>
50273 <p>On Windows Node.js follows the concept of per-drive working directory.
50277 <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-r…
50284 <td><p>Passing a non-string as the <code>path</code> argument will throw now.</p></td></tr>
50291 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50292 <li><code>ext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
50293 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50298 <pre><code class="language-js">path.<span class="hljs-title function_">basename</span>(<span class=…
50299 <span class="hljs-comment">// Returns: 'quux.html'</span>
50301 ….<span class="hljs-title function_">basename</span>(<span class="hljs-string">'/foo/bar/baz/asdf/q…
50302 <span class="hljs-comment">// Returns: 'quux'</span></code></pre>
50304 case-insensitive manner, this function does not. For example, <code>C:\\foo.html</code> and
50306 case-sensitive string:</p>
50307-js">path.<span class="hljs-property">win32</span>.<span class="hljs-title function_">basename</sp…
50308 <span class="hljs-comment">// Returns: 'foo'</span>
50310 …"hljs-property">win32</span>.<span class="hljs-title function_">basename</span>(<span class="hljs-
50311 <span class="hljs-comment">// Returns: 'foo.HTML'</span></code></pre>
50319 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
50321 <p>Provides the platform-specific path delimiter:</p>
50327-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
50328 <span class="hljs-comment">// Prints: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'</span>
50330 …lass="hljs-property">env</span>.<span class="hljs-property">PATH</span>.<span class="hljs-title fu…
50331 <span class="hljs-comment">// Returns: ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']…
50333-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
50334 <span class="hljs-comment">// Prints: 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'</span>
50336 …lass="hljs-property">env</span>.<span class="hljs-property">PATH</span>.<span class="hljs-title fu…
50337 <span class="hljs-comment">// Returns ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\…
50344 <td><p>Passing a non-string as the <code>path</code> argument will throw now.</p></td></tr>
50351 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50352 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50357 <pre><code class="language-js">path.<span class="hljs-title function_">dirname</span>(<span class="…
50358 <span class="hljs-comment">// Returns: '/foo/bar/baz/asdf'</span></code></pre>
50366 <td><p>Passing a non-string as the <code>path</code> argument will throw now.</p></td></tr>
50373 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50374 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50381 <pre><code class="language-js">path.<span class="hljs-title function_">extname</span>(<span class="…
50382 <span class="hljs-comment">// Returns: '.html'</span>
50384 path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index.coffee.md'<…
50385 <span class="hljs-comment">// Returns: '.md'</span>
50387 path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index.'</span>);
50388 <span class="hljs-comment">// Returns: '.'</span>
50390 path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index'</span>);
50391 <span class="hljs-comment">// Returns: ''</span>
50393 path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'.index'</span>);
50394 <span class="hljs-comment">// Returns: ''</span>
50396 path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'.index.md'</span>…
50397 <span class="hljs-comment">// Returns: '.md'</span></code></pre>
50404 <li><code>pathObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere…
50406 <li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
50407 <li><code>root</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50408 <li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50409 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50410 <li><code>ext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
50413 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50424 <pre><code class="language-js"><span class="hljs-comment">// If `dir`, `root` and `base` are provid…
50425 <span class="hljs-comment">// `${dir}${path.sep}${base}`</span>
50426 <span class="hljs-comment">// will be returned. `root` is ignored.</span>
50427 path.<span class="hljs-title function_">format</span>({
50428 <span class="hljs-attr">root</span>: <span class="hljs-string">'/ignored'</span>,
50429 <span class="hljs-attr">dir</span>: <span class="hljs-string">'/home/user/dir'</span>,
50430 <span class="hljs-attr">base</span>: <span class="hljs-string">'file.txt'</span>
50432 <span class="hljs-comment">// Returns: '/home/user/dir/file.txt'</span>
50434 <span class="hljs-comment">// `root` will be used if `dir` is not specified.</span>
50435 <span class="hljs-comment">// If only `root` is provided or `dir` is equal to `root` then the</span>
50436 <span class="hljs-comment">// platform separator will not be included. `ext` will be ignored.</span>
50437 path.<span class="hljs-title function_">format</span>({
50438 <span class="hljs-attr">root</span>: <span class="hljs-string">'/'</span>,
50439 <span class="hljs-attr">base</span>: <span class="hljs-string">'file.txt'</span>,
50440 <span class="hljs-attr">ext</span>: <span class="hljs-string">'ignored'</span>
50442 <span class="hljs-comment">// Returns: '/file.txt'</span>
50444 <span class="hljs-comment">// `name` + `ext` will be used if `base` is not specified.</span>
50445 path.<span class="hljs-title function_">format</span>({
50446 <span class="hljs-attr">root</span>: <span class="hljs-string">'/'</span>,
50447 <span class="hljs-attr">name</span>: <span class="hljs-string">'file'</span>,
50448 <span class="hljs-attr">ext</span>: <span class="hljs-string">'.txt'</span>
50450 <span class="hljs-comment">// Returns: '/file.txt'</span></code></pre>
50452 <pre><code class="language-js">path.<span class="hljs-title function_">format</span>({
50453 <span class="hljs-attr">dir</span>: <span class="hljs-string">'C:\\path\\dir'</span>,
50454 <span class="hljs-attr">base</span>: <span class="hljs-string">'file.txt'</span>
50456 <span class="hljs-comment">// Returns: 'C:\\path\\dir\\file.txt'</span></code></pre>
50462 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50463 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
50466 <p>If the given <code>path</code> is a zero-length string, <code>false</code> will be returned.</p>
50468 …ass="language-js">path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-stri…
50469 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'/baz/..'</span…
50470 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'qux/'</span>);…
50471 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'.'</span>); …
50473 …s="language-js">path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string…
50474 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'\\\\server'</s…
50475 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'C:/foo/..'</sp…
50476 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'C:\\foo\\..'</…
50477 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'bar\\baz'</spa…
50478 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'bar/baz'</span…
50479 path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'.'</span>); …
50486 <li><code>...paths</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
50487 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50490 platform-specific separator as a delimiter, then normalizes the resulting path.</p>
50491 <p>Zero-length <code>path</code> segments are ignored. If the joined path string is a
50492 zero-length string then <code>'.'</code> will be returned, representing the current
50494-js">path.<span class="hljs-title function_">join</span>(<span class="hljs-string">'/foo'</span>, …
50495 <span class="hljs-comment">// Returns: '/foo/bar/baz/asdf'</span>
50497 path.<span class="hljs-title function_">join</span>(<span class="hljs-string">'foo'</span>, {}, <sp…
50498 <span class="hljs-comment">// Throws 'TypeError: Path must be a string. Received {}'</span></code><…
50505 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50506 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50511 …de> on POSIX and either <code>\</code> or <code>/</code> on Windows), they are replaced by a single
50512 instance of the platform-specific path segment separator (<code>/</code> on POSIX and
50514 <p>If the <code>path</code> is a zero-length string, <code>'.'</code> is returned, representing the
50517 <pre><code class="language-js">path.<span class="hljs-title function_">normalize</span>(<span class…
50518 <span class="hljs-comment">// Returns: '/foo/bar/baz/asdf'</span></code></pre>
50520 <pre><code class="language-js">path.<span class="hljs-title function_">normalize</span>(<span class…
50521 <span class="hljs-comment">// Returns: 'C:\\temp\\foo\\'</span></code></pre>
50524 … class="language-js">path.<span class="hljs-property">win32</span>.<span class="hljs-title functio…
50525 <span class="hljs-comment">// Returns: 'C:\\temp\\foo\\bar'</span></code></pre>
50532 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50533 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
50540 <li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
50541 <li><code>root</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50542 <li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50543 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50544 <li><code>ext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
50547 <pre><code class="language-js">path.<span class="hljs-title function_">parse</span>(<span class="hl…
50548 <span class="hljs-comment">// Returns:</span>
50549 <span class="hljs-comment">// { root: '/',</span>
50550 <span class="hljs-comment">// dir: '/home/user/dir',</span>
50551 <span class="hljs-comment">// base: 'file.txt',</span>
50552 <span class="hljs-comment">// ext: '.txt',</span>
50553 <span class="hljs-comment">// name: 'file' }</span></code></pre>
50554 <pre><code class="language-text">┌─────────────────────┬────────────┐
50562 <pre><code class="language-js">path.<span class="hljs-title function_">parse</span>(<span class="hl…
50563 <span class="hljs-comment">// Returns:</span>
50564 <span class="hljs-comment">// { root: 'C:\\',</span>
50565 <span class="hljs-comment">// dir: 'C:\\path\\dir',</span>
50566 <span class="hljs-comment">// base: 'file.txt',</span>
50567 <span class="hljs-comment">// ext: '.txt',</span>
50568 <span class="hljs-comment">// name: 'file' }</span></code></pre>
50569 <pre><code class="language-text">┌─────────────────────┬────────────┐
50582 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
50599 <li><code>from</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50600 <li><code>to</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
50601 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50605 path (after calling <code>path.resolve()</code> on each), a zero-length string is returned.</p>
50606 <p>If a zero-length string is passed as <code>from</code> or <code>to</code>, the current working
50607 directory will be used instead of the zero-length strings.</p>
50609 …anguage-js">path.<span class="hljs-title function_">relative</span>(<span class="hljs-string">'/da…
50610 <span class="hljs-comment">// Returns: '../../impl/bbb'</span></code></pre>
50612 …anguage-js">path.<span class="hljs-title function_">relative</span>(<span class="hljs-string">'C:\…
50613 <span class="hljs-comment">// Returns: '..\\..\\impl\\bbb'</span></code></pre>
50620 <li><code>...paths</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
50621 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50634 <p>Zero-length <code>path</code> segments are ignored.</p>
50637 …class="language-js">path.<span class="hljs-title function_">resolve</span>(<span class="hljs-strin…
50638 <span class="hljs-comment">// Returns: '/foo/bar/baz'</span>
50640 path.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'/foo/bar'</span>,…
50641 <span class="hljs-comment">// Returns: '/tmp/file'</span>
50643 …js-title function_">resolve</span>(<span class="hljs-string">'wwwroot'</span>, <span class="hljs-s…
50644 <span class="hljs-comment">// If the current working directory is /home/myself/node,</span>
50645 <span class="hljs-comment">// this returns '/home/myself/node/wwwroot/static_files/gif/image.gif'</…
50652 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
50654 <p>Provides the platform-specific path segment separator:</p>
50660 …class="language-js"><span class="hljs-string">'foo/bar/baz'</span>.<span class="hljs-title functio…
50661 <span class="hljs-comment">// Returns: ['foo', 'bar', 'baz']</span></code></pre>
50663 …lass="language-js"><span class="hljs-string">'foo\\bar\\baz'</span>.<span class="hljs-title functi…
50664 <span class="hljs-comment">// Returns: ['foo', 'bar', 'baz']</span></code></pre>
50673 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50674 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
50676 …valent <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#namespaces">…
50680 method is non-operational and always returns <code>path</code> without modifications.</p>
50686 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
50688 <p>The <code>path.win32</code> property provides access to Windows-specific implementations
50693 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
50696 <a href="https://w3c.github.io/perf-timing-primer/">Web Performance APIs</a> as well as additional …
50697 Node.js-specific performance measurements.</p>
50698 <p>Node.js supports the following <a href="https://w3c.github.io/perf-timing-primer/">Web Performan…
50700 <li><a href="https://www.w3.org/TR/hr-time-2">High Resolution Time</a></li>
50701 <li><a href="https://w3c.github.io/performance-timeline/">Performance Timeline</a></li>
50702 <li><a href="https://www.w3.org/TR/user-timing/">User Timing</a></li>
50704-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">PerformanceObserver…
50706-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
50707-variable language_">console</span>.<span class="hljs-title function_">log</span>(items.<span clas…
50708 performance.<span class="hljs-title function_">clearMarks</span>();
50710 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: …
50711 performance.<span class="hljs-title function_">measure</span>(<span class="hljs-string">'Start to N…
50713 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'A'</span>);
50714 <span class="hljs-title function_">doSomeLongRunningProcess</span>(<span class="hljs-function">() =…
50715 …erformance.<span class="hljs-title function_">measure</span>(<span class="hljs-string">'A to Now'<…
50717 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'B'</span>);
50718 …lass="hljs-title function_">measure</span>(<span class="hljs-string">'A to B'</span>, <span class=…
50725 Node.js instance. It is similar to <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window…
50731 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50740 <li><code>utilization1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
50742 <li><code>utilization2</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
50744 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objec…
50746 <li><code>idle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50747 <li><code>active</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
50748 <li><code>utilization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
50766 single subtraction.</p>
50772 <pre><code class="language-js"><span class="hljs-meta">'use strict'</span>;
50773-keyword">const</span> { eventLoopUtilization } = <span class="hljs-built_in">require</span>(<span…
50774 <span class="hljs-keyword">const</span> { spawnSync } = <span class="hljs-built_in">require</span>(…
50776 <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> {
50777 …<span class="hljs-keyword">const</span> elu = <span class="hljs-title function_">eventLoopUtilizat…
50778 …<span class="hljs-title function_">spawnSync</span>(<span class="hljs-string">'sleep'</span>, [<sp…
50779-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
50784 <p>Passing in a user-defined object instead of the result of a previous call to
50792 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50812 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
50813 <li><code>startMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
50814 <li><code>endMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
50824 …code>startMark</code> is set to <a href="https://w3c.github.io/hr-time/#dom-performance-timeorigin…
50845 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
50854 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
50856 <p>The <a href="https://w3c.github.io/hr-time/#dom-performance-timeorigin"><code>timeOrigin</code><…
50863 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
50869 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
50871 <span class="hljs-title class_">PerformanceObserver</span>
50872 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
50874 <span class="hljs-keyword">function</span> <span class="hljs-title function_">someFunction</span>(<…
50875 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
50878 <span class="hljs-keyword">const</span> wrapped = performance.<span class="hljs-title function_">ti…
50880-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
50881-variable language_">console</span>.<span class="hljs-title function_">log</span>(list.<span class…
50882 obs.<span class="hljs-title function_">disconnect</span>();
50884 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: …
50886 <span class="hljs-comment">// A performance timeline entry will be created</span>
50887 <span class="hljs-title function_">wrapped</span>();</code></pre>
50897 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
50906 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
50923 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
50943 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
50951 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
50968 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
50987 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
50991 has the value of -1.</p>
50997 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51006 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51018 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51021 exited. If the event loop has not yet exited, the property has the value of -1.
51022 It can only have a value of not -1 in a handler of the <a href="#process_event_exit"><code>'exit'</…
51028 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51032 main script), the property has the value of -1.</p>
51038 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51047 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51057 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
51066 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
51068 <span class="hljs-title class_">PerformanceObserver</span>
51069 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51071-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51072 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
51073 observer.<span class="hljs-title function_">disconnect</span>();
51075-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51077 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'test'</span>)…
51096 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
51098 <li><code>entryTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
51101 <li><code>buffered</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
51112 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
51114 <span class="hljs-title class_">PerformanceObserver</span>
51115 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51117-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51118 <span class="hljs-comment">// Called three times synchronously. `list` contains one item.</span>
51120 obs.<span class="hljs-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: …
51122 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-num…
51123 …performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">`test<span cl…
51124 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
51126 <span class="hljs-title class_">PerformanceObserver</span>
51127 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51129-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51130 <span class="hljs-comment">// Called once. `list` contains three items.</span>
51132-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51134 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-num…
51135 …performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">`test<span cl…
51152 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
51154 <span class="hljs-title class_">PerformanceObserver</span>
51155 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51157-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51158 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
51159 <span class="hljs-comment">/**
51175 observer.<span class="hljs-title function_">disconnect</span>();
51177-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51179 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'test'</span>);
51180 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'meow'</span>)…
51186 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
51187 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
51194 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
51196 <span class="hljs-title class_">PerformanceObserver</span>
51197 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51199-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51200-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList…
51201 <span class="hljs-comment">/**
51211-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList…
51213-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList…
51214 <span class="hljs-comment">/**
51224-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList…
51225 observer.<span class="hljs-title function_">disconnect</span>();
51227-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51229 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'test'</span>);
51230 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'meow'</span>)…
51236 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
51242 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
51244 <span class="hljs-title class_">PerformanceObserver</span>
51245 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51247-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51248-variable language_">console</span>.<span class="hljs-title function_">log</span>(perfObserverList…
51249 <span class="hljs-comment">/**
51265 observer.<span class="hljs-title function_">disconnect</span>();
51267-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51269 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'test'</span>);
51270 performance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">'meow'</span>)…
51276 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
51278 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
51280 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
51282 <li><code>figures</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
51294 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
51296 <li><code>resolution</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
51310 …"language-js"><span class="hljs-keyword">const</span> { monitorEventLoopDelay } = <span class="hlj…
51311-keyword">const</span> h = <span class="hljs-title function_">monitorEventLoopDelay</span>({ <span…
51312 h.<span class="hljs-title function_">enable</span>();
51313 <span class="hljs-comment">// Do something.</span>
51314 h.<span class="hljs-title function_">disable</span>();
51315 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
51316 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
51317 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
51318 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
51319 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
51320-variable language_">console</span>.<span class="hljs-title function_">log</span>(h.<span class="h…
51321-variable language_">console</span>.<span class="hljs-title function_">log</span>(h.<span class="h…
51331 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51340 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51348 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51356 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51364 <li><code>percentile</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
51365 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
51373 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" …
51386 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
51396 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
51405 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
51422 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
51435 <pre><code class="language-js"><span class="hljs-meta">'use strict'</span>;
51436 <span class="hljs-keyword">const</span> async_hooks = <span class="hljs-built_in">require</span>(<s…
51437 <span class="hljs-keyword">const</span> {
51439 <span class="hljs-title class_">PerformanceObserver</span>
51440 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51442 <span class="hljs-keyword">const</span> set = <span class="hljs-keyword">new</span> <span class="hl…
51443 <span class="hljs-keyword">const</span> hook = async_hooks.<span class="hljs-title function_">creat…
51444 <span class="hljs-title function_">init</span>(<span class="hljs-params">id, type</span>) {
51445 <span class="hljs-keyword">if</span> (type === <span class="hljs-string">'Timeout'</span>) {
51446 …ormance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">`Timeout-<span cl…
51447 set.<span class="hljs-title function_">add</span>(id);
51450 <span class="hljs-title function_">destroy</span>(<span class="hljs-params">id</span>) {
51451 <span class="hljs-keyword">if</span> (set.<span class="hljs-title function_">has</span>(id)) {
51452 set.<span class="hljs-title function_">delete</span>(id);
51453 …ormance.<span class="hljs-title function_">mark</span>(<span class="hljs-string">`Timeout-<span cl…
51454 …performance.<span class="hljs-title function_">measure</span>(<span class="hljs-string">`Timeout-<…
51455 … <span class="hljs-string">`Timeout-<span class="hljs-subst">${id}</span>-Init`</span>,
51456 … <span class="hljs-string">`Timeout-<span class="hljs-subst">${id}</span>-Destroy`</span>);
51460 hook.<span class="hljs-title function_">enable</span>();
51462-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51463-variable language_">console</span>.<span class="hljs-title function_">log</span>(list.<span class…
51464 performance.<span class="hljs-title function_">clearMarks</span>();
51465 observer.<span class="hljs-title function_">disconnect</span>();
51467-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51469 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {}, <span cl…
51473 <!-- eslint-disable no-global-assign -->
51474 <pre><code class="language-js"><span class="hljs-meta">'use strict'</span>;
51475 <span class="hljs-keyword">const</span> {
51477 <span class="hljs-title class_">PerformanceObserver</span>
51478 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'perf_hooks'</span>);
51479 <span class="hljs-keyword">const</span> mod = <span class="hljs-built_in">require</span>(<span clas…
51481 <span class="hljs-comment">// Monkey patch the require function</span>
51482 …pan class="hljs-property">Module</span>.<span class="hljs-property"><span class="hljs-keyword">pro…
51483-title function_">timerify</span>(mod.<span class="hljs-property">Module</span>.<span class="hljs-
51484 <span class="hljs-built_in">require</span> = performance.<span class="hljs-title function_">timerif…
51486 <span class="hljs-comment">// Activate the observer</span>
51487-keyword">const</span> obs = <span class="hljs-keyword">new</span> <span class="hljs-title class_"…
51488 …<span class="hljs-keyword">const</span> entries = list.<span class="hljs-title function_">getEntri…
51489 …entries.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class…
51490-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
51492 obs.<span class="hljs-title function_">disconnect</span>();
51494-title function_">observe</span>({ <span class="hljs-attr">entryTypes</span>: [<span class="hljs-s…
51496 <span class="hljs-built_in">require</span>(<span class="hljs-string">'some-module'</span>);</code><…
51501 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
51509 <p>A best practice would be to ensure that the policy manifest is read-only for
51516 <p>The <code>--experimental-policy</code> flag can be used to enable features for policies
51520 <pre><code class="language-bash">node --experimental-policy=policy.json app.js</code></pre>
51524 the policy file itself may be provided via <code>--policy-integrity</code>.
51527 <pre><code class="language-bash">node --experimental-policy=policy.json --policy-integrity=<span cl…
51541 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51542 …<span class="hljs-attr">"onerror"</span><span class="hljs-punctuation">:</span> <span class="hljs-
51543 …<span class="hljs-attr">"resources"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51544 …<span class="hljs-attr">"./app/checked.js"</span><span class="hljs-punctuation">:</span> <span cla…
51545 …<span class="hljs-attr">"integrity"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51546 <span class="hljs-punctuation">}</span>
51547 <span class="hljs-punctuation">}</span>
51548 <span class="hljs-punctuation">}</span></code></pre>
51552 <a href="https://www.w3.org/TR/SRI/#the-integrity-attribute">integrity attribute</a>
51558 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51559 …<span class="hljs-attr">"resources"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51560 …<span class="hljs-attr">"./app/checked.js"</span><span class="hljs-punctuation">:</span> <span cla…
51561 …<span class="hljs-attr">"integrity"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51562 <span class="hljs-punctuation">}</span>
51563 <span class="hljs-punctuation">}</span>
51564 <span class="hljs-punctuation">}</span></code></pre>
51568 <li>A <a href="https://url.spec.whatwg.org/#relative-url-with-fragment-string">relative-URL string<…
51575 <code>node -e 'process.stdout.write("sha256-");process.stdin.pipe(crypto.createHash("sha256").setEn…
51586 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51587 …<span class="hljs-attr">"resources"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51588 …<span class="hljs-attr">"./app/checked.js"</span><span class="hljs-punctuation">:</span> <span cla…
51589 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
51590 …<span class="hljs-attr">"fs"</span><span class="hljs-punctuation">:</span> <span class="hljs-keywo…
51591 …ss="hljs-attr">"os"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"./app…
51592-attr">"http"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span…
51593 <span class="hljs-punctuation">}</span>
51594 <span class="hljs-punctuation">}</span>
51595 <span class="hljs-punctuation">}</span>
51596 <span class="hljs-punctuation">}</span></code></pre>
51608 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51609 …<span class="hljs-attr">"resources"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51610 …<span class="hljs-attr">"file:///C:/app/utils.js"</span><span class="hljs-punctuation">:</span> <s…
51611 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
51612 …<span class="hljs-attr">"./utils.js"</span><span class="hljs-punctuation">:</span> <span class="hl…
51613 <span class="hljs-punctuation">}</span>
51614 <span class="hljs-punctuation">}</span>
51615 <span class="hljs-punctuation">}</span>
51616 <span class="hljs-punctuation">}</span></code></pre>
51618 and redirected to <code>file:///C:/app/utils-v2.js</code> instead regardless of using an
51650 …de class="language-js"><span class="hljs-keyword">const</span> original = <span class="hljs-built_…
51651-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-
51652 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">time</span…
51653 <span class="hljs-keyword">try</span> {
51654 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span>.<span class="hljs-p…
51655 …<span class="hljs-title class_">Reflect</span>.<span class="hljs-title function_">construct</span>…
51656 …<span class="hljs-title class_">Reflect</span>.<span class="hljs-title function_">apply</span>(ori…
51657 } <span class="hljs-keyword">finally</span> {
51658 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">timeEnd</s…
51667 <a href="https://url.spec.whatwg.org/#special-scheme">special schemes</a>, keeping trailing <code>"…
51669 If the URL is non-special the scope will be located by the URL's origin. If no
51680 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51681 …<span class="hljs-attr">"scopes"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51682-attr">"file:///C:/app/"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuati…
51683-attr">"file:"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</spa…
51684 …<span class="hljs-attr">""</span><span class="hljs-punctuation">:</span> <span class="hljs-punctua…
51685 <span class="hljs-punctuation">}</span>
51686 <span class="hljs-punctuation">}</span></code></pre>
51738 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51739 …<span class="hljs-attr">"scopes"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51740 …<span class="hljs-attr">"file:"</span><span class="hljs-punctuation">:</span> <span class="hljs-pu…
51741 …<span class="hljs-attr">"integrity"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51742 <span class="hljs-punctuation">}</span>
51743 <span class="hljs-punctuation">}</span>
51744 <span class="hljs-punctuation">}</span></code></pre>
51748 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51749 …<span class="hljs-attr">"resources"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51750 …<span class="hljs-attr">"./app/checked.js"</span><span class="hljs-punctuation">:</span> <span cla…
51751 …<span class="hljs-attr">"cascade"</span><span class="hljs-punctuation">:</span> <span class="hljs-
51752 …<span class="hljs-attr">"integrity"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51753 <span class="hljs-punctuation">}</span>
51754 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
51755 …<span class="hljs-attr">"scopes"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51756 …<span class="hljs-attr">"./app/"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51757 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
51758 …<span class="hljs-attr">"fs"</span><span class="hljs-punctuation">:</span> <span class="hljs-keywo…
51759 <span class="hljs-punctuation">}</span>
51760 <span class="hljs-punctuation">}</span>
51761 <span class="hljs-punctuation">}</span>
51762 <span class="hljs-punctuation">}</span></code></pre>
51764 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51765 …<span class="hljs-attr">"resources"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51766 …<span class="hljs-attr">"data:text/javascript,import('fs');"</span><span class="hljs-punctuation">…
51767 …<span class="hljs-attr">"cascade"</span><span class="hljs-punctuation">:</span> <span class="hljs-
51768 …<span class="hljs-attr">"integrity"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51769 <span class="hljs-punctuation">}</span>
51770 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
51771 …<span class="hljs-attr">"scopes"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51772 …<span class="hljs-attr">"data:"</span><span class="hljs-punctuation">:</span> <span class="hljs-pu…
51773 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
51774 …<span class="hljs-attr">"fs"</span><span class="hljs-punctuation">:</span> <span class="hljs-keywo…
51775 <span class="hljs-punctuation">}</span>
51776 <span class="hljs-punctuation">}</span>
51777 <span class="hljs-punctuation">}</span>
51778 <span class="hljs-punctuation">}</span></code></pre>
51779 <h5>Example: <a href="https://url.spec.whatwg.org/#relative-url-with-fragment-string">import maps</…
51781 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51782 …<span class="hljs-attr">"imports"</span><span class="hljs-punctuation">:</span> <span class="hljs-
51783 …<span class="hljs-attr">"react"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
51784 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
51785 …<span class="hljs-attr">"scopes"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51786 …<span class="hljs-attr">"./ssr/"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51787 …<span class="hljs-attr">"react"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
51788 <span class="hljs-punctuation">}</span>
51789 <span class="hljs-punctuation">}</span>
51790 <span class="hljs-punctuation">}</span></code></pre>
51791 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
51792 …an class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="hlj…
51793 …<span class="hljs-attr">"scopes"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51794 …<span class="hljs-attr">""</span><span class="hljs-punctuation">:</span> <span class="hljs-punctua…
51795 …<span class="hljs-attr">"cascade"</span><span class="hljs-punctuation">:</span> <span class="hljs-
51796 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
51797 …<span class="hljs-attr">"react"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
51798 <span class="hljs-punctuation">}</span>
51799 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
51800 …<span class="hljs-attr">"./ssr/"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
51801 …<span class="hljs-attr">"cascade"</span><span class="hljs-punctuation">:</span> <span class="hljs-
51802 …<span class="hljs-attr">"dependencies"</span><span class="hljs-punctuation">:</span> <span class="…
51803 …<span class="hljs-attr">"react"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
51804 <span class="hljs-punctuation">}</span>
51805 <span class="hljs-punctuation">}</span>
51806 <span class="hljs-punctuation">}</span>
51807 <span class="hljs-punctuation">}</span></code></pre>
51810 Policies require this to be opt-in since it enables all resources of the
51814 <p>Import maps only have a single top level scope for their "imports". So for
51834 …de class="language-js"><span class="hljs-keyword">const</span> process = <span class="hljs-built_i…
51852-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'beforeExit'</…
51853 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
51856 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
51857 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
51860 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
51862 <span class="hljs-comment">// Prints:</span>
51863 <span class="hljs-comment">// This message is displayed first.</span>
51864 <span class="hljs-comment">// Process beforeExit event with code: 0</span>
51865 <span class="hljs-comment">// Process exit event with code: 0</span></code></pre>
51878 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
51891 …age-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</spa…
51892-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
51898 …age-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</spa…
51899 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
51900 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
51901 }, <span class="hljs-number">0</span>);
51908-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href=…
51928 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
51929 <li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
51930 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
51943 example, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/…
51944-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'multipleResol…
51945 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
51946-title function_">setImmediate</span>(<span class="hljs-function">() =></span> process.<span class…
51949 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
51950 <span class="hljs-keyword">try</span> {
51951-keyword">return</span> <span class="hljs-keyword">await</span> <span class="hljs-keyword">new</sp…
51952 … <span class="hljs-title function_">resolve</span>(<span class="hljs-string">'First call'</span>);
51953 …<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'Swallowed resolve'</s…
51954 …lass="hljs-title function_">reject</span>(<span class="hljs-keyword">new</span> <span class="hljs-
51956 } <span class="hljs-keyword">catch</span> {
51957 …an class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title…
51961 …s-title function_">main</span>().<span class="hljs-title function_">then</span>(<span class="hljs-
51962 <span class="hljs-comment">// resolve: Promise { 'First call' } 'Swallowed resolve'</span>
51963 <span class="hljs-comment">// reject: Promise { 'First call' } Error: Swallowed reject</span>
51964 <span class="hljs-comment">// at Promise (*)</span>
51965 <span class="hljs-comment">// at new Promise (&#x3C;anonymous>)</span>
51966 <span class="hljs-comment">// at main (*)</span>
51967 <span class="hljs-comment">// First call</span></code></pre>
51973 <li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
51976 and an error handler was attached to it (using <a href="https://developer.mozilla.org/en-US/docs/We…
51986 an ever-growing list of unhandled exceptions, with Promises there can be a
51987 growing-and-shrinking list of unhandled rejections.</p>
51993 …class="language-js"><span class="hljs-keyword">const</span> unhandledRejections = <span class="hlj…
51994 …class="hljs-title function_">on</span>(<span class="hljs-string">'unhandledRejection'</span>, <spa…
51995 unhandledRejections.<span class="hljs-title function_">set</span>(promise, reason);
51997 … class="hljs-title function_">on</span>(<span class="hljs-string">'rejectionHandled'</span>, <span…
51998 unhandledRejections.<span class="hljs-title function_">delete</span>(promise);
52003 likely best for long-running application) or upon process exit (which is likely
52018 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
52019 <li><code>origin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
52022 <a href="#cli_unhandled_rejections_mode"><code>--unhandled-rejections</code></a> flag set to <code>…
52034-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'uncaughtExcep…
52035 fs.<span class="hljs-title function_">writeSync</span>(
52036 process.<span class="hljs-property">stderr</span>.<span class="hljs-property">fd</span>,
52037 <span class="hljs-string">`Caught exception: <span class="hljs-subst">${err}</span>\n`</span> +
52038 <span class="hljs-string">`Exception origin: <span class="hljs-subst">${origin}</span>`</span>
52042 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
52043 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
52044 }, <span class="hljs-number">500</span>);
52046 <span class="hljs-comment">// Intentionally cause an exception, but don't catch it.</span>
52047 <span class="hljs-title function_">nonexistentFunc</span>();
52048 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
52060 process will exit with a non-zero exit code and the stack trace will be printed.
52078 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
52079 <li><code>origin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
52082 <a href="#cli_unhandled_rejections_mode"><code>--unhandled-rejections</code></a> flag set to <code>…
52091-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'uncaughtExcep…
52092 …<span class="hljs-title class_">MyMonitoringTool</span>.<span class="hljs-title function_">logSync…
52095 <span class="hljs-comment">// Intentionally cause an exception, but don't catch it.</span>
52096 <span class="hljs-title function_">nonexistentFunc</span>();
52097 <span class="hljs-comment">// Still crashes Node.js</span></code></pre>
52113 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> |…
52115 <li><code>promise</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
52120 promises". Rejections can be caught and handled using <a href="https://developer.mozilla.org/en-US/…
52124-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'unhandledReje…
52125-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
52126 …<span class="hljs-comment">// Application specific logging, throwing an error, or other logic here…
52129 somePromise.<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class…
52130-keyword">return</span> <span class="hljs-title function_">reportToUser</span>(<span class="hljs-v…
52131 }); <span class="hljs-comment">// No `.catch()` or `.then()`</span></code></pre>
52134 … class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_"…
52135 <span class="hljs-comment">// Initially set the loaded status to a rejected promise</span>
52136-variable language_">this</span>.<span class="hljs-property">loaded</span> = <span class="hljs-tit…
52139 <span class="hljs-keyword">const</span> resource = <span class="hljs-keyword">new</span> <span clas…
52140 <span class="hljs-comment">// no .catch or .then on resource.loaded for at least a turn</span></cod…
52143 address such failures, a non-operational
52144 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/c…
52152 <li><code>warning</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
52154 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52155 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#…
52156 <li><code>stack</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
52166 lead to sub-optimal application performance, bugs, or security vulnerabilities.</p>
52167 …ge-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'warning'</s…
52168-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52169-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52170-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52172 <p>By default, Node.js will print process warnings to <code>stderr</code>. The <code>--no-warnings<…
52173 command-line option can be used to suppress the default console output but the
52177 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
52178 <span class="hljs-meta">> </span><span class="language-bash">events.defaultMaxListeners = 1;</span>
52179 <span class="hljs-meta">> </span><span class="language-bash">process.on(<span class="hljs-string">'…
52180 <span class="hljs-meta">> </span><span class="language-bash">process.on(<span class="hljs-string">'…
52181 <span class="hljs-meta">> </span><span class="language-bash">(node:38638) MaxListenersExceededWarni…
52185 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
52186-meta">> </span><span class="language-bash">const p = process.on(<span class="hljs-string">'warnin…
52187 <span class="hljs-meta">> </span><span class="language-bash">events.defaultMaxListeners = 1;</span>
52188 <span class="hljs-meta">> </span><span class="language-bash">process.on(<span class="hljs-string">'…
52189 <span class="hljs-meta">> </span><span class="language-bash">process.on(<span class="hljs-string">'…
52190 <span class="hljs-meta">> </span><span class="language-bash">Do not <span class="hljs-keyword">do</…
52191 <p>The <code>--trace-warnings</code> command-line option can be used to have the default
52193 <p>Launching Node.js using the <code>--throw-deprecation</code> command-line flag will
52195 <p>Using the <code>--trace-deprecation</code> command-line flag will cause the custom
52197 <p>Using the <code>--no-deprecation</code> command-line flag will suppress all reporting
52199 <p>The <code>*-deprecation</code> command-line flags only affect warnings that use the name
52211 custom or application-specific warnings.</p>
52217 <li><code>'DeprecationWarning'</code> - Indicates use of a deprecated Node.js API or feature.
52220 <li><code>'ExperimentalWarning'</code> - Indicates use of an experimental Node.js API or
52222 time and are not subject to the same strict semantic-versioning and long-term
52224 <li><code>'MaxListenersExceededWarning'</code> - Indicates that too many listeners for a
52227 <li><code>'TimeoutOverflowWarning'</code> - Indicates that a numeric value that cannot fit
52228 within a 32-bit signed integer has been provided to either the <code>setTimeout()</code>
52230 <li><code>'UnsupportedWarning'</code> - Indicates use of an unsupported option or feature
52238 refer to <a href="http://man7.org/linux/man-pages/man7/signal.7.html"><code>signal(7)</code></a> fo…
52245 <pre><code class="language-js"><span class="hljs-comment">// Begin reading from stdin so the proces…
52246 process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">resume</span>();
52248 process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGINT'</span>, <sp…
52249 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
52252 <span class="hljs-comment">// Using a single function to handle multiple signals</span>
52253 <span class="hljs-keyword">function</span> <span class="hljs-title function_">handle</span>(<span c…
52254 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
52257 process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGINT'</span>, han…
52258 process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGTERM'</span>, ha…
52262 <li><code>'SIGTERM'</code> and <code>'SIGINT'</code> have default handlers on non-Windows platforms…
52268 other platforms under various similar conditions. See <a href="http://man7.org/linux/man-pages/man7…
52270 Windows about 10 seconds later. On non-Windows platforms, the default
52279 pressed. On non-Windows platforms, it can be listened on, but there is no way
52288 artificially using <a href="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code><…
52299 termination of the target process, and afterwards, subprocess will report that
52316 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set" …
52319 read-only <code>Set</code> of flags allowable within the <a href="#cli_node_options_options"><code>…
52326 <li>Flags may omit leading single (<code>-</code>) or double (<code>--</code>) dashes; e.g.,
52327 <code>inspect-brk</code> for <code>--inspect-brk</code>, or <code>r</code> for <code>-r</code>.</li>
52328 <li>Flags passed through to V8 (as listed in <code>--v8-options</code>) may replace
52329 one or more <em>non-leading</em> dashes for an underscore, or vice-versa;
52330 e.g., <code>--perf_basic_prof</code>, <code>--perf-basic-prof</code>, <code>--perf_basic-prof</code…
52334 e.g., <code>--stack-trace-limit=100</code>.</li>
52339 passed through to V8 will contain underscores instead of non-leading
52341-js">process.<span class="hljs-property">allowedNodeEnvironmentFlags</span>.<span class="hljs-titl…
52342 <span class="hljs-comment">// -r</span>
52343 <span class="hljs-comment">// --inspect-brk</span>
52344 <span class="hljs-comment">// --abort_on_uncaught_exception</span>
52345 <span class="hljs-comment">// ...</span>
52358 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
52363-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
52369 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
52371 <p>The <code>process.argv</code> property returns an array containing the command-line
52375 file being executed. The remaining elements will be any additional command-line
52377 <p>For example, assuming the following script for <code>process-args.js</code>:</p>
52378 <pre><code class="language-js"><span class="hljs-comment">// print process.argv</span>
52379 …span class="hljs-property">argv</span>.<span class="hljs-title function_">forEach</span>(<span cla…
52380-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
52383 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
52385 <pre><code class="language-text">0: /usr/local/bin/node
52386 1: /Users/mjr/work/node/process-args.js
52395 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
52397 <p>The <code>process.argv0</code> property stores a read-only copy of the original value of
52399 …code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">bash -c…
52400 <span class="hljs-meta">> </span><span class="language-bash">process.argv[0]</span>
52402 <span class="hljs-meta">> </span><span class="language-bash">process.argv0</span>
52417 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
52446 <li><code>directory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
52451-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
52452 <span class="hljs-keyword">try</span> {
52453 process.<span class="hljs-title function_">chdir</span>(<span class="hljs-string">'/tmp'</span>);
52454 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
52455 } <span class="hljs-keyword">catch</span> (err) {
52456 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span …
52464 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
52471 <!-- eslint-skip -->
52472 <pre><code class="language-js">{
52473 <span class="hljs-attr">target_defaults</span>:
52474 { <span class="hljs-attr">cflags</span>: [],
52475 … <span class="hljs-attr">default_configuration</span>: <span class="hljs-string">'Release'</span>,
52476 <span class="hljs-attr">defines</span>: [],
52477 <span class="hljs-attr">include_dirs</span>: [],
52478 <span class="hljs-attr">libraries</span>: [] },
52479 <span class="hljs-attr">variables</span>:
52481 <span class="hljs-attr">host_arch</span>: <span class="hljs-string">'x64'</span>,
52482 <span class="hljs-attr">napi_build_version</span>: <span class="hljs-number">5</span>,
52483 <span class="hljs-attr">node_install_npm</span>: <span class="hljs-string">'true'</span>,
52484 <span class="hljs-attr">node_prefix</span>: <span class="hljs-string">''</span>,
52485 <span class="hljs-attr">node_shared_cares</span>: <span class="hljs-string">'false'</span>,
52486 … <span class="hljs-attr">node_shared_http_parser</span>: <span class="hljs-string">'false'</span>,
52487 <span class="hljs-attr">node_shared_libuv</span>: <span class="hljs-string">'false'</span>,
52488 <span class="hljs-attr">node_shared_zlib</span>: <span class="hljs-string">'false'</span>,
52489 <span class="hljs-attr">node_use_dtrace</span>: <span class="hljs-string">'false'</span>,
52490 <span class="hljs-attr">node_use_openssl</span>: <span class="hljs-string">'true'</span>,
52491 <span class="hljs-attr">node_shared_openssl</span>: <span class="hljs-string">'false'</span>,
52492 <span class="hljs-attr">strict_aliasing</span>: <span class="hljs-string">'true'</span>,
52493 <span class="hljs-attr">target_arch</span>: <span class="hljs-string">'x64'</span>,
52494 <span class="hljs-attr">v8_use_snapshot</span>: <span class="hljs-number">1</span>
52497 <p>The <code>process.config</code> property is <strong>not</strong> read-only and there are existing
52505 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
52518 <li><code>previousValue</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
52520 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
52522 <li><code>user</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52523 <li><code>system</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
52534 <pre><code class="language-js"><span class="hljs-keyword">const</span> startUsage = process.<span c…
52535 <span class="hljs-comment">// { user: 38579, system: 6986 }</span>
52537 <span class="hljs-comment">// spin the CPU for 500 milliseconds</span>
52538 <span class="hljs-keyword">const</span> now = <span class="hljs-title class_">Date</span>.<span cla…
52539 …ljs-keyword">while</span> (<span class="hljs-title class_">Date</span>.<span class="hljs-title fun…
52541 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
52542 <span class="hljs-comment">// { user: 514883, system: 11226 }</span></code></pre>
52548 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
52552-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
52558 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
52561 <pre><code class="language-js">process.<span class="hljs-property">debugPort</span> = <span class="…
52587 <li><code>module</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
52588 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
52605 …<code class="language-js"><span class="hljs-keyword">const</span> os = <span class="hljs-built_in"…
52606 <span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span cla…
52607 <span class="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = { <sp…
52608-title function_">dlopen</span>(<span class="hljs-variable language_">module</span>, path.<span cl…
52609 …os.<span class="hljs-property">constants</span>.<span class="hljs-property">dlopen</span>.<span cl…
52610 <span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span>.<spa…
52616 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
52617 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
52619 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52621 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52622 <li><code>ctor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
52625 <li><code>detail</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
52632 <pre><code class="language-js"><span class="hljs-comment">// Emit a warning with a code and additio…
52633 process.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Something …
52634 <span class="hljs-attr">code</span>: <span class="hljs-string">'MY_WARNING'</span>,
52635 …<span class="hljs-attr">detail</span>: <span class="hljs-string">'This is some additional informat…
52637 <span class="hljs-comment">// Emits:</span>
52638 <span class="hljs-comment">// (node:56338) [MY_WARNING] Warning: Something happened!</span>
52639 <span class="hljs-comment">// This is some additional information</span></code></pre>
52643 …ge-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'warning'</s…
52644-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52645-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52646-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52647-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52648-variable language_">console</span>.<span class="hljs-title function_">warn</span>(warning.<span c…
52656 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
52657 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52659 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52660 <li><code>ctor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
52667 <pre><code class="language-js"><span class="hljs-comment">// Emit a warning using a string.</span>
52668 process.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Something …
52669 <span class="hljs-comment">// Emits: (node: 56338) Warning: Something happened!</span></code></pre>
52670 <pre><code class="language-js"><span class="hljs-comment">// Emit a warning using a string and a ty…
52671 …ess.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Something Hap…
52672 <span class="hljs-comment">// Emits: (node:56338) CustomWarning: Something Happened!</span></code><…
52673-js">process.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Some…
52674 <span class="hljs-comment">// Emits: (node:56338) [WARN001] CustomWarning: Something happened!</spa…
52678 …ge-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'warning'</s…
52679 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span…
52680 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span…
52681 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span…
52682 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">warn</span…
52687 <pre><code class="language-js"><span class="hljs-comment">// Emit a warning using an Error object.<…
52688 …ss="hljs-keyword">const</span> myWarning = <span class="hljs-keyword">new</span> <span class="hljs
52689 <span class="hljs-comment">// Use the Error name property to specify the type name</span>
52690 myWarning.<span class="hljs-property">name</span> = <span class="hljs-string">'CustomWarning'</span…
52691 myWarning.<span class="hljs-property">code</span> = <span class="hljs-string">'WARN001'</span>;
52693 process.<span class="hljs-title function_">emitWarning</span>(myWarning);
52694 <span class="hljs-comment">// Emits: (node:56338) [WARN001] CustomWarning: Something happened!</spa…
52702 <li>If the <code>--throw-deprecation</code> command-line flag is used, the deprecation
52704 <li>If the <code>--no-deprecation</code> command-line flag is used, the deprecation
52706 <li>If the <code>--trace-deprecation</code> command-line flag is used, the deprecation
52713 …class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">…
52714 <span class="hljs-keyword">if</span> (!emitMyWarning.<span class="hljs-property">warned</span>) {
52715 … emitMyWarning.<span class="hljs-property">warned</span> = <span class="hljs-literal">true</span>;
52716 …process.<span class="hljs-title function_">emitWarning</span>(<span class="hljs-string">'Only warn…
52719 <span class="hljs-title function_">emitMyWarning</span>();
52720 <span class="hljs-comment">// Emits: (node: 56339) Warning: Only warn once!</span>
52721 <span class="hljs-title function_">emitMyWarning</span>();
52722 <span class="hljs-comment">// Emits nothing</span></code></pre>
52738 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
52741 See <a href="http://man7.org/linux/man-pages/man7/environ.7.html"><code>environ(7)</code></a>.</p>
52743 <!-- eslint-skip -->
52744 <pre><code class="language-js">{
52745 <span class="hljs-attr">TERM</span>: <span class="hljs-string">'xterm-256color'</span>,
52746 <span class="hljs-attr">SHELL</span>: <span class="hljs-string">'/usr/local/bin/bash'</span>,
52747 <span class="hljs-attr">USER</span>: <span class="hljs-string">'maciej'</span>,
52748 …<span class="hljs-attr">PATH</span>: <span class="hljs-string">'~/.bin/:/usr/bin:/bin:/usr/sbin:/s…
52749 <span class="hljs-attr">PWD</span>: <span class="hljs-string">'/Users/maciej'</span>,
52750 <span class="hljs-attr">EDITOR</span>: <span class="hljs-string">'vim'</span>,
52751 <span class="hljs-attr">SHLVL</span>: <span class="hljs-string">'1'</span>,
52752 <span class="hljs-attr">HOME</span>: <span class="hljs-string">'/Users/maciej'</span>,
52753 <span class="hljs-attr">LOGNAME</span>: <span class="hljs-string">'maciej'</span>,
52754 <span class="hljs-attr">_</span>: <span class="hljs-string">'/usr/local/bin/node'</span>
52760-console"><span class="hljs-meta">$ </span><span class="language-bash">node -e <span class="hljs-s…
52762 …<code class="language-js">process.<span class="hljs-property">env</span>.<span class="hljs-propert…
52763 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span…
52767 …code class="language-js">process.<span class="hljs-property">env</span>.<span class="hljs-property…
52768 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span…
52769 <span class="hljs-comment">// => 'null'</span>
52770 process.<span class="hljs-property">env</span>.<span class="hljs-property">test</span> = <span clas…
52771 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span…
52772 <span class="hljs-comment">// => 'undefined'</span></code></pre>
52774 …code class="language-js">process.<span class="hljs-property">env</span>.<span class="hljs-property…
52775 <span class="hljs-keyword">delete</span> process.<span class="hljs-property">env</span>.<span class…
52776 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span…
52777 <span class="hljs-comment">// => undefined</span></code></pre>
52778 <p>On Windows operating systems, environment variables are case-insensitive.</p>
52779 …code class="language-js">process.<span class="hljs-property">env</span>.<span class="hljs-property…
52780 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span…
52781 <span class="hljs-comment">// => 1</span></code></pre>
52787 are visible to the operating system or to native add-ons.</p>
52793 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
52795 <p>The <code>process.execArgv</code> property returns the set of Node.js-specific command-line
52801 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
52803 <!-- eslint-disable semi -->
52804 <pre><code class="language-js">[<span class="hljs-string">'--harmony'</span>]</code></pre>
52806 <!-- eslint-disable semi -->
52807 …language-js">[<span class="hljs-string">'/usr/local/bin/node'</span>, <span class="hljs-string">'s…
52815 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
52819 <!-- eslint-disable semi -->
52820 <pre><code class="language-js"><span class="hljs-string">'/usr/local/bin/node'</span></code></pre>
52826 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52834 <pre><code class="language-js">process.<span class="hljs-title function_">exit</span>(<span class="…
52847 <pre><code class="language-js"><span class="hljs-comment">// This is an example of what *not* to do…
52848 <span class="hljs-keyword">if</span> (<span class="hljs-title function_">someConditionNotMet</span>…
52849 <span class="hljs-title function_">printUsageToStdout</span>();
52850 process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">1</span>);
52859 <pre><code class="language-js"><span class="hljs-comment">// How to properly set the exit code whil…
52860 <span class="hljs-comment">// the process exit gracefully.</span>
52861 <span class="hljs-keyword">if</span> (<span class="hljs-title function_">someConditionNotMet</span>…
52862 <span class="hljs-title function_">printUsageToStdout</span>();
52863 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
52875 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
52887 of the Node.js process. (See <a href="http://man7.org/linux/man-pages/man2/getegid.2.html"><code>ge…
52888 <pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-prop…
52889 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
52898 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
52901 the process. (See <a href="http://man7.org/linux/man-pages/man2/geteuid.2.html"><code>geteuid(2)</c…
52902 <pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-prop…
52903 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
52912 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
52915 process. (See <a href="http://man7.org/linux/man-pages/man2/getgid.2.html"><code>getgid(2)</code></…
52916 <pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-prop…
52917 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
52926 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
52931 <pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-prop…
52932-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span cl…
52941 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
52944 (See <a href="http://man7.org/linux/man-pages/man2/getuid.2.html"><code>getuid(2)</code></a>.)</p>
52945 <pre><code class="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-prop…
52946 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
52955 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
52963 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy. Use <a href=…
52965 <li><code>time</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
52966 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
52970 <p>The <code>process.hrtime()</code> method returns the current high-resolution real time
52976 user-defined array instead of the result of a previous call to
52981 … class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-variable constant_"…
52982 <span class="hljs-keyword">const</span> time = process.<span class="hljs-title function_">hrtime</s…
52983 <span class="hljs-comment">// [ 1800216, 25 ]</span>
52985 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
52986 …<span class="hljs-keyword">const</span> diff = process.<span class="hljs-title function_">hrtime</…
52987 <span class="hljs-comment">// [ 1, 552 ]</span>
52989-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
52990 <span class="hljs-comment">// Benchmark took 1000000552 nanoseconds</span>
52991 }, <span class="hljs-number">1000</span>);</code></pre>
52997 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
53000 current high-resolution real time in nanoseconds as a <code>bigint</code>.</p>
53004 … class="language-js"><span class="hljs-keyword">const</span> start = process.<span class="hljs-pro…
53005 <span class="hljs-comment">// 191051479007711n</span>
53007 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
53008 …<span class="hljs-keyword">const</span> end = process.<span class="hljs-property">hrtime</span>.<s…
53009 <span class="hljs-comment">// 191052633396993n</span>
53011-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
53012 <span class="hljs-comment">// Benchmark took 1154389282 nanoseconds</span>
53013 }, <span class="hljs-number">1000</span>);</code></pre>
53019 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53020 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53027-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53028 …"hljs-title function_">initgroups</span>(<span class="hljs-string">'nodeuser'</span>, <span class=…
53029-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span cl…
53030 …ess.<span class="hljs-title function_">setgid</span>(<span class="hljs-number">1000</span>); …
53031-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span cl…
53040 <li><code>pid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
53041 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53047 and <a href="http://man7.org/linux/man-pages/man2/kill.2.html"><code>kill(2)</code></a> for more in…
53048 <p>This method will throw an error if the target <code>pid</code> does not exist. As a special
53054 other than kill the target process.</p>
53055 …e class="language-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-strin…
53056 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
53059 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
53060 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
53061 process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>);
53062 }, <span class="hljs-number">100</span>);
53064 process.<span class="hljs-title function_">kill</span>(process.<span class="hljs-property">pid</spa…
53071 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
53073 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
53097 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
53099 <li><code>rss</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
53100 <li><code>heapTotal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
53101 <li><code>heapUsed</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
53102 <li><code>external</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
53103 <li><code>arrayBuffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
53109 …s="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title funct…
53110 <span class="hljs-comment">// Prints:</span>
53111 <span class="hljs-comment">// {</span>
53112 <span class="hljs-comment">// rss: 4935680,</span>
53113 <span class="hljs-comment">// heapTotal: 1826816,</span>
53114 <span class="hljs-comment">// heapUsed: 650472,</span>
53115 <span class="hljs-comment">// external: 49879,</span>
53116 <span class="hljs-comment">// arrayBuffers: 9386</span>
53117 <span class="hljs-comment">// }</span></code></pre>
53141 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
53150-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53151 <span class="hljs-comment">// 35655680</span></code></pre>
53165 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
53166 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
53172 See the <a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#process-nexttick…
53173 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
53174 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span>…
53175 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
53177 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
53178 <span class="hljs-comment">// Output:</span>
53179 <span class="hljs-comment">// start</span>
53180 <span class="hljs-comment">// scheduled</span>
53181 <span class="hljs-comment">// nextTick callback</span></code></pre>
53185 …de class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function…
53186 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setupOptions<…
53188 …process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span…
53189 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">startDoingStu…
53193 <span class="hljs-keyword">const</span> thing = <span class="hljs-keyword">new</span> <span class="…
53194 thing.<span class="hljs-title function_">getReadyForStuff</span>();
53196 <span class="hljs-comment">// thing.startDoingStuff() gets called now, not before.</span></code></p…
53199 <pre><code class="language-js"><span class="hljs-comment">// WARNING! DO NOT USE! BAD UNSAFE HAZA…
53200 <span class="hljs-keyword">function</span> <span class="hljs-title function_">maybeSync</span>(<spa…
53201 <span class="hljs-keyword">if</span> (arg) {
53202 <span class="hljs-title function_">cb</span>();
53203 <span class="hljs-keyword">return</span>;
53206 fs.<span class="hljs-title function_">stat</span>(<span class="hljs-string">'file'</span>, cb);
53209-js"><span class="hljs-keyword">const</span> maybeTrue = <span class="hljs-title class_">Math</spa…
53211 <span class="hljs-title function_">maybeSync</span>(maybeTrue, <span class="hljs-function">() =></s…
53212 <span class="hljs-title function_">foo</span>();
53215 <span class="hljs-title function_">bar</span>();</code></pre>
53218 …lass="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">d…
53219 <span class="hljs-keyword">if</span> (arg) {
53220 process.<span class="hljs-title function_">nextTick</span>(cb);
53221 <span class="hljs-keyword">return</span>;
53224 fs.<span class="hljs-title function_">stat</span>(<span class="hljs-string">'file'</span>, cb);
53232-js"><span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</sp…
53233-title function_">queueMicrotask</span>(<span class="hljs-function">() =></span> <span class="hljs
53234-title function_">nextTick</span>(<span class="hljs-function">() =></span> <span class="hljs-varia…
53235 <span class="hljs-comment">// Output:</span>
53236 <span class="hljs-comment">// 1</span>
53237 <span class="hljs-comment">// 2</span>
53238 <span class="hljs-comment">// 3</span></code></pre>
53242 In simple scenarios, <code>queueMicrotask()</code> can be a drop-in replacement for
53244 …class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title f…
53245 <span class="hljs-title function_">queueMicrotask</span>(<span class="hljs-function">() =></span> {
53246 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
53248 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
53249 <span class="hljs-comment">// Output:</span>
53250 <span class="hljs-comment">// start</span>
53251 <span class="hljs-comment">// scheduled</span>
53252 <span class="hljs-comment">// microtask callback</span></code></pre>
53253 <p>One note-worthy difference between the two APIs is that <code>process.nextTick()</code>
53257 …de class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function…
53258 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
53261 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
53262-title function_">queueMicrotask</span>(deferred.<span class="hljs-title function_">bind</span>(<s…
53263 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
53264 <span class="hljs-comment">// Output:</span>
53265 <span class="hljs-comment">// start</span>
53266 <span class="hljs-comment">// scheduled</span>
53267 <span class="hljs-comment">// microtask 3</span></code></pre>
53280 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53282 <p>The <code>process.noDeprecation</code> property indicates whether the <code>--no-deprecation</co…
53292 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
53295-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53301 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
53315-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53318 …s://github.com/nodejs/node/blob/HEAD/BUILDING.md#androidandroid-based-devices-eg-firefox-os">is ex…
53324 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
53328-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53342 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
53345 to the current release, including URLs for the source tarball and headers-only
53349 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
53350 <li><code>sourceUrl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
53352 <li><code>headersUrl</code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
53355 Node.js native add-ons.</li>
53356 <li><code>libUrl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
53358 compiling Node.js native add-ons. <em>This property is only present on Windows
53360 <li><code>lts</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
53371 <!-- eslint-skip -->
53372 <pre><code class="language-js">{
53373 <span class="hljs-attr">name</span>: <span class="hljs-string">'node'</span>,
53374 <span class="hljs-attr">lts</span>: <span class="hljs-string">'Erbium'</span>,
53375 …<span class="hljs-attr">sourceUrl</span>: <span class="hljs-string">'https://nodejs.org/download/r…
53376 …<span class="hljs-attr">headersUrl</span>: <span class="hljs-string">'https://nodejs.org/download/…
53377 …<span class="hljs-attr">libUrl</span>: <span class="hljs-string">'https://nodejs.org/download/rele…
53379 <p>In custom builds from non-release versions of the source tree, only the
53395 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
53405 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53407 <p>Write reports in a compact format, single-line JSON, more easily consumable
53408 by log processing systems than the default multi-line format designed for
53410-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53424 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
53429-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53443 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
53448-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53462 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
53463 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
53468 … class="language-js"><span class="hljs-keyword">const</span> data = process.<span class="hljs-prop…
53469 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data.<span cl…
53471 <span class="hljs-comment">// Similar to process.report.writeReport()</span>
53472 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
53473-title function_">writeFileSync</span>(<span class="hljs-string">'my-report.log'</span>, util.<spa…
53488 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53492-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53506 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53510-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53524 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53527-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53541 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
53545-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
53560 <p><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
53566 <p><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
53569 <p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String…
53575 <pre><code class="language-js">process.<span class="hljs-property">report</span>.<span class="hljs-
53582 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
53586 <li><code>userCPUTime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
53588 <li><code>systemCPUTime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
53590 <li><code>maxRSS</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
53592 <li><code>sharedMemorySize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
53594 <li><code>unsharedDataSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
53596 <li><code>unsharedStackSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
53598 <li><code>minorPageFault</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
53601 <li><code>majorPageFault</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
53605 <li><code>swappedOut</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
53607 <li><code>fsRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
53609 <li><code>fsWrite</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
53611 <li><code>ipcSent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
53613 <li><code>ipcReceived</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
53615 <li><code>signalsCount</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
53617 <li><code>voluntaryContextSwitches</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
53621 <li><code>involuntaryContextSwitches</code> <a href="https://developer.mozilla.org/en-US/docs/Web/J…
53628 …s="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title funct…
53629 <span class="hljs-comment">/*
53655 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
53657 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
53660 <li><code>keepOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
53665 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
53666 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
53680 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53683 (See <a href="http://man7.org/linux/man-pages/man2/setegid.2.html"><code>setegid(2)</code></a>.) Th…
53686 …="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getegid</…
53687 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
53688 <span class="hljs-keyword">try</span> {
53689 process.<span class="hljs-title function_">setegid</span>(<span class="hljs-number">501</span>);
53690 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
53691 } <span class="hljs-keyword">catch</span> (err) {
53692 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
53703 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53706 (See <a href="http://man7.org/linux/man-pages/man2/seteuid.2.html"><code>seteuid(2)</code></a>.) Th…
53709 …="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">geteuid</…
53710 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
53711 <span class="hljs-keyword">try</span> {
53712 process.<span class="hljs-title function_">seteuid</span>(<span class="hljs-number">501</span>);
53713 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
53714 } <span class="hljs-keyword">catch</span> (err) {
53715 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
53726 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
53729 <a href="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>.) The <code…
53732 …="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getgid</s…
53733 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
53734 <span class="hljs-keyword">try</span> {
53735 process.<span class="hljs-title function_">setgid</span>(<span class="hljs-number">501</span>);
53736 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
53737 } <span class="hljs-keyword">catch</span> (err) {
53738 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
53749 <li><code>groups</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
53755 …"language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getgroups<…
53756 <span class="hljs-keyword">try</span> {
53757 …process.<span class="hljs-title function_">setgroups</span>([<span class="hljs-number">501</span>]…
53758-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span cl…
53759 } <span class="hljs-keyword">catch</span> (err) {
53760 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
53771 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> |…
53774 <a href="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>.) The <code…
53777 …="language-js"><span class="hljs-keyword">if</span> (process.<span class="hljs-property">getuid</s…
53778 …="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span clas…
53779 <span class="hljs-keyword">try</span> {
53780 process.<span class="hljs-title function_">setuid</span>(<span class="hljs-number">501</span>);
53781 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
53782 } <span class="hljs-keyword">catch</span> (err) {
53783 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="…
53793 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
53795 <li><code>val</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
53800 <code>--enable-source-maps</code>.</p>
53808 …a.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a…
53814 not be emitted. If <code>--abort-on-uncaught-exception</code> was passed from the
53820 method with a non-<code>null</code> argument while another capture function is set will
53823 <a href="domain.html"><code>domain</code></a> built-in module.</p>
53836 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
53858 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
53872 …ass="language-js">process.<span class="hljs-property">stdin</span>.<span class="hljs-title functio…
53877 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
53914 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
53916 …s-meta">$ </span><span class="language-bash"><span class="hljs-built_in">echo</span> <span class="…
53918 <span class="hljs-meta">$ </span><span class="language-bash">node -p <span class="hljs-string">"Boo…
53920 <span class="hljs-meta">$ </span><span class="language-bash">node -p <span class="hljs-string">"Boo…
53928 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53931 <code>--throw-deprecation</code> flag is set on the current Node.js process.
53936 …de class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node --th…
53938 <span class="hljs-meta">$ </span><span class="language-bash">node -p <span class="hljs-string">"pro…
53940 <span class="hljs-meta">$ </span><span class="language-bash">node</span>
53941 …pan class="hljs-meta">> </span><span class="language-bash">process.emitWarning(<span class="hljs-s…
53943 <span class="hljs-meta">> </span><span class="language-bash">(node:26598) DeprecationWarning: <span…
53944 <span class="hljs-meta">> </span><span class="language-bash">process.throwDeprecation = <span class…
53946 …pan class="hljs-meta">> </span><span class="language-bash">process.emitWarning(<span class="hljs-s…
53954 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
53962 binary name plus the length of the command-line arguments because setting the
53975 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
53978 <code>--trace-deprecation</code> flag is set on the current Node.js process. See the
53994 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated. Calling …
53995 the process-wide umask to be written twice. This introduces a race condition
53997 cross-platform alternative API.</div><p></p>
54005 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
54009 <pre><code class="language-js"><span class="hljs-keyword">const</span> newmask = <span class="hljs-
54010 <span class="hljs-keyword">const</span> oldmask = process.<span class="hljs-title function_">umask<…
54011 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
54012-string">`Changed umask from <span class="hljs-subst">${oldmask.toString(<span class="hljs-number"…
54020 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
54031 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
54034-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
54035 <span class="hljs-comment">// Version: v14.8.0</span></code></pre>
54053 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
54059 …s="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title funct…
54061 <pre><code class="language-console">{ node: '11.13.0',
54062 v8: '7.0.276.38-node.18',
54095 <li><code>6</code> <strong>Non-function Internal Exception Handler</strong>: There was an
54097 function was somehow set to a non-function, and could not be called.</li>
54098 <li><code>7</code> <strong>Internal Exception Handler Run-Time Failure</strong>: There was an
54107 <li><code>10</code> <strong>Internal JavaScript Run-Time Failure</strong>: The JavaScript
54111 …ode> <strong>Invalid Debug Argument</strong>: The <code>--inspect</code> and/or <code>--inspect-br…
54113 <li><code>13</code> <strong>Unfinished Top-Level Await</strong>: <code>await</code> was used outsid…
54114 in the top-level code, but the passed <code>Promise</code> never resolved.</li>
54118 exit codes are defined to be 7-bit integers, and signal exits set
54119 the high-order bit, and then contain the value of the signal code.
54129 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
54134 userland-provided <a href="https://github.com/bestiejs/punycode.js">Punycode.js</a> module instead.…
54139 …de class="language-js"><span class="hljs-keyword">const</span> punycode = <span class="hljs-built_…
54143 non-ASCII characters must be converted into ASCII using the Punycode scheme.
54147 <code>'xn--fsq.com'</code>.</p>
54149 <p>The <code>punycode</code> module is a third-party dependency used by Node.js and
54157 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54159 …> method converts a <a href="https://tools.ietf.org/html/rfc3492">Punycode</a> string of ASCII-only
54161 …ss="language-js">punycode.<span class="hljs-title function_">decode</span>(<span class="hljs-strin…
54162 …ode.<span class="hljs-title function_">decode</span>(<span class="hljs-string">'--dqo34k'</span>);…
54168 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54171 <a href="https://tools.ietf.org/html/rfc3492">Punycode</a> string of ASCII-only characters.</p>
54172 …uage-js">punycode.<span class="hljs-title function_">encode</span>(<span class="hljs-string">'maña…
54173 …ycode.<span class="hljs-title function_">encode</span>(<span class="hljs-string">'☃-⌘'</span>); <s…
54179 <li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54182 …Name to <a href="https://tools.ietf.org/html/rfc3492">Punycode</a>. Only the non-ASCII parts of the
54185 <pre><code class="language-js"><span class="hljs-comment">// encode domain names</span>
54186 …pan class="hljs-title function_">toASCII</span>(<span class="hljs-string">'mañana.com'</span>); <…
54187 ….<span class="hljs-title function_">toASCII</span>(<span class="hljs-string">'☃-⌘.com'</span>); …
54188 punycode.<span class="hljs-title function_">toASCII</span>(<span class="hljs-string">'example.com'<…
54194 <li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54199 <pre><code class="language-js"><span class="hljs-comment">// decode domain names</span>
54200 …code.<span class="hljs-title function_">toUnicode</span>(<span class="hljs-string">'xn--maana-pta.…
54201 …pan class="hljs-title function_">toUnicode</span>(<span class="hljs-string">'xn----dqo34k.com'</sp…
54202 …code.<span class="hljs-title function_">toUnicode</span>(<span class="hljs-string">'example.com'</…
54212 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54216-js">punycode.<span class="hljs-property">ucs2</span>.<span class="hljs-title function_">decode</s…
54217 <span class="hljs-comment">// surrogate pair for U+1D306 tetragram for centre:</span>
54218 …="hljs-property">ucs2</span>.<span class="hljs-title function_">decode</span>(<span class="hljs-st…
54224 <li><code>codePoints</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
54228-js">punycode.<span class="hljs-property">ucs2</span>.<span class="hljs-title function_">encode</s…
54229 …ass="hljs-property">ucs2</span>.<span class="hljs-title function_">encode</span>([<span class="hlj…
54235 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
54241 …ty api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy</div><p></p>
54246 … class="language-js"><span class="hljs-keyword">const</span> querystring = <span class="hljs-built…
54264 <li><code>str</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
54266 <p>The <code>querystring.escape()</code> method performs URL percent-encoding on the given
54271 application code to provide a replacement percent-encoding implementation if
54290 <li><code>str</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
54291 <li><code>sep</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
54293 <li><code>eq</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
54295 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
54297 <li><code>decodeURIComponent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
54298 percent-encoded characters in the query string. <strong>Default:</strong>
54300 <li><code>maxKeys</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
54308 <!-- eslint-skip -->
54309 <pre><code class="language-js">{
54310 <span class="hljs-attr">foo</span>: <span class="hljs-string">'bar'</span>,
54311 …<span class="hljs-attr">abc</span>: [<span class="hljs-string">'xyz'</span>, <span class="hljs-str…
54317 <p>By default, percent-encoded characters within the query string will be assumed
54318 to use UTF-8 encoding. If an alternative character encoding is used, then an
54320 <pre><code class="language-js"><span class="hljs-comment">// Assuming gbkDecodeURIComponent functio…
54322-title function_">parse</span>(<span class="hljs-string">'w=%D6%D0%CE%C4&#x26;foo=bar'</span>, <sp…
54323 … { <span class="hljs-attr">decodeURIComponent</span>: gbkDecodeURIComponent });</code></pre>
54329 <li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
54330 <li><code>sep</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
54332 <li><code>eq</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
54336 <li><code>encodeURIComponent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
54337 URL-unsafe characters to percent-encoding in the query string. <strong>Default:</strong>
54345-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
54348-js">querystring.<span class="hljs-title function_">stringify</span>({ <span class="hljs-attr">foo…
54349 <span class="hljs-comment">// Returns 'foo=bar&#x26;baz=qux&#x26;baz=quux&#x26;corge='</span>
54351-title function_">stringify</span>({ <span class="hljs-attr">foo</span>: <span class="hljs-string"…
54352 <span class="hljs-comment">// Returns 'foo:bar;baz:qux'</span></code></pre>
54353 <p>By default, characters requiring percent-encoding within the query string will
54354 be encoded as UTF-8. If an alternative encoding is required, then an alternative
54356 <pre><code class="language-js"><span class="hljs-comment">// Assuming gbkEncodeURIComponent functio…
54358-title function_">stringify</span>({ <span class="hljs-attr">w</span>: <span class="hljs-string">'…
54359 … { <span class="hljs-attr">encodeURIComponent</span>: gbkEncodeURIComponent });</code></pre>
54365 <li><code>str</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
54367 <p>The <code>querystring.unescape()</code> method performs decoding of URL percent-encoded
54374 JavaScript built-in <code>decodeURIComponent()</code> method to decode. If that fails,
54379 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
54384 …de class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_…
54386 …de class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_…
54388 <span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInte…
54389 <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>,
54390 <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>
54393 …js-title function_">question</span>(<span class="hljs-string">'What do you think of Node.js? '</sp…
54394 …<span class="hljs-comment">// <span class="hljs-doctag">TODO:</span> Log the answer in a database<…
54395-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
54397 rl.<span class="hljs-title function_">close</span>();
54411 single <code>input</code> <a href="#stream_readable_streams">Readable</a> stream and a single <code…
54424 end-of-transmission (EOT);</li>
54437 end-of-line input (<code>\n</code>, <code>\r</code>, or <code>\r\n</code>). This usually occurs whe…
54439 <p>The listener function is called with a string containing the single line of
54441 …nguage-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>…
54442 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
54456 …uage-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'history'</span…
54457 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
54470 …code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string"…
54471 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
54479 …ode class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">…
54480 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
54488 brought back to the foreground using <a href="http://man7.org/linux/man-pages/man1/fg.1p.html"><cod…
54492 …ode class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">…
54493 <span class="hljs-comment">// `prompt` will automatically resume the stream</span>
54494 rl.<span class="hljs-title function_">prompt</span>();
54506 …ode class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">…
54507 …js-title function_">question</span>(<span class="hljs-string">'Are you sure you want to exit? '</s…
54508 …hljs-keyword">if</span> (answer.<span class="hljs-title function_">match</span>(<span class="hljs-
54519 <p>When the program is resumed using <a href="http://man7.org/linux/man-pages/man1/fg.1p.html"><cod…
54524 …ode class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">…
54525 …<span class="hljs-comment">// This will override SIGTSTP and prevent the program from going to the…
54526 <span class="hljs-comment">// background.</span>
54527 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
54552 <li><code>preserveCursor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
54567 <li><code>query</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
54569 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
54575 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
54589-js">rl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What is your…
54590-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
54593 …e><code class="language-js"><span class="hljs-keyword">const</span> ac = <span class="hljs-keyword…
54594 <span class="hljs-keyword">const</span> signal = ac.<span class="hljs-property">signal</span>;
54596-title function_">question</span>(<span class="hljs-string">'What is your favorite food? '</span>,…
54597-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
54600 signal.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'abort'…
54601 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
54602 }, { <span class="hljs-attr">once</span>: <span class="hljs-literal">true</span> });
54604 …"hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> ac.<span class="hljs-ti…
54608 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
54609-keyword">const</span> question = util.<span class="hljs-title function_">promisify</span>(rl.<spa…
54611 …"hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title fun…
54612 <span class="hljs-keyword">try</span> {
54613 …="hljs-keyword">const</span> answer = <span class="hljs-keyword">await</span> <span class="hljs-ti…
54614-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
54615 } <span class="hljs-keyword">catch</span> (err) {
54616 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
54619 <span class="hljs-title function_">questionExample</span>();</code></pre>
54630 <li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54639 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
54647 <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
54648 <li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
54650 <li><code>ctrl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
54651 <li><code>meta</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
54652 <li><code>shift</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
54653 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
54666 <pre><code class="language-js">rl.<span class="hljs-title function_">write</span>(<span class="hljs
54667 <span class="hljs-comment">// Simulate Ctrl+U to delete the line written previously</span>
54668-title function_">write</span>(<span class="hljs-literal">null</span>, { <span class="hljs-attr">c…
54684 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x…
54694 instead for performance-sensitive applications.</p>
54695-js"><span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span clas…
54696 …<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInt…
54697 <span class="hljs-comment">// ...</span>
54700 …an class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyw…
54701 …<span class="hljs-comment">// Each line in the readline input will be successively available here …
54702 <span class="hljs-comment">// `line`.</span>
54721 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
54732 … class="language-js"><span class="hljs-keyword">const</span> values = [<span class="hljs-string">'…
54733 …span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInte…
54734 <span class="hljs-keyword">const</span> showResults = <span class="hljs-title function_">debounce</…
54735 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
54736 <span class="hljs-string">'\n'</span>,
54737-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">val</span>)…
54739 }, <span class="hljs-number">300</span>);
54740-property">stdin</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'ke…
54741 <span class="hljs-title function_">showResults</span>();
54748 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | …
54760 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
54762 <li><code>rows</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
54763 <li><code>cols</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
54784 <li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
54786 <li><code>-1</code>: to the left from cursor</li>
54791 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
54792 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
54812 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
54813 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
54844 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
54850 <li><code>completer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
54851 <li><code>terminal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
54854 <li><code>history</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
54858 <li><code>historySize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
54863 <li><code>removeHistoryDuplicates</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
54866 <li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
54867 <li><code>crlfDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
54869 end-of-line input. <code>crlfDelay</code> will be coerced to a number no less than
54871 will always be considered a single newline (which may be reasonable for
54873 <li><code>escapeCodeTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
54878 <li><code>tabSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
54888 …de class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_…
54889 <span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInte…
54890 <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>,
54891 <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>
54895 …nguage-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>…
54896 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
54908 <pre><code class="language-js">process.<span class="hljs-property">stdin</span>.<span class="hljs-t…
54917 …e class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_…
54918-keyword">const</span> completions = <span class="hljs-string">'.help .error .exit .quit .q'</span…
54919-keyword">const</span> hits = completions.<span class="hljs-title function_">filter</span>(<span c…
54920 <span class="hljs-comment">// Show all completions if none found</span>
54921 …<span class="hljs-keyword">return</span> [hits.<span class="hljs-property">length</span> ? hits : …
54925 …e class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_…
54926 …<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, [[<span…
54942 <li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
54943 <li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
54944 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
54945 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
54962 autocompletion is disabled when copy-pasted input is detected.</p>
54967 <pre><code class="language-js">readline.<span class="hljs-title function_">emitKeypressEvents</span…
54968 <span class="hljs-keyword">if</span> (process.<span class="hljs-property">stdin</span>.<span class=…
54969 …process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">setRawMode</sp…
54984 <li><code>dx</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
54985 <li><code>dy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
54986 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
54987 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
54995 implement a small command-line interface:</p>
54996 …de class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_…
54997 <span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInte…
54998 <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>,
54999 <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>,
55000 <span class="hljs-attr">prompt</span>: <span class="hljs-string">'OHAI> '</span>
55003 rl.<span class="hljs-title function_">prompt</span>();
55005 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class…
55006 <span class="hljs-keyword">switch</span> (line.<span class="hljs-title function_">trim</span>()) {
55007 <span class="hljs-keyword">case</span> <span class="hljs-string">'hello'</span>:
55008 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
55009 <span class="hljs-keyword">break</span>;
55010 <span class="hljs-attr">default</span>:
55011-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
55012 <span class="hljs-keyword">break</span>;
55014 rl.<span class="hljs-title function_">prompt</span>();
55015 }).<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span cla…
55016 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
55017 process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>);
55019 </section><section><h3>Example: Read file stream line-by-Line<span><a class="mark" href="#readline_…
55023 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
55024 <span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span…
55026 …hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title func…
55027 …<span class="hljs-keyword">const</span> fileStream = fs.<span class="hljs-title function_">createR…
55029 …<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInt…
55030 <span class="hljs-attr">input</span>: fileStream,
55031 <span class="hljs-attr">crlfDelay</span>: <span class="hljs-title class_">Infinity</span>
55033 …<span class="hljs-comment">// Note: we use the crlfDelay option to recognize all instances of CR L…
55034 <span class="hljs-comment">// ('\r\n') in input.txt as a single line break.</span>
55036 …an class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyw…
55037 …<span class="hljs-comment">// Each line in input.txt will be successively available here as `line`…
55038 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
55042 <span class="hljs-title function_">processLineByLine</span>();</code></pre>
55044 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
55045 <span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span…
55047 <span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInte…
55048 …<span class="hljs-attr">input</span>: fs.<span class="hljs-title function_">createReadStream</span…
55049 <span class="hljs-attr">crlfDelay</span>: <span class="hljs-title class_">Infinity</span>
55052 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class…
55053 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
55057 …de class="language-js"><span class="hljs-keyword">const</span> { once } = <span class="hljs-built_…
55058 <span class="hljs-keyword">const</span> { createReadStream } = <span class="hljs-built_in">require<…
55059 <span class="hljs-keyword">const</span> { createInterface } = <span class="hljs-built_in">require</…
55061 …hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title func…
55062 <span class="hljs-keyword">try</span> {
55063 …<span class="hljs-keyword">const</span> rl = <span class="hljs-title function_">createInterface</s…
55064 …<span class="hljs-attr">input</span>: <span class="hljs-title function_">createReadStream</span>(<…
55065 <span class="hljs-attr">crlfDelay</span>: <span class="hljs-title class_">Infinity</span>
55068 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class…
55069 <span class="hljs-comment">// Process the line.</span>
55072 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(rl, <span c…
55074 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
55075 } <span class="hljs-keyword">catch</span> (err) {
55076 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
55205 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
55207 <p>The <code>repl</code> module provides a Read-Eval-Print-Loop (REPL) implementation that
55210 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55214 evaluate those according to a user-defined evaluation function, then output the
55218 completion preview, simplistic Emacs-style line editing, multi-line inputs,
55219 …dia.org/wiki/Z_shell">ZSH</a>-like reverse-i-search, <a href="https://en.wikipedia.org/wiki/Z_shel…
55220 ANSI-styled output, saving and restoring current REPL session state, error
55222 ANSI styles and Emacs-style line editing automatically fall back to a limited
55227 <li><code>.break</code>: When in the process of inputting a multi-line expression, enter
55231 multi-line expression being input.</li>
55241 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">.e…
55251 <span class="hljs-meta">></span></code></pre>
55263 <p>For key bindings related to the reverse-i-search, see <a href="#repl_reverse_i_search"><code>rev…
55267 that evaluates JavaScript expressions and provides access to Node.js built-in
55272 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">1 …
55274 <span class="hljs-meta">> </span><span class="language-bash">const m = 2</span>
55276 <span class="hljs-meta">> </span><span class="language-bash">m + 1</span>
55285 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55286 <span class="hljs-keyword">const</span> msg = <span class="hljs-string">'message'</span>;
55288 …class="hljs-title function_">start</span>(<span class="hljs-string">'> '</span>).<span class="hljs
55290 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
55291 <span class="hljs-meta">> </span><span class="language-bash">m</span>
55293 <p>Context properties are not read-only by default. To specify read-only globals,
55295 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55296 <span class="hljs-keyword">const</span> msg = <span class="hljs-string">'message'</span>;
55298 <span class="hljs-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>(<s…
55299 …s-title class_">Object</span>.<span class="hljs-title function_">defineProperty</span>(r.<span cla…
55300 <span class="hljs-attr">configurable</span>: <span class="hljs-literal">false</span>,
55301 <span class="hljs-attr">enumerable</span>: <span class="hljs-literal">true</span>,
55302 <span class="hljs-attr">value</span>: msg
55307 global or scoped variable, the input <code>fs</code> will be evaluated on-demand as
55309 …><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">fs.cr…
55328 <pre><code class="language-js"><span class="hljs-keyword">const</span> r = repl.<span class="hljs-t…
55330 r.<span class="hljs-title function_">write</span>(<span class="hljs-string">'process.on("uncaughtEx…
55331 <span class="hljs-comment">// Output stream includes:</span>
55332 <span class="hljs-comment">// TypeError [ERR_INVALID_REPL_INPUT]: Listeners for `uncaughtExceptio…
55333 <span class="hljs-comment">// cannot be used in the REPL</span>
55335 r.<span class="hljs-title function_">close</span>();</code></pre>
55355-console"><span class="hljs-meta">> </span><span class="language-bash">[ <span class="hljs-string"…
55357 <span class="hljs-meta">> </span><span class="language-bash">_.length</span>
55359 <span class="hljs-meta">> </span><span class="language-bash">_ += 1</span>
55362 <span class="hljs-meta">> </span><span class="language-bash">1 + 1</span>
55364 <span class="hljs-meta">> </span><span class="language-bash">_</span>
55368 …re><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">thr…
55370 <span class="hljs-meta">> </span><span class="language-bash">_error.message</span>
55373 …With the <a href="#cli_experimental_repl_await"><code>--experimental-repl-await</code></a> command
55375 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">aw…
55377 <span class="hljs-meta">> </span><span class="language-bash">await Promise.reject(new Error(<span c…
55380 <span class="hljs-meta">> </span><span class="language-bash">const timeout = util.promisify(setTime…
55382 <span class="hljs-meta">> </span><span class="language-bash">const old = Date.now(); await timeout(…
55389 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">co…
55391 <span class="hljs-meta">> </span><span class="language-bash">m</span>
55393 <span class="hljs-meta">> </span><span class="language-bash">const m = await Promise.resolve(234)</…
55395 <span class="hljs-meta">> </span><span class="language-bash">m</span>
55397 <h4>Reverse-i-search<span><a class="mark" href="#repl_reverse_i_search" id="repl_reverse_i_search">…
55401 <p>The REPL supports bi-directional reverse-i-search similar to <a href="https://en.wikipedia.org/w…
55417 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55418 …="hljs-keyword">const</span> { <span class="hljs-title class_">Translator</span> } = <span class="…
55420-keyword">const</span> myTranslator = <span class="hljs-keyword">new</span> <span class="hljs-titl…
55422 <span class="hljs-keyword">function</span> <span class="hljs-title function_">myEval</span>(<span c…
55423 …<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, myTrans…
55426 … class="hljs-title function_">start</span>({ <span class="hljs-attr">prompt</span>: <span class="h…
55429 the <code>eval</code> function. In order to support multi-line input, the <code>eval</code> function
55431 …ode class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title functio…
55432 <span class="hljs-keyword">let</span> result;
55433 <span class="hljs-keyword">try</span> {
55434 result = vm.<span class="hljs-title function_">runInThisContext</span>(cmd);
55435 } <span class="hljs-keyword">catch</span> (e) {
55436 …<span class="hljs-keyword">if</span> (<span class="hljs-title function_">isRecoverableError</span>…
55437 …ss="hljs-keyword">return</span> <span class="hljs-title function_">callback</span>(<span class="hl…
55440 …<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, result);
55443 <span class="hljs-keyword">function</span> <span class="hljs-title function_">isRecoverableError</s…
55444 …<span class="hljs-keyword">if</span> (error.<span class="hljs-property">name</span> === <span clas…
55445-keyword">return</span> <span class="hljs-regexp">/^(Unexpected end of input|Unexpected token)/</s…
55447 <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
55462 …lass="language-console"><span class="hljs-meta">> </span><span class="language-bash">util.inspect.…
55464 <span class="hljs-meta">> </span><span class="language-bash">[1]</span>
55468 <span class="hljs-meta">></span></code></pre>
55472 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55474-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>({ <span class="hlj…
55476 <span class="hljs-keyword">function</span> <span class="hljs-title function_">myEval</span>(<span c…
55477 <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, cmd);
55480 <span class="hljs-keyword">function</span> <span class="hljs-title function_">myWriter</span>(<span…
55481 …<span class="hljs-keyword">return</span> output.<span class="hljs-title function_">toUpperCase</sp…
55488 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
55493 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55495 <span class="hljs-keyword">const</span> options = { <span class="hljs-attr">useColors</span>: <span…
55497 <span class="hljs-keyword">const</span> firstInstance = repl.<span class="hljs-title function_">sta…
55498 <span class="hljs-keyword">const</span> secondInstance = <span class="hljs-keyword">new</span> repl…
55509 … class="language-js">replServer.<span class="hljs-title function_">on</span>(<span class="hljs-str…
55510 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
55511 process.<span class="hljs-title function_">exit</span>();
55522 <p>This can be used primarily to re-initialize REPL context to some pre-defined
55524 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55526 <span class="hljs-keyword">function</span> <span class="hljs-title function_">initializeContext</sp…
55527 context.<span class="hljs-property">m</span> = <span class="hljs-string">'test'</span>;
55530 …s="hljs-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>({ <span cla…
55531 <span class="hljs-title function_">initializeContext</span>(r.<span class="hljs-property">context</…
55533 r.<span class="hljs-title function_">on</span>(<span class="hljs-string">'reset'</span>, initialize…
55536 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">./…
55537 <span class="hljs-meta">> </span><span class="language-bash">m</span>
55539 <span class="hljs-meta">> </span><span class="language-bash">m = 1</span>
55541 <span class="hljs-meta">> </span><span class="language-bash">m</span>
55543 <span class="hljs-meta">> </span><span class="language-bash">.clear</span>
55545 <span class="hljs-meta">> </span><span class="language-bash">m</span>
55547 <span class="hljs-meta">></span></code></pre>
55553 <li><code>keyword</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
55554 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
55556 <p>The <code>replServer.defineCommand()</code> method is used to add new <code>.</code>-prefixed co…
55561 <li><code>help</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
55562 …/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The…
55566 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55568 …js-keyword">const</span> replServer = repl.<span class="hljs-title function_">start</span>({ <span…
55569 replServer.<span class="hljs-title function_">defineCommand</span>(<span class="hljs-string">'sayhe…
55570 <span class="hljs-attr">help</span>: <span class="hljs-string">'Say hello'</span>,
55571 <span class="hljs-title function_">action</span>(<span class="hljs-params">name</span>) {
55572 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">clearBuffered…
55573 …ass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span c…
55574 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">displayPrompt…
55577-title function_">defineCommand</span>(<span class="hljs-string">'saybye'</span>, <span class="hlj…
55578 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
55579 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">close</span>(…
55582 <pre><code class="language-console"><span class="hljs-meta">> </span><span class="language-bash">.s…
55584 <span class="hljs-meta">> </span><span class="language-bash">.saybye</span>
55591 <li><code>preserveCursor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
55596 <p>When multi-line input is being entered, an ellipsis is printed rather than the
55614 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated.</div><p>…
55616 <li><code>keyword</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
55617 <li><code>rest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
55618 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
55627 <li><code>historyPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
55628 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
55630 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
55636 Node.js binary and using the command-line REPL, a history file is initialized
55645 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
55669 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
55671 <li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
55677 <li><code>terminal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
55681 <li><code>eval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
55685 <li><code>useColors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
55690 <li><code>useGlobal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
55694 <li><code>ignoreUndefined</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
55697 <li><code>writer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
55699 <li><code>completer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
55701 <li><code>replMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
55710 <li><code>breakEvalOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
55714 <li><code>preview</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
55724 …code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in…
55726 <span class="hljs-comment">// a Unix style prompt</span>
55727 repl.<span class="hljs-title function_">start</span>(<span class="hljs-string">'$ '</span>);</code>…
55731 without passing any arguments (or by passing the <code>-i</code> argument):</p>
55732 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
55733 <span class="hljs-meta">> </span><span class="language-bash">const a = [1, 2, 3];</span>
55735 <span class="hljs-meta">> </span><span class="language-bash">a</span>
55737 <span class="hljs-meta">> </span><span class="language-bash">a.forEach((v) => {</span>
55757 <code>'sloppy'</code>, which will allow non-strict mode code to be run.</li>
55764 <h4>Using the Node.js REPL with advanced line-editors<span><a class="mark" href="#repl_using_the_no…
55765 <p>For advanced line-editors, start Node.js with the environment variable
55769 <pre><code class="language-text">alias node="env NODE_NO_READLINE=1 rlwrap node"</code></pre>
55770 <h4>Starting multiple REPL instances against a single running instance<span><a class="mark" href="#…
55771 <p>It is possible to create and run multiple REPL instances against a single
55772 running instance of Node.js that share a single <code>global</code> object but have
55776 …code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in"…
55777 <span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span cla…
55778 <span class="hljs-keyword">let</span> connections = <span class="hljs-number">0</span>;
55780 repl.<span class="hljs-title function_">start</span>({
55781 <span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via stdin> '</span>,
55782 <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>,
55783 <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>
55786 net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class…
55787 connections += <span class="hljs-number">1</span>;
55788 repl.<span class="hljs-title function_">start</span>({
55789 …<span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via Unix socket> '</span…
55790 <span class="hljs-attr">input</span>: socket,
55791 <span class="hljs-attr">output</span>: socket
55792 …}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span cla…
55793 socket.<span class="hljs-title function_">end</span>();
55795 }).<span class="hljs-title function_">listen</span>(<span class="hljs-string">'/tmp/node-repl-sock'…
55797 net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class…
55798 connections += <span class="hljs-number">1</span>;
55799 repl.<span class="hljs-title function_">start</span>({
55800 …<span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via TCP socket> '</span>,
55801 <span class="hljs-attr">input</span>: socket,
55802 <span class="hljs-attr">output</span>: socket
55803 …}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span cla…
55804 socket.<span class="hljs-title function_">end</span>();
55806 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">5001</span>);</code><…
55811 <p>By starting a REPL from a Unix socket-based server instead of stdin, it is
55812 possible to connect to a long-running Node.js process without restarting it.</p>
55813 <p>For an example of running a "full-featured" (<code>terminal</code>) REPL over
55822 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
55824 <p>Delivers a JSON-formatted diagnostic summary, written to a file.</p>
55833 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
55834 …<span class="hljs-attr">"header"</span><span class="hljs-punctuation">:</span> <span class="hljs-p…
55835 …span class="hljs-attr">"reportVersion"</span><span class="hljs-punctuation">:</span> <span class="…
55836 …pan class="hljs-attr">"event"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
55837 …an class="hljs-attr">"trigger"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55838-attr">"filename"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"report.…
55839 …"hljs-attr">"dumpEventTime"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
55840 …="hljs-attr">"dumpEventTimeStamp"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55841 …span class="hljs-attr">"processId"</span><span class="hljs-punctuation">:</span> <span class="hljs
55842 …s="hljs-attr">"cwd"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/home…
55843 …<span class="hljs-attr">"commandLine"</span><span class="hljs-punctuation">:</span> <span class="h…
55844 …<span class="hljs-string">"/home/nodeuser/project/node/out/Release/node"</span><span class="hljs-p…
55845 …<span class="hljs-string">"--report-uncaught-exception"</span><span class="hljs-punctuation">,</sp…
55846 …<span class="hljs-string">"/home/nodeuser/project/node/test/report/test-exception.js"</span><span …
55847 <span class="hljs-string">"child"</span>
55848 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
55849 …lass="hljs-attr">"nodejsVersion"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55850 …lass="hljs-attr">"glibcVersionRuntime"</span><span class="hljs-punctuation">:</span> <span class="…
55851 …lass="hljs-attr">"glibcVersionCompiler"</span><span class="hljs-punctuation">:</span> <span class=…
55852 …pan class="hljs-attr">"wordSize"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55853 …<span class="hljs-attr">"arch"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55854 …pan class="hljs-attr">"platform"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55855 …<span class="hljs-attr">"componentVersions"</span><span class="hljs-punctuation">:</span> <span cl…
55856 …pan class="hljs-attr">"node"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
55857 … class="hljs-attr">"v8"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"7…
55858 …<span class="hljs-attr">"uv"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
55859 …<span class="hljs-attr">"zlib"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55860 …<span class="hljs-attr">"ares"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55861 …<span class="hljs-attr">"modules"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55862 …pan class="hljs-attr">"nghttp2"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
55863 …<span class="hljs-attr">"napi"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55864 …span class="hljs-attr">"llhttp"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
55865 …<span class="hljs-attr">"openssl"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55866 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55867 …<span class="hljs-attr">"release"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55868 …<span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55869 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55870 …span class="hljs-attr">"osName"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
55871 …s="hljs-attr">"osRelease"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
55872-attr">"osVersion"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"#1 SMP…
55873 …an class="hljs-attr">"osMachine"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55874 …<span class="hljs-attr">"cpus"</span><span class="hljs-punctuation">:</span> <span class="hljs-pun…
55875 <span class="hljs-punctuation">{</span>
55876-attr">"model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Intel(R) C…
55877 …<span class="hljs-attr">"speed"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
55878 …<span class="hljs-attr">"user"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55879 …<span class="hljs-attr">"nice"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55880 …<span class="hljs-attr">"sys"</span><span class="hljs-punctuation">:</span> <span class="hljs-numb…
55881 …span class="hljs-attr">"idle"</span><span class="hljs-punctuation">:</span> <span class="hljs-numb…
55882 …<span class="hljs-attr">"irq"</span><span class="hljs-punctuation">:</span> <span class="hljs-numb…
55883 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55884 <span class="hljs-punctuation">{</span>
55885-attr">"model"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Intel(R) C…
55886 …<span class="hljs-attr">"speed"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
55887 …<span class="hljs-attr">"user"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55888 …<span class="hljs-attr">"nice"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55889 …<span class="hljs-attr">"sys"</span><span class="hljs-punctuation">:</span> <span class="hljs-numb…
55890 …span class="hljs-attr">"idle"</span><span class="hljs-punctuation">:</span> <span class="hljs-numb…
55891 …<span class="hljs-attr">"irq"</span><span class="hljs-punctuation">:</span> <span class="hljs-numb…
55892 <span class="hljs-punctuation">}</span>
55893 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
55894 …<span class="hljs-attr">"networkInterfaces"</span><span class="hljs-punctuation">:</span> <span cl…
55895 <span class="hljs-punctuation">{</span>
55896 …<span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55897 …span class="hljs-attr">"internal"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55898 … class="hljs-attr">"mac"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
55899 …an class="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55900 … class="hljs-attr">"netmask"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
55901 …<span class="hljs-attr">"family"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55902 <span class="hljs-punctuation">}</span>
55903 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
55904 …<span class="hljs-attr">"host"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
55905 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55906 …<span class="hljs-attr">"javascriptStack"</span><span class="hljs-punctuation">:</span> <span clas…
55907-attr">"message"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Error: *…
55908 …<span class="hljs-attr">"stack"</span><span class="hljs-punctuation">:</span> <span class="hljs-pu…
55909 …<span class="hljs-string">"at myException (/home/nodeuser/project/node/test/report/test-exception.…
55910 …n class="hljs-string">"at Object.&#x3C;anonymous> (/home/nodeuser/project/node/test/report/test-ex…
55911 …<span class="hljs-string">"at Module._compile (internal/modules/cjs/loader.js:718:30)"</span><span…
55912 …<span class="hljs-string">"at Object.Module._extensions..js (internal/modules/cjs/loader.js:729:10…
55913 …<span class="hljs-string">"at Module.load (internal/modules/cjs/loader.js:617:32)"</span><span cla…
55914 …<span class="hljs-string">"at tryModuleLoad (internal/modules/cjs/loader.js:560:12)"</span><span c…
55915 …<span class="hljs-string">"at Function.Module._load (internal/modules/cjs/loader.js:552:3)"</span>…
55916 …<span class="hljs-string">"at Function.Module.runMain (internal/modules/cjs/loader.js:771:12)"</sp…
55917 <span class="hljs-string">"at executeUserCode (internal/bootstrap/node.js:332:15)"</span>
55918 <span class="hljs-punctuation">]</span>
55919 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55920 …<span class="hljs-attr">"nativeStack"</span><span class="hljs-punctuation">:</span> <span class="h…
55921 <span class="hljs-punctuation">{</span>
55922 … class="hljs-attr">"pc"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"0…
55923 …<span class="hljs-attr">"symbol"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55924 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55925 <span class="hljs-punctuation">{</span>
55926 … class="hljs-attr">"pc"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"0…
55927 …<span class="hljs-attr">"symbol"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55928 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55929 <span class="hljs-punctuation">{</span>
55930 … class="hljs-attr">"pc"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"0…
55931 …<span class="hljs-attr">"symbol"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55932 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55933 <span class="hljs-punctuation">{</span>
55934 … class="hljs-attr">"pc"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"0…
55935 …<span class="hljs-attr">"symbol"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55936 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55937 <span class="hljs-punctuation">{</span>
55938 … class="hljs-attr">"pc"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"0…
55939 …<span class="hljs-attr">"symbol"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
55940 <span class="hljs-punctuation">}</span>
55941 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
55942 …<span class="hljs-attr">"javascriptHeap"</span><span class="hljs-punctuation">:</span> <span class…
55943 …an class="hljs-attr">"totalMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs
55944 …ass="hljs-attr">"totalCommittedMemory"</span><span class="hljs-punctuation">:</span> <span class="…
55945 …an class="hljs-attr">"usedMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55946 …lass="hljs-attr">"availableMemory"</span><span class="hljs-punctuation">:</span> <span class="hljs
55947 … class="hljs-attr">"memoryLimit"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
55948 …<span class="hljs-attr">"heapSpaces"</span><span class="hljs-punctuation">:</span> <span class="hl…
55949 …<span class="hljs-attr">"read_only_space"</span><span class="hljs-punctuation">:</span> <span clas…
55950 …pan class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hljs
55951 …n class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class="h…
55952 …span class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55953 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55954 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55955 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55956 …<span class="hljs-attr">"new_space"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55957 …an class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55958 … class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class="hl…
55959 …pan class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
55960 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55961 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55962 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55963 …<span class="hljs-attr">"old_space"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55964 …an class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55965 … class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class="hl…
55966 …pan class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
55967 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55968 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55969 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55970 …<span class="hljs-attr">"code_space"</span><span class="hljs-punctuation">:</span> <span class="hl…
55971 …pan class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hljs
55972 … class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class="hl…
55973 …span class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55974 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55975 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55976 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55977 …<span class="hljs-attr">"map_space"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55978 …pan class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hljs
55979 … class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class="hl…
55980 …span class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs-
55981 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55982 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55983 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55984 …<span class="hljs-attr">"large_object_space"</span><span class="hljs-punctuation">:</span> <span c…
55985 …<span class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hl…
55986 …pan class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class=…
55987 …an class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
55988 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55989 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55990 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
55991 …<span class="hljs-attr">"new_large_object_space"</span><span class="hljs-punctuation">:</span> <sp…
55992 …<span class="hljs-attr">"memorySize"</span><span class="hljs-punctuation">:</span> <span class="hl…
55993 …pan class="hljs-attr">"committedMemory"</span><span class="hljs-punctuation">:</span> <span class=…
55994 …<span class="hljs-attr">"capacity"</span><span class="hljs-punctuation">:</span> <span class="hljs
55995 …<span class="hljs-attr">"used"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
55996 …<span class="hljs-attr">"available"</span><span class="hljs-punctuation">:</span> <span class="hlj…
55997 <span class="hljs-punctuation">}</span>
55998 <span class="hljs-punctuation">}</span>
55999 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56000 …<span class="hljs-attr">"resourceUsage"</span><span class="hljs-punctuation">:</span> <span class=…
56001 … class="hljs-attr">"userCpuSeconds"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56002 …class="hljs-attr">"kernelCpuSeconds"</span><span class="hljs-punctuation">:</span> <span class="hl…
56003 …ss="hljs-attr">"cpuConsumptionPercent"</span><span class="hljs-punctuation">:</span> <span class="…
56004 …span class="hljs-attr">"maxRss"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
56005 …<span class="hljs-attr">"pageFaults"</span><span class="hljs-punctuation">:</span> <span class="hl…
56006 …<span class="hljs-attr">"IORequired"</span><span class="hljs-punctuation">:</span> <span class="hl…
56007 …<span class="hljs-attr">"IONotRequired"</span><span class="hljs-punctuation">:</span> <span class=…
56008 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56009 …<span class="hljs-attr">"fsActivity"</span><span class="hljs-punctuation">:</span> <span class="hl…
56010 …<span class="hljs-attr">"reads"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
56011 …<span class="hljs-attr">"writes"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
56012 <span class="hljs-punctuation">}</span>
56013 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56014 …<span class="hljs-attr">"uvthreadResourceUsage"</span><span class="hljs-punctuation">:</span> <spa…
56015 … class="hljs-attr">"userCpuSeconds"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56016 …class="hljs-attr">"kernelCpuSeconds"</span><span class="hljs-punctuation">:</span> <span class="hl…
56017 …ss="hljs-attr">"cpuConsumptionPercent"</span><span class="hljs-punctuation">:</span> <span class="…
56018 …<span class="hljs-attr">"fsActivity"</span><span class="hljs-punctuation">:</span> <span class="hl…
56019 …<span class="hljs-attr">"reads"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
56020 …<span class="hljs-attr">"writes"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
56021 <span class="hljs-punctuation">}</span>
56022 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56023 …<span class="hljs-attr">"libuv"</span><span class="hljs-punctuation">:</span> <span class="hljs-pu…
56024 <span class="hljs-punctuation">{</span>
56025 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56026 …span class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs
56027 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56028 …ass="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
56029 …<span class="hljs-attr">"details"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56030 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56031 <span class="hljs-punctuation">{</span>
56032 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56033 …pan class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56034 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56035 …ass="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
56036 …<span class="hljs-attr">"repeat"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
56037 …ss="hljs-attr">"firesInMsFromNow"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56038 …<span class="hljs-attr">"expired"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56039 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56040 <span class="hljs-punctuation">{</span>
56041 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56042 …span class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs
56043 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56044 …<span class="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56045 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56046 <span class="hljs-punctuation">{</span>
56047 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56048 …pan class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56049 …an class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hl…
56050 …<span class="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56051 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56052 <span class="hljs-punctuation">{</span>
56053 …span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
56054 …pan class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56055 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56056 …<span class="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56057 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56058 <span class="hljs-punctuation">{</span>
56059 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56060 …pan class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56061 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56062 …<span class="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56063 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56064 <span class="hljs-punctuation">{</span>
56065 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56066 …span class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs
56067 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56068 …<span class="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56069 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56070 <span class="hljs-punctuation">{</span>
56071 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56072 …pan class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56073 …an class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hl…
56074 …ass="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
56075 …<span class="hljs-attr">"width"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
56076 …<span class="hljs-attr">"height"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
56077 …<span class="hljs-attr">"fd"</span><span class="hljs-punctuation">:</span> <span class="hljs-numbe…
56078 …pan class="hljs-attr">"writeQueueSize"</span><span class="hljs-punctuation">:</span> <span class="…
56079 …span class="hljs-attr">"readable"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56080 …<span class="hljs-attr">"writable"</span><span class="hljs-punctuation">:</span> <span class="hljs
56081 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56082 <span class="hljs-punctuation">{</span>
56083 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56084 …span class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs
56085 …n class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56086 …ass="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
56087 …<span class="hljs-attr">"signum"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
56088 …<span class="hljs-attr">"signal"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
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 …an class="hljs-attr">"is_referenced"</span><span class="hljs-punctuation">:</span> <span class="hl…
56094 …ass="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
56095 …<span class="hljs-attr">"width"</span><span class="hljs-punctuation">:</span> <span class="hljs-nu…
56096 …<span class="hljs-attr">"height"</span><span class="hljs-punctuation">:</span> <span class="hljs-n…
56097 …<span class="hljs-attr">"fd"</span><span class="hljs-punctuation">:</span> <span class="hljs-numbe…
56098 …pan class="hljs-attr">"writeQueueSize"</span><span class="hljs-punctuation">:</span> <span class="…
56099 …span class="hljs-attr">"readable"</span><span class="hljs-punctuation">:</span> <span class="hljs-
56100 …<span class="hljs-attr">"writable"</span><span class="hljs-punctuation">:</span> <span class="hljs
56101 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56102 <span class="hljs-punctuation">{</span>
56103 …<span class="hljs-attr">"type"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56104 …span class="hljs-attr">"is_active"</span><span class="hljs-punctuation">:</span> <span class="hljs
56105 …ass="hljs-attr">"address"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">…
56106 …<span class="hljs-attr">"loopIdleTimeSeconds"</span><span class="hljs-punctuation">:</span> <span …
56107 <span class="hljs-punctuation">}</span>
56108 <span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
56109-attr">"workers"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</s…
56110 …<span class="hljs-attr">"environmentVariables"</span><span class="hljs-punctuation">:</span> <span…
56111 …n class="hljs-attr">"REMOTEHOST"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
56112-attr">"MANPATH"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/opt/rh/…
56113 … class="hljs-attr">"XDG_SESSION_ID"</span><span class="hljs-punctuation">:</span> <span class="hlj…
56114 … class="hljs-attr">"HOSTNAME"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
56115 …an class="hljs-attr">"HOST"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
56116 …n class="hljs-attr">"TERM"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
56117 …pan class="hljs-attr">"SHELL"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
56118 …n class="hljs-attr">"SSH_CLIENT"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
56119-attr">"PERL5LIB"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/opt/rh…
56120 …ljs-attr">"OLDPWD"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/home/…
56121-attr">"JAVACONFDIRS"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/op…
56122 …n class="hljs-attr">"SSH_TTY"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
56123 …="hljs-attr">"PCP_DIR"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/o…
56124 …an class="hljs-attr">"GROUP"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56125 …span class="hljs-attr">"USER"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
56126-attr">"LD_LIBRARY_PATH"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
56127 … class="hljs-attr">"HOSTTYPE"</span><span class="hljs-punctuation">:</span> <span class="hljs-stri…
56128-attr">"XDG_CONFIG_DIRS"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"…
56129 …ss="hljs-attr">"MAIL"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/va…
56130-attr">"PATH"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/home/nodeu…
56131 …s="hljs-attr">"PWD"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/home…
56132 …an class="hljs-attr">"LANG"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
56133-attr">"PS1"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"\\u@\\h : \\…
56134 …<span class="hljs-attr">"SHLVL"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
56135 …n class="hljs-attr">"HOME"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
56136 …span class="hljs-attr">"OSTYPE"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
56137 …pan class="hljs-attr">"VENDOR"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56138-attr">"PYTHONPATH"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/opt/…
56139 …pan class="hljs-attr">"MACHTYPE"</span><span class="hljs-punctuation">:</span> <span class="hljs-s…
56140 …an class="hljs-attr">"LOGNAME"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56141-attr">"XDG_DATA_DIRS"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/o…
56142 …"hljs-attr">"LESSOPEN"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"||…
56143-attr">"INFOPATH"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"/opt/rh…
56144 …s="hljs-attr">"XDG_RUNTIME_DIR"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
56145 …<span class="hljs-attr">"_"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
56146 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56147 …<span class="hljs-attr">"userLimits"</span><span class="hljs-punctuation">:</span> <span class="hl…
56148 …<span class="hljs-attr">"core_file_size_blocks"</span><span class="hljs-punctuation">:</span> <spa…
56149 …<span class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56150 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56151 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56152 …<span class="hljs-attr">"data_seg_size_kbytes"</span><span class="hljs-punctuation">:</span> <span…
56153 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56154 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56155 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56156 …<span class="hljs-attr">"file_size_blocks"</span><span class="hljs-punctuation">:</span> <span cla…
56157 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56158 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56159 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56160 …<span class="hljs-attr">"max_locked_memory_bytes"</span><span class="hljs-punctuation">:</span> <s…
56161 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56162 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
56163 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56164 …<span class="hljs-attr">"max_memory_size_kbytes"</span><span class="hljs-punctuation">:</span> <sp…
56165 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56166 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56167 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56168 …<span class="hljs-attr">"open_files"</span><span class="hljs-punctuation">:</span> <span class="hl…
56169 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56170 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
56171 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56172 …<span class="hljs-attr">"stack_size_bytes"</span><span class="hljs-punctuation">:</span> <span cla…
56173 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56174 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56175 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56176 …<span class="hljs-attr">"cpu_time_seconds"</span><span class="hljs-punctuation">:</span> <span cla…
56177 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56178 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56179 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56180 …<span class="hljs-attr">"max_user_processes"</span><span class="hljs-punctuation">:</span> <span c…
56181 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56182 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-num…
56183 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56184 …<span class="hljs-attr">"virtual_memory_kbytes"</span><span class="hljs-punctuation">:</span> <spa…
56185 …pan class="hljs-attr">"soft"</span><span class="hljs-punctuation">:</span> <span class="hljs-strin…
56186 …<span class="hljs-attr">"hard"</span><span class="hljs-punctuation">:</span> <span class="hljs-str…
56187 <span class="hljs-punctuation">}</span>
56188 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
56189 …<span class="hljs-attr">"sharedObjects"</span><span class="hljs-punctuation">:</span> <span class=…
56190 <span class="hljs-string">"/lib64/libdl.so.2"</span><span class="hljs-punctuation">,</span>
56191 <span class="hljs-string">"/lib64/librt.so.1"</span><span class="hljs-punctuation">,</span>
56192 <span class="hljs-string">"/lib64/libstdc++.so.6"</span><span class="hljs-punctuation">,</span>
56193 <span class="hljs-string">"/lib64/libm.so.6"</span><span class="hljs-punctuation">,</span>
56194 <span class="hljs-string">"/lib64/libgcc_s.so.1"</span><span class="hljs-punctuation">,</span>
56195 <span class="hljs-string">"/lib64/libpthread.so.0"</span><span class="hljs-punctuation">,</span>
56196 <span class="hljs-string">"/lib64/libc.so.6"</span><span class="hljs-punctuation">,</span>
56197 <span class="hljs-string">"/lib64/ld-linux-x86-64.so.2"</span>
56198 <span class="hljs-punctuation">]</span>
56199 <span class="hljs-punctuation">}</span></code></pre>
56201 <pre><code class="language-bash">node --report-uncaught-exception --report-on-signal \
56202 --report-on-fatalerror app.js</code></pre>
56205 <p><code>--report-uncaught-exception</code> Enables report to be generated on
56206 un-caught exceptions. Useful when inspecting JavaScript stack in conjunction
56210 <p><code>--report-on-signal</code> Enables report to be generated upon receiving
56225 <p><code>--report-on-fatalerror</code> Enables the report to be triggered on fatal errors
56232 <p><code>--report-compact</code> Write reports in a compact format, single-line JSON, more
56233 easily consumable by log processing systems than the default multi-line format
56237 <p><code>--report-directory</code> Location at which the report will be
56241 <p><code>--report-filename</code> Name of the file to which the report will be
56245 <p><code>--report-signal</code> Sets or resets the signal for report generation
56250 <pre><code class="language-js">process.<span class="hljs-property">report</span>.<span class="hljs-
56253 …ass="language-js">process.<span class="hljs-property">report</span>.<span class="hljs-title functi…
56259 <pre><code class="language-js"><span class="hljs-keyword">try</span> {
56260 …process.<span class="hljs-title function_">chdir</span>(<span class="hljs-string">'/non-existent-p…
56261 } <span class="hljs-keyword">catch</span> (err) {
56262 …process.<span class="hljs-property">report</span>.<span class="hljs-title function_">writeReport</…
56264 <span class="hljs-comment">// Any other code</span></code></pre>
56267 <pre><code class="language-js"><span class="hljs-keyword">try</span> {
56268 …process.<span class="hljs-title function_">chdir</span>(<span class="hljs-string">'/non-existent-p…
56269 } <span class="hljs-keyword">catch</span> (err) {
56270 …process.<span class="hljs-property">report</span>.<span class="hljs-title function_">writeReport</…
56272 <span class="hljs-comment">// Any other code</span></code></pre>
56275 …class="language-js"><span class="hljs-keyword">const</span> report = process.<span class="hljs-pro…
56276-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
56278 <span class="hljs-comment">// Similar to process.report.writeReport() output</span>
56279-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
56283-js"><span class="hljs-keyword">const</span> report = process.<span class="hljs-property">report</…
56284-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
56286 the application, in expectation of self-adjusting the resource consumption,
56294 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
56295 <span class="hljs-meta">> </span><span class="language-bash">process.report.writeReport();</span>
56298 <span class="hljs-meta">></span></code></pre>
56325 <pre><code class="language-js"><span class="hljs-comment">// Trigger report only on uncaught except…
56326 process.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnFatalError</s…
56327 process.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnSignal</span>…
56328 …ocess.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnUncaughtExcept…
56330 <span class="hljs-comment">// Trigger report for both internal errors as well as external signal.</…
56331 process.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnFatalError</s…
56332 process.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnSignal</span>…
56333 …ocess.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnUncaughtExcept…
56335 <span class="hljs-comment">// Change the default signal to 'SIGQUIT' and enable it.</span>
56336 process.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnFatalError</s…
56337 …ocess.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnUncaughtExcept…
56338 process.<span class="hljs-property">report</span>.<span class="hljs-property">reportOnSignal</span>…
56339 process.<span class="hljs-property">report</span>.<span class="hljs-property">signal</span> = <span…
56342 <pre><code class="language-bash">NODE_OPTIONS=<span class="hljs-string">"--report-uncaught-exceptio…
56343 --report-on-fatalerror --report-on-signal \
56344 --report-signal=SIGUSR2 --report-filename=./report.json \
56345 --report-directory=/home/nodeuser"</span></code></pre>
56369 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
56379 …ode class="language-js"><span class="hljs-keyword">const</span> stream = <span class="hljs-built_i…
56458 …code class="language-js"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in…
56460 …s="hljs-keyword">const</span> server = http.<span class="hljs-title function_">createServer</span>…
56461 <span class="hljs-comment">// `req` is an http.IncomingMessage, which is a readable stream.</span>
56462 <span class="hljs-comment">// `res` is an http.ServerResponse, which is a writable stream.</span>
56464 <span class="hljs-keyword">let</span> body = <span class="hljs-string">''</span>;
56465 <span class="hljs-comment">// Get the data as utf8 strings.</span>
56466 <span class="hljs-comment">// If an encoding is not set, Buffer objects will be received.</span>
56467 …req.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span>);
56469 <span class="hljs-comment">// Readable streams emit 'data' events once a listener is added.</span>
56470 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
56474 …<span class="hljs-comment">// The 'end' event indicates that the entire body has been received.</s…
56475 …req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span cla…
56476 <span class="hljs-keyword">try</span> {
56477 …<span class="hljs-keyword">const</span> data = <span class="hljs-variable constant_">JSON</span>.<…
56478 <span class="hljs-comment">// Write back something interesting to the user:</span>
56479 …res.<span class="hljs-title function_">write</span>(<span class="hljs-keyword">typeof</span> data);
56480 res.<span class="hljs-title function_">end</span>();
56481 } <span class="hljs-keyword">catch</span> (er) {
56482 <span class="hljs-comment">// uh oh! bad json!</span>
56483 res.<span class="hljs-property">statusCode</span> = <span class="hljs-number">400</span>;
56484 …n class="hljs-keyword">return</span> res.<span class="hljs-title function_">end</span>(<span class…
56489 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);
56491 <span class="hljs-comment">// $ curl localhost:1337 -d "{}"</span>
56492 <span class="hljs-comment">// object</span>
56493 <span class="hljs-comment">// $ curl localhost:1337 -d "\"foo\""</span>
56494 <span class="hljs-comment">// string</span>
56495 <span class="hljs-comment">// $ curl localhost:1337 -d "not json"</span>
56496 <span class="hljs-comment">// error: Unexpected token o in JSON at position 1</span></code></pre>
56532 <pre><code class="language-js"><span class="hljs-keyword">const</span> myStream = <span class="hljs
56533 myStream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some data'</spa…
56534 myStream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some more data'…
56535 myStream.<span class="hljs-title function_">end</span>(<span class="hljs-string">'done writing data…
56565 <pre><code class="language-js"><span class="hljs-comment">// Write the data to the supplied writabl…
56566 <span class="hljs-comment">// Be attentive to back-pressure.</span>
56567 <span class="hljs-keyword">function</span> <span class="hljs-title function_">writeOneMillionTimes<…
56568 <span class="hljs-keyword">let</span> i = <span class="hljs-number">1000000</span>;
56569 <span class="hljs-title function_">write</span>();
56570 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">write</span>(<span c…
56571 <span class="hljs-keyword">let</span> ok = <span class="hljs-literal">true</span>;
56572 <span class="hljs-keyword">do</span> {
56573 i--;
56574 <span class="hljs-keyword">if</span> (i === <span class="hljs-number">0</span>) {
56575 <span class="hljs-comment">// Last time!</span>
56576 writer.<span class="hljs-title function_">write</span>(data, encoding, callback);
56577 } <span class="hljs-keyword">else</span> {
56578 <span class="hljs-comment">// See if we should continue, or wait.</span>
56579 <span class="hljs-comment">// Don't pass the callback, because we're not done yet.</span>
56580 ok = writer.<span class="hljs-title function_">write</span>(data, encoding);
56582 …} <span class="hljs-keyword">while</span> (i > <span class="hljs-number">0</span> &#x26;&#x26; ok);
56583 <span class="hljs-keyword">if</span> (i > <span class="hljs-number">0</span>) {
56584 <span class="hljs-comment">// Had to stop early!</span>
56585 <span class="hljs-comment">// Write some more once it drains.</span>
56586 …writer.<span class="hljs-title function_">once</span>(<span class="hljs-string">'drain'</span>, wr…
56595 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error…
56598 data. The listener callback is passed a single <code>Error</code> argument when called.</p>
56610 <pre><code class="language-js"><span class="hljs-keyword">const</span> writer = <span class="hljs-t…
56611 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
56612 …writer.<span class="hljs-title function_">write</span>(<span class="hljs-string">`hello, #<span cl…
56614 writer.<span class="hljs-title function_">on</span>(<span class="hljs-string">'finish'</span>, <spa…
56615 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
56617 writer.<span class="hljs-title function_">end</span>(<span class="hljs-string">'This is the end\n'<…
56627 <pre><code class="language-js"><span class="hljs-keyword">const</span> writer = <span class="hljs-t…
56628 <span class="hljs-keyword">const</span> reader = <span class="hljs-title function_">getReadableStre…
56629 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'pipe'</span>, <span class…
56630 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
56631 assert.<span class="hljs-title function_">equal</span>(src, reader);
56633 reader.<span class="hljs-title function_">pipe</span>(writer);</code></pre>
56647 <pre><code class="language-js"><span class="hljs-keyword">const</span> writer = <span class="hljs-t…
56648 <span class="hljs-keyword">const</span> reader = <span class="hljs-title function_">getReadableStre…
56649 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'unpipe'</span>, <span clas…
56650 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
56651 assert.<span class="hljs-title function_">equal</span>(src, reader);
56653 reader.<span class="hljs-title function_">pipe</span>(writer);
56654 reader.<span class="hljs-title function_">unpipe</span>(writer);</code></pre>
56666 all to <code>writable._writev()</code>, if present. This prevents a head-of-line blocking
56677 <td><p>Work as a no-op on a stream that has already been destroyed.</p></td></tr>
56684 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
56685 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
56696-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><pre><code cl…
56698 <span class="hljs-keyword">const</span> myStream = <span class="hljs-keyword">new</span> <span clas…
56700 …lass="hljs-keyword">const</span> fooErr = <span class="hljs-keyword">new</span> <span class="hljs-
56701 myStream.<span class="hljs-title function_">destroy</span>(fooErr);
56702-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">…
56704 <span class="hljs-keyword">const</span> myStream = <span class="hljs-keyword">new</span> <span clas…
56706 myStream.<span class="hljs-title function_">destroy</span>();
56707-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword">f…
56708-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span>…
56710 <span class="hljs-keyword">const</span> myStream = <span class="hljs-keyword">new</span> <span clas…
56711 myStream.<span class="hljs-title function_">destroy</span>();
56713-title function_">write</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function"…
56714 <span class="hljs-comment">// ERR_STREAM_DESTROYED</span></code></pre>
56715 <p>Once <code>destroy()</code> has been called any further calls will be a no-op and no
56724 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
56727-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span>…
56729 <span class="hljs-keyword">const</span> myStream = <span class="hljs-keyword">new</span> <span clas…
56731-variable language_">console</span>.<span class="hljs-title function_">log</span>(myStream.<span c…
56732 myStream.<span class="hljs-title function_">destroy</span>();
56733-variable language_">console</span>.<span class="hljs-title function_">log</span>(myStream.<span c…
56751-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
56755 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
56756 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
56758 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
56767 <pre><code class="language-js"><span class="hljs-comment">// Write 'hello, ' and then end with 'wor…
56768 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
56769 <span class="hljs-keyword">const</span> file = fs.<span class="hljs-title function_">createWriteStr…
56770 file.<span class="hljs-title function_">write</span>(<span class="hljs-string">'hello, '</span>);
56771 file.<span class="hljs-title function_">end</span>(<span class="hljs-string">'world!'</span>);
56772 <span class="hljs-comment">// Writing more now is not allowed!</span></code></pre>
56786 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
56787 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
56801 <pre><code class="language-js">stream.<span class="hljs-title function_">cork</span>();
56802 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some '</span>);
56803 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'data '</span>);
56804 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span>…
56808 <pre><code class="language-js">stream.<span class="hljs-title function_">cork</span>();
56809 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some '</span>);
56810 stream.<span class="hljs-title function_">cork</span>();
56811 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'data '</span>);
56812 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span>…
56813 stream.<span class="hljs-title function_">uncork</span>();
56814 …<span class="hljs-comment">// The data will not be flushed until uncork() is called a second time.…
56815 stream.<span class="hljs-title function_">uncork</span>();
56823 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
56832 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
56842 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
56851 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
56859 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
56868 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
56878 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
56886 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
56904-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
56908 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
56909 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
56910 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
56945 …ode class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title functio…
56946 …<span class="hljs-keyword">if</span> (!stream.<span class="hljs-title function_">write</span>(data…
56947 …stream.<span class="hljs-title function_">once</span>(<span class="hljs-string">'drain'</span>, cb…
56948 } <span class="hljs-keyword">else</span> {
56949 process.<span class="hljs-title function_">nextTick</span>(cb);
56953 <span class="hljs-comment">// Wait for cb to be called before doing any other write.</span>
56954 <span class="hljs-title function_">write</span>(<span class="hljs-string">'hello'</span>, <span cla…
56955 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57045-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">PassThrough</span>,…
57046 <span class="hljs-keyword">const</span> pass = <span class="hljs-keyword">new</span> <span class="h…
57047 <span class="hljs-keyword">const</span> writable = <span class="hljs-keyword">new</span> <span clas…
57049 pass.<span class="hljs-title function_">pipe</span>(writable);
57050 pass.<span class="hljs-title function_">unpipe</span>(writable);
57051 <span class="hljs-comment">// readableFlowing is now false.</span>
57053-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(…
57054 pass.<span class="hljs-title function_">write</span>(<span class="hljs-string">'ok'</span>); <span…
57055 pass.<span class="hljs-title function_">resume</span>(); <span class="hljs-comment">// Must be …
57062 to consume data from a single stream. Specifically, using a combination
57067 require more fine-grained control over the transfer and generation of data can
57097 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
57115 <pre><code class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs
57116 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
57117 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
57129 <pre><code class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs
57130 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
57131 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
57133 readable.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span…
57134 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57141 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error…
57147 <p>The listener callback will be passed a single <code>Error</code> object.</p>
57171 <pre><code class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs
57172 …ass="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hlj…
57173 <span class="hljs-comment">// There is some data to read now.</span>
57174 <span class="hljs-keyword">let</span> data;
57176 …<span class="hljs-keyword">while</span> (data = <span class="hljs-variable language_">this</span>.…
57177 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57187 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
57188 <span class="hljs-keyword">const</span> rr = fs.<span class="hljs-title function_">createReadStream…
57189 rr.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span …
57190 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
57192 rr.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class…
57193 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57196 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
57221 <td><p>Work as a no-op on a stream that has already been destroyed.</p></td></tr>
57228 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
57229 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57235 <p>Once <code>destroy()</code> has been called any further calls will be a no-op and no
57244 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
57252 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
57258 …e class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs-keyword…
57260 readable.<span class="hljs-title function_">isPaused</span>(); <span class="hljs-comment">// === fa…
57261 readable.<span class="hljs-title function_">pause</span>();
57262 readable.<span class="hljs-title function_">isPaused</span>(); <span class="hljs-comment">// === tr…
57263 readable.<span class="hljs-title function_">resume</span>();
57264 readable.<span class="hljs-title function_">isPaused</span>(); <span class="hljs-comment">// === fa…
57270 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57275 <pre><code class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs
57276 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
57277 …ss="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span cl…
57278 readable.<span class="hljs-title function_">pause</span>();
57279 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57280 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
57281 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57282 readable.<span class="hljs-title function_">resume</span>();
57283 }, <span class="hljs-number">1000</span>);
57293 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
57295 <li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
57308 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
57309 <span class="hljs-keyword">const</span> readable = <span class="hljs-title function_">getReadableSt…
57310 <span class="hljs-keyword">const</span> writable = fs.<span class="hljs-title function_">createWrit…
57311 <span class="hljs-comment">// All the data from readable goes into 'file.txt'.</span>
57312 readable.<span class="hljs-title function_">pipe</span>(writable);</code></pre>
57313 <p>It is possible to attach multiple <code>Writable</code> streams to a single <code>Readable</code>
57317 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
57318 <span class="hljs-keyword">const</span> r = fs.<span class="hljs-title function_">createReadStream<…
57319 <span class="hljs-keyword">const</span> z = zlib.<span class="hljs-title function_">createGzip</spa…
57320 <span class="hljs-keyword">const</span> w = fs.<span class="hljs-title function_">createWriteStream…
57321 r.<span class="hljs-title function_">pipe</span>(z).<span class="hljs-title function_">pipe</span>(…
57326 …class="language-js">reader.<span class="hljs-title function_">pipe</span>(writer, { <span class="h…
57327 reader.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span c…
57328 …writer.<span class="hljs-title function_">end</span>(<span class="hljs-string">'Goodbye\n'</span>);
57341 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
57342-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
57359 <pre><code class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs
57361 <span class="hljs-comment">// 'readable' may be triggered multiple times as data is buffered in</sp…
57362 readable.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, …
57363 <span class="hljs-keyword">let</span> chunk;
57364 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57365 <span class="hljs-comment">// Use a loop to make sure we read all currently available data</span>
57366 …<span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = reada…
57367 …lass="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span …
57371 <span class="hljs-comment">// 'end' will be triggered once when there is no more data available</sp…
57372 readable.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span…
57373 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57384 <pre><code class="language-js"><span class="hljs-keyword">const</span> chunks = [];
57386 readable.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, …
57387 <span class="hljs-keyword">let</span> chunk;
57388 …<span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = reada…
57389 chunks.<span class="hljs-title function_">push</span>(chunk);
57393 readable.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span…
57394 …<span class="hljs-keyword">const</span> content = chunks.<span class="hljs-title function_">join</…
57396 <p>A <code>Readable</code> stream in object mode will always return a single item from
57408 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
57417 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
57427 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type">&#x3C;null></a> | <a…
57436 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
57444 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
57453 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
57462 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
57472 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
57488 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57494 <pre><code class="language-js"><span class="hljs-title function_">getReadableStreamSomehow</span>()
57495 .<span class="hljs-title function_">resume</span>()
57496 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span class=…
57497 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57506 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
57507 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57515 output data to be interpreted as UTF-8 data, and passed as strings. Calling
57518 <p>The <code>Readable</code> stream will properly handle multi-byte characters delivered
57521 <pre><code class="language-js"><span class="hljs-keyword">const</span> readable = <span class="hljs
57522 readable.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</sp…
57523 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
57524 …assert.<span class="hljs-title function_">equal</span>(<span class="hljs-keyword">typeof</span> ch…
57525-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
57533 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57540 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
57541 <span class="hljs-keyword">const</span> readable = <span class="hljs-title function_">getReadableSt…
57542 <span class="hljs-keyword">const</span> writable = fs.<span class="hljs-title function_">createWrit…
57543 <span class="hljs-comment">// All the data from readable goes into 'file.txt',</span>
57544 <span class="hljs-comment">// but only for the first second.</span>
57545 readable.<span class="hljs-title function_">pipe</span>(writable);
57546 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
57547 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57548 readable.<span class="hljs-title function_">unpipe</span>(writable);
57549 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57550 writable.<span class="hljs-title function_">end</span>();
57551 }, <span class="hljs-number">1000</span>);</code></pre>
57565-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> | …
57569 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
57578 code that needs to "un-consume" some amount of data that it has optimistically
57585 <pre><code class="language-js"><span class="hljs-comment">// Pull off a header delimited by \n\n.</…
57586 <span class="hljs-comment">// Use unshift() if we get too much.</span>
57587 <span class="hljs-comment">// Call the callback with (error, header, stream).</span>
57588 …"hljs-keyword">const</span> { <span class="hljs-title class_">StringDecoder</span> } = <span class…
57589 <span class="hljs-keyword">function</span> <span class="hljs-title function_">parseHeader</span>(<s…
57590 …stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, call…
57591 …stream.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, o…
57592 …"hljs-keyword">const</span> decoder = <span class="hljs-keyword">new</span> <span class="hljs-titl…
57593 <span class="hljs-keyword">let</span> header = <span class="hljs-string">''</span>;
57594 …<span class="hljs-keyword">function</span> <span class="hljs-title function_">onReadable</span>(<s…
57595 <span class="hljs-keyword">let</span> chunk;
57596 …<span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (chunk = strea…
57597 …<span class="hljs-keyword">const</span> str = decoder.<span class="hljs-title function_">write</sp…
57598 …<span class="hljs-keyword">if</span> (str.<span class="hljs-title function_">match</span>(<span cl…
57599 <span class="hljs-comment">// Found the header boundary.</span>
57600 …<span class="hljs-keyword">const</span> split = str.<span class="hljs-title function_">split</span…
57601 header += split.<span class="hljs-title function_">shift</span>();
57602 …<span class="hljs-keyword">const</span> remaining = split.<span class="hljs-title function_">join<…
57603 …s-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
57604 …stream.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'error'<…
57605 <span class="hljs-comment">// Remove the 'readable' listener before unshifting.</span>
57606 …stream.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'readabl…
57607 <span class="hljs-keyword">if</span> (buf.<span class="hljs-property">length</span>)
57608 stream.<span class="hljs-title function_">unshift</span>(buf);
57609 … <span class="hljs-comment">// Now the body of the message can be read from the stream.</span>
57610 …<span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, header,…
57611 } <span class="hljs-keyword">else</span> {
57612 <span class="hljs-comment">// Still reading the header.</span>
57632 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57643-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">OldReader</span> } …
57644 …s="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } = <span class="h…
57645 <span class="hljs-keyword">const</span> oreader = <span class="hljs-keyword">new</span> <span class…
57646 …="hljs-keyword">const</span> myReader = <span class="hljs-keyword">new</span> <span class="hljs-ti…
57648 myReader.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, …
57649 …myReader.<span class="hljs-title function_">read</span>(); <span class="hljs-comment">// etc.</spa…
57664 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x…
57666 …<code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in"…
57668 …lass="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-titl…
57669 …readable.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</s…
57670 <span class="hljs-keyword">let</span> data = <span class="hljs-string">''</span>;
57671 …n class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keywo…
57674 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57677-title function_">print</span>(fs.<span class="hljs-title function_">createReadStream</span>(<span…
57681 option. In the code example above, data will be in a single chunk if the file
57725 <td><p>Work as a no-op on a stream that has already been destroyed.</p></td></tr>
57732 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
57733 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
57741 <p>Once <code>destroy()</code> has been called, any further calls will be a no-op and no
57761 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
57763 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
57765 <li><code>readable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
57768 <li><code>writable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
57773 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
57775 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
57780 … class="language-js"><span class="hljs-keyword">const</span> { finished } = <span class="hljs-buil…
57782 <span class="hljs-keyword">const</span> rs = fs.<span class="hljs-title function_">createReadStream…
57784 <span class="hljs-title function_">finished</span>(rs, <span class="hljs-function">(<span class="hl…
57785 <span class="hljs-keyword">if</span> (err) {
57786 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
57787 } <span class="hljs-keyword">else</span> {
57788 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57792 rs.<span class="hljs-title function_">resume</span>(); <span class="hljs-comment">// Drain the stre…
57796 <p>The <code>finished</code> API is promisify-able as well;</p>
57797 …anguage-js"><span class="hljs-keyword">const</span> finished = util.<span class="hljs-title functi…
57799 <span class="hljs-keyword">const</span> rs = fs.<span class="hljs-title function_">createReadStream…
57801 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
57802 <span class="hljs-keyword">await</span> <span class="hljs-title function_">finished</span>(rs);
57803 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57806 …s-title function_">run</span>().<span class="hljs-title function_">catch</span>(<span class="hljs-
57807 rs.<span class="hljs-title function_">resume</span>(); <span class="hljs-comment">// Drain the stre…
57814-js"><span class="hljs-keyword">const</span> cleanup = <span class="hljs-title function_">finished…
57815 <span class="hljs-title function_">cleanup</span>();
57816 <span class="hljs-comment">// ...</span>
57834-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type">&#x3C;It…
57835-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type">&#x3C;It…
57837-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type">&#x3C;It…
57840 …" class="type">&#x3C;Stream></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
57842 <li><code>source</code> <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class…
57843 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type">&#x…
57846 …" class="type">&#x3C;Stream></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
57848 <li><code>source</code> <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class…
57849 …github.io/ecma262/#sec-asynciterable-interface" class="type">&#x3C;AsyncIterable></a> | <a href="h…
57852 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
57854 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
57862 … class="language-js"><span class="hljs-keyword">const</span> { pipeline } = <span class="hljs-buil…
57863 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
57864 <span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in">require</span>(<span cla…
57866 <span class="hljs-comment">// Use the pipeline API to easily pipe a series of streams</span>
57867 <span class="hljs-comment">// together and get notified when the pipeline is fully done.</span>
57869 <span class="hljs-comment">// A pipeline to gzip a potentially huge tar file efficiently:</span>
57871 <span class="hljs-title function_">pipeline</span>(
57872 …fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'archive.t…
57873 zlib.<span class="hljs-title function_">createGzip</span>(),
57874 …fs.<span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'archive.…
57875 <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
57876 <span class="hljs-keyword">if</span> (err) {
57877 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
57878 } <span class="hljs-keyword">else</span> {
57879 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57883 <p>The <code>pipeline</code> API is promisify-able as well:</p>
57884 …anguage-js"><span class="hljs-keyword">const</span> pipeline = util.<span class="hljs-title functi…
57886 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
57887 <span class="hljs-keyword">await</span> <span class="hljs-title function_">pipeline</span>(
57888 …fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'archive.t…
57889 zlib.<span class="hljs-title function_">createGzip</span>(),
57890 …fs.<span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'archive.…
57892 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57895 …s-title function_">run</span>().<span class="hljs-title function_">catch</span>(<span class="hljs-
57897 …anguage-js"><span class="hljs-keyword">const</span> pipeline = util.<span class="hljs-title functi…
57898 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
57900 …class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-tit…
57901 <span class="hljs-keyword">await</span> <span class="hljs-title function_">pipeline</span>(
57902 …fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'lowercase…
57903 <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>* (source) {
57904 …source.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</spa…
57905 …n class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keywo…
57906 …<span class="hljs-keyword">yield</span> chunk.<span class="hljs-title function_">toUpperCase</span…
57909 …fs.<span class="hljs-title function_">createWriteStream</span>(<span class="hljs-string">'uppercas…
57911 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57914 …s-title function_">run</span>().<span class="hljs-title function_">catch</span>(<span class="hljs-
57928 <li><code>iterable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
57931 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
57937-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
57939 …ss="hljs-keyword">async</span> <span class="hljs-keyword">function</span> * <span class="hljs-titl…
57940 <span class="hljs-keyword">yield</span> <span class="hljs-string">'hello'</span>;
57941 <span class="hljs-keyword">yield</span> <span class="hljs-string">'streams'</span>;
57944 …ljs-keyword">const</span> readable = <span class="hljs-title class_">Readable</span>.<span class="…
57946 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
57947 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
57960 <!-- eslint-disable no-useless-constructor -->
57961-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
57963 …lass="hljs-keyword">class</span> <span class="hljs-title class_">MyWritable</span> <span class="hl…
57964 …<span class="hljs-title function_">constructor</span>(<span class="hljs-params">{ highWaterMark, .…
57965 <span class="hljs-variable language_">super</span>({ highWaterMark });
57966 <span class="hljs-comment">// ...</span>
58007 <table><thead><tr><th>Use-case</th><th>Class</th><th>Method(s) to implement</th></tr></thead><tbody…
58026-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58028 <span class="hljs-keyword">const</span> myWritable = <span class="hljs-keyword">new</span> <span cl…
58029 …<span class="hljs-title function_">write</span>(<span class="hljs-params">chunk, encoding, callbac…
58030 <span class="hljs-comment">// ...</span>
58053 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
58055 <li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
58058 <li><code>decodeStrings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
58064 <li><code>defaultEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
58067 <li><code>objectMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
58072 <li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
58074 <li><code>write</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
58076 <li><code>writev</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
58078 <li><code>destroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
58080 <li><code>final</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
58082 <li><code>autoDestroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
58087 <!-- eslint-disable no-useless-constructor -->
58088-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58090 …lass="hljs-keyword">class</span> <span class="hljs-title class_">MyWritable</span> <span class="hl…
58091 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">options</span>) {
58092 <span class="hljs-comment">// Calls the stream.Writable() constructor.</span>
58093 <span class="hljs-variable language_">super</span>(options);
58094 <span class="hljs-comment">// ...</span>
58097 <p>Or, when using pre-ES6 style constructors:</p>
58098-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58099 <span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span cla…
58101 <span class="hljs-keyword">function</span> <span class="hljs-title function_">MyWritable</span>(<sp…
58102 …ss="hljs-keyword">if</span> (!(<span class="hljs-variable language_">this</span> <span class="hljs
58103 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-t…
58104 …<span class="hljs-title class_">Writable</span>.<span class="hljs-title function_">call</span>(<sp…
58106 util.<span class="hljs-title function_">inherits</span>(<span class="hljs-title class_">MyWritable<…
58108-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58110 <span class="hljs-keyword">const</span> myWritable = <span class="hljs-keyword">new</span> <span cl…
58111 …<span class="hljs-title function_">write</span>(<span class="hljs-params">chunk, encoding, callbac…
58112 <span class="hljs-comment">// ...</span>
58114 …<span class="hljs-title function_">writev</span>(<span class="hljs-params">chunks, callback</span>…
58115 <span class="hljs-comment">// ...</span>
58129 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
58134 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58137 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58170-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object[]></a> The data…
58174 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
58177 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58181 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58199 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
58200 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58210 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58227-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58229 <span class="hljs-keyword">const</span> myWritable = <span class="hljs-keyword">new</span> <span cl…
58230 …<span class="hljs-title function_">write</span>(<span class="hljs-params">chunk, encoding, callbac…
58231-keyword">if</span> (chunk.<span class="hljs-title function_">toString</span>().<span class="hljs-
58232 …ass="hljs-title function_">callback</span>(<span class="hljs-keyword">new</span> <span class="hljs
58233 } <span class="hljs-keyword">else</span> {
58234 <span class="hljs-title function_">callback</span>();
58243-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58245 …lass="hljs-keyword">class</span> <span class="hljs-title class_">MyWritable</span> <span class="hl…
58246 …<span class="hljs-title function_">_write</span>(<span class="hljs-params">chunk, encoding, callba…
58247-keyword">if</span> (chunk.<span class="hljs-title function_">toString</span>().<span class="hljs-
58248 …ass="hljs-title function_">callback</span>(<span class="hljs-keyword">new</span> <span class="hljs
58249 } <span class="hljs-keyword">else</span> {
58250 <span class="hljs-title function_">callback</span>();
58256 input is a string. This is not a trivial process when using multi-byte
58257 characters encoding, such as UTF-8. The following example shows how to decode
58258 multi-byte strings using <code>StringDecoder</code> and <a href="#stream_class_stream_writable"><co…
58259-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Writable</span> } =…
58260 …"hljs-keyword">const</span> { <span class="hljs-title class_">StringDecoder</span> } = <span class…
58262 …ss="hljs-keyword">class</span> <span class="hljs-title class_">StringWritable</span> <span class="…
58263 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">options</span>) {
58264 <span class="hljs-variable language_">super</span>(options);
58265-variable language_">this</span>.<span class="hljs-property">_decoder</span> = <span class="hljs-k…
58266 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">data</span> = <span …
58268 …<span class="hljs-title function_">_write</span>(<span class="hljs-params">chunk, encoding, callba…
58269 <span class="hljs-keyword">if</span> (encoding === <span class="hljs-string">'buffer'</span>) {
58270 …chunk = <span class="hljs-variable language_">this</span>.<span class="hljs-property">_decoder</sp…
58272 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">data</span> += chunk;
58273 <span class="hljs-title function_">callback</span>();
58275 <span class="hljs-title function_">_final</span>(<span class="hljs-params">callback</span>) {
58276-variable language_">this</span>.<span class="hljs-property">data</span> += <span class="hljs-vari…
58277 <span class="hljs-title function_">callback</span>();
58281-keyword">const</span> euro = [[<span class="hljs-number">0xE2</span>, <span class="hljs-number">0…
58282 <span class="hljs-keyword">const</span> w = <span class="hljs-keyword">new</span> <span class="hljs
58284 w.<span class="hljs-title function_">write</span>(<span class="hljs-string">'currency: '</span>);
58285 w.<span class="hljs-title function_">write</span>(euro[<span class="hljs-number">0</span>]);
58286 w.<span class="hljs-title function_">end</span>(euro[<span class="hljs-number">1</span>]);
58288 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(w.<span clas…
58306 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
58308 <li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
58311 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58313 <li><code>objectMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
58315 a single value instead of a <code>Buffer</code> of size <code>n</code>. <strong>Default:</strong> <…
58316 <li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
58318 <li><code>read</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
58320 <li><code>destroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
58322 <li><code>autoDestroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
58327 <!-- eslint-disable no-useless-constructor -->
58328-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
58330 …lass="hljs-keyword">class</span> <span class="hljs-title class_">MyReadable</span> <span class="hl…
58331 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">options</span>) {
58332 <span class="hljs-comment">// Calls the stream.Readable(options) constructor.</span>
58333 <span class="hljs-variable language_">super</span>(options);
58334 <span class="hljs-comment">// ...</span>
58337 <p>Or, when using pre-ES6 style constructors:</p>
58338-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
58339 <span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span cla…
58341 <span class="hljs-keyword">function</span> <span class="hljs-title function_">MyReadable</span>(<sp…
58342 …ss="hljs-keyword">if</span> (!(<span class="hljs-variable language_">this</span> <span class="hljs
58343 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-t…
58344 …<span class="hljs-title class_">Readable</span>.<span class="hljs-title function_">call</span>(<sp…
58346 util.<span class="hljs-title function_">inherits</span>(<span class="hljs-title class_">MyReadable<…
58348-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
58350 <span class="hljs-keyword">const</span> myReadable = <span class="hljs-keyword">new</span> <span cl…
58351 <span class="hljs-title function_">read</span>(<span class="hljs-params">size</span>) {
58352 <span class="hljs-comment">// ...</span>
58360 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
58380 single operation that returns data can use the <code>size</code> argument to determine how
58392 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
58393 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58409-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> | …
58413 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58415 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
58429 example, when wrapping a lower-level source that provides some form of
58430 pause/resume mechanism, and a data callback, the low-level source can be wrapped
58432 <pre><code class="language-js"><span class="hljs-comment">// `_source` is an object with readStop()…
58433 <span class="hljs-comment">// and an `ondata` member that gets called when it has data, and</span>
58434 <span class="hljs-comment">// an `onend` member that gets called when the data is over.</span>
58436 …ss="hljs-keyword">class</span> <span class="hljs-title class_">SourceWrapper</span> <span class="h…
58437 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">options</span>) {
58438 <span class="hljs-variable language_">super</span>(options);
58440 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">_source</span> = <sp…
58442 <span class="hljs-comment">// Every time there's data, push it into the internal buffer.</span>
58443-variable language_">this</span>.<span class="hljs-property">_source</span>.<span class="hljs-prop…
58444 <span class="hljs-comment">// If push() returns false, then stop reading from source.</span>
58445 …<span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span cla…
58446 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">_source</span>.<span…
58449 <span class="hljs-comment">// When the source ends, push the EOF-signaling `null` chunk.</span>
58450 …ss="hljs-variable language_">this</span>.<span class="hljs-property">_source</span>.<span class="h…
58451 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">push</span>(<…
58454 …<span class="hljs-comment">// _read() will be called when the stream wants to pull more data in.</…
58455 <span class="hljs-comment">// The advisory size argument is ignored in this case.</span>
58456 <span class="hljs-title function_">_read</span>(<span class="hljs-params">size</span>) {
58457 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">_source</span>.<span…
58470-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
58472 <span class="hljs-keyword">const</span> myReadable = <span class="hljs-keyword">new</span> <span cl…
58473 <span class="hljs-title function_">read</span>(<span class="hljs-params">size</span>) {
58474 …<span class="hljs-keyword">const</span> err = <span class="hljs-title function_">checkSomeErrorCon…
58475 <span class="hljs-keyword">if</span> (err) {
58476 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">destroy</span…
58477 } <span class="hljs-keyword">else</span> {
58478 <span class="hljs-comment">// Do some work.</span>
58486-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
58488 …class="hljs-keyword">class</span> <span class="hljs-title class_">Counter</span> <span class="hljs
58489 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">opt</span>) {
58490 <span class="hljs-variable language_">super</span>(opt);
58491 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">_max</span> = <span …
58492 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">_index</span> = <spa…
58495 <span class="hljs-title function_">_read</span>(<span class="hljs-params"></span>) {
58496 …<span class="hljs-keyword">const</span> i = <span class="hljs-variable language_">this</span>.<spa…
58497 …<span class="hljs-keyword">if</span> (i > <span class="hljs-variable language_">this</span>.<span …
58498 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">push</span>(<…
58499 <span class="hljs-keyword">else</span> {
58500 … <span class="hljs-keyword">const</span> str = <span class="hljs-title class_">String</span>(i);
58501 …hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-t…
58502 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">push</span>(b…
58514 both base classes due to overriding <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
58530 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
58533 <li><code>allowHalfOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
58536 <li><code>readable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58538 <li><code>writable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58540 <li><code>readableObjectMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
58542 <li><code>writableObjectMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
58544 <li><code>readableHighWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
58546 <li><code>writableHighWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
58551 <!-- eslint-disable no-useless-constructor -->
58552-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Duplex</span> } = <…
58554 …class="hljs-keyword">class</span> <span class="hljs-title class_">MyDuplex</span> <span class="hlj…
58555 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">options</span>) {
58556 <span class="hljs-variable language_">super</span>(options);
58557 <span class="hljs-comment">// ...</span>
58560 <p>Or, when using pre-ES6 style constructors:</p>
58561-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Duplex</span> } = <…
58562 <span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span cla…
58564 <span class="hljs-keyword">function</span> <span class="hljs-title function_">MyDuplex</span>(<span…
58565 …ss="hljs-keyword">if</span> (!(<span class="hljs-variable language_">this</span> <span class="hljs
58566 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-t…
58567 …<span class="hljs-title class_">Duplex</span>.<span class="hljs-title function_">call</span>(<span…
58569 util.<span class="hljs-title function_">inherits</span>(<span class="hljs-title class_">MyDuplex</s…
58571-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Duplex</span> } = <…
58573 <span class="hljs-keyword">const</span> myDuplex = <span class="hljs-keyword">new</span> <span clas…
58574 <span class="hljs-title function_">read</span>(<span class="hljs-params">size</span>) {
58575 <span class="hljs-comment">// ...</span>
58577 …<span class="hljs-title function_">write</span>(<span class="hljs-params">chunk, encoding, callbac…
58578 <span class="hljs-comment">// ...</span>
58583 hypothetical lower-level source object to which data can be written, and
58589-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Duplex</span> } = <…
58590 <span class="hljs-keyword">const</span> kSource = <span class="hljs-title class_">Symbol</span>(<sp…
58592 …class="hljs-keyword">class</span> <span class="hljs-title class_">MyDuplex</span> <span class="hlj…
58593 …<span class="hljs-title function_">constructor</span>(<span class="hljs-params">source, options</s…
58594 <span class="hljs-variable language_">super</span>(options);
58595 <span class="hljs-variable language_">this</span>[kSource] = source;
58598 …<span class="hljs-title function_">_write</span>(<span class="hljs-params">chunk, encoding, callba…
58599 <span class="hljs-comment">// The underlying source only deals with strings.</span>
58600 …<span class="hljs-keyword">if</span> (<span class="hljs-title class_">Buffer</span>.<span class="h…
58601 chunk = chunk.<span class="hljs-title function_">toString</span>();
58602 …<span class="hljs-variable language_">this</span>[kSource].<span class="hljs-title function_">writ…
58603 <span class="hljs-title function_">callback</span>();
58606 <span class="hljs-title function_">_read</span>(<span class="hljs-params">size</span>) {
58607 …s-variable language_">this</span>[kSource].<span class="hljs-title function_">fetchSomeData</span>…
58608 …hljs-variable language_">this</span>.<span class="hljs-title function_">push</span>(<span class="h…
58613 <code>Writable</code> sides operate independently of one another despite co-existing within
58614 a single object instance.</p>
58623-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Transform</span> } …
58625 <span class="hljs-comment">// All Transform streams are also Duplex Streams.</span>
58626 <span class="hljs-keyword">const</span> myTransform = <span class="hljs-keyword">new</span> <span c…
58627 <span class="hljs-attr">writableObjectMode</span>: <span class="hljs-literal">true</span>,
58629 …<span class="hljs-title function_">transform</span>(<span class="hljs-params">chunk, encoding, cal…
58630 <span class="hljs-comment">// Coerce the chunk to a number if necessary.</span>
58631 chunk |= <span class="hljs-number">0</span>;
58633 <span class="hljs-comment">// Transform the chunk into something else.</span>
58634 …<span class="hljs-keyword">const</span> data = chunk.<span class="hljs-title function_">toString</…
58636 <span class="hljs-comment">// Push the data onto the readable queue.</span>
58637-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-string…
58641 myTransform.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'ascii'…
58642-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(…
58644 myTransform.<span class="hljs-title function_">write</span>(<span class="hljs-number">1</span>);
58645 <span class="hljs-comment">// Prints: 01</span>
58646 myTransform.<span class="hljs-title function_">write</span>(<span class="hljs-number">10</span>);
58647 <span class="hljs-comment">// Prints: 0a</span>
58648 myTransform.<span class="hljs-title function_">write</span>(<span class="hljs-number">100</span>);
58649 <span class="hljs-comment">// Prints: 64</span></code></pre>
58656 only ever have a single chunk of output which is provided when the input is
58670 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
58673 <li><code>transform</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
58675 <li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
58680 <!-- eslint-disable no-useless-constructor -->
58681-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Transform</span> } …
58683 …ass="hljs-keyword">class</span> <span class="hljs-title class_">MyTransform</span> <span class="hl…
58684 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">options</span>) {
58685 <span class="hljs-variable language_">super</span>(options);
58686 <span class="hljs-comment">// ...</span>
58689 <p>Or, when using pre-ES6 style constructors:</p>
58690-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Transform</span> } …
58691 <span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span cla…
58693 <span class="hljs-keyword">function</span> <span class="hljs-title function_">MyTransform</span>(<s…
58694 …ss="hljs-keyword">if</span> (!(<span class="hljs-variable language_">this</span> <span class="hljs
58695 …<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-t…
58696 …<span class="hljs-title class_">Transform</span>.<span class="hljs-title function_">call</span>(<s…
58698 util.<span class="hljs-title function_">inherits</span>(<span class="hljs-title class_">MyTransform…
58700-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Transform</span> } …
58702 <span class="hljs-keyword">const</span> myTransform = <span class="hljs-keyword">new</span> <span c…
58703 …<span class="hljs-title function_">transform</span>(<span class="hljs-params">chunk, encoding, cal…
58704 <span class="hljs-comment">// ...</span>
58719 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58742 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
58747 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58750 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
58762 output from a single input chunk, depending on how much is to be output
58770-js">transform.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<spa…
58771 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">push</span>(d…
58772 <span class="hljs-title function_">callback</span>();
58775-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">_transfo…
58776 <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, data);
58793 generators are effectively a first-class language-level stream construct at
58798 …e><code class="language-js">(<span class="hljs-keyword">async</span> <span class="hljs-keyword">fu…
58799 …n class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keywo…
58800 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
58804 unhandled post-destroy errors.</p>
58808-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Readable</span> } =…
58810 …ss="hljs-keyword">async</span> <span class="hljs-keyword">function</span> * <span class="hljs-titl…
58811 <span class="hljs-keyword">yield</span> <span class="hljs-string">'a'</span>;
58812 <span class="hljs-keyword">yield</span> <span class="hljs-string">'b'</span>;
58813 <span class="hljs-keyword">yield</span> <span class="hljs-string">'c'</span>;
58816 …ljs-keyword">const</span> readable = <span class="hljs-title class_">Readable</span>.<span class="…
58818 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
58819 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
58824 the handling of backpressure and backpressure-related errors:</p>
58825 … class="language-js"><span class="hljs-keyword">const</span> { pipeline } = <span class="hljs-buil…
58826 <span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span cla…
58827 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
58829 <span class="hljs-keyword">const</span> writable = fs.<span class="hljs-title function_">createWrit…
58831 <span class="hljs-comment">// Callback Pattern</span>
58832 <span class="hljs-title function_">pipeline</span>(iterator, writable, <span class="hljs-function">…
58833 <span class="hljs-keyword">if</span> (err) {
58834 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
58835 } <span class="hljs-keyword">else</span> {
58836 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
58840 <span class="hljs-comment">// Promise Pattern</span>
58841 <span class="hljs-keyword">const</span> pipelinePromise = util.<span class="hljs-title function_">p…
58842 <span class="hljs-title function_">pipelinePromise</span>(iterator, writable)
58843 ….<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-par…
58844 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
58846 ….<span class="hljs-title function_">catch</span>(<span class="hljs-variable language_">console</sp…
58876 <pre><code class="language-js"><span class="hljs-comment">// WARNING! BROKEN!</span>
58877 net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class…
58879 <span class="hljs-comment">// We add an 'end' listener, but never consume the data.</span>
58880 …socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span …
58881 <span class="hljs-comment">// It will never get here.</span>
58882 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'The message was re…
58885 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);</code><…
58890 <pre><code class="language-js"><span class="hljs-comment">// Workaround.</span>
58891 net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class…
58892 …socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span …
58893 …socket.<span class="hljs-title function_">end</span>(<span class="hljs-string">'The message was re…
58896 <span class="hljs-comment">// Start the flow of data, discarding it.</span>
58897 socket.<span class="hljs-title function_">resume</span>();
58898 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);</code><…
58900 pre-0.10 style streams can be wrapped in a <code>Readable</code> class using the
58909 a low-level <a href="#stream_readable_read_size_1"><code>stream._read()</code></a> call.</p>
58915 <p>Pushing a zero-byte string, <code>Buffer</code> or <code>Uint8Array</code> to a stream that is n…
58922 <code>highWaterMark</code> operates in non-object mode.</p>
58928 contain multi-byte characters.</p></section>
58932 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
58935 strings in a manner that preserves encoded multi-byte UTF-8 and UTF-16
58937-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">StringDecoder</span…
58939-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">StringDecoder</span…
58940 …"hljs-keyword">const</span> decoder = <span class="hljs-keyword">new</span> <span class="hljs-titl…
58942-keyword">const</span> cent = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
58943 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
58945-keyword">const</span> euro = <span class="hljs-title class_">Buffer</span>.<span class="hljs-titl…
58946 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
58951 <p>In the following example, the three UTF-8 encoded bytes of the European Euro
58953-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">StringDecoder</span…
58954 …"hljs-keyword">const</span> decoder = <span class="hljs-keyword">new</span> <span class="hljs-titl…
58956 …"hljs-title function_">write</span>(<span class="hljs-title class_">Buffer</span>.<span class="hlj…
58957 …"hljs-title function_">write</span>(<span class="hljs-title class_">Buffer</span>.<span class="hlj…
58958-variable language_">console</span>.<span class="hljs-title function_">log</span>(decoder.<span cl…
58965 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
58974 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
58976 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
58979 representing incomplete UTF-8 and UTF-16 characters will be replaced with
58990 <td><p>Each invalid character is now replaced by a single replacement character instead of one for …
58997 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
58999 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
59008 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
59015 …ode.js <a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-
59029 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
59069 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
59115 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
59133 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59134 …ode.js <a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-
59135 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
59148 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
59149 <span class="hljs-keyword">const</span> setImmediatePromise = util.<span class="hljs-title function…
59151-title function_">setImmediatePromise</span>(<span class="hljs-string">'foobar'</span>).<span clas…
59152 <span class="hljs-comment">// value === 'foobar' (passing values is optional)</span>
59153 <span class="hljs-comment">// This is executed after all I/O callbacks.</span>
59156 <span class="hljs-comment">// Or with async function</span>
59157 …s="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title f…
59158 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
59159 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">setImmediatePromise</sp…
59160 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
59162 <span class="hljs-title function_">timerExample</span>();</code></pre>
59168 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59169 <li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
59171 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
59176 set to <code>1</code>. Non-integer delays are truncated to an integer.</p>
59183 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59184 <li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
59186 <li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
59189 <p>Schedules execution of a one-time <code>callback</code> after <code>delay</code> milliseconds.</…
59195 will be set to <code>1</code>. Non-integer delays are truncated to an integer.</p>
59199 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
59200 <span class="hljs-keyword">const</span> setTimeoutPromise = util.<span class="hljs-title function_"…
59202-number">40</span>, <span class="hljs-string">'foobar'</span>).<span class="hljs-title function_">…
59203 <span class="hljs-comment">// value === 'foobar' (passing values is optional)</span>
59204 <span class="hljs-comment">// This is executed after about 40 milliseconds.</span>
59214 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
59215 <span class="hljs-keyword">const</span> setImmediatePromise = util.<span class="hljs-title function…
59217 <span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hlj…
59218 <span class="hljs-keyword">const</span> signal = ac.<span class="hljs-property">signal</span>;
59220 <span class="hljs-title function_">setImmediatePromise</span>(<span class="hljs-string">'foobar'</s…
59221 ….<span class="hljs-title function_">then</span>(<span class="hljs-variable language_">console</spa…
59222 ….<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-pa…
59223 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">name</span> === <span class=…
59224 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
59227 ac.<span class="hljs-title function_">abort</span>();</code></pre>
59229 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
59230 <span class="hljs-keyword">const</span> setTimeoutPromise = util.<span class="hljs-title function_"…
59232 <span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hlj…
59233 <span class="hljs-keyword">const</span> signal = ac.<span class="hljs-property">signal</span>;
59235 setTimeoutPromise(<span class="hljs-number">1000</span>, <span class="hljs-string">'foobar'</span>,…
59236 ….<span class="hljs-title function_">then</span>(<span class="hljs-variable language_">console</spa…
59237 ….<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-pa…
59238 …<span class="hljs-keyword">if</span> (err.<span class="hljs-property">name</span> === <span class=…
59239 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
59242 ac.<span class="hljs-title function_">abort</span>();</code></pre>
59257 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
59266 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
59273 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
59278 …code class="language-js"><span class="hljs-keyword">const</span> tls = <span class="hljs-built_in"…
59283 use of the OpenSSL command-line interface to generate a 2048-bit RSA private
59285 <pre><code class="language-bash">openssl genrsa -out ryans-key.pem 2048</code></pre>
59289 private key (such certificates are referred to as "self-signed"). The first
59292 <p>The OpenSSL command-line interface can be used to generate a CSR for a private
59294 <pre><code class="language-bash">openssl req -new -sha256 -key ryans-key.pem -out ryans-csr.pem</co…
59296 Authority for signing or used to generate a self-signed certificate.</p>
59297 <p>Creating a self-signed certificate using the OpenSSL command-line interface
59299 …<code class="language-bash">openssl x509 -req -<span class="hljs-keyword">in</span> ryans-csr.pem
59302 …s="language-bash">openssl pkcs12 -<span class="hljs-built_in">export</span> -<span class="hljs-key…
59303 -certfile ca-cert.pem -out ryans.pfx</code></pre>
59309 a single file, e.g. <code>cat ca1-cert.pem ca2-cert.pem > ca-cert.pem</code></li>
59314 of key-agreement (i.e., key-exchange) methods. That is, the server and client
59318 by eavesdroppers if the attacker manages to obtain the key-pair specifically
59321 key-agreement on every TLS/SSL handshake (in contrast to using the same key for
59326 …80%93Hellman_key_exchange">DHE</a>: An ephemeral version of the Diffie-Hellman key-agreement proto…
59327 …_curve_Diffie%E2%80%93Hellman">ECDHE</a>: An ephemeral version of the Elliptic Curve Diffie-Hellman
59328 key-agreement protocol.</li>
59333 to generate Diffie-Hellman parameters and specify them with the <code>dhparam</code>
59335 the OpenSSL command-line interface to generate such parameters:</p>
59336 <pre><code class="language-bash">openssl dhparam -outform PEM -out dhparam.pem 2048</code></pre>
59337 <p>If using perfect forward secrecy using <code>ECDHE</code>, Diffie-Hellman parameters are
59345 <p>ALPN (Application-Layer Protocol Negotiation Extension) and
59352 <h4>Pre-shared keys<span><a class="mark" href="#tls_pre_shared_keys" id="tls_pre_shared_keys">#</a>…
59354 <p>TLS-PSK support is available as an alternative to normal certificate-based
59355 authentication. It uses a pre-shared key instead of certificates to
59357 TLS-PSK and public key infrastructure are not mutually exclusive. Clients and
59360 <p>TLS-PSK is only a good choice where means exist to securely share a
59363 The TLS-PSK implementation in OpenSSL has seen many security flaws in
59368 low-entropy sources is not secure.</p>
59369 <p>PSK ciphers are disabled by default, and using TLS-PSK thus requires explicitly
59371 ciphers can be retrieved via <code>openssl ciphers -v 'PSK'</code>. All TLS 1.3
59373 supported they can be retrieved via <code>openssl ciphers -v -s -tls1_3 -psk</code>.</p>
59379 <h4>Client-initiated renegotiation attack mitigation<span><a class="mark" href="#tls_client_initiat…
59383 of server-side resources, making it a potential vector for denial-of-service
59389 <li><code>tls.CLIENT_RENEG_LIMIT</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
59391 <li><code>tls.CLIENT_RENEG_WINDOW</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
59421 in the initial connection. This mechanism avoids the need for server-side
59430 ticket, otherwise it contains client-side session state.</p>
59434 <p>Single process servers need no specific implementation to use session tickets.
59436 all have the same ticket keys. There are three 16-byte keys internally, but the
59437 tls API exposes them as a single 48-byte buffer for convenience.</p>
59458 <code>-reconnect</code> option to <code>openssl s_client</code>, for example:</p>
59459 …code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">openssl…
59462 <pre><code class="language-text">New, TLSv1.2, Cipher is ECDHE-RSA-AES128-GCM-SHA256</code></pre>
59464 <pre><code class="language-text">Reused, TLSv1.2, Cipher is ECDHE-RSA-AES128-GCM-SHA256</code></pre>
59470 <pre><code class="language-console">node -p crypto.constants.defaultCoreCipherList | tr ':' '\n'
59474 ECDHE-RSA-AES128-GCM-SHA256
59475 ECDHE-ECDSA-AES128-GCM-SHA256
59476 ECDHE-RSA-AES256-GCM-SHA384
59477 ECDHE-ECDSA-AES256-GCM-SHA384
59478 DHE-RSA-AES128-GCM-SHA256
59479 ECDHE-RSA-AES128-SHA256
59480 DHE-RSA-AES128-SHA256
59481 ECDHE-RSA-AES256-SHA384
59482 DHE-RSA-AES256-SHA384
59483 ECDHE-RSA-AES256-SHA256
59484 DHE-RSA-AES256-SHA256
59495 … replaced entirely using the <a href="#cli_tls_cipher_list_list"><code>--tls-cipher-list</code></a>
59496 command-line switch (directly, or via the <a href="#cli_node_options_options"><code>NODE_OPTIONS</c…
59497 variable). For instance, the following makes <code>ECDHE-RSA-AES128-GCM-SHA256:!RC4</code>
59499 <pre><code class="language-bash">node --tls-cipher-list=<span class="hljs-string">'ECDHE-RSA-AES128
59501 <span class="hljs-built_in">export</span> NODE_OPTIONS=--tls-cipher-list=<span class="hljs-string">…
59510 the OpenSSL <a href="https://www.openssl.org/docs/man1.1.1/man1/ciphers.html#CIPHER-LIST-FORMAT">ci…
59522 of an application. The <code>--tls-cipher-list</code> switch and <code>ciphers</code> option should…
59524 …ciphers for <a href="https://www.chromium.org/Home/chromium-security/education/tls#TOC-Cipher-Suit…
59529 <p>Old clients that rely on insecure and deprecated RC4 or DES-based ciphers
59533 …L <a href="https://www.openssl.org/docs/man1.1.1/man1/ciphers.html#CIPHER-LIST-FORMAT">cipher list…
59542 <p>The first 3 are enabled by default. The last 2 <code>CCM</code>-based suites are supported
59549 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
59563 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
59611-js"><span class="hljs-keyword">const</span> logFile = fs.<span class="hljs-title function_">creat…
59612 <span class="hljs-comment">// ...</span>
59613 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'keylog'</span>, <span clas…
59614 …<span class="hljs-keyword">if</span> (tlsSocket.<span class="hljs-property">remoteAddress</span> !…
59615 …<span class="hljs-keyword">return</span>; <span class="hljs-comment">// Only log keys for a partic…
59616 logFile.<span class="hljs-title function_">write</span>(line);
59637 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59651 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59657 Both <code>certificate</code> and <code>issuer</code> are <code>Buffer</code> DER-representations o…
59676 <p>The <code>issuer</code> can be <code>null</code> if the certificate is either self-signed or the
59691 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59694 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
59709 <pre><code class="language-js"><span class="hljs-keyword">const</span> tlsSessionStore = {};
59710 …pan class="hljs-title function_">on</span>(<span class="hljs-string">'newSession'</span>, <span cl…
59711 …tlsSessionStore[id.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'h…
59712 <span class="hljs-title function_">cb</span>();
59714 …an class="hljs-title function_">on</span>(<span class="hljs-string">'resumeSession'</span>, <span …
59715-title function_">cb</span>(<span class="hljs-literal">null</span>, tlsSessionStore[id.<span class…
59723 single argument when called:</p>
59745 <li><code>exception</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
59754 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
59755 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
59766 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
59776 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
59787 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
59789 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
59797 <li>Returns: <a href="#buffer_class_buffer" class="type">&#x3C;Buffer></a> A 48-byte buffer contain…
59809 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
59820-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
59859 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
59874 <li><code>requestOCSP</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
59900-js"><span class="hljs-keyword">const</span> logFile = fs.<span class="hljs-title function_">creat…
59901 <span class="hljs-comment">// ...</span>
59902-title function_">on</span>(<span class="hljs-string">'keylog'</span>, <span class="hljs-function"…
59909 The listener callback is passed a single argument when called:</p>
59948 the handshake is complete. For TLSv1.3, only ticket-based resumption is allowed
59955-js">tlsSocket.<span class="hljs-title function_">once</span>(<span class="hljs-string">'session'<…
59956 <span class="hljs-comment">// The session can be used immediately or later.</span>
59957 tls.<span class="hljs-title function_">connect</span>({
59958 <span class="hljs-attr">session</span>: session,
59959 <span class="hljs-comment">// Other connect options...</span>
59967 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
59983 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
59999 …ut is identical to the output of <code>openssl s_client -trace</code> or <code>openssl s_server -t…
60013 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60036 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60038 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60039 <li><code>standardName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
60040 <li><code>version</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
60047 <pre><code class="language-json"><span class="hljs-punctuation">{</span>
60048 …n class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string"…
60049-attr">"standardName"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"TLS…
60050 …<span class="hljs-attr">"version"</span><span class="hljs-punctuation">:</span> <span class="hljs-
60051 <span class="hljs-punctuation">}</span></code></pre>
60060 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60074 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60083 to implement the <code>tls-unique</code> channel binding from <a href="https://tools.ietf.org/html/…
60089 <li><code>detailed</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
60091 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60113 <li><code>subject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60118 <li><code>issuer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
60120 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#…
60121 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#…
60122 <li><code>serialNumber</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
60124 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#…
60126 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#…
60129 <li><code>ext_key_usage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
60130 <li><code>subjectaltname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
60132 <li><code>infoAccess</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere…
60134 <li><code>issuerCertificate</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
60135 self-signed certificates, this may be a circular reference.</li>
60141 <li><code>bits</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60142 <li><code>exponent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
60144 <li><code>modulus</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
60151 <li><code>bits</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60152 <li><code>asn1Curve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
60153 curve. Well-known curves are identified by an OID. While it is unusual, it is
60156 <li><code>nistCurve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
60157 has one (not all well-known curves have been assigned names by NIST). Example:
60158 <code>'P-256'</code>.</li>
60161 <!-- eslint-skip -->
60162 <pre><code class="language-js">{ <span class="hljs-attr">subject</span>:
60163 …{ <span class="hljs-attr">OU</span>: [ <span class="hljs-string">'Domain Control Validated'</span>…
60164 <span class="hljs-attr">CN</span>: <span class="hljs-string">'*.nodejs.org'</span> },
60165 <span class="hljs-attr">issuer</span>:
60166 { <span class="hljs-attr">C</span>: <span class="hljs-string">'GB'</span>,
60167 <span class="hljs-attr">ST</span>: <span class="hljs-string">'Greater Manchester'</span>,
60168 <span class="hljs-attr">L</span>: <span class="hljs-string">'Salford'</span>,
60169 <span class="hljs-attr">O</span>: <span class="hljs-string">'COMODO CA Limited'</span>,
60170 …<span class="hljs-attr">CN</span>: <span class="hljs-string">'COMODO RSA Domain Validation Secure …
60171 …<span class="hljs-attr">subjectaltname</span>: <span class="hljs-string">'DNS:*.nodejs.org, DNS:no…
60172 <span class="hljs-attr">infoAccess</span>:
60173 { <span class="hljs-string">'CA Issuers - URI'</span>:
60174 …[ <span class="hljs-string">'http://crt.comodoca.com/COMODORSADomainValidationSecureServerCA.crt'<…
60175 …<span class="hljs-string">'OCSP - URI'</span>: [ <span class="hljs-string">'http://ocsp.comodoca.c…
60176 …<span class="hljs-attr">modulus</span>: <span class="hljs-string">'B56CE45CB740B09A13F64AC543B712F…
60177 <span class="hljs-attr">exponent</span>: <span class="hljs-string">'0x10001'</span>,
60178 <span class="hljs-attr">pubkey</span>: &#x3C;Buffer ... >,
60191 …" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60200 to implement the <code>tls-unique</code> channel binding from <a href="https://tools.ietf.org/html/…
60206 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
60240 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60252 <p><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
60255 <p><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60257 <a href="https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#exporter-labels">IANA…
60269 <pre><code class="language-js"><span class="hljs-keyword">const</span> keyingMaterial = tlsSocket.<…
60270 <span class="hljs-number">128</span>,
60271 <span class="hljs-string">'client finished'</span>);
60273 <span class="hljs-comment">/**
60297 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
60305 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
60313 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
60321 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
60330 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
60332 <p>Returns the string representation of the remote IP family. <code>'IPv4'</code> or <code>'IPv6'</…
60338 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
60347 <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
60349 <li><code>rejectUnauthorized</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60357 <p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60364 <p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolea…
60368 Upon completion, the <code>callback</code> function will be passed a single argument
60381 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60383 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
60399 …ier</code> subject alternative names has been disabled in response to CVE-2021-44531.</p></td></tr>
60406 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
60408 <li><code>cert</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
60409 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a> |…
60412 <p>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Object…
60413 failure. On success, returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
60422 was present (see <a href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-44531">CVE-2021-4…
60458 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60461 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60463 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60464 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60475 <li><code>allowHalfOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
60479 <li><code>rejectUnauthorized</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60483 <li><code>pskCallback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer…
60485 <li>hint: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_t…
60488 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
60492 <code>identity</code> must use UTF-8 encoding.</li>
60494 When negotiating TLS-PSK (pre-shared keys), this function is called
60502-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="#…
60504 single <code>Buffer</code> or <code>TypedArray</code> or <code>DataView</code> containing the suppo…
60510 <li><code>servername</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
60512 name, and not an IP address. It can be used by a multi-homed server to
60515 <li><code>checkServerIdentity(servername, cert)</code> <a href="https://developer.mozilla.org/en-US…
60518 …certificate. This should return an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60522 <li><code>minDHSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
60526 <li><code>highWaterMark</code>: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
60532 <li><code>onread</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
60533 stored in a single <code>buffer</code> and passed to the supplied <code>callback</code> when
60541 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
60554 <pre><code class="language-js"><span class="hljs-comment">// Assumes an echo server that is listeni…
60555 <span class="hljs-keyword">const</span> tls = <span class="hljs-built_in">require</span>(<span clas…
60556 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
60558 <span class="hljs-keyword">const</span> options = {
60559 …<span class="hljs-comment">// Necessary only if the server requires client certificate authenticat…
60560 …<span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<spa…
60561 …<span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<sp…
60563 <span class="hljs-comment">// Necessary only if the server uses a self-signed certificate.</span>
60564 …<span class="hljs-attr">ca</span>: [ fs.<span class="hljs-title function_">readFileSync</span>(<sp…
60566 <span class="hljs-comment">// Necessary only if the server's cert isn't for "localhost".</span>
60567 …ss="hljs-attr">checkServerIdentity</span>: <span class="hljs-function">() =></span> { <span class=…
60570-keyword">const</span> socket = tls.<span class="hljs-title function_">connect</span>(<span class=…
60571 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
60572 …socket.<span class="hljs-property">authorized</span> ? <span class="hljs-string">'authorized'</spa…
60573 …process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">pipe</span>(so…
60574 …process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">resume</span>(…
60576 socket.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</span…
60577 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
60578 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
60580 socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'end'</span>, <span c…
60581 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
60588 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60589 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60590 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
60601 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60602 <li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
60603 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60604 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
60635 <td><p>The <code>ca</code> option can now be a single string containing multiple CA certificates.</…
60642 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60644 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
60645 certificates. Default is to trust the well-known CAs curated by Mozilla.
60651 certificates that are not chainable to a well-known CA, the certificate's CA
60657 For self-signed certificates, the certificate is its own CA, and must be
60662 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
60666 including the root CA (the root CA must be pre-known to the peer, see <code>ca</code>).
60671 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&…
60673 algorithms (<code>RSA-PSS</code>, <code>ECDSA</code> etc.), combination of both (e.g
60677 <li><code>ciphers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
60681 <li><code>clientCertEngine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
60683 …zilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | …
60685-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="#bu…
60691 <li><code>ecdhCurve</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
60692 list of curve NIDs or names, for example <code>P-521:P-384:P-256</code>, to use for
60695 available curve names. On recent releases, <code>openssl ecparam -list_curves</code>
60698 <li><code>honorCipherOrder</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
60702-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
60711 <li><code>privateKeyEngine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
60713 <li><code>privateKeyIdentifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
60717 <li><code>maxVersion</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
60721 <li><code>minVersion</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
60727 …er.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></…
60729-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="htt…
60738 <li><code>secureOptions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
60742 <li><code>secureProtocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
60746 <a href="https://www.openssl.org/docs/man1.1.1/man7/ssl.html#Dealing-with-Protocol-Methods">SSL_MET…
60751 <li><code>sessionIdContext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
60753 …#buffer_class_buffer" class="type">&#x3C;Buffer></a> 48-bytes of cryptographically strong pseudo-r…
60755 <li><code>sessionTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
60772 <a href="https://hg.mozilla.org/mozilla-central/raw-file/tip/security/nss/lib/ckfw/builtins/certdat…
60787 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
60789 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60791 <li><code>isServer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
60793 <li><code>requestCert</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
60795 <li><code>rejectUnauthorized</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60801 <li><code>isServer</code>: If <code>true</code> the TLS socket will be instantiated in server-mode.
60809 <li><code>requestOCSP</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
60825 <pre><code class="language-js">pair = tls.<span class="hljs-title function_">createSecurePair</span…
60826 pair.<span class="hljs-property">encrypted</span>.<span class="hljs-title function_">pipe</span>(so…
60827 socket.<span class="hljs-title function_">pipe</span>(pair.<span class="hljs-property">encrypted</s…
60829 <pre><code class="language-js">secureSocket = tls.<span class="hljs-title function_">TLSSocket</spa…
60850 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
60852-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="#…
60853 …of strings, <code>Buffer</code>s or <code>TypedArray</code>s or <code>DataView</code>s, or a single
60859 <li><code>clientCertEngine</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
60861 <li><code>enableTrace</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
60865 <li><code>handshakeTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
60869 <li><code>rejectUnauthorized</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
60872 <li><code>requestCert</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
60875 <li><code>sessionTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
60878 <li><code>SNICallback(servername, callback)</code> <a href="https://developer.mozilla.org/en-US/doc…
60881 error-first callback that takes two optional arguments: <code>error</code> and <code>ctx</code>.
60886 default callback with high-level API will be used (see below).</li>
60887 …#buffer_class_buffer" class="type">&#x3C;Buffer></a> 48-bytes of cryptographically strong pseudo-r…
60889 <li><code>pskCallback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer…
60893 <li>identity: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Stri…
60894-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
60898 When negotiating TLS-PSK (pre-shared keys), this function is called
60905 PSK ciphers are disabled by default, and using TLS-PSK thus
60908 <li><code>pskIdentityHint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
60909 with selecting the identity during TLS-PSK negotiation. Will be ignored
60918 <li><code>secureConnectionListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
60926 …code class="language-js"><span class="hljs-keyword">const</span> tls = <span class="hljs-built_in"…
60927 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
60929 <span class="hljs-keyword">const</span> options = {
60930 …<span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<spa…
60931 …<span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<sp…
60933 …<span class="hljs-comment">// This is necessary only if using client certificate authentication.</…
60934 <span class="hljs-attr">requestCert</span>: <span class="hljs-literal">true</span>,
60936 …<span class="hljs-comment">// This is necessary only if the client uses a self-signed certificate.…
60937 …<span class="hljs-attr">ca</span>: [ fs.<span class="hljs-title function_">readFileSync</span>(<sp…
60940 …ljs-keyword">const</span> server = tls.<span class="hljs-title function_">createServer</span>(opti…
60941 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
60942 …socket.<span class="hljs-property">authorized</span> ? <span class="hljs-string">'authorized'</spa…
60943 …socket.<span class="hljs-title function_">write</span>(<span class="hljs-string">'welcome!\n'</spa…
60944 …socket.<span class="hljs-title function_">setEncoding</span>(<span class="hljs-string">'utf8'</spa…
60945 socket.<span class="hljs-title function_">pipe</span>(socket);
60947 server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>, <spa…
60948 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
60957 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
60960 lower-case for historical reasons, but must be uppercased to be used in
60964-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
60970 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
60996 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
61000 <code>--tls-max-v1.2</code> sets the default to <code>'TLSv1.2'</code>. Using <code>--tls-max-v1.3<…
61009 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
61013 <code>--tls-min-v1.0</code> sets the default to <code>'TLSv1'</code>. Using <code>--tls-min-v1.1</c…
61014 the default to <code>'TLSv1.1'</code>. Using <code>--tls-min-v1.3</code> sets the default to
61021 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
61025 <p>Tracing can be enabled with the <code>--trace-event-categories</code> command-line flag
61026 or by using the <code>trace_events</code> module. The <code>--trace-event-categories</code> flag
61027 accepts a list of comma-separated category names.</p>
61049 of unhandled Promise rejections and handled-after-rejections.</li>
61055 <pre><code class="language-bash">node --trace-event-categories v8,node,node.async_hooks server.js</…
61056 <p>Prior versions of Node.js required the use of the <code>--trace-events-enabled</code>
61058 <code>--trace-events-enabled</code> flag <em>may</em> still be used and will enable the
61060 <pre><code class="language-bash">node --trace-events-enabled
61062 <span class="hljs-comment"># is equivalent to</span>
61064 node --trace-event-categories v8,node,node.async_hooks</code></pre>
61066 … class="language-js"><span class="hljs-keyword">const</span> trace_events = <span class="hljs-buil…
61067-keyword">const</span> tracing = trace_events.<span class="hljs-title function_">createTracing</sp…
61068 tracing.<span class="hljs-title function_">enable</span>(); <span class="hljs-comment">// Enable t…
61070 <span class="hljs-comment">// do work</span>
61072 tracing.<span class="hljs-title function_">disable</span>(); <span class="hljs-comment">// Disable…
61074 in the <a href="https://www.chromium.org/developers/how-tos/trace-event-profiling-tool"><code>chrom…
61077 <code>${rotation}</code> is an incrementing log-rotation id. The filepath pattern can
61078 be specified with <code>--trace-event-file-pattern</code> that accepts a template
61080 <pre><code class="language-bash">node --trace-event-categories v8 --trace-event-file-pattern <span …
61083 However the trace-event timestamps are expressed in microseconds,
61106 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
61108 <p>A comma-separated list of the trace event categories covered by this
61116 and <em>not</em> specified by the <code>--trace-event-categories</code> flag will be disabled.</p>
61117 … class="language-js"><span class="hljs-keyword">const</span> trace_events = <span class="hljs-buil…
61118-keyword">const</span> t1 = trace_events.<span class="hljs-title function_">createTracing</span>({…
61119-keyword">const</span> t2 = trace_events.<span class="hljs-title function_">createTracing</span>({…
61120 t1.<span class="hljs-title function_">enable</span>();
61121 t2.<span class="hljs-title function_">enable</span>();
61123 <span class="hljs-comment">// Prints 'node,node.perf,v8'</span>
61124 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
61126 t2.<span class="hljs-title function_">disable</span>(); <span class="hljs-comment">// Will only dis…
61128 <span class="hljs-comment">// Prints 'node,v8'</span>
61129 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
61141 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
61148 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
61150 <li><code>categories</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
61158 … class="language-js"><span class="hljs-keyword">const</span> trace_events = <span class="hljs-buil…
61159 <span class="hljs-keyword">const</span> categories = [<span class="hljs-string">'node.perf'</span>,…
61160 <span class="hljs-keyword">const</span> tracing = trace_events.<span class="hljs-title function_">c…
61161 tracing.<span class="hljs-title function_">enable</span>();
61162 <span class="hljs-comment">// do stuff</span>
61163 tracing.<span class="hljs-title function_">disable</span>();</code></pre>
61169 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
61171 <p>Returns a comma-separated list of all currently-enabled trace event
61173 by the <em>union</em> of all currently-enabled <code>Tracing</code> objects and any categories
61174 enabled using the <code>--trace-event-categories</code> flag.</p>
61176 <code>node --trace-event-categories node.perf test.js</code> will print
61178 … class="language-js"><span class="hljs-keyword">const</span> trace_events = <span class="hljs-buil…
61179-keyword">const</span> t1 = trace_events.<span class="hljs-title function_">createTracing</span>({…
61180-keyword">const</span> t2 = trace_events.<span class="hljs-title function_">createTracing</span>({…
61181-keyword">const</span> t3 = trace_events.<span class="hljs-title function_">createTracing</span>({…
61183 t1.<span class="hljs-title function_">enable</span>();
61184 t2.<span class="hljs-title function_">enable</span>();
61186 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
61190 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
61195 …code class="language-js"><span class="hljs-keyword">const</span> tty = <span class="hljs-built_in"…
61202 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">no…
61204 <span class="hljs-meta">$ </span><span class="language-bash">node -p -e <span class="hljs-string">"…
61235 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61239 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/t…
61242 <p>When in raw mode, input is always available character-by-character, not
61264-js">process.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">on</span…
61265 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
61266-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
61281 <li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
61283 <li><code>-1</code>: to the left from cursor</li>
61288 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61289 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
61308 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61309 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
61334 <li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
61335 <li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures…
61336 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61337 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
61348 <li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
61351 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
61381 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
61392 <li><code>count</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
61394 <li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
61397 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
61403 <pre><code class="language-js">process.<span class="hljs-property">stdout</span>.<span class="hljs-
61404 <span class="hljs-comment">// Returns true or false depending on if `stdout` supports at least 16 c…
61405 process.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">hasColors</spa…
61406 <span class="hljs-comment">// Returns true or false depending on if `stdout` supports at least 256 …
61407 …ass="hljs-property">stdout</span>.<span class="hljs-title function_">hasColors</span>({ <span clas…
61408 <span class="hljs-comment">// Returns true.</span>
61409-property">stdout</span>.<span class="hljs-title function_">hasColors</span>(<span class="hljs-num…
61410 <span class="hljs-comment">// Returns false (the environment setting pretends to support 2 ** 8 col…
61429 <li><code>dx</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
61430 <li><code>dy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
61431 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61432 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
61449 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
61450 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
61453 a TTY and <code>false</code> if it is not, including whenever <code>fd</code> is not a non-negative
61458 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
61462 …ode class="language-js"><span class="hljs-keyword">const</span> dgram = <span class="hljs-built_in…
61463 <span class="hljs-keyword">const</span> server = dgram.<span class="hljs-title function_">createSoc…
61465 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
61466 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
61467 server.<span class="hljs-title function_">close</span>();
61470 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
61471-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
61474 server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</span>, <…
61475 …<span class="hljs-keyword">const</span> address = server.<span class="hljs-title function_">addres…
61476-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
61479 server.<span class="hljs-title function_">bind</span>(<span class="hljs-number">41234</span>);
61480 <span class="hljs-comment">// Prints: server listening 0.0.0.0:41234</span></code></pre>
61508 <li><code>exception</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen…
61511 function is passed a single <code>Error</code> object.</p>
61529 <li><code>rinfo</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
61531 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
61532 ….org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The add…
61533 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61534 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61538 <p>If the source address of the incoming packet is an IPv6 link-local
61548 <li><code>multicastAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
61549 <li><code>multicastInterface</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
61561 …de class="language-js"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_i…
61562 <span class="hljs-keyword">const</span> dgram = <span class="hljs-built_in">require</span>(<span cl…
61563 <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isMaster</span>) {
61564 …cluster.<span class="hljs-title function_">fork</span>(); <span class="hljs-comment">// Works ok.<…
61565 …cluster.<span class="hljs-title function_">fork</span>(); <span class="hljs-comment">// Fails with…
61566 } <span class="hljs-keyword">else</span> {
61567 …<span class="hljs-keyword">const</span> s = dgram.<span class="hljs-title function_">createSocket<…
61568 …s.<span class="hljs-title function_">bind</span>(<span class="hljs-number">1234</span>, <span clas…
61569 …s.<span class="hljs-title function_">addMembership</span>(<span class="hljs-string">'224.0.0.114'<…
61577 <li><code>sourceAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
61578 <li><code>groupAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
61579 <li><code>multicastInterface</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
61581 <p>Tells the kernel to join a source-specific multicast channel at the given
61594 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
61613 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61614 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
61615 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61632 …ode class="language-js"><span class="hljs-keyword">const</span> dgram = <span class="hljs-built_in…
61633 <span class="hljs-keyword">const</span> server = dgram.<span class="hljs-title function_">createSoc…
61635 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span clas…
61636 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
61637 server.<span class="hljs-title function_">close</span>();
61640 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
61641-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
61644 server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</span>, <…
61645 …<span class="hljs-keyword">const</span> address = server.<span class="hljs-title function_">addres…
61646-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
61649 server.<span class="hljs-title function_">bind</span>(<span class="hljs-number">41234</span>);
61650 <span class="hljs-comment">// Prints: server listening 0.0.0.0:41234</span></code></pre>
61656 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
61658 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61659 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
61660 <li><code>exclusive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
61661 <li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
61664 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61692 <pre><code class="language-js">socket.<span class="hljs-title function_">bind</span>({
61693 <span class="hljs-attr">address</span>: <span class="hljs-string">'localhost'</span>,
61694 <span class="hljs-attr">port</span>: <span class="hljs-number">8000</span>,
61695 <span class="hljs-attr">exclusive</span>: <span class="hljs-literal">true</span>
61702 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61711 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61712 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
61713 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61737 <li><code>multicastAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
61738 <li><code>multicastInterface</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
61751 <li><code>sourceAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
61752 <li><code>groupAddress</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
61753 <li><code>multicastInterface</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
61755 <p>Instructs the kernel to leave a source-specific multicast channel at the given
61767 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
61775 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
61798 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
61826-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
61827 <li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
61828 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
61829 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61830 <li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
61831 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
61844 contain multi-byte characters, <code>offset</code> and <code>length</code> will be calculated with
61867 …ode class="language-js"><span class="hljs-keyword">const</span> dgram = <span class="hljs-built_in…
61868 …hljs-keyword">const</span> message = <span class="hljs-title class_">Buffer</span>.<span class="hl…
61869 <span class="hljs-keyword">const</span> client = dgram.<span class="hljs-title function_">createSoc…
61870-title function_">send</span>(message, <span class="hljs-number">41234</span>, <span class="hljs-s…
61871 client.<span class="hljs-title function_">close</span>();
61875 …ode class="language-js"><span class="hljs-keyword">const</span> dgram = <span class="hljs-built_in…
61876 …="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
61877 …="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hlj…
61878 <span class="hljs-keyword">const</span> client = dgram.<span class="hljs-title function_">createSoc…
61879 …class="hljs-title function_">send</span>([buf1, buf2], <span class="hljs-number">41234</span>, <sp…
61880 client.<span class="hljs-title function_">close</span>();
61884 determine the optimal strategy on a case-by-case basis. Generally speaking,
61888 …ode class="language-js"><span class="hljs-keyword">const</span> dgram = <span class="hljs-built_in…
61889 …hljs-keyword">const</span> message = <span class="hljs-title class_">Buffer</span>.<span class="hl…
61890 <span class="hljs-keyword">const</span> client = dgram.<span class="hljs-title function_">createSoc…
61891-title function_">connect</span>(<span class="hljs-number">41234</span>, <span class="hljs-string"…
61892 …client.<span class="hljs-title function_">send</span>(message, <span class="hljs-function">(<span …
61893 client.<span class="hljs-title function_">close</span>();
61911 as the <code>MTU</code> for dial-up type applications), whether they arrive whole or in
61913 <p>For IPv6, the minimum <code>MTU</code> is 1280 octets. However, the mandatory minimum
61928 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
61938 <li><code>multicastInterface</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
61941 <a href="https://en.wikipedia.org/wiki/IPv6_address#Scoped_literal_IPv6_addresses">IPv6 Zone Indice…
61950 <p>For IPv6 sockets, <code>multicastInterface</code> should include a scope to indicate the
61951 interface as in the examples that follow. In IPv6, individual <code>send</code> calls can
61956 <h5>Example: IPv6 outgoing multicast interface<span><a class="mark" href="#dgram_example_ipv6_outgo…
61958 …="language-js"><span class="hljs-keyword">const</span> socket = dgram.<span class="hljs-title func…
61960 socket.<span class="hljs-title function_">bind</span>(<span class="hljs-number">1234</span>, <span …
61961 …socket.<span class="hljs-title function_">setMulticastInterface</span>(<span class="hljs-string">'…
61964 …="language-js"><span class="hljs-keyword">const</span> socket = dgram.<span class="hljs-title func…
61966 socket.<span class="hljs-title function_">bind</span>(<span class="hljs-number">1234</span>, <span …
61967 …socket.<span class="hljs-title function_">setMulticastInterface</span>(<span class="hljs-string">'…
61971 …="language-js"><span class="hljs-keyword">const</span> socket = dgram.<span class="hljs-title func…
61973 socket.<span class="hljs-title function_">bind</span>(<span class="hljs-number">1234</span>, <span …
61974 …socket.<span class="hljs-title function_">setMulticastInterface</span>(<span class="hljs-string">'…
61984 <p>On IPv6, most errors with specifying or omitting scope will result in the socket
61986 <p>A socket's address family's ANY address (IPv4 <code>'0.0.0.0'</code> or IPv6 <code>'::'</code>) …
61994 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62004 <li><code>ttl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
62018 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62028 <li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62038 <li><code>ttl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
62083 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
62085 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62087 <li><code>reuseAddr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
62090 <li><code>ipv6Only</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
62091 disable dual-stack support, i.e., binding to address <code>::</code> won't make
62093 <li><code>recvBufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
62094 <li><code>sendBufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
62095 <li><code>lookup</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
62099 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
62111 …ode class="language-js"><span class="hljs-keyword">const</span> controller = <span class="hljs-key…
62112 <span class="hljs-keyword">const</span> { signal } = controller;
62113 …s-keyword">const</span> server = dgram.<span class="hljs-title function_">createSocket</span>({ <s…
62114 …<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span cla…
62115-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
62117 <span class="hljs-comment">// Later, when you want to close the server.</span>
62118 controller.<span class="hljs-title function_">abort</span>();</code></pre>
62124 <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62125 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
62138 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
62142 …code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in"…
62156 <pre><code class="language-text">┌─────────────────────────────────────────────────────────────────…
62175 <pre><code class="language-js"><span class="hljs-keyword">const</span> myURL =
62176 …<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="h…
62178 …code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in"…
62179 <span class="hljs-keyword">const</span> myURL =
62180 …url.<span class="hljs-title function_">parse</span>(<span class="hljs-string">'https://user:pass@s…
62184-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62185 myURL.<span class="hljs-property">pathname</span> = <span class="hljs-string">'/a/b/c'</span>;
62186 myURL.<span class="hljs-property">search</span> = <span class="hljs-string">'?d=e'</span>;
62187 myURL.<span class="hljs-property">hash</span> = <span class="hljs-string">'#fgh'</span>;</code></pr…
62188 <pre><code class="language-js"><span class="hljs-keyword">const</span> pathname = <span class="hljs
62189 <span class="hljs-keyword">const</span> search = <span class="hljs-string">'?d=e'</span>;
62190 <span class="hljs-keyword">const</span> hash = <span class="hljs-string">'#fgh'</span>;
62191-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-title funct…
62193 …ss="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title func…
62207 <p>Browser-compatible <code>URL</code> class, implemented by following the WHATWG URL
62208 Standard. <a href="https://url.spec.whatwg.org/#example-url-parsing">Examples of parsed URLs</a> ma…
62217 <li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
62220 <li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62225-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62226 <span class="hljs-comment">// https://example.org/foo</span></code></pre>
62228 It can also be imported from the built-in url module:</p>
62229-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
62233-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62234 <span class="hljs-comment">// https://example.org/</span></code></pre>
62236 automatically converted to ASCII using the <a href="https://tools.ietf.org/html/rfc5891#section-4.4…
62237-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62238 <span class="hljs-comment">// https://xn--g6w251d/</span></code></pre>
62244-js"><span class="hljs-keyword">let</span> myURL = <span class="hljs-keyword">new</span> <span cla…
62245 <span class="hljs-comment">// http://example.com/</span>
62247 …"hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">…
62248 <span class="hljs-comment">// https://example.com/</span>
62250 …="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string"…
62251 <span class="hljs-comment">// foo://Example.com/</span>
62253 …s="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string…
62254 <span class="hljs-comment">// http://example.com/</span>
62256 …="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string"…
62257 <span class="hljs-comment">// https://example.org/Example.com/</span>
62259 …s="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string…
62260 <span class="hljs-comment">// foo:Example.com/</span></code></pre>
62263 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62266-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62267 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62268 <span class="hljs-comment">// Prints #bar</span>
62270 myURL.<span class="hljs-property">hash</span> = <span class="hljs-string">'baz'</span>;
62271 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62272 <span class="hljs-comment">// Prints https://example.org/foo#baz</span></code></pre>
62274 are <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which characters to
62275 percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystring_s…
62279 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62282-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62283 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62284 <span class="hljs-comment">// Prints example.org:81</span>
62286 myURL.<span class="hljs-property">host</span> = <span class="hljs-string">'example.com:82'</span>;
62287 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62288 <span class="hljs-comment">// Prints https://example.com:82/foo</span></code></pre>
62292 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62297-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62298 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62299 <span class="hljs-comment">// Prints example.org</span>
62301 <span class="hljs-comment">// Setting the hostname does not change the port</span>
62302 myURL.<span class="hljs-property">hostname</span> = <span class="hljs-string">'example.com:82'</spa…
62303 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62304 <span class="hljs-comment">// Prints https://example.com:81/foo</span>
62306 <span class="hljs-comment">// Use myURL.host to change the hostname and port</span>
62307 myURL.<span class="hljs-property">host</span> = <span class="hljs-string">'example.org:82'</span>;
62308 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62309 <span class="hljs-comment">// Prints https://example.org:82/foo</span></code></pre>
62313 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62316-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62317 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62318 <span class="hljs-comment">// Prints https://example.org/foo</span>
62320 myURL.<span class="hljs-property">href</span> = <span class="hljs-string">'https://example.com/bar'…
62321 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62322 <span class="hljs-comment">// Prints https://example.com/bar</span></code></pre>
62332 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62334 <p>Gets the read-only serialization of the URL's origin.</p>
62335-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62336 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62337 <span class="hljs-comment">// Prints https://example.org</span></code></pre>
62338-js"><span class="hljs-keyword">const</span> idnURL = <span class="hljs-keyword">new</span> <span …
62339 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62340 <span class="hljs-comment">// Prints https://xn--g6w251d</span>
62342 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62343 <span class="hljs-comment">// Prints xn--g6w251d</span></code></pre>
62346 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62349-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62350 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62351 <span class="hljs-comment">// Prints xyz</span>
62353 myURL.<span class="hljs-property">password</span> = <span class="hljs-string">'123'</span>;
62354 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62355 <span class="hljs-comment">// Prints https://abc:123@example.com</span></code></pre>
62357 are <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which characters to
62358 percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystring_s…
62362 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62365-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62366 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62367 <span class="hljs-comment">// Prints /abc/xyz</span>
62369 myURL.<span class="hljs-property">pathname</span> = <span class="hljs-string">'/abcdef'</span>;
62370 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62371 <span class="hljs-comment">// Prints https://example.org/abcdef?123</span></code></pre>
62373 property are <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which charact…
62374 to percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_parsequerystrin…
62378 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62430-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62431 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62432 <span class="hljs-comment">// Prints 8888</span>
62434 <span class="hljs-comment">// Default ports are automatically transformed to the empty string</span>
62435 <span class="hljs-comment">// (HTTPS protocol's default port is 443)</span>
62436 myURL.<span class="hljs-property">port</span> = <span class="hljs-string">'443'</span>;
62437 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62438 <span class="hljs-comment">// Prints the empty string</span>
62439 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62440 <span class="hljs-comment">// Prints https://example.org/</span>
62442 myURL.<span class="hljs-property">port</span> = <span class="hljs-number">1234</span>;
62443 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62444 <span class="hljs-comment">// Prints 1234</span>
62445 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62446 <span class="hljs-comment">// Prints https://example.org:1234/</span>
62448 <span class="hljs-comment">// Completely invalid port strings are ignored</span>
62449 myURL.<span class="hljs-property">port</span> = <span class="hljs-string">'abcd'</span>;
62450 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62451 <span class="hljs-comment">// Prints 1234</span>
62453 <span class="hljs-comment">// Leading numbers are treated as a port number</span>
62454 myURL.<span class="hljs-property">port</span> = <span class="hljs-string">'5678abcd'</span>;
62455 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62456 <span class="hljs-comment">// Prints 5678</span>
62458 <span class="hljs-comment">// Non-integers are truncated</span>
62459 myURL.<span class="hljs-property">port</span> = <span class="hljs-number">1234.5678</span>;
62460 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62461 <span class="hljs-comment">// Prints 1234</span>
62463 <span class="hljs-comment">// Out-of-range numbers which are not represented in scientific notation…
62464 <span class="hljs-comment">// will be ignored.</span>
62465 …span class="hljs-property">port</span> = <span class="hljs-number">1e10</span>; <span class="hljs-
62466 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62467 <span class="hljs-comment">// Prints 1234</span></code></pre>
62469 such as floating-point numbers or numbers in scientific notation,
62473 <pre><code class="language-js">myURL.<span class="hljs-property">port</span> = <span class="hljs-nu…
62474 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62475 <span class="hljs-comment">// Prints 4 (because it is the leading number in the string '4.567e21')<…
62478 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62481-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62482 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62483 <span class="hljs-comment">// Prints https:</span>
62485 myURL.<span class="hljs-property">protocol</span> = <span class="hljs-string">'ftp'</span>;
62486 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62487 <span class="hljs-comment">// Prints ftp://example.org/</span></code></pre>
62494 non-special protocol, and vice versa.</p>
62496 …e-js"><span class="hljs-keyword">const</span> u = <span class="hljs-keyword">new</span> <span clas…
62497 u.<span class="hljs-property">protocol</span> = <span class="hljs-string">'https'</span>;
62498 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62499 <span class="hljs-comment">// https://example.org</span></code></pre>
62502 …e-js"><span class="hljs-keyword">const</span> u = <span class="hljs-keyword">new</span> <span clas…
62503 u.<span class="hljs-property">protocol</span> = <span class="hljs-string">'fish'</span>;
62504 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62505 <span class="hljs-comment">// http://example.org</span></code></pre>
62506 <p>Likewise, changing from a non-special protocol to a special protocol is also
62508 …e-js"><span class="hljs-keyword">const</span> u = <span class="hljs-keyword">new</span> <span clas…
62509 u.<span class="hljs-property">protocol</span> = <span class="hljs-string">'http'</span>;
62510 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62511 <span class="hljs-comment">// fish://example.org</span></code></pre>
62516 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62519-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62520 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62521 <span class="hljs-comment">// Prints ?123</span>
62523 myURL.<span class="hljs-property">search</span> = <span class="hljs-string">'abc=xyz'</span>;
62524 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62525 <span class="hljs-comment">// Prints https://example.org/abc?abc=xyz</span></code></pre>
62527 property will be <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which
62528 characters to percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_pars…
62535 URL. This property is read-only but the <code>URLSearchParams</code> object it provides
62541 different rules to determine which characters to percent-encode. For
62544-js"><span class="hljs-keyword">const</span> myUrl = <span class="hljs-keyword">new</span> <span c…
62546 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(myUrl.<span cla…
62548 <span class="hljs-comment">// Modify the URL via searchParams...</span>
62549 myUrl.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">sort</span…
62551 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(myUrl.<span cla…
62554 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
62557-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62558 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62559 <span class="hljs-comment">// Prints abc</span>
62561 myURL.<span class="hljs-property">username</span> = <span class="hljs-string">'123'</span>;
62562 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62563 <span class="hljs-comment">// Prints https://123:xyz@example.com/</span></code></pre>
62565 property will be <a href="#whatwg-percent-encoding">percent-encoded</a>. The selection of which
62566 characters to percent-encode may vary somewhat from what the <a href="#url_url_parse_urlstring_pars…
62570 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62579 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62585 with <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON…
62586 <pre><code class="language-js"><span class="hljs-keyword">const</span> myURLs = [
62587 …<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="h…
62588 …<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="h…
62590 …s-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hl…
62591 <span class="hljs-comment">// Prints ["https://www.example.com/","https://test.example.org/"]</span…
62612-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62613-variable language_">console</span>.<span class="hljs-title function_">log</span>(myURL.<span clas…
62614 <span class="hljs-comment">// Prints 123</span>
62616 …s="hljs-property">searchParams</span>.<span class="hljs-title function_">append</span>(<span class…
62617 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62618 <span class="hljs-comment">// Prints https://example.org/?abc=123&#x26;abc=xyz</span>
62620 myURL.<span class="hljs-property">searchParams</span>.<span class="hljs-title function_">delete</sp…
62621 …lass="hljs-property">searchParams</span>.<span class="hljs-title function_">set</span>(<span class…
62622 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62623 <span class="hljs-comment">// Prints https://example.org/?a=b</span>
62625-keyword">const</span> newSearchParams = <span class="hljs-keyword">new</span> <span class="hljs-t…
62626 <span class="hljs-comment">// The above is equivalent to</span>
62627 <span class="hljs-comment">// const newSearchParams = new URLSearchParams(myURL.search);</span>
62629 …ewSearchParams.<span class="hljs-title function_">append</span>(<span class="hljs-string">'a'</spa…
62630 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62631 <span class="hljs-comment">// Prints https://example.org/?a=b</span>
62632 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62633 <span class="hljs-comment">// Prints a=b&#x26;a=c</span>
62635 <span class="hljs-comment">// newSearchParams.toString() is implicitly called</span>
62636 myURL.<span class="hljs-property">search</span> = newSearchParams;
62637 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62638 <span class="hljs-comment">// Prints https://example.org/?a=b&#x26;a=c</span>
62639 newSearchParams.<span class="hljs-title function_">delete</span>(<span class="hljs-string">'a'</spa…
62640 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62641 <span class="hljs-comment">// Prints https://example.org/?a=b&#x26;a=c</span></code></pre>
62646 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
62650 <pre><code class="language-js"><span class="hljs-keyword">let</span> params;
62652 params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</…
62653-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span cla…
62654 <span class="hljs-comment">// Prints 'abc'</span>
62655 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62656 <span class="hljs-comment">// Prints 'user=abc&#x26;query=xyz'</span>
62658 params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</…
62659 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62660 <span class="hljs-comment">// Prints 'user=abc&#x26;query=xyz'</span></code></pre>
62666 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>…
62671 not allowed. Arrays are stringified using <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
62673 …<code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keywo…
62674 <span class="hljs-attr">user</span>: <span class="hljs-string">'abc'</span>,
62675 …<span class="hljs-attr">query</span>: [<span class="hljs-string">'first'</span>, <span class="hljs
62677-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span cla…
62678 <span class="hljs-comment">// Prints [ 'first,second' ]</span>
62679 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62680 <span class="hljs-comment">// Prints 'user=abc&#x26;query=first%2Csecond'</span></code></pre>
62686-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type">&#x3C;It…
62689 is similar to <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obj…
62692 <code>URLSearchParams</code>. Elements of <code>iterable</code> are key-value pairs, and can
62695 <pre><code class="language-js"><span class="hljs-keyword">let</span> params;
62697 <span class="hljs-comment">// Using an array</span>
62698 params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</…
62699 [<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>],
62700 [<span class="hljs-string">'query'</span>, <span class="hljs-string">'first'</span>],
62701 [<span class="hljs-string">'query'</span>, <span class="hljs-string">'second'</span>],
62703 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62704 <span class="hljs-comment">// Prints 'user=abc&#x26;query=first&#x26;query=second'</span>
62706 <span class="hljs-comment">// Using a Map object</span>
62707 <span class="hljs-keyword">const</span> map = <span class="hljs-keyword">new</span> <span class="hl…
62708 map.<span class="hljs-title function_">set</span>(<span class="hljs-string">'user'</span>, <span cl…
62709 map.<span class="hljs-title function_">set</span>(<span class="hljs-string">'query'</span>, <span c…
62710 params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</…
62711 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62712 <span class="hljs-comment">// Prints 'user=abc&#x26;query=xyz'</span>
62714 <span class="hljs-comment">// Using a generator function</span>
62715 <span class="hljs-keyword">function</span>* <span class="hljs-title function_">getQueryPairs</span>…
62716 …<span class="hljs-keyword">yield</span> [<span class="hljs-string">'user'</span>, <span class="hlj…
62717 …<span class="hljs-keyword">yield</span> [<span class="hljs-string">'query'</span>, <span class="hl…
62718 …<span class="hljs-keyword">yield</span> [<span class="hljs-string">'query'</span>, <span class="hl…
62720 params = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</…
62721 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62722 <span class="hljs-comment">// Prints 'user=abc&#x26;query=first&#x26;query=second'</span>
62724 <span class="hljs-comment">// Each key-value pair must have exactly two elements</span>
62725 <span class="hljs-keyword">new</span> <span class="hljs-title function_">URLSearchParams</span>([
62726 …[<span class="hljs-string">'user'</span>, <span class="hljs-string">'abc'</span>, <span class="hlj…
62728 <span class="hljs-comment">// Throws TypeError [ERR_INVALID_TUPLE]:</span>
62729 <span class="hljs-comment">// Each query pair must be an iterable [name, value] tuple</span>…
62732 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62733 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
62735 <p>Append a new name-value pair to the query string.</p>
62738 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62740 <p>Remove all name-value pairs whose name is <code>name</code>.</p>
62743 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
62745 <p>Returns an ES6 <code>Iterator</code> over each of the name-value pairs in the query.
62751 …oper.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;F…
62752 <li><code>thisArg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
62754 <p>Iterates over each name-value pair in the query and invokes the given function.</p>
62755-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
62756 … class="hljs-property">searchParams</span>.<span class="hljs-title function_">forEach</span>(<span…
62757 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
62759 <span class="hljs-comment">// Prints:</span>
62760 <span class="hljs-comment">// a b true</span>
62761 <span class="hljs-comment">// c d true</span></code></pre>
62764 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62765 …mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> …
62768 <p>Returns the value of the first name-value pair whose name is <code>name</code>. If there
62772 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62773 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62775 <p>Returns the values of all name-value pairs whose name is <code>name</code>. If there are
62779 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62780 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
62782 <p>Returns <code>true</code> if there is at least one name-value pair whose name is <code>name</cod…
62785 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
62787 <p>Returns an ES6 <code>Iterator</code> over the names of each name-value pair.</p>
62788-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span …
62789 … class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="hljs-k…
62790 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
62792 <span class="hljs-comment">// Prints:</span>
62793 <span class="hljs-comment">// foo</span>
62794 <span class="hljs-comment">// foo</span></code></pre>
62797 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62798 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
62801 <code>value</code>. If there are any pre-existing name-value pairs whose names are <code>name</code…
62803 append the name-value pair to the query string.</p>
62804 …<code class="language-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keywo…
62805 params.<span class="hljs-title function_">append</span>(<span class="hljs-string">'foo'</span>, <sp…
62806 params.<span class="hljs-title function_">append</span>(<span class="hljs-string">'foo'</span>, <sp…
62807 params.<span class="hljs-title function_">append</span>(<span class="hljs-string">'abc'</span>, <sp…
62808 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62809 <span class="hljs-comment">// Prints foo=bar&#x26;foo=baz&#x26;abc=def</span>
62811 params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'foo'</span>, <span …
62812 params.<span class="hljs-title function_">set</span>(<span class="hljs-string">'xyz'</span>, <span …
62813 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62814 <span class="hljs-comment">// Prints foo=def&#x26;abc=def&#x26;xyz=opq</span></code></pre>
62819 <p>Sort all existing name-value pairs in-place by their names. Sorting is done
62820 …rting_algorithm#Stability">stable sorting algorithm</a>, so relative order between name-value pairs
62823-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span …
62824 params.<span class="hljs-title function_">sort</span>();
62825 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62826 <span class="hljs-comment">// Prints query%5B%5D=abc&#x26;query%5B%5D=123&#x26;type=search</span></…
62829 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62832 percent-encoded where necessary.</p>
62835 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
62837 <p>Returns an ES6 <code>Iterator</code> over the values of each name-value pair.</p>
62840 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_p…
62842 <p>Returns an ES6 <code>Iterator</code> over each of the name-value pairs in the query string.
62846-js"><span class="hljs-keyword">const</span> params = <span class="hljs-keyword">new</span> <span …
62847 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> [name, value] <span …
62848 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
62850 <span class="hljs-comment">// Prints:</span>
62851 <span class="hljs-comment">// foo bar</span>
62852 <span class="hljs-comment">// xyz baz</span></code></pre>
62858 <li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
62859 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62861 <p>Returns the <a href="https://tools.ietf.org/html/rfc5891#section-4.4">Punycode</a> ASCII seriali…
62866 …code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in"…
62867-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62868 <span class="hljs-comment">// Prints xn--espaol-zwa.com</span>
62869-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62870 <span class="hljs-comment">// Prints xn--fiq228c.com</span>
62871-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62872 <span class="hljs-comment">// Prints an empty string</span></code></pre>
62878 <li><code>domain</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
62879 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62886 …code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in"…
62887-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62888 <span class="hljs-comment">// Prints español.com</span>
62889-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62890 <span class="hljs-comment">// Prints 中文.com</span>
62891-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62892 <span class="hljs-comment">// Prints an empty string</span></code></pre>
62898 …api" class="type">&#x3C;URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
62899 …loper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string…
62901 <p>This function ensures the correct decodings of percent-encoded characters as
62902 well as ensuring a cross-platform valid absolute path string.</p>
62904-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
62906-keyword">const</span> __filename = <span class="hljs-title function_">fileURLToPath</span>(<span …
62908-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62909 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///C:/path/…
62911-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62912 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file://nas/foo.t…
62914-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62915 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///你好.txt'<…
62917-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62918-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///hello world'</span>); …
62919-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62920 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///C:/path/…
62922-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62923 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file://nas/foo.t…
62925-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62926 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///你好.txt'<…
62928-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file…
62929 <span class="hljs-title function_">fileURLToPath</span>(<span class="hljs-string">'file:///hello wo…
62936 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
62938 <li><code>auth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62940 <li><code>fragment</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
62942 <li><code>search</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
62944 <li><code>unicode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
62949 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
62958-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
62959 …lass="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-t…
62961 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62962 <span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
62964 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62965 <span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
62967-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62968-comment">// Prints 'https://測試/?abc'</span></code><code class="language-js cjs"><span class="hljs
62969 …lass="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-t…
62971 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62972 <span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
62974 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
62975 <span class="hljs-comment">// Prints https://a:b@xn--g6w251d/?abc#foo</span>
62977-variable language_">console</span>.<span class="hljs-title function_">log</span>(url.<span class=…
62978 <span class="hljs-comment">// Prints 'https://測試/?abc'</span></code></pre>
62984 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
62990-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
62992-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/foo…
62993 <span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/foo#1'</span>);…
62995-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/som…
62996-title function_">pathToFileURL</span>(<span class="hljs-string">'/some/path%.c'</span>); <s…
62997 <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(__filename); …
62998 <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(__filename); …
62999 <span class="hljs-title function_">pathToFileURL</span>(__filename); <span class="hljs-c…
63000 <span class="hljs-title function_">pathToFileURL</span>(__filename); <span class="hljs-c…
63002-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/foo…
63003 <span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/foo#1'</span>);…
63005-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'/som…
63006 <span class="hljs-title function_">pathToFileURL</span>(<span class="hljs-string">'/some/path%.c'</…
63013 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
63015 <li><code>protocol</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
63016 <li><code>hostname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
63018 <li><code>hash</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63019 <li><code>search</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
63020 <li><code>pathname</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
63021 <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63025 <li><code>href</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63026 <li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63027 <li><code>auth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63034 …ss="language-js"><span class="hljs-keyword">const</span> { urlToHttpOptions } = <span class="hljs-
63035 …lass="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span class="hljs-t…
63037 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
63038 <span class="hljs-comment">/**
63041 hostname: 'xn--g6w251d',
63046 href: 'https://a:b<span class="hljs-doctag">@xn</span>--g6w251d/?abc#foo',
63062 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHAT…
63075 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHAT…
63090 <p>The <code>host</code> property is the full lower-cased host portion of the URL, including
63094 <p>The <code>hostname</code> property is the lower-cased host name portion of the <code>host</code>
63099 <code>protocol</code> and <code>host</code> components converted to lower-case.</p>
63117 <p>The <code>protocol</code> property identifies the URL's lower-cased protocol scheme.</p>
63134 forward-slash characters (<code>/</code>) are required following the colon in the
63152 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHAT…
63154 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
63160 …code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in"…
63161 url.<span class="hljs-title function_">format</span>({
63162 <span class="hljs-attr">protocol</span>: <span class="hljs-string">'https'</span>,
63163 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'example.com'</span>,
63164 <span class="hljs-attr">pathname</span>: <span class="hljs-string">'/some/path'</span>,
63165 <span class="hljs-attr">query</span>: {
63166 <span class="hljs-attr">page</span>: <span class="hljs-number">1</span>,
63167 <span class="hljs-attr">format</span>: <span class="hljs-string">'json'</span>
63171 <span class="hljs-comment">// => 'https://example.com/some/path?page=1&#x26;format=json'</span></co…
63177 <li>If <code>urlObject.protocol</code> is a string, it is appended as-is to <code>result</code>.</l…
63262 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHAT…
63264 <li><code>urlString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
63265 <li><code>parseQueryString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
63269 <li><code>slashesDenoteHost</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
63281 lenient, non-standard algorithm for parsing URL strings, security
63304 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use the WHAT…
63306 <li><code>from</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63307 <li><code>to</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
63309 <p>The <code>url.resolve()</code> method resolves a target URL relative to a base URL in a
63311 …code class="language-js"><span class="hljs-keyword">const</span> url = <span class="hljs-built_in"…
63312-title function_">resolve</span>(<span class="hljs-string">'/one/two/three'</span>, <span class="h…
63313-title function_">resolve</span>(<span class="hljs-string">'http://example.com/'</span>, <span cla…
63314-title function_">resolve</span>(<span class="hljs-string">'http://example.com/one'</span>, <span …
63316 …guage-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">resolve</s…
63317-keyword">const</span> resolvedUrl = <span class="hljs-keyword">new</span> <span class="hljs-title…
63318 …<span class="hljs-keyword">if</span> (resolvedUrl.<span class="hljs-property">protocol</span> === …
63319 <span class="hljs-comment">// `from` is a relative URL.</span>
63320 <span class="hljs-keyword">const</span> { pathname, search, hash } = resolvedUrl;
63321 <span class="hljs-keyword">return</span> pathname + search + hash;
63323 …<span class="hljs-keyword">return</span> resolvedUrl.<span class="hljs-title function_">toString</…
63326-title function_">resolve</span>(<span class="hljs-string">'/one/two/three'</span>, <span class="h…
63327-title function_">resolve</span>(<span class="hljs-string">'http://example.com/'</span>, <span cla…
63328-title function_">resolve</span>(<span class="hljs-string">'http://example.com/one'</span>, <span …
63329 <p><a id="whatwg-percent-encoding"></a></p>
63330 </section><section><h3>Percent-encoding in URLs<span><a class="mark" href="#url_percent_encoding_in…
63338 <pre><code class="language-text">&#x3C; > " ` \r \n \t { } | \ ^ '</code></pre>
63344 <p>The WHATWG algorithm defines four "percent-encode sets" that describe ranges
63345 of characters that must be percent-encoded:</p>
63348 <p>The <em>C0 control percent-encode set</em> includes code points in range U+0000 to
63352 <p>The <em>fragment percent-encode set</em> includes the <em>C0 control percent-encode set</em>
63356 <p>The <em>path percent-encode set</em> includes the <em>C0 control percent-encode set</em>
63361 <p>The <em>userinfo encode set</em> includes the <em>path percent-encode set</em> and code
63366 <p>The <em>userinfo percent-encode set</em> is used exclusively for username and
63367 passwords encoded within the URL. The <em>path percent-encode set</em> is used for the
63368 path of most URLs. The <em>fragment percent-encode set</em> is used for URL fragments.
63369 The <em>C0 control percent-encode set</em> is used for host and path under certain
63371 <p>When non-ASCII characters appear within a host name, the host name is encoded
63372 using the <a href="https://tools.ietf.org/html/rfc5891#section-4.4">Punycode</a> algorithm. Note, h…
63373 <em>both</em> Punycode encoded and percent-encoded characters:</p>
63374-js"><span class="hljs-keyword">const</span> myURL = <span class="hljs-keyword">new</span> <span c…
63375 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
63376 <span class="hljs-comment">// Prints https://xn--1xa.example.com/foo</span>
63377 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
63378 <span class="hljs-comment">// Prints https://xn--1xa.example.com</span></code></pre></section>
63382 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
63387 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63393 <li><code>original</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
63394 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
63397 function following the error-first callback style, i.e. taking
63401 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63403 … class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-ti…
63404 <span class="hljs-keyword">return</span> <span class="hljs-string">'hello world'</span>;
63406 <span class="hljs-keyword">const</span> callbackFunction = util.<span class="hljs-title function_">…
63408 <span class="hljs-title function_">callbackFunction</span>(<span class="hljs-function">(<span class…
63409 <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
63410 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
63413 <pre><code class="language-text">hello world</code></pre>
63421 …<code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title funct…
63422 …s="hljs-keyword">return</span> <span class="hljs-title class_">Promise</span>.<span class="hljs-ti…
63424 <span class="hljs-keyword">const</span> callbackFunction = util.<span class="hljs-title function_">…
63426 <span class="hljs-title function_">callbackFunction</span>(<span class="hljs-function">(<span class…
63427 …<span class="hljs-comment">// When the Promise was rejected with `null` it is wrapped with an Erro…
63428 <span class="hljs-comment">// the original value is stored in `reason`.</span>
63429-title function_">hasOwnProperty</span>(<span class="hljs-string">'reason'</span>) &#x26;&#x26; er…
63436 <li><code>section</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
63438 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
63440 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
63446 …ror_data_args"><code>console.error()</code></a>. If not, then the returned function is a no-op.</p>
63447 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63448 <span class="hljs-keyword">const</span> debuglog = util.<span class="hljs-title function_">debuglog…
63450 <span class="hljs-title function_">debuglog</span>(<span class="hljs-string">'hello from foo [%d]'<…
63453 <pre><code class="language-console">FOO 3245: hello from foo [123]</code></pre>
63457 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63458 …span class="hljs-keyword">const</span> debuglog = util.<span class="hljs-title function_">debuglog…
63460 <span class="hljs-title function_">debuglog</span>(<span class="hljs-string">'hi there, it\'s foo-b…
63463 <pre><code class="language-console">FOO-BAR 3257: hi there, it's foo-bar [2333]</code></pre>
63464 <p>Multiple comma-separated <code>section</code> names may be specified in the <code>NODE_DEBUG</co…
63469 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63470-keyword">let</span> debuglog = util.<span class="hljs-title function_">debuglog</span>(<span clas…
63471 <span class="hljs-comment">// Replace with a logging function that optimizes out</span>
63472 <span class="hljs-comment">// testing if the section is enabled</span>
63480 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
63487 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63488 …ljs-keyword">const</span> enabled = util.<span class="hljs-title function_">debuglog</span>(<span …
63489 <span class="hljs-keyword">if</span> (enabled) {
63490-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
63494 <pre><code class="language-console">hello from foo [123]</code></pre>
63514 <li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob…
63515 <li><code>msg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
63517 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
63519 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
63523 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63525-built_in">exports</span>.<span class="hljs-property">obsoleteFunction</span> = util.<span class="…
63526 <span class="hljs-comment">// Do something here.</span>
63527 }, <span class="hljs-string">'obsoleteFunction() is deprecated. Use newShinyFunction() instead.'</s…
63535 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63537 …class="hljs-keyword">const</span> fn1 = util.<span class="hljs-title function_">deprecate</span>(s…
63538 …="hljs-keyword">const</span> fn2 = util.<span class="hljs-title function_">deprecate</span>(someOt…
63539 <span class="hljs-title function_">fn1</span>(); <span class="hljs-comment">// Emits a deprecation …
63540 <span class="hljs-title function_">fn2</span>(); <span class="hljs-comment">// Does not emit a depr…
63541 <p>If either the <code>--no-deprecation</code> or <code>--no-warnings</code> command-line flags are
63544 <p>If the <code>--trace-deprecation</code> or <code>--trace-warnings</code> command-line flags are …
63548 <p>If the <code>--throw-deprecation</code> command-line flag is set, or the
63551 <p>The <code>--throw-deprecation</code> command-line flag and <code>process.throwDeprecation</code>
63552 property take precedence over <code>--trace-deprecation</code> and
63581 …eveloper.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;str…
63584 as a <code>printf</code>-like format string which can contain zero or more format
63589 and <code>-0</code>. <code>BigInt</code> values will be represented with an <code>n</code> and Obje…
63602 including non-enumerable properties and proxies.</li>
63605 the full object not including non-enumerable properties and proxies.</li>
63607 <li><code>%%</code>: single percent sign (<code>'%'</code>). This does not consume an argument.</li>
63608 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
63611 …e class="language-js">util.<span class="hljs-title function_">format</span>(<span class="hljs-stri…
63612 <span class="hljs-comment">// Returns: 'foo:%s'</span></code></pre>
63618-js">util.<span class="hljs-title function_">format</span>(<span class="hljs-string">'%s:%s'</span…
63619 <span class="hljs-comment">// Returns: 'foo:bar baz'</span></code></pre>
63622 …ge-js">util.<span class="hljs-title function_">format</span>(<span class="hljs-number">1</span>, <…
63623 <span class="hljs-comment">// Returns: '1 2 3'</span></code></pre>
63626 <pre><code class="language-js">util.<span class="hljs-title function_">format</span>(<span class="h…
63627 <span class="hljs-comment">// Returns: '%% %s'</span></code></pre>
63636 <li><code>inspectOptions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
63637 <li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
63642-js">util.<span class="hljs-title function_">formatWithOptions</span>({ <span class="hljs-attr">co…
63643 <span class="hljs-comment">// Returns 'See object { foo: 42 }', where `42` is colored as a number</…
63644 <span class="hljs-comment">// when printed to a terminal.</span></code></pre>
63650 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
63651 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
63654 The mapping between error codes and error names is platform-dependent.
63656-js">fs.<span class="hljs-title function_">access</span>(<span class="hljs-string">'file/that/does…
63657 …<span class="hljs-keyword">const</span> name = util.<span class="hljs-title function_">getSystemEr…
63658 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
63665 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
63668 The mapping between error codes and error names is platform-dependent.
63670-js">fs.<span class="hljs-title function_">access</span>(<span class="hljs-string">'file/that/does…
63671 …<span class="hljs-keyword">const</span> errorMap = util.<span class="hljs-title function_">getSyst…
63672 …<span class="hljs-keyword">const</span> name = errorMap.<span class="hljs-title function_">get</sp…
63673 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
63687 … api_stability_3"><a href="#documentation_stability_index">Stability: 3</a> - Legacy: Use ES2015 c…
63689 <li><code>constructor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer…
63690 <li><code>superConstructor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
63695 <p>Inherit the prototype methods from one <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
63702 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63703 …s="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="h…
63705 <span class="hljs-keyword">function</span> <span class="hljs-title function_">MyStream</span>(<span…
63706 …<span class="hljs-title class_">EventEmitter</span>.<span class="hljs-title function_">call</span>…
63709 util.<span class="hljs-title function_">inherits</span>(<span class="hljs-title class_">MyStream</s…
63711-title class_">MyStream</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</s…
63712 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<…
63715 <span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class=…
63717-variable language_">console</span>.<span class="hljs-title function_">log</span>(stream <span cla…
63718-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
63720 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
63721 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
63723 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'It works!'</span>…
63725-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> =…
63727 …class="hljs-keyword">class</span> <span class="hljs-title class_">MyStream</span> <span class="hlj…
63728 <span class="hljs-title function_">write</span>(<span class="hljs-params">data</span>) {
63729 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">emit</span>(<…
63733 <span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> <span class=…
63735 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class…
63736 …"hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class…
63738 stream.<span class="hljs-title function_">write</span>(<span class="hljs-string">'With ES6'</span>)…
63748 …text</code> now, a custom inspection function on it will not receive context-specific arguments an…
63791 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
63792 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
63794 …la.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If …
63795 properties are included in the formatted result. <a href="https://developer.mozilla.org/en-US/docs/…
63796 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet">…
63798 <li><code>depth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
63802 <li><code>colors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
63805 <li><code>customInspect</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
63808 <li><code>showProxy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
63809 …ef="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy#Termino…
63810 <li><code>maxArrayLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
63811-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>, <a href=…
63814 <li><code>maxStringLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
63817 <li><code>breakLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
63818 multiple lines. Set to <code>Infinity</code> to format the input as a single line
63821 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
63824 are united on a single line as long as all properties fit into
63828 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
63830 string. If set to <code>true</code> the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
63831 it is used as a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_O…
63832 …illa.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> |…
63839 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
63847 <pre><code class="language-js"><span class="hljs-keyword">class</span> <span class="hljs-title clas…
63848 …get [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toStringTag</span>]…
63849 <span class="hljs-keyword">return</span> <span class="hljs-string">'bar'</span>;
63853 <span class="hljs-keyword">class</span> <span class="hljs-title class_">Bar</span> {}
63855-keyword">const</span> baz = <span class="hljs-title class_">Object</span>.<span class="hljs-title…
63857 …ss="hljs-title function_">inspect</span>(<span class="hljs-keyword">new</span> <span class="hljs-t…
63858 …ss="hljs-title function_">inspect</span>(<span class="hljs-keyword">new</span> <span class="hljs-t…
63859 util.<span class="hljs-title function_">inspect</span>(baz); <span class="hljs-comment">// '[…
63861 … class="language-js"><span class="hljs-keyword">const</span> { inspect } = <span class="hljs-built…
63863 <span class="hljs-keyword">const</span> obj = {};
63864 obj.<span class="hljs-property">a</span> = [obj];
63865 obj.<span class="hljs-property">b</span> = {};
63866 obj.<span class="hljs-property">b</span>.<span class="hljs-property">inner</span> = obj.<span class…
63867 obj.<span class="hljs-property">b</span>.<span class="hljs-property">obj</span> = obj;
63869 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
63870 <span class="hljs-comment">// &#x3C;ref *1> {</span>
63871 <span class="hljs-comment">// a: [ [Circular *1] ],</span>
63872 <span class="hljs-comment">// b: &#x3C;ref *2> { inner: [Circular *2], obj: [Circular *1] }</span>
63873 <span class="hljs-comment">// }</span></code></pre>
63875 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63877-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class…
63879 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
63881 <span class="hljs-keyword">const</span> o = {
63882 …<span class="hljs-attr">a</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">2…
63883 …<span class="hljs-string">'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do '</span…
63884 … <span class="hljs-string">'eiusmod tempor incididunt ut labore et dolore magna aliqua.'</span>,
63885 <span class="hljs-string">'test'</span>,
63886 <span class="hljs-string">'foo'</span>]], <span class="hljs-number">4</span>],
63887-attr">b</span>: <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>(…
63889-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class…
63891 <span class="hljs-comment">// { a:</span>
63892 <span class="hljs-comment">// [ 1,</span>
63893 <span class="hljs-comment">// 2,</span>
63894 <span class="hljs-comment">// [ [ 'Lorem ipsum dolor sit amet, consectetur [...]', // A long li…
63895 <span class="hljs-comment">// 'test',</span>
63896 <span class="hljs-comment">// 'foo' ] ],</span>
63897 <span class="hljs-comment">// 4 ],</span>
63898 <span class="hljs-comment">// b: Map(2) { 'za' => 1, 'zb' => 'test' } }</span>
63900 <span class="hljs-comment">// Setting `compact` to false changes the output to be more reader frien…
63901-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class…
63903 <span class="hljs-comment">// {</span>
63904 <span class="hljs-comment">// a: [</span>
63905 <span class="hljs-comment">// 1,</span>
63906 <span class="hljs-comment">// 2,</span>
63907 <span class="hljs-comment">// [</span>
63908 <span class="hljs-comment">// [</span>
63909 <span class="hljs-comment">// 'Lorem ipsum dolor sit amet, consectetur ' +</span>
63910 <span class="hljs-comment">// 'adipiscing elit, sed do eiusmod tempor ' +</span>
63911 <span class="hljs-comment">// 'incididunt ut labore et dolore magna ' +</span>
63912 <span class="hljs-comment">// 'aliqua.,</span>
63913 <span class="hljs-comment">// 'test',</span>
63914 <span class="hljs-comment">// 'foo'</span>
63915 <span class="hljs-comment">// ]</span>
63916 <span class="hljs-comment">// ],</span>
63917 <span class="hljs-comment">// 4</span>
63918 <span class="hljs-comment">// ],</span>
63919 <span class="hljs-comment">// b: Map(2) {</span>
63920 <span class="hljs-comment">// 'za' => 1,</span>
63921 <span class="hljs-comment">// 'zb' => 'test'</span>
63922 <span class="hljs-comment">// }</span>
63923 <span class="hljs-comment">// }</span>
63925 <span class="hljs-comment">// Setting `breakLength` to e.g. 150 will print the "Lorem ipsum" text i…
63926 <span class="hljs-comment">// single line.</span>
63927 <span class="hljs-comment">// Reducing the `breakLength` will split the "Lorem ipsum" text in small…
63928 <span class="hljs-comment">// chunks.</span></code></pre>
63929 …zilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and…
63932 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet">…
63934 … class="language-js"><span class="hljs-keyword">const</span> { inspect } = <span class="hljs-built…
63936 <span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">a</span>: <span class="hljs
63937 <span class="hljs-keyword">const</span> obj2 = { <span class="hljs-attr">b</span>: <span class="hlj…
63938 <span class="hljs-keyword">const</span> weakSet = <span class="hljs-keyword">new</span> <span class…
63940-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
63941 <span class="hljs-comment">// WeakSet { { a: 1 }, { b: 2 } }</span></code></pre>
63944 … class="language-js"><span class="hljs-keyword">const</span> { inspect } = <span class="hljs-built…
63945 <span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span c…
63947 <span class="hljs-keyword">const</span> o1 = {
63948 …<span class="hljs-attr">b</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">3…
63949 <span class="hljs-attr">a</span>: <span class="hljs-string">'`a` comes before `b`'</span>,
63950-attr">c</span>: <span class="hljs-keyword">new</span> <span class="hljs-title class_">Set</span>(…
63952-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
63953 <span class="hljs-comment">// { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set(3) { 1, 2, 3 } }<…
63954-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
63955 <span class="hljs-comment">// { c: Set(3) { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' }<…
63957 <span class="hljs-keyword">const</span> o2 = {
63958-attr">c</span>: <span class="hljs-keyword">new</span> <span class="hljs-title class_">Set</span>(…
63959 <span class="hljs-attr">a</span>: <span class="hljs-string">'`a` comes before `b`'</span>,
63960 …<span class="hljs-attr">b</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">3…
63962 assert.<span class="hljs-property">strict</span>.<span class="hljs-title function_">equal</span>(
63963 …<span class="hljs-title function_">inspect</span>(o1, { <span class="hljs-attr">sorted</span>: <sp…
63964 …<span class="hljs-title function_">inspect</span>(o2, { <span class="hljs-attr">sorted</span>: <sp…
63998 <li><code>reset</code> - Resets all (color) modifiers to their defaults</li>
63999 <li><strong>bold</strong> - Make text bold</li>
64000 <li><em>italic</em> - Make text italic</li>
64001 <li><span style="border-bottom: 1px;">underline</span> - Make text underlined</li>
64002 <li><del>strikethrough</del> - Puts a horizontal line through the center of the text
64004 <li><code>hidden</code> - Prints the text, but makes it invisible (Alias: conceal)</li>
64005 <li><span style="opacity: 0.5;">dim</span> - Decreased color intensity (Alias:
64007 <li><span style="border-top: 1px">overlined</span> - Make text overlined</li>
64008 <li>blink - Hides and shows the text in an interval</li>
64009 <li><span style="filter: invert(100%)">inverse</span> - Swap foreground and
64011 <li><span style="border-bottom: 1px double;">doubleunderline</span> - Make text
64013 <li><span style="border: 1px">framed</span> - Draw a frame around the text</li>
64059 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64061 <span class="hljs-keyword">class</span> <span class="hljs-title class_">Box</span> {
64062 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">value</span>) {
64063 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span> = value;
64066 …[util.<span class="hljs-property">inspect</span>.<span class="hljs-property">custom</span>](depth,…
64067 <span class="hljs-keyword">if</span> (depth &#x3C; <span class="hljs-number">0</span>) {
64068 …"hljs-keyword">return</span> options.<span class="hljs-title function_">stylize</span>(<span class…
64071 …<span class="hljs-keyword">const</span> newOptions = <span class="hljs-title class_">Object</span>…
64072-attr">depth</span>: options.<span class="hljs-property">depth</span> === <span class="hljs-litera…
64075 <span class="hljs-comment">// Five space padding because that's the size of "Box&#x3C; ".</span>
64076 …ss="hljs-keyword">const</span> padding = <span class="hljs-string">' '</span>.<span class="hljs-ti…
64077-keyword">const</span> inner = util.<span class="hljs-title function_">inspect</span>(<span class=…
64078 …span class="hljs-title function_">replace</span>(<span class="hljs-regexp">/\n/g</span>, <span cla…
64079-keyword">return</span> <span class="hljs-string">`<span class="hljs-subst">${options.stylize(<spa…
64083 … class="hljs-keyword">const</span> box = <span class="hljs-keyword">new</span> <span class="hljs-t…
64085 util.<span class="hljs-title function_">inspect</span>(box);
64086 <span class="hljs-comment">// Returns: "Box&#x3C; true >"</span></code></pre>
64090 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64092 <span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">foo</span>: <span class="hl…
64093 …<span class="hljs-property">inspect</span>.<span class="hljs-property">custom</span>] = <span clas…
64094 …<span class="hljs-keyword">return</span> { <span class="hljs-attr">bar</span>: <span class="hljs-s…
64097 util.<span class="hljs-title function_">inspect</span>(obj);
64098 <span class="hljs-comment">// Returns: "{ bar: 'baz' }"</span></code></pre>
64112 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" c…
64115 symbol is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects…
64117-js"><span class="hljs-keyword">const</span> inspect = <span class="hljs-title class_">Symbol</spa…
64119 <span class="hljs-keyword">class</span> <span class="hljs-title class_">Password</span> {
64120 <span class="hljs-title function_">constructor</span>(<span class="hljs-params">value</span>) {
64121 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span> = value;
64124 <span class="hljs-title function_">toString</span>(<span class="hljs-params"></span>) {
64125 <span class="hljs-keyword">return</span> <span class="hljs-string">'xxxxxxxx'</span>;
64129 …<span class="hljs-keyword">return</span> <span class="hljs-string">`Password &#x3C;<span class="hl…
64133 …s="hljs-keyword">const</span> password = <span class="hljs-keyword">new</span> <span class="hljs-t…
64134 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
64135 <span class="hljs-comment">// Prints Password &#x3C;xxxxxxxx></span></code></pre>
64146 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64147-keyword">const</span> arr = <span class="hljs-title class_">Array</span>(<span class="hljs-number…
64149 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
64150 …ljs-property">inspect</span>.<span class="hljs-property">defaultOptions</span>.<span class="hljs-p…
64151 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
64157 <li><code>val1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
64158 <li><code>val2</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
64159 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64170 <li><code>original</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
64171 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
64173 <p>Takes a function following the common error-first callback style, i.e. taking
64176 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64177 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
64179 <span class="hljs-keyword">const</span> stat = util.<span class="hljs-title function_">promisify</s…
64180-title function_">stat</span>(<span class="hljs-string">'.'</span>).<span class="hljs-title functi…
64181 <span class="hljs-comment">// Do something with `stats`</span>
64182 }).<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-p…
64183 <span class="hljs-comment">// Handle the error.</span>
64186 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64187 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
64189 <span class="hljs-keyword">const</span> stat = util.<span class="hljs-title function_">promisify</s…
64191 …ass="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title…
64192 …ss="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span class="hljs-t…
64193-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
64200 an error-first callback, it will still be passed an error-first
64204 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64206 <span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> {
64207 <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) {
64208 …<span class="hljs-variable language_">this</span>.<span class="hljs-property">a</span> = <span cla…
64211 <span class="hljs-title function_">bar</span>(<span class="hljs-params">callback</span>) {
64212 …"hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, <span class="hljs-v…
64216 <span class="hljs-keyword">const</span> foo = <span class="hljs-keyword">new</span> <span class="hl…
64218 <span class="hljs-keyword">const</span> naiveBar = util.<span class="hljs-title function_">promisif…
64219 <span class="hljs-comment">// TypeError: Cannot read property 'a' of undefined</span>
64220 <span class="hljs-comment">// naiveBar().then(a => console.log(a));</span>
64222-title function_">call</span>(foo).<span class="hljs-title function_">then</span>(<span class="hlj…
64224 <span class="hljs-keyword">const</span> bindBar = naiveBar.<span class="hljs-title function_">bind<…
64225-title function_">bindBar</span>().<span class="hljs-title function_">then</span>(<span class="hlj…
64229 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
64231 <span class="hljs-keyword">function</span> <span class="hljs-title function_">doSomething</span>(<s…
64232 <span class="hljs-comment">// ...</span>
64235 …span class="hljs-property">promisify</span>.<span class="hljs-property">custom</span>] = <span cla…
64236 …<span class="hljs-keyword">return</span> <span class="hljs-title function_">getPromiseSomehow</spa…
64239 <span class="hljs-keyword">const</span> promisified = util.<span class="hljs-title function_">promi…
64240-variable language_">console</span>.<span class="hljs-title function_">log</span>(promisified === …
64241 <span class="hljs-comment">// prints 'true'</span></code></pre>
64243 standard format of taking an error-first callback as the last argument.</p>
64246-js">doSomething[util.<span class="hljs-property">promisify</span>.<span class="hljs-property">cus…
64247-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Prom…
64248 <span class="hljs-title function_">doSomething</span>(foo, resolve, reject);
64266 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" c…
64270 symbol is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects…
64274-js"><span class="hljs-keyword">const</span> kCustomPromisifiedSymbol = <span class="hljs-title cl…
64276 doSomething[kCustomPromisifiedSymbol] = <span class="hljs-function">(<span class="hljs-params">foo<…
64277-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Prom…
64278 <span class="hljs-title function_">doSomething</span>(foo, resolve, reject);
64286-js"><span class="hljs-keyword">const</span> decoder = <span class="hljs-keyword">new</span> <span…
64287 <span class="hljs-keyword">let</span> string = <span class="hljs-string">''</span>;
64288 <span class="hljs-keyword">let</span> buffer;
64289 <span class="hljs-keyword">while</span> (buffer = <span class="hljs-title function_">getNextChunkSo…
64290 …= decoder.<span class="hljs-title function_">decode</span>(buffer, { <span class="hljs-attr">strea…
64292 string += decoder.<span class="hljs-title function_">decode</span>(); <span class="hljs-comment">//…
64445-8859-2'</code></td><td><code>'csisolatin2'</code>, <code>'iso-ir-101'</code>, <code>'iso8859-2'</…
64446 <h5>Encodings supported when Node.js is built with the <code>small-icu</code> option<span><a class=…
64468-8'</code></td><td><code>'unicode-1-1-utf-8'</code>, <code>'utf8'</code></td></tr><tr><td><code>'u…
64487 …<code>'utf-8'</code></td><td><code>'unicode-1-1-utf-8'</code>, <code>'utf8'</code></td></tr><tr><t…
64488 <p>The <code>'iso-8859-16'</code> encoding listed in the <a href="https://encoding.spec.whatwg.org/…
64503 <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
64504 supports. <strong>Default:</strong> <code>'utf-8'</code>.</li>
64505 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
64507 <li><code>fatal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64510 <li><code>ignoreBOM</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
64513 <code>'utf-8'</code>, <code>'utf-16be'</code> or <code>'utf-16le'</code>. <strong>Default:</strong>…
64522-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> …
64524 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
64526 <li><code>stream</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
64530 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
64539 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
64544 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
64550 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
64567 instances of <code>TextEncoder</code> only support UTF-8 encoding.</p>
64568 …<code class="language-js"><span class="hljs-keyword">const</span> encoder = <span class="hljs-keyw…
64569 <span class="hljs-keyword">const</span> uint8array = encoder.<span class="hljs-title function_">enc…
64573 <li><code>input</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64574 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
64576 <p>UTF-8 encodes the <code>input</code> string and returns a <code>Uint8Array</code> containing the
64580 <li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
64581 <li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
64582 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
64584 <li><code>read</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
64585 …//developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;…
64589 <p>UTF-8 encodes the <code>src</code> string to the <code>dest</code> Uint8Array and returns an obj…
64590 containing the read Unicode code units and written UTF-8 bytes.</p>
64591 …<code class="language-js"><span class="hljs-keyword">const</span> encoder = <span class="hljs-keyw…
64592 <span class="hljs-keyword">const</span> src = <span class="hljs-string">'this is some data'</span>;
64593-keyword">const</span> dest = <span class="hljs-keyword">new</span> <span class="hljs-title class_…
64594 <span class="hljs-keyword">const</span> { read, written } = encoder.<span class="hljs-title functio…
64597 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
64599 <p>The encoding supported by the <code>TextEncoder</code> instance. Always set to <code>'utf-8'</co…
64605 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
64614 <p><code>util.types</code> provides type checks for different kinds of built-in objects.
64626 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64627 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64629 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64630 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArr…
64633-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isAnyArrayBu…
64634-property">types</span>.<span class="hljs-title function_">isAnyArrayBuffer</span>(<span class="hl…
64640 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64641 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64643 …value is an instance of one of the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
64644 views, such as typed array objects or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
64645 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuff…
64646-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isArrayBuffe…
64647-property">types</span>.<span class="hljs-title function_">isArrayBufferView</span>(<span class="h…
64648-property">types</span>.<span class="hljs-title function_">isArrayBufferView</span>(<span class="h…
64649-property">types</span>.<span class="hljs-title function_">isArrayBufferView</span>(<span class="h…
64655 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64656 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64659 <!-- eslint-disable prefer-rest-params -->
64660 …code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title functi…
64661-property">types</span>.<span class="hljs-title function_">isArgumentsObject</span>(<span class="h…
64668 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64669 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64671 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64672 This does <em>not</em> include <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
64674-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isArrayBuffe…
64675-property">types</span>.<span class="hljs-title function_">isArrayBuffer</span>(<span class="hljs-
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…
64684 <p>Returns <code>true</code> if the value is an <a href="https://developer.mozilla.org/en-US/docs/W…
64688-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isAsyncFunct…
64689-property">types</span>.<span class="hljs-title function_">isAsyncFunction</span>(<span class="hlj…
64695 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64696 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64699-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBigInt64Ar…
64700-property">types</span>.<span class="hljs-title function_">isBigInt64Array</span>(<span class="hlj…
64706 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64707 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64710-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBigUint64A…
64711-property">types</span>.<span class="hljs-title function_">isBigUint64Array</span>(<span class="hl…
64717 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64718 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64722-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBooleanObj…
64723 …"hljs-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class…
64724-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class="hlj…
64725-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class="hlj…
64726-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class="hlj…
64727-property">types</span>.<span class="hljs-title function_">isBooleanObject</span>(<span class="hlj…
64733 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64734 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64739-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isBoxedPrimi…
64740-property">types</span>.<span class="hljs-title function_">isBoxedPrimitive</span>(<span class="hl…
64741-property">types</span>.<span class="hljs-title function_">isBoxedPrimitive</span>(<span class="hl…
64742-property">types</span>.<span class="hljs-title function_">isBoxedPrimitive</span>(<span class="hl…
64743-property">types</span>.<span class="hljs-title function_">isBoxedPrimitive</span>(<span class="hl…
64749 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64750 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64752 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64753-js"><span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">new</span> <span clas…
64754-property">types</span>.<span class="hljs-title function_">isDataView</span>(<span class="hljs-key…
64755-property">types</span>.<span class="hljs-title function_">isDataView</span>(<span class="hljs-key…
64756 <p>See also <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objec…
64762 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64763 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64765 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64766-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isDate</span…
64772 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64773 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64779 addons. In JavaScript, they are <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
64781 <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <s…
64782 <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&#x3C;…
64784 <span class="hljs-keyword">static</span> napi_value <span class="hljs-title function_">MyNapi</span…
64785 …pan class="hljs-type">int</span>* raw = (<span class="hljs-type">int</span>*) <span class="hljs-bu…
64786 …e_external(env, (<span class="hljs-type">void</span>*) raw, <span class="hljs-literal">NULL</span>…
64787 <span class="hljs-keyword">if</span> (status != napi_ok) {
64788 …napi_throw_error(env, <span class="hljs-literal">NULL</span>, <span class="hljs-string">"napi_crea…
64789 <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;
64791 <span class="hljs-keyword">return</span> result;
64794 DECLARE_NAPI_PROPERTY(<span class="hljs-string">"myNapi"</span>, MyNapi)
64796 …ode class="language-js"><span class="hljs-keyword">const</span> native = <span class="hljs-built_i…
64797 <span class="hljs-keyword">const</span> data = native.<span class="hljs-title function_">myNapi</sp…
64798 util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isExternal</span>(…
64799 …lass="hljs-property">types</span>.<span class="hljs-title function_">isExternal</span>(<span class…
64800-property">types</span>.<span class="hljs-title function_">isExternal</span>(<span class="hljs-key…
64808 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64809 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64811 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64812-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isFloat32Arr…
64813-property">types</span>.<span class="hljs-title function_">isFloat32Array</span>(<span class="hljs
64814-property">types</span>.<span class="hljs-title function_">isFloat32Array</span>(<span class="hljs
64820 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64821 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64823 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64824-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isFloat64Arr…
64825-property">types</span>.<span class="hljs-title function_">isFloat64Array</span>(<span class="hljs
64826-property">types</span>.<span class="hljs-title function_">isFloat64Array</span>(<span class="hljs
64832 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64833 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64839-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isGeneratorF…
64840-property">types</span>.<span class="hljs-title function_">isGeneratorFunction</span>(<span class=…
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…
64850 built-in generator function.
64854 …code class="language-js"><span class="hljs-keyword">function</span>* <span class="hljs-title funct…
64855 <span class="hljs-keyword">const</span> generator = <span class="hljs-title function_">foo</span>();
64856 …l.<span class="hljs-property">types</span>.<span class="hljs-title function_">isGeneratorObject</s…
64862 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64863 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64865 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64866-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isInt8Array<…
64867-property">types</span>.<span class="hljs-title function_">isInt8Array</span>(<span class="hljs-ke…
64868-property">types</span>.<span class="hljs-title function_">isInt8Array</span>(<span class="hljs-ke…
64874 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64875 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64877 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64878-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isInt16Array…
64879-property">types</span>.<span class="hljs-title function_">isInt16Array</span>(<span class="hljs-k…
64880-property">types</span>.<span class="hljs-title function_">isInt16Array</span>(<span class="hljs-k…
64886 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64887 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64889 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64890-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isInt32Array…
64891-property">types</span>.<span class="hljs-title function_">isInt32Array</span>(<span class="hljs-k…
64892-property">types</span>.<span class="hljs-title function_">isInt32Array</span>(<span class="hljs-k…
64898 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64899 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64901 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64902-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isMap</span>…
64908 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64909 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64911 <p>Returns <code>true</code> if the value is an iterator returned for a built-in
64912 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><cod…
64913 …e><code class="language-js"><span class="hljs-keyword">const</span> map = <span class="hljs-keywor…
64914-property">types</span>.<span class="hljs-title function_">isMapIterator</span>(map.<span class="h…
64915-property">types</span>.<span class="hljs-title function_">isMapIterator</span>(map.<span class="h…
64916-property">types</span>.<span class="hljs-title function_">isMapIterator</span>(map.<span class="h…
64917-property">types</span>.<span class="hljs-title function_">isMapIterator</span>(map[<span class="h…
64923 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64924 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64926 …is an instance of a <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects">…
64927 <!-- eslint-skip -->
64928 …uage-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> ns <span …
64930 …l.<span class="hljs-property">types</span>.<span class="hljs-title function_">isModuleNamespaceObj…
64936 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64937 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64939 <p>Returns <code>true</code> if the value is an instance of a built-in <a href="#errors_class_error…
64940-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isNativeErro…
64941-property">types</span>.<span class="hljs-title function_">isNativeError</span>(<span class="hljs-
64942-property">types</span>.<span class="hljs-title function_">isNativeError</span>(<span class="hljs-
64948 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64949 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64953-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isNumberObje…
64954-property">types</span>.<span class="hljs-title function_">isNumberObject</span>(<span class="hljs
64960 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64961 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64963 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64964-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isPromise</s…
64970 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64971 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64973 <p>Returns <code>true</code> if the value is a <a href="https://developer.mozilla.org/en-US/docs/We…
64974 <pre><code class="language-js"><span class="hljs-keyword">const</span> target = {};
64975 …span class="hljs-keyword">const</span> proxy = <span class="hljs-keyword">new</span> <span class="…
64976 util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isProxy</span>(tar…
64977 util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isProxy</span>(pro…
64983 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64984 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64987-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isRegExp</sp…
64988-property">types</span>.<span class="hljs-title function_">isRegExp</span>(<span class="hljs-keywo…
64994 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
64995 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
64997 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
64998-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSet</span>…
65004 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65005 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65007 <p>Returns <code>true</code> if the value is an iterator returned for a built-in
65008 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set"><cod…
65009 …e><code class="language-js"><span class="hljs-keyword">const</span> set = <span class="hljs-keywor…
65010-property">types</span>.<span class="hljs-title function_">isSetIterator</span>(set.<span class="h…
65011-property">types</span>.<span class="hljs-title function_">isSetIterator</span>(set.<span class="h…
65012-property">types</span>.<span class="hljs-title function_">isSetIterator</span>(set.<span class="h…
65013-property">types</span>.<span class="hljs-title function_">isSetIterator</span>(set[<span class="h…
65019 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65020 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65022 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65023 This does <em>not</em> include <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
65025-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSharedArra…
65026-property">types</span>.<span class="hljs-title function_">isSharedArrayBuffer</span>(<span class=…
65032 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65033 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65037-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isStringObje…
65038-property">types</span>.<span class="hljs-title function_">isStringObject</span>(<span class="hljs
65044 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65045 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65049 …e class="language-js"><span class="hljs-keyword">const</span> symbol = <span class="hljs-title cla…
65050 util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isSymbolObject</sp…
65051-property">types</span>.<span class="hljs-title function_">isSymbolObject</span>(<span class="hljs
65057 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65058 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65060 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65061-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isTypedArray…
65062-property">types</span>.<span class="hljs-title function_">isTypedArray</span>(<span class="hljs-k…
65063-property">types</span>.<span class="hljs-title function_">isTypedArray</span>(<span class="hljs-k…
65064 <p>See also <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objec…
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…
65073 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65074-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint8Array…
65075-property">types</span>.<span class="hljs-title function_">isUint8Array</span>(<span class="hljs-k…
65076-property">types</span>.<span class="hljs-title function_">isUint8Array</span>(<span class="hljs-k…
65082 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65083 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65085 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65086-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint8Clamp…
65087-property">types</span>.<span class="hljs-title function_">isUint8ClampedArray</span>(<span class=…
65088-property">types</span>.<span class="hljs-title function_">isUint8ClampedArray</span>(<span class=…
65094 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65095 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65097 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65098-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint16Arra…
65099-property">types</span>.<span class="hljs-title function_">isUint16Array</span>(<span class="hljs-
65100-property">types</span>.<span class="hljs-title function_">isUint16Array</span>(<span class="hljs-
65106 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65107 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65109 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65110-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isUint32Arra…
65111-property">types</span>.<span class="hljs-title function_">isUint32Array</span>(<span class="hljs-
65112-property">types</span>.<span class="hljs-title function_">isUint32Array</span>(<span class="hljs-
65118 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65119 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65121 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65122-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isWeakMap</s…
65128 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65129 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65131 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65132-js">util.<span class="hljs-property">types</span>.<span class="hljs-title function_">isWeakSet</s…
65137 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65139 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65140 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65142 <p>Returns <code>true</code> if the value is a built-in <a href="https://developer.mozilla.org/en-U…
65143-js"><span class="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> =…
65144-property">types</span>.<span class="hljs-title function_">isWebAssemblyCompiledModule</span>(<spa…
65148 <h4><code>util._extend(target, source)</code><span><a class="mark" href="#util_util_extend_target_s…
65152 …cumentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="https://developer.mozilla…
65154 <li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
65155 <li><code>source</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
65160 similar built-in functionality through <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
65165 …cumentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="https://developer.mozilla…
65167 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65168 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65170 <p>Alias for <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
65172 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65174 util.<span class="hljs-title function_">isArray</span>([]);
65175 <span class="hljs-comment">// Returns: true</span>
65176 util.<span class="hljs-title function_">isArray</span>(<span class="hljs-keyword">new</span> <span …
65177 <span class="hljs-comment">// Returns: true</span>
65178 util.<span class="hljs-title function_">isArray</span>({});
65179 <span class="hljs-comment">// Returns: false</span></code></pre>
65184 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65186 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65187 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65190 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65192 util.<span class="hljs-title function_">isBoolean</span>(<span class="hljs-number">1</span>);
65193 <span class="hljs-comment">// Returns: false</span>
65194 util.<span class="hljs-title function_">isBoolean</span>(<span class="hljs-number">0</span>);
65195 <span class="hljs-comment">// Returns: false</span>
65196 util.<span class="hljs-title function_">isBoolean</span>(<span class="hljs-literal">false</span>);
65197 <span class="hljs-comment">// Returns: true</span></code></pre>
65202 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
65204 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65205 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65208 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65210 util.<span class="hljs-title function_">isBuffer</span>({ <span class="hljs-attr">length</span>: <s…
65211 <span class="hljs-comment">// Returns: false</span>
65212 util.<span class="hljs-title function_">isBuffer</span>([]);
65213 <span class="hljs-comment">// Returns: false</span>
65214 …hljs-title function_">isBuffer</span>(<span class="hljs-title class_">Buffer</span>.<span class="h…
65215 <span class="hljs-comment">// Returns: true</span></code></pre>
65220 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
65222 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65223 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65226 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65228 util.<span class="hljs-title function_">isDate</span>(<span class="hljs-keyword">new</span> <span c…
65229 <span class="hljs-comment">// Returns: true</span>
65230 util.<span class="hljs-title function_">isDate</span>(<span class="hljs-title class_">Date</span>()…
65231 <span class="hljs-comment">// false (without 'new' returns a String)</span>
65232 util.<span class="hljs-title function_">isDate</span>({});
65233 <span class="hljs-comment">// Returns: false</span></code></pre>
65238 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
65240 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65241 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65245 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65247 util.<span class="hljs-title function_">isError</span>(<span class="hljs-keyword">new</span> <span …
65248 <span class="hljs-comment">// Returns: true</span>
65249 util.<span class="hljs-title function_">isError</span>(<span class="hljs-keyword">new</span> <span …
65250 <span class="hljs-comment">// Returns: true</span>
65251-title function_">isError</span>({ <span class="hljs-attr">name</span>: <span class="hljs-string">…
65252 <span class="hljs-comment">// Returns: false</span></code></pre>
65256 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65257-keyword">const</span> obj = { <span class="hljs-attr">name</span>: <span class="hljs-string">'Err…
65259 util.<span class="hljs-title function_">isError</span>(obj);
65260 <span class="hljs-comment">// Returns: false</span>
65261 obj[<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toStringTag</span>] =…
65262 util.<span class="hljs-title function_">isError</span>(obj);
65263 <span class="hljs-comment">// Returns: true</span></code></pre>
65268 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65270 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65271 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65275 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65277 <span class="hljs-keyword">function</span> <span class="hljs-title function_">Foo</span>(<span clas…
65278 <span class="hljs-keyword">const</span> <span class="hljs-title function_">Bar</span> = (<span clas…
65280 util.<span class="hljs-title function_">isFunction</span>({});
65281 <span class="hljs-comment">// Returns: false</span>
65282 util.<span class="hljs-title function_">isFunction</span>(<span class="hljs-title class_">Foo</span…
65283 <span class="hljs-comment">// Returns: true</span>
65284 util.<span class="hljs-title function_">isFunction</span>(<span class="hljs-title class_">Bar</span…
65285 <span class="hljs-comment">// Returns: true</span></code></pre>
65290 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65292 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65293 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65297 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65299 util.<span class="hljs-title function_">isNull</span>(<span class="hljs-number">0</span>);
65300 <span class="hljs-comment">// Returns: false</span>
65301 util.<span class="hljs-title function_">isNull</span>(<span class="hljs-literal">undefined</span>);
65302 <span class="hljs-comment">// Returns: false</span>
65303 util.<span class="hljs-title function_">isNull</span>(<span class="hljs-literal">null</span>);
65304 <span class="hljs-comment">// Returns: true</span></code></pre>
65309 …bility api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use
65312 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65313 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65317 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65319 util.<span class="hljs-title function_">isNullOrUndefined</span>(<span class="hljs-number">0</span>…
65320 <span class="hljs-comment">// Returns: false</span>
65321 util.<span class="hljs-title function_">isNullOrUndefined</span>(<span class="hljs-literal">undefin…
65322 <span class="hljs-comment">// Returns: true</span>
65323 util.<span class="hljs-title function_">isNullOrUndefined</span>(<span class="hljs-literal">null</s…
65324 <span class="hljs-comment">// Returns: true</span></code></pre>
65329 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65331 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65332 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65335 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65337 util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-literal">false</span>);
65338 <span class="hljs-comment">// Returns: false</span>
65339 util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-title class_">Infinity</s…
65340 <span class="hljs-comment">// Returns: true</span>
65341 util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-number">0</span>);
65342 <span class="hljs-comment">// Returns: true</span>
65343 util.<span class="hljs-title function_">isNumber</span>(<span class="hljs-title class_">Na</span>N);
65344 <span class="hljs-comment">// Returns: true</span></code></pre>
65349 …_stability api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated:
65352 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65353 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65358 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65360 util.<span class="hljs-title function_">isObject</span>(<span class="hljs-number">5</span>);
65361 <span class="hljs-comment">// Returns: false</span>
65362 util.<span class="hljs-title function_">isObject</span>(<span class="hljs-literal">null</span>);
65363 <span class="hljs-comment">// Returns: false</span>
65364 util.<span class="hljs-title function_">isObject</span>({});
65365 <span class="hljs-comment">// Returns: true</span>
65366 util.<span class="hljs-title function_">isObject</span>(<span class="hljs-function">() =></span> {}…
65367 <span class="hljs-comment">// Returns: false</span></code></pre>
65372 …bility api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use
65376 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65377 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65381 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65383 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-number">5</span>);
65384 <span class="hljs-comment">// Returns: true</span>
65385 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-string">'foo'</span>);
65386 <span class="hljs-comment">// Returns: true</span>
65387 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-literal">false</span>);
65388 <span class="hljs-comment">// Returns: true</span>
65389 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-literal">null</span>);
65390 <span class="hljs-comment">// Returns: true</span>
65391 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-literal">undefined</sp…
65392 <span class="hljs-comment">// Returns: true</span>
65393 util.<span class="hljs-title function_">isPrimitive</span>({});
65394 <span class="hljs-comment">// Returns: false</span>
65395 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-function">() =></span>…
65396 <span class="hljs-comment">// Returns: false</span>
65397 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-regexp">/^$/</span>);
65398 <span class="hljs-comment">// Returns: false</span>
65399 util.<span class="hljs-title function_">isPrimitive</span>(<span class="hljs-keyword">new</span> <s…
65400 <span class="hljs-comment">// Returns: false</span></code></pre>
65405 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated</div><p><…
65407 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65408 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65411 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65413 util.<span class="hljs-title function_">isRegExp</span>(<span class="hljs-regexp">/some regexp/</sp…
65414 <span class="hljs-comment">// Returns: true</span>
65415 …ss="hljs-title function_">isRegExp</span>(<span class="hljs-keyword">new</span> <span class="hljs-
65416 <span class="hljs-comment">// Returns: true</span>
65417 util.<span class="hljs-title function_">isRegExp</span>({});
65418 <span class="hljs-comment">// Returns: false</span></code></pre>
65423 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65425 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65426 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65429 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65431 util.<span class="hljs-title function_">isString</span>(<span class="hljs-string">''</span>);
65432 <span class="hljs-comment">// Returns: true</span>
65433 util.<span class="hljs-title function_">isString</span>(<span class="hljs-string">'foo'</span>);
65434 <span class="hljs-comment">// Returns: true</span>
65435 util.<span class="hljs-title function_">isString</span>(<span class="hljs-title class_">String</spa…
65436 <span class="hljs-comment">// Returns: true</span>
65437 util.<span class="hljs-title function_">isString</span>(<span class="hljs-number">5</span>);
65438 <span class="hljs-comment">// Returns: false</span></code></pre>
65443 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65445 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65446 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65449 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65451 util.<span class="hljs-title function_">isSymbol</span>(<span class="hljs-number">5</span>);
65452 <span class="hljs-comment">// Returns: false</span>
65453 util.<span class="hljs-title function_">isSymbol</span>(<span class="hljs-string">'foo'</span>);
65454 <span class="hljs-comment">// Returns: false</span>
65455 util.<span class="hljs-title function_">isSymbol</span>(<span class="hljs-title class_">Symbol</spa…
65456 <span class="hljs-comment">// Returns: true</span></code></pre>
65461 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <cod…
65463 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65464 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
65467 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65469 <span class="hljs-keyword">const</span> foo = <span class="hljs-literal">undefined</span>;
65470 util.<span class="hljs-title function_">isUndefined</span>(<span class="hljs-number">5</span>);
65471 <span class="hljs-comment">// Returns: false</span>
65472 util.<span class="hljs-title function_">isUndefined</span>(foo);
65473 <span class="hljs-comment">// Returns: true</span>
65474 util.<span class="hljs-title function_">isUndefined</span>(<span class="hljs-literal">null</span>);
65475 <span class="hljs-comment">// Returns: false</span></code></pre>
65480 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use a th…
65482 <li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65486 …code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in…
65488 util.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Timestamped message.'…
65495 …<code class="language-js"><span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in"…
65501 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
65504 command-line flags, and detected CPU features. This is useful for determining
65507-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</…
65508 <span class="hljs-comment">// The value returned by v8.cachedDataVersionTag() is derived from the V…
65509 <span class="hljs-comment">// version, command-line flags, and detected CPU features. Test that the…
65510 <span class="hljs-comment">// does indeed update when flags are toggled.</span>
65511 v8.<span class="hljs-title function_">setFlagsFromString</span>(<span class="hljs-string">'--allow_…
65512-variable language_">console</span>.<span class="hljs-title function_">log</span>(v8.<span class="…
65518 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
65522 <li><code>code_and_metadata_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
65523 <li><code>bytecode_and_metadata_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/J…
65524 <li><code>external_script_source_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/…
65526 <!-- eslint-skip -->
65527 <pre><code class="language-js">{
65528 <span class="hljs-attr">code_and_metadata_size</span>: <span class="hljs-number">212208</span>,
65529 …<span class="hljs-attr">bytecode_and_metadata_size</span>: <span class="hljs-number">161368</span>,
65530 …<span class="hljs-attr">external_script_source_size</span>: <span class="hljs-number">1410794</spa…
65544 <pre><code class="language-js"><span class="hljs-comment">// Print heap snapshot to the console</sp…
65545 <span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class…
65546 <span class="hljs-keyword">const</span> stream = v8.<span class="hljs-title function_">getHeapSnaps…
65547 stream.<span class="hljs-title function_">pipe</span>(process.<span class="hljs-property">stdout</s…
65554 <td><p>Support values exceeding the 32-bit unsigned integer range.</p></td></tr>
65561 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
65566 <a href="https://v8docs.nodesource.com/node-13.2/d5/dda/classv8_1_1_isolate.html#ac673576f24fdc7a33…
65570 <li><code>space_name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
65571 <li><code>space_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
65572 <li><code>space_used_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
65573 <li><code>space_available_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
65574 <li><code>physical_space_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
65576 <pre><code class="language-json"><span class="hljs-punctuation">[</span>
65577 <span class="hljs-punctuation">{</span>
65578 … class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
65579 …an class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-
65580 … class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hl…
65581 …lass="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class=…
65582 …<span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span …
65583 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
65584 <span class="hljs-punctuation">{</span>
65585 … class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
65586 …an class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-
65587 … class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hl…
65588 … class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span clas…
65589 …<span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span …
65590 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
65591 <span class="hljs-punctuation">{</span>
65592 … class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
65593 …an class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-
65594 … class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hl…
65595 …class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class…
65596 …<span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span …
65597 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
65598 <span class="hljs-punctuation">{</span>
65599 … class="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-st…
65600 …an class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hljs-
65601 … class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class="hl…
65602 … class="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span clas…
65603 …<span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span …
65604 <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
65605 <span class="hljs-punctuation">{</span>
65606 …ss="hljs-attr">"space_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string…
65607 …<span class="hljs-attr">"space_size"</span><span class="hljs-punctuation">:</span> <span class="hl…
65608 …pan class="hljs-attr">"space_used_size"</span><span class="hljs-punctuation">:</span> <span class=…
65609 …ss="hljs-attr">"space_available_size"</span><span class="hljs-punctuation">:</span> <span class="h…
65610 …<span class="hljs-attr">"physical_space_size"</span><span class="hljs-punctuation">:</span> <span …
65611 <span class="hljs-punctuation">}</span>
65612 <span class="hljs-punctuation">]</span></code></pre>
65619 <td><p>Support values exceeding the 32-bit unsigned integer range.</p></td></tr>
65628 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
65632 <li><code>total_heap_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
65633 <li><code>total_heap_size_executable</code> <a href="https://developer.mozilla.org/en-US/docs/Web/J…
65634 <li><code>total_physical_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScri…
65635 <li><code>total_available_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
65636 <li><code>used_heap_size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Da…
65637 <li><code>heap_size_limit</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
65638 <li><code>malloced_memory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
65639 <li><code>peak_malloced_memory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
65640 <li><code>does_zap_garbage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
65641 <li><code>number_of_native_contexts</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Ja…
65642 <li><code>number_of_detached_contexts</code> <a href="https://developer.mozilla.org/en-US/docs/Web/…
65645 <code>--zap_code_space</code> option is enabled or not. This makes V8 overwrite heap
65650 top-level contexts currently active. Increase of this number over time indicates
65654 being non-zero indicates a potential memory leak.</p>
65655 <!-- eslint-skip -->
65656 <pre><code class="language-js">{
65657 <span class="hljs-attr">total_heap_size</span>: <span class="hljs-number">7326976</span>,
65658 …<span class="hljs-attr">total_heap_size_executable</span>: <span class="hljs-number">4194304</span…
65659 <span class="hljs-attr">total_physical_size</span>: <span class="hljs-number">7326976</span>,
65660 <span class="hljs-attr">total_available_size</span>: <span class="hljs-number">1152656</span>,
65661 <span class="hljs-attr">used_heap_size</span>: <span class="hljs-number">3476208</span>,
65662 <span class="hljs-attr">heap_size_limit</span>: <span class="hljs-number">1535115264</span>,
65663 <span class="hljs-attr">malloced_memory</span>: <span class="hljs-number">16384</span>,
65664 <span class="hljs-attr">peak_malloced_memory</span>: <span class="hljs-number">1127496</span>,
65665 <span class="hljs-attr">does_zap_garbage</span>: <span class="hljs-number">0</span>,
65666 <span class="hljs-attr">number_of_native_contexts</span>: <span class="hljs-number">1</span>,
65667 <span class="hljs-attr">number_of_detached_contexts</span>: <span class="hljs-number">0</span>
65674 <li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65677 V8 command-line flags. This method should be used with care. Changing settings
65681 <code>node --v8-options</code>.</p>
65683 <pre><code class="language-js"><span class="hljs-comment">// Print GC events to stdout for one minu…
65684 <span class="hljs-keyword">const</span> v8 = <span class="hljs-built_in">require</span>(<span class…
65685 v8.<span class="hljs-title function_">setFlagsFromString</span>(<span class="hljs-string">'--trace_…
65686-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { v8.<span class="hljs-title…
65711 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
65713 <code>'Heap-${yyyymmdd}-${hhmmss}-${pid}-${thread_id}.heapsnapshot'</code> will be
65717 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin…
65723 <p>A heap snapshot is specific to a single V8 isolate. When using
65726 …s="language-js"><span class="hljs-keyword">const</span> { writeHeapSnapshot } = <span class="hljs-
65727 <span class="hljs-keyword">const</span> {
65728 <span class="hljs-title class_">Worker</span>,
65731 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'worker_threads'</span>);
65733 <span class="hljs-keyword">if</span> (isMainThread) {
65734 …<span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class…
65736 …span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span cl…
65737 …hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class=…
65738 <span class="hljs-comment">// Now get a heapdump for the main thread.</span>
65739 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="h…
65742 <span class="hljs-comment">// Tell the worker to create a heapdump.</span>
65743 …worker.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'heapdump'<…
65744 } <span class="hljs-keyword">else</span> {
65745 …span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span cl…
65746 <span class="hljs-keyword">if</span> (message === <span class="hljs-string">'heapdump'</span>) {
65747 <span class="hljs-comment">// Generate a heapdump for the worker</span>
65748 <span class="hljs-comment">// and return the filename to the parent.</span>
65749 …parentPort.<span class="hljs-title function_">postMessage</span>(<span class="hljs-title function_…
65755 that is compatible with the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_A…
65756 <p>The format is backward-compatible (i.e. safe to store to disk).
65763 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65772 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
65786 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65800 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&…
65801 <li><code>arrayBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer…
65808 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65810 <p>Write a raw 32-bit unsigned integer.
65814 <li><code>hi</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
65815 <li><code>lo</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure…
65817 <p>Write a raw 64-bit unsigned integer, split into high and low 32-bit parts.
65821 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
65827 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
65834 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
65843 <li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
65851 <li><code>sharedArrayBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
65854 <code>SharedArrayBuffer</code> object. It must return an unsigned 32-bit integer ID for
65863 <li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
65873 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
65885 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&…
65886 …rg/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer>…
65894 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
65901 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
65903 <p>Read a raw 32-bit unsigned integer and return it.
65907 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
65909 <p>Read a raw 64-bit unsigned integer and return it as an array <code>[hi, lo]</code>
65910 with two 32-bit unsigned integer entries.
65914 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Numbe…
65920 <li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
65949 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
65963 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
65965 <span class="hljs-keyword">const</span> x = <span class="hljs-number">1</span>;
65967 <span class="hljs-keyword">const</span> context = { <span class="hljs-attr">x</span>: <span class="…
65968 vm.<span class="hljs-title function_">createContext</span>(context); <span class="hljs-comment">// …
65970 <span class="hljs-keyword">const</span> code = <span class="hljs-string">'x += 40; var y = 17;'</sp…
65971 <span class="hljs-comment">// `x` and `y` are global variables in the context.</span>
65972 <span class="hljs-comment">// Initially, x has the value 2 because that is the value of context.x.<…
65973 vm.<span class="hljs-title function_">runInContext</span>(code, context);
65975 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(context.<span…
65976 …ljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(context.<span…
65978 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
66000 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66001 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
66003 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
66005 <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
66007 …developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;nu…
66009 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
66013 <li><code>produceCachedData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
66021 <li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
66027 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
66029 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="t…
66051-js"><span class="hljs-keyword">const</span> script = <span class="hljs-keyword">new</span> vm.<sp…
66059 <span class="hljs-keyword">const</span> cacheWithoutX = script.<span class="hljs-title function_">c…
66061 script.<span class="hljs-title function_">runInThisContext</span>();
66063 <span class="hljs-keyword">const</span> cacheWithX = script.<span class="hljs-title function_">crea…
66077 <li><code>contextifiedObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
66079 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66081 <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66084 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66087 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66094 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
66102 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
66104 <span class="hljs-keyword">const</span> context = {
66105 <span class="hljs-attr">animal</span>: <span class="hljs-string">'cat'</span>,
66106 <span class="hljs-attr">count</span>: <span class="hljs-number">2</span>
66109 …ss="hljs-keyword">const</span> script = <span class="hljs-keyword">new</span> vm.<span class="hljs
66111 vm.<span class="hljs-title function_">createContext</span>(context);
66112 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
66113 script.<span class="hljs-title function_">runInContext</span>(context);
66116 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
66117 <span class="hljs-comment">// Prints: { animal: 'cat', count: 12, name: 'kitty' }</span></code></pr…
66119 and corresponding threads being started, which have a non-zero performance
66138 <li><code>contextObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
66140 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66142 <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66145 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66148 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66153 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&…
66156 …ozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <…
66162 <li><code>contextCodeGeneration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
66164 <li><code>strings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66167 <li><code>wasm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66171 <li><code>microtaskMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66177 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
66185 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
66187 …ss="hljs-keyword">const</span> script = <span class="hljs-keyword">new</span> vm.<span class="hljs
66189 <span class="hljs-keyword">const</span> contexts = [{}, {}, {}];
66190 contexts.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class…
66191 script.<span class="hljs-title function_">runInNewContext</span>(context);
66194 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
66195 <span class="hljs-comment">// Prints: [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'se…
66209 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66211 <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66214 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66217 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66224 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
66231 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
66233 <span class="hljs-variable language_">global</span>.<span class="hljs-property">globalVar</span> = …
66235-keyword">const</span> script = <span class="hljs-keyword">new</span> vm.<span class="hljs-title c…
66237 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
66238 script.<span class="hljs-title function_">runInThisContext</span>();
66241 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
66243 <span class="hljs-comment">// 1000</span></code></pre>
66248 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
66249 <p>This feature is only available with the <code>--experimental-vm-modules</code> command
66251 <p>The <code>vm.Module</code> class provides a low-level interface for using
66253 class that closely mirrors <a href="https://www.ecma-international.org/ecma-262/#sec-abstract-modul…
66266-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
66268 <span class="hljs-keyword">const</span> contextifiedObject = vm.<span class="hljs-title function_">…
66269 <span class="hljs-attr">secret</span>: <span class="hljs-number">42</span>,
66270 …<span class="hljs-attr">print</span>: <span class="hljs-variable language_">console</span>.<span c…
66273 <span class="hljs-comment">// Step 1</span>
66274 <span class="hljs-comment">//</span>
66275 <span class="hljs-comment">// Create a Module by constructing a new `vm.SourceTextModule` object. T…
66276 <span class="hljs-comment">// parses the provided source text, throwing a `SyntaxError` if anything…
66277 <span class="hljs-comment">// wrong. By default, a Module is created in the top context. But here, …
66278 <span class="hljs-comment">// specify `contextifiedObject` as the context this Module belongs to.</…
66279 <span class="hljs-comment">//</span>
66280 <span class="hljs-comment">// Here, we attempt to obtain the default export from the module "foo", …
66281 <span class="hljs-comment">// put it into local binding "secret".</span>
66283 …hljs-keyword">const</span> bar = <span class="hljs-keyword">new</span> vm.<span class="hljs-title …
66287 `</span>, { <span class="hljs-attr">context</span>: contextifiedObject });
66289 <span class="hljs-comment">// Step 2</span>
66290 <span class="hljs-comment">//</span>
66291 <span class="hljs-comment">// "Link" the imported dependencies of this Module to it.</span>
66292 <span class="hljs-comment">//</span>
66293 <span class="hljs-comment">// The provided linking callback (the "linker") accepts two arguments: t…
66294 <span class="hljs-comment">// parent module (`bar` in this case) and the string that is the specifi…
66295 <span class="hljs-comment">// the imported module. The callback is expected to return a Module that…
66296 <span class="hljs-comment">// corresponds to the provided specifier, with certain requirements docu…
66297 <span class="hljs-comment">// in `module.link()`.</span>
66298 <span class="hljs-comment">//</span>
66299 <span class="hljs-comment">// If linking has not started for the returned Module, the same linker</…
66300 <span class="hljs-comment">// callback will be called on the returned Module.</span>
66301 <span class="hljs-comment">//</span>
66302 <span class="hljs-comment">// Even top-level Modules without dependencies must be explicitly linked…
66303 <span class="hljs-comment">// callback provided would never be called, however.</span>
66304 <span class="hljs-comment">//</span>
66305 <span class="hljs-comment">// The link() method returns a Promise that will be resolved when all th…
66306 <span class="hljs-comment">// Promises returned by the linker resolve.</span>
66307 <span class="hljs-comment">//</span>
66308 <span class="hljs-comment">// Note: This is a contrived example in that the linker function creates…
66309 <span class="hljs-comment">// "foo" module every time it is called. In a full-fledged module system…
66310 <span class="hljs-comment">// cache would probably be used to avoid duplicated modules.</span>
66312 …lass="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-titl…
66313 <span class="hljs-keyword">if</span> (specifier === <span class="hljs-string">'foo'</span>) {
66314 …s="hljs-keyword">return</span> <span class="hljs-keyword">new</span> vm.<span class="hljs-title cl…
66318 …`</span>, { <span class="hljs-attr">context</span>: referencingModule.<span class="hljs-property">…
66320 …<span class="hljs-comment">// Using `contextifiedObject` instead of `referencingModule.context`</s…
66321 <span class="hljs-comment">// here would work as well.</span>
66323-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error…
66325 <span class="hljs-keyword">await</span> bar.<span class="hljs-title function_">link</span>(linker);
66327 <span class="hljs-comment">// Step 3</span>
66328 <span class="hljs-comment">//</span>
66329 <span class="hljs-comment">// Evaluate the Module. The evaluate() method returns a promise which wi…
66330 <span class="hljs-comment">// resolve after the module has finished evaluating.</span>
66332 <span class="hljs-comment">// Prints 42.</span>
66333-keyword">await</span> bar.evaluate();</code><code class="language-js cjs"><span class="hljs-keywo…
66335 <span class="hljs-keyword">const</span> contextifiedObject = vm.<span class="hljs-title function_">…
66336 <span class="hljs-attr">secret</span>: <span class="hljs-number">42</span>,
66337 …<span class="hljs-attr">print</span>: <span class="hljs-variable language_">console</span>.<span c…
66340 (<span class="hljs-keyword">async</span> () => {
66341 <span class="hljs-comment">// Step 1</span>
66342 <span class="hljs-comment">//</span>
66343 …<span class="hljs-comment">// Create a Module by constructing a new `vm.SourceTextModule` object. …
66344 …<span class="hljs-comment">// parses the provided source text, throwing a `SyntaxError` if anythin…
66345 …<span class="hljs-comment">// wrong. By default, a Module is created in the top context. But here,…
66346 …<span class="hljs-comment">// specify `contextifiedObject` as the context this Module belongs to.<…
66347 <span class="hljs-comment">//</span>
66348 …<span class="hljs-comment">// Here, we attempt to obtain the default export from the module "foo",…
66349 <span class="hljs-comment">// put it into local binding "secret".</span>
66351 …hljs-keyword">const</span> bar = <span class="hljs-keyword">new</span> vm.<span class="hljs-title …
66355 `</span>, { <span class="hljs-attr">context</span>: contextifiedObject });
66357 <span class="hljs-comment">// Step 2</span>
66358 <span class="hljs-comment">//</span>
66359 <span class="hljs-comment">// "Link" the imported dependencies of this Module to it.</span>
66360 <span class="hljs-comment">//</span>
66361 …<span class="hljs-comment">// The provided linking callback (the "linker") accepts two arguments: …
66362 …<span class="hljs-comment">// parent module (`bar` in this case) and the string that is the specif…
66363 …<span class="hljs-comment">// the imported module. The callback is expected to return a Module tha…
66364 …<span class="hljs-comment">// corresponds to the provided specifier, with certain requirements doc…
66365 <span class="hljs-comment">// in `module.link()`.</span>
66366 <span class="hljs-comment">//</span>
66367 …<span class="hljs-comment">// If linking has not started for the returned Module, the same linker<…
66368 <span class="hljs-comment">// callback will be called on the returned Module.</span>
66369 <span class="hljs-comment">//</span>
66370 …<span class="hljs-comment">// Even top-level Modules without dependencies must be explicitly linke…
66371 <span class="hljs-comment">// callback provided would never be called, however.</span>
66372 <span class="hljs-comment">//</span>
66373 …<span class="hljs-comment">// The link() method returns a Promise that will be resolved when all t…
66374 <span class="hljs-comment">// Promises returned by the linker resolve.</span>
66375 <span class="hljs-comment">//</span>
66376 …<span class="hljs-comment">// Note: This is a contrived example in that the linker function create…
66377 …<span class="hljs-comment">// "foo" module every time it is called. In a full-fledged module syste…
66378 <span class="hljs-comment">// cache would probably be used to avoid duplicated modules.</span>
66380 …lass="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-titl…
66381 <span class="hljs-keyword">if</span> (specifier === <span class="hljs-string">'foo'</span>) {
66382 …s="hljs-keyword">return</span> <span class="hljs-keyword">new</span> vm.<span class="hljs-title cl…
66386 …`</span>, { <span class="hljs-attr">context</span>: referencingModule.<span class="hljs-property">…
66388 …<span class="hljs-comment">// Using `contextifiedObject` instead of `referencingModule.context`</s…
66389 <span class="hljs-comment">// here would work as well.</span>
66391-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error…
66393 …<span class="hljs-keyword">await</span> bar.<span class="hljs-title function_">link</span>(linker);
66395 <span class="hljs-comment">// Step 3</span>
66396 <span class="hljs-comment">//</span>
66397 …<span class="hljs-comment">// Evaluate the Module. The evaluate() method returns a promise which w…
66398 <span class="hljs-comment">// resolve after the module has finished evaluating.</span>
66400 <span class="hljs-comment">// Prints 42.</span>
66401 <span class="hljs-keyword">await</span> bar.evaluate();
66405 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
66409 …uestedModules]]</code> field of <a href="https://tc39.es/ecma262/#sec-cyclic-module-records">Cycli…
66413 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" cl…
66420 …aluationError]]</code> field of <a href="https://tc39.es/ecma262/#sec-cyclic-module-records">Cycli…
66424 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66426 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66429 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66436 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
66442 (<code>module.status</code> is <code>'evaluated'</code>) or it will re-throw the exception that the
66446 …/tc39.es/ecma262/#sec-moduleevaluation">Evaluate() concrete method</a> field of <a href="https://t…
66450 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
66455 <li><code>linker</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
66458 <p><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
66459 …<code class="language-js mjs"><span class="hljs-keyword">import</span> foo <span class="hljs-keywo…
66460 <span class="hljs-comment">// ^^^^^ the module specifier</span></code></pre>
66466 …lass="type">&#x3C;vm.Module></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
66470 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
66487 <p>The linker function roughly corresponds to the implementation-defined
66488 <a href="https://tc39.es/ecma262/#sec-hostresolveimportedmodule">HostResolveImportedModule</a> abst…
66492 <a href="https://tc39.es/ecma262/#sec-hostresolveimportedmodule">HostResolveImportedModule</a> is s…
66494 <p>The actual <a href="https://tc39.es/ecma262/#sec-hostresolveimportedmodule">HostResolveImportedM…
66497 <a href="https://tc39.es/ecma262/#sec-hostresolveimportedmodule">HostResolveImportedModule</a> impl…
66499 …c39.es/ecma262/#sec-moduledeclarationlinking">Link() concrete method</a> field of <a href="https:/…
66503 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
66507 <p>Corresponds to the <a href="https://tc39.es/ecma262/#sec-getmodulenamespace">GetModuleNamespace<…
66511 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" c…
66538 <a href="https://tc39.es/ecma262/#sec-cyclic-module-records">Cyclic Module Record</a>'s <code>[[Sta…
66545 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
66546 <p>This feature is only available with the <code>--experimental-vm-modules</code> command
66551 …le</code> class provides the <a href="https://tc39.es/ecma262/#sec-source-text-module-records">Sou…
66555 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66558 <li><code>identifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
66559 <strong>Default:</strong> <code>'vm:module(i)'</code> where <code>i</code> is a context-specific as…
66561 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
66565 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66567 <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
66569 …developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;in…
66571 <li><code>initializeImportMeta</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScr…
66578 <li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
66582 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
66584 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="t…
66597-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
66599-keyword">const</span> contextifiedObject = vm.<span class="hljs-title function_">createContext</s…
66601 …ss="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = <span class="…
66602 <span class="hljs-string">'Object.getPrototypeOf(import.meta.prop).secret = secret;'</span>,
66604 …<span class="hljs-title function_">initializeImportMeta</span>(<span class="hljs-params">meta</spa…
66605 <span class="hljs-comment">// Note: this object is created in the top context. As such,</span>
66606 <span class="hljs-comment">// Object.getPrototypeOf(import.meta.prop) points to the</span>
66607 <span class="hljs-comment">// Object.prototype in the top context rather than that in</span>
66608 <span class="hljs-comment">// the contextified object.</span>
66609 meta.<span class="hljs-property">prop</span> = {};
66612 <span class="hljs-comment">// Since module has no dependencies, the linker function will never be c…
66613 …="hljs-keyword">await</span> <span class="hljs-variable language_">module</span>.<span class="hljs
66614 <span class="hljs-keyword">await</span> <span class="hljs-variable language_">module</span>.evaluat…
66616 <span class="hljs-comment">// Now, Object.prototype.secret will be equal to 42.</span>
66617 <span class="hljs-comment">//</span>
66618 <span class="hljs-comment">// To fix this problem, replace</span>
66619 <span class="hljs-comment">// meta.prop = {};</span>
66620 <span class="hljs-comment">// above with</span>
66621-comment">// meta.prop = vm.runInContext('{}', contextifiedObject);</span></code><code class="…
66622-keyword">const</span> contextifiedObject = vm.<span class="hljs-title function_">createContext</s…
66623 (<span class="hljs-keyword">async</span> () => {
66624 …ss="hljs-keyword">const</span> <span class="hljs-variable language_">module</span> = <span class="…
66625 <span class="hljs-string">'Object.getPrototypeOf(import.meta.prop).secret = secret;'</span>,
66627 …<span class="hljs-title function_">initializeImportMeta</span>(<span class="hljs-params">meta</spa…
66628 … <span class="hljs-comment">// Note: this object is created in the top context. As such,</span>
66629 <span class="hljs-comment">// Object.getPrototypeOf(import.meta.prop) points to the</span>
66630 <span class="hljs-comment">// Object.prototype in the top context rather than that in</span>
66631 <span class="hljs-comment">// the contextified object.</span>
66632 meta.<span class="hljs-property">prop</span> = {};
66635 …<span class="hljs-comment">// Since module has no dependencies, the linker function will never be …
66636 …="hljs-keyword">await</span> <span class="hljs-variable language_">module</span>.<span class="hljs
66637 …<span class="hljs-keyword">await</span> <span class="hljs-variable language_">module</span>.evalua…
66638 <span class="hljs-comment">// Now, Object.prototype.secret will be equal to 42.</span>
66639 <span class="hljs-comment">//</span>
66640 <span class="hljs-comment">// To fix this problem, replace</span>
66641 <span class="hljs-comment">// meta.prop = {};</span>
66642 <span class="hljs-comment">// above with</span>
66643 <span class="hljs-comment">// meta.prop = vm.runInContext('{}', contextifiedObject);</span>
66655 <pre><code class="language-js"><span class="hljs-comment">// Create an initial module</span>
66656-keyword">const</span> <span class="hljs-variable language_">module</span> = <span class="hljs-key…
66658 <span class="hljs-comment">// Create cached data from this module</span>
66659 <span class="hljs-keyword">const</span> cachedData = <span class="hljs-variable language_">module</…
66661 <span class="hljs-comment">// Create a new module using the cached data. The code must be the same.…
66662 …js-keyword">const</span> module2 = <span class="hljs-keyword">new</span> vm.<span class="hljs-titl…
66667 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
66668 <p>This feature is only available with the <code>--experimental-vm-modules</code> command
66673 …> class provides the <a href="https://heycam.github.io/webidl/#synthetic-module-records">Synthetic…
66675 provide a generic interface for exposing non-JavaScript sources to ECMAScript
66677 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
66679 <span class="hljs-keyword">const</span> source = <span class="hljs-string">'{ "a": 1 }'</span>;
66680-keyword">const</span> <span class="hljs-variable language_">module</span> = <span class="hljs-key…
66681 …<span class="hljs-keyword">const</span> obj = <span class="hljs-variable constant_">JSON</span>.<s…
66682 …<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setExport</sp…
66685 <span class="hljs-comment">// Use `module` in linking...</span></code></pre>
66691 <li><code>exportNames</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
66692 <li><code>evaluateCallback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
66695 <li><code>identifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
66697 <strong>Default:</strong> <code>'vm:module(i)'</code> where <code>i</code> is a context-specific as…
66700 <li><code>context</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66714 <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66715 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
66721-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
66723-keyword">const</span> m = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_…
66724 …m.<span class="hljs-title function_">setExport</span>(<span class="hljs-string">'x'</span>, <span …
66727 <span class="hljs-keyword">await</span> m.<span class="hljs-title function_">link</span>(<span clas…
66728 <span class="hljs-keyword">await</span> m.evaluate();
66730-title function_">strictEqual</span>(m.<span class="hljs-property">namespace</span>.<span class="h…
66731 (<span class="hljs-keyword">async</span> () => {
66732-keyword">const</span> m = <span class="hljs-keyword">new</span> vm.<span class="hljs-title class_…
66733 …m.<span class="hljs-title function_">setExport</span>(<span class="hljs-string">'x'</span>, <span …
66735 …<span class="hljs-keyword">await</span> m.<span class="hljs-title function_">link</span>(<span cla…
66736 <span class="hljs-keyword">await</span> m.evaluate();
66737 …="hljs-title function_">strictEqual</span>(m.<span class="hljs-property">namespace</span>.<span cl…
66754 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66755 <li><code>params</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
66757 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66759 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
66761 <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
66763 …developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;nu…
66765 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
66768 <li><code>produceCachedData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
66770 <li><code>parsingContext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
66772 <li><code>contextExtensions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
66777 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
66799 <li><code>contextObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
66800 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66802 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&…
66805 …ozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <…
66811 <li><code>codeGeneration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
66813 <li><code>strings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66816 <li><code>wasm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66820 <li><code>microtaskMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66826 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
66832 properties but also having the built-in objects and functions any standard
66835 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
66837 <span class="hljs-variable language_">global</span>.<span class="hljs-property">globalVar</span> = …
66839 <span class="hljs-keyword">const</span> context = { <span class="hljs-attr">globalVar</span>: <span…
66840 vm.<span class="hljs-title function_">createContext</span>(context);
66842 vm.<span class="hljs-title function_">runInContext</span>(<span class="hljs-string">'globalVar *= 2…
66844 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
66845 <span class="hljs-comment">// Prints: { globalVar: 2 }</span>
66847-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
66848 <span class="hljs-comment">// Prints: 3</span></code></pre>
66851 <p>The <code>vm.createContext()</code> method is primarily useful for creating a single
66853 web browser, the method can be used to create a single context representing a
66863 <li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
66864 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boole…
66872 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
66876 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
66878 <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66883 <li><code>execution</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
66891 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
66901 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
66902 <span class="hljs-comment">// Measure the memory used by the main context.</span>
66903 vm.<span class="hljs-title function_">measureMemory</span>({ <span class="hljs-attr">mode</span>: <…
66904 <span class="hljs-comment">// This is the same as vm.measureMemory()</span>
66905 ….<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-par…
66906 <span class="hljs-comment">// The current format is:</span>
66907 <span class="hljs-comment">// {</span>
66908 <span class="hljs-comment">// total: {</span>
66909 …<span class="hljs-comment">// jsMemoryEstimate: 2418479, jsMemoryRange: [ 2418479, 2745799 ]<…
66910 <span class="hljs-comment">// }</span>
66911 <span class="hljs-comment">// }</span>
66912 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
66915 …ljs-keyword">const</span> context = vm.<span class="hljs-title function_">createContext</span>({ <…
66916-title function_">measureMemory</span>({ <span class="hljs-attr">mode</span>: <span class="hljs-st…
66917 ….<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-par…
66918 <span class="hljs-comment">// Reference the context here so that it won't be GC'ed</span>
66919 <span class="hljs-comment">// until the measurement is complete.</span>
66920 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
66921 <span class="hljs-comment">// {</span>
66922 <span class="hljs-comment">// total: {</span>
66923 <span class="hljs-comment">// jsMemoryEstimate: 2574732,</span>
66924 <span class="hljs-comment">// jsMemoryRange: [ 2574732, 2904372 ]</span>
66925 <span class="hljs-comment">// },</span>
66926 <span class="hljs-comment">// current: {</span>
66927 <span class="hljs-comment">// jsMemoryEstimate: 2438996,</span>
66928 <span class="hljs-comment">// jsMemoryRange: [ 2438996, 2768636 ]</span>
66929 <span class="hljs-comment">// },</span>
66930 <span class="hljs-comment">// other: [</span>
66931 <span class="hljs-comment">// {</span>
66932 <span class="hljs-comment">// jsMemoryEstimate: 135736,</span>
66933 <span class="hljs-comment">// jsMemoryRange: [ 135736, 465376 ]</span>
66934 <span class="hljs-comment">// }</span>
66935 <span class="hljs-comment">// ]</span>
66936 <span class="hljs-comment">// }</span>
66937 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
66952 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
66953 <li><code>contextifiedObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
66955 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
66957 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
66959 <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
66961 …developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;nu…
66963 <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66966 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
66969 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
66974 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
66978 <li><code>produceCachedData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
66986 <li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
66992 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
66994 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="t…
67001 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
67008 <p>The following example compiles and executes different scripts using a single
67010 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
67012 <span class="hljs-keyword">const</span> contextObject = { <span class="hljs-attr">globalVar</span>:…
67013 vm.<span class="hljs-title function_">createContext</span>(contextObject);
67015 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
67016 …vm.<span class="hljs-title function_">runInContext</span>(<span class="hljs-string">'globalVar *= …
67018 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67019 <span class="hljs-comment">// Prints: { globalVar: 1024 }</span></code></pre>
67037 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
67038 <li><code>contextObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref…
67040 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
67042 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
67044 <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
67046 …developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;nu…
67048 <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
67051 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
67054 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
67059 …ttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&…
67062 …ozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> <…
67068 <li><code>contextCodeGeneration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaSc…
67070 <li><code>strings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
67073 <li><code>wasm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
67077 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
67081 <li><code>produceCachedData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
67089 <li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
67095 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
67097 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="t…
67102 <li><code>microtaskMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
67108 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
67117 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
67119 <span class="hljs-keyword">const</span> contextObject = {
67120 <span class="hljs-attr">animal</span>: <span class="hljs-string">'cat'</span>,
67121 <span class="hljs-attr">count</span>: <span class="hljs-number">2</span>
67124 vm.<span class="hljs-title function_">runInNewContext</span>(<span class="hljs-string">'count += 1;…
67125 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67126 <span class="hljs-comment">// Prints: { animal: 'cat', count: 3, name: 'kitty' }</span></code></pre>
67140 <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
67141 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
67143 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
67145 <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
67147 …developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;nu…
67149 <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
67152 <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru…
67155 <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Dat…
67160 …org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray><…
67164 <li><code>produceCachedData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
67172 <li><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Java…
67178 <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
67180 <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="t…
67187 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
67194 the JavaScript <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Ob…
67195 <!-- eslint-disable prefer-const -->
67196 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
67197 <span class="hljs-keyword">let</span> localVar = <span class="hljs-string">'initial value'</span>;
67199 <span class="hljs-keyword">const</span> vmResult = vm.<span class="hljs-title function_">runInThisC…
67200-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
67201 <span class="hljs-comment">// Prints: vmResult: 'vm', localVar: 'initial value'</span>
67203 <span class="hljs-keyword">const</span> evalResult = <span class="hljs-built_in">eval</span>(<span …
67204-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
67205 <span class="hljs-comment">// Prints: evalResult: 'eval', localVar: 'eval'</span></code></pre>
67207 <code>localVar</code> is unchanged. In contrast, <a href="https://developer.mozilla.org/en-US/docs/…
67218 <pre><code class="language-js"><span class="hljs-meta">'use strict'</span>;
67219 <span class="hljs-keyword">const</span> vm = <span class="hljs-built_in">require</span>(<span class…
67221 <span class="hljs-keyword">const</span> code = <span class="hljs-string">`
67226 response.writeHead(200, { 'Content-Type': 'text/plain' });
67233 vm.<span class="hljs-title function_">runInThisContext</span>(code)(<span class="hljs-built_in">req…
67242 JavaScript applications to run in a single instance of V8. You must explicitly
67246 (or a newly-created object if <code>contextObject</code> is <code>undefined</code>) is associated
67261 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
67263 <span class="hljs-keyword">function</span> <span class="hljs-title function_">loop</span>(<span cla…
67264 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
67265-keyword">while</span> (<span class="hljs-number">1</span>) <span class="hljs-variable language_">…
67268 vm.<span class="hljs-title function_">runInNewContext</span>(
67269 <span class="hljs-string">'Promise.resolve().then(() => loop());'</span>,
67270 { loop, <span class="hljs-variable language_">console</span> },
67271 { <span class="hljs-attr">timeout</span>: <span class="hljs-number">5</span> }
67273 <span class="hljs-comment">// This is printed *before* 'entering loop' (!)</span>
67274 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67277 …<code class="language-js"><span class="hljs-keyword">const</span> vm = <span class="hljs-built_in"…
67279 <span class="hljs-keyword">function</span> <span class="hljs-title function_">loop</span>(<span cla…
67280-keyword">while</span> (<span class="hljs-number">1</span>) <span class="hljs-variable language_">…
67283 vm.<span class="hljs-title function_">runInNewContext</span>(
67284 <span class="hljs-string">'Promise.resolve().then(() => loop());'</span>,
67285 { loop, <span class="hljs-variable language_">console</span> },
67286 …pan class="hljs-attr">timeout</span>: <span class="hljs-number">5</span>, <span class="hljs-attr">…
67305 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
67309 underlying operating system via a collection of POSIX-like functions.</p>
67311-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="…
67312 …ass="hljs-keyword">import</span> { <span class="hljs-variable constant_">WASI</span> } <span class…
67314 <span class="hljs-keyword">const</span> wasi = <span class="hljs-keyword">new</span> <span class="h…
67315 <span class="hljs-attr">args</span>: process.<span class="hljs-property">argv</span>,
67316 <span class="hljs-attr">env</span>: process.<span class="hljs-property">env</span>,
67317 <span class="hljs-attr">preopens</span>: {
67318 …<span class="hljs-string">'/sandbox'</span>: <span class="hljs-string">'/some/real/path/that/wasm/…
67321 <span class="hljs-keyword">const</span> importObject = { <span class="hljs-attr">wasi_snapshot_prev…
67323-keyword">const</span> wasm = <span class="hljs-keyword">await</span> <span class="hljs-title clas…
67324 …"hljs-keyword">const</span> instance = <span class="hljs-keyword">await</span> <span class="hljs-t…
67326 wasi.<span class="hljs-title function_">start</span>(instance);</code><code class="language-js cjs"…
67327 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
67328 …="hljs-keyword">const</span> { <span class="hljs-variable constant_">WASI</span> } = <span class="…
67329 <span class="hljs-keyword">const</span> wasi = <span class="hljs-keyword">new</span> <span class="h…
67330 <span class="hljs-attr">args</span>: process.<span class="hljs-property">argv</span>,
67331 <span class="hljs-attr">env</span>: process.<span class="hljs-property">env</span>,
67332 <span class="hljs-attr">preopens</span>: {
67333 …<span class="hljs-string">'/sandbox'</span>: <span class="hljs-string">'/some/real/path/that/wasm/…
67336 <span class="hljs-keyword">const</span> importObject = { <span class="hljs-attr">wasi_snapshot_prev…
67338 (<span class="hljs-keyword">async</span> () => {
67339-keyword">const</span> wasm = <span class="hljs-keyword">await</span> <span class="hljs-title clas…
67340 …"hljs-keyword">const</span> instance = <span class="hljs-keyword">await</span> <span class="hljs-t…
67342 wasi.<span class="hljs-title function_">start</span>(instance);
67346 <pre><code class="language-text">(module
67349 ;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
67361 …(i32.store (i32.const 0) (i32.const 8)) ;; iov.iov_base - This is a pointer to the start of the '…
67362 …(i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string
67365 (i32.const 1) ;; file_descriptor - 1 for stdout
67366 … (i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
67367 (i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
67368 (i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
67374 <pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">wa…
67375 <p>The <code>--experimental-wasi-unstable-preview1</code> and <code>--experimental-wasm-bigint</cod…
67382 methods for working with WASI-based applications. Each <code>WASI</code> instance
67384 instance must have its command-line arguments, environment variables, and
67391 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
67393 <li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl…
67394 see as command-line arguments. The first argument is the virtual path to the
67396 <li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
67398 <li><code>preopens</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
67402 <li><code>returnOnExit</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data…
67406 <li><code>stdin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
67408 <li><code>stdout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
67410 <li><code>stderr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
67420 <li><code>instance</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
67425 …at <code>instance</code> exports a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
67433 <li><code>instance</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
67438 …at <code>instance</code> exports a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
67446 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
67450 of a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebA…
67454 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
67458 …ode class="language-js"><span class="hljs-keyword">const</span> worker = <span class="hljs-built_i…
67459 <p>Workers (threads) are useful for performing CPU-intensive JavaScript operations.
67460 They will not help much with I/O-intensive work. Node.js’s built-in asynchronous
67465 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
67466 <span class="hljs-title class_">Worker</span>, isMainThread, parentPort, workerData
67467 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'worker_threads'</span>);
67469 <span class="hljs-keyword">if</span> (isMainThread) {
67470-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-
67471-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Prom…
67472 …<span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class…
67473 <span class="hljs-attr">workerData</span>: script
67475 …worker.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, re…
67476 …worker.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, reje…
67477 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
67478 <span class="hljs-keyword">if</span> (code !== <span class="hljs-number">0</span>)
67479-title function_">reject</span>(<span class="hljs-keyword">new</span> <span class="hljs-title clas…
67483 } <span class="hljs-keyword">else</span> {
67484 …span class="hljs-keyword">const</span> { parse } = <span class="hljs-built_in">require</span>(<spa…
67485 <span class="hljs-keyword">const</span> script = workerData;
67486 …parentPort.<span class="hljs-title function_">postMessage</span>(<span class="hljs-title function_…
67494 <a href="#async-resource-worker-pool">"Using <code>AsyncResource</code> for a <code>Worker</code> t…
67496 <p>Worker threads inherit non-process-specific options by default. Refer to
67503 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
67505 <li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
67506 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" clas…
67507 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_…
67513 <pre><code class="language-js"><span class="hljs-keyword">const</span> {
67514 <span class="hljs-title class_">Worker</span>,
67518 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'worker_threads'</span>);
67520 <span class="hljs-keyword">if</span> (isMainThread) {
67521 …<span class="hljs-title function_">setEnvironmentData</span>(<span class="hljs-string">'Hello'</sp…
67522 …<span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class…
67523 } <span class="hljs-keyword">else</span> {
67524-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hlj…
67531 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" …
67534-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, isMa…
67536 <span class="hljs-keyword">if</span> (isMainThread) {
67537 <span class="hljs-comment">// This re-loads the current file inside a Worker instance.</span>
67538 <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename);
67539 } <span class="hljs-keyword">else</span> {
67540 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
67541 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
67554-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</spa…
67556 …hljs-keyword">const</span> pooledBuffer = <span class="hljs-keyword">new</span> <span class="hljs-
67557 …ss="hljs-keyword">const</span> typedArray1 = <span class="hljs-keyword">new</span> <span class="hl…
67558 …s="hljs-keyword">const</span> typedArray2 = <span class="hljs-keyword">new</span> <span class="hlj…
67560 <span class="hljs-title function_">markAsUntransferable</span>(pooledBuffer);
67562 <span class="hljs-keyword">const</span> { port1 } = <span class="hljs-keyword">new</span> <span cla…
67563 port1.<span class="hljs-title function_">postMessage</span>(typedArray1, [ typedArray1.<span class=…
67565 <span class="hljs-comment">// The following line prints the contents of typedArray1 -- it still own…
67566 <span class="hljs-comment">// its memory and has been cloned, not transferred. Without</span>
67567 <span class="hljs-comment">// `markAsUntransferable()`, this would print an empty Uint8Array.</span>
67568 <span class="hljs-comment">// typedArray2 is intact as well.</span>
67569 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67570 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67581 <p><code>contextifiedSandbox</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScrip…
67591 <p>The returned <code>MessagePort</code> will be an object in the target context, and will
67593 …per.mozilla.org/en-US/docs/Web/API/MessagePort/onmessage"><code>port.onmessage()</code></a> listen…
67596 …eloper.mozilla.org/en-US/docs/Web/API/EventTarget"><code>EventTarget</code></a>, and only <a href=…
67603 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" cla…
67611-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, isMa…
67613 <span class="hljs-keyword">if</span> (isMainThread) {
67614 …<span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class…
67615 …span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span cl…
67616 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
67618 …worker.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'Hello, wor…
67619 } <span class="hljs-keyword">else</span> {
67620 …<span class="hljs-comment">// When a message from the parent thread is received, send it back:</sp…
67621 …span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span cl…
67622 parentPort.<span class="hljs-title function_">postMessage</span>(message);
67634 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> …
67637 <p>Receive a single message from a given <code>MessagePort</code>. If no message is available,
67638 <code>undefined</code> is returned, otherwise an object with a single <code>message</code> property
67641-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</spa…
67642 <span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <s…
67643 port1.<span class="hljs-title function_">postMessage</span>({ <span class="hljs-attr">hello</span>:…
67645 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67646 <span class="hljs-comment">// Prints: { message: { hello: 'world' } }</span>
67647 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(…
67648 <span class="hljs-comment">// Prints: undefined</span></code></pre>
67656 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
67658 <li><code>maxYoungGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
67659 <li><code>maxOldGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
67660 <li><code>codeRangeSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
67661 <li><code>stackSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
67674 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" c…
67679-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, <spa…
67680-keyword">new</span> <span class="hljs-title class_">Worker</span>(<span class="hljs-string">'proc…
67681 ….<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class…
67682-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span cl…
67688 … api_stability_1"><a href="#documentation_stability_index">Stability: 1</a> - Experimental</div><p…
67690 <li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structur…
67691 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" clas…
67692 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
67704 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
67708 …a href="#worker_threads_class_worker"><code>Worker</code></a> instance inside a single process.</p>
67716 according to the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structur…
67717-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, isMa…
67719 <span class="hljs-keyword">if</span> (isMainThread) {
67720-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title clas…
67721 } <span class="hljs-keyword">else</span> {
67722 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
67729 two-way communications channel.
67733-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</spa…
67735 <span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <s…
67736-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function…
67737 port2.<span class="hljs-title function_">postMessage</span>({ <span class="hljs-attr">foo</span>: <…
67738 <span class="hljs-comment">// Prints: received { foo: 'bar' } from the `port1.on('message')` listen…
67755 asynchronous, two-way communications channel. It can be used to transfer
67758 <p>This implementation matches <a href="https://developer.mozilla.org/en-US/docs/Web/API/MessagePor…
67765-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</spa…
67766 <span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <s…
67768 <span class="hljs-comment">// Prints:</span>
67769 <span class="hljs-comment">// foobar</span>
67770 <span class="hljs-comment">// closed!</span>
67771-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function…
67772-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">…
67774 port1.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'foobar'</spa…
67775 port1.<span class="hljs-title function_">close</span>();</code></pre>
67781 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
67792 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
67828 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
67829 <li><code>transferList</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
67833 the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algo…
67841 <li><code>value</code> may contain <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
67842 <li><code>value</code> may not contain native (C++-backed) objects other than:
67853-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</spa…
67854 <span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <s…
67856-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function…
67858 <span class="hljs-keyword">const</span> circularData = {};
67859 circularData.<span class="hljs-property">foo</span> = circularData;
67860 <span class="hljs-comment">// Prints: { foo: [Circular] }</span>
67861 port2.<span class="hljs-title function_">postMessage</span>(circularData);</code></pre>
67862 <p><code>transferList</code> may be a list of <a href="https://developer.mozilla.org/en-US/docs/Web…
67868 <p>If <code>value</code> contains <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/…
67872-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">MessageChannel</spa…
67873 <span class="hljs-keyword">const</span> { port1, port2 } = <span class="hljs-keyword">new</span> <s…
67875-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function…
67877-keyword">const</span> uint8Array = <span class="hljs-keyword">new</span> <span class="hljs-title …
67878 <span class="hljs-comment">// This posts a copy of `uint8Array`:</span>
67879 port2.<span class="hljs-title function_">postMessage</span>(uint8Array);
67880 <span class="hljs-comment">// This does not copy data, but renders `uint8Array` unusable:</span>
67881 port2.<span class="hljs-title function_">postMessage</span>(uint8Array, [ uint8Array.<span class="h…
67883 <span class="hljs-comment">// The memory for the `sharedUint8Array` will be accessible from both th…
67884 <span class="hljs-comment">// original and the copy received by `.on('message')`:</span>
67885-keyword">const</span> sharedUint8Array = <span class="hljs-keyword">new</span> <span class="hljs-
67886 port2.<span class="hljs-title function_">postMessage</span>(sharedUint8Array);
67888 <span class="hljs-comment">// This transfers a freshly created message port to the receiver.</span>
67889 <span class="hljs-comment">// This can be used, for example, to create communication channels betwe…
67890 <span class="hljs-comment">// multiple `Worker` threads that are children of the same parent thread…
67891 <span class="hljs-keyword">const</span> otherChannel = <span class="hljs-keyword">new</span> <span …
67892-title function_">postMessage</span>({ <span class="hljs-attr">port</span>: otherChannel.<span cla…
67894 non-enumerable properties, property accessors, and object prototypes are
67896 plain <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uin…
67910-js"><span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">new</span> <span clas…
67912 … class="hljs-keyword">const</span> u1 = <span class="hljs-keyword">new</span> <span class="hljs-ti…
67913 … class="hljs-keyword">const</span> u2 = <span class="hljs-keyword">new</span> <span class="hljs-ti…
67915 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(u2.<span class…
67917 port.<span class="hljs-title function_">postMessage</span>(u1, [u1.<span class="hljs-property">buff…
67919 …js-variable language_">console</span>.<span class="hljs-title function_">log</span>(u2.<span class…
67986 …exit_code"><code>process.exit()</code></a> does not stop the whole program, just the single thread,
67992 threads, and will not be visible to native add-ons (unless
68001 <li>Native add-ons can only be loaded from multiple threads if they fulfill
68005 …er.mozilla.org/en-US/docs/Web/API/Web_Workers_API">Web Workers</a> and the <a href="cluster.html">…
68006 achieved through inter-thread message passing. Internally, a <code>Worker</code> has a
68007 built-in pair of <a href="#worker_threads_class_messageport"><code>MessagePort</code></a>s that are…
68016 pre-existing channel, such as the global one.</p>
68020 …ode class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_i…
68021 <span class="hljs-keyword">const</span> {
68022 …<span class="hljs-title class_">Worker</span>, <span class="hljs-title class_">MessageChannel</spa…
68023 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'worker_threads'</span>);
68024 <span class="hljs-keyword">if</span> (isMainThread) {
68025 …<span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class…
68026 …<span class="hljs-keyword">const</span> subChannel = <span class="hljs-keyword">new</span> <span c…
68027-title function_">postMessage</span>({ <span class="hljs-attr">hereIsYourPort</span>: subChannel.<…
68028-property">port2</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'me…
68029 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
68031 } <span class="hljs-keyword">else</span> {
68032 …span class="hljs-title function_">once</span>(<span class="hljs-string">'message'</span>, <span cl…
68033 …s-title function_">assert</span>(value.<span class="hljs-property">hereIsYourPort</span> <span cla…
68034 …value.<span class="hljs-property">hereIsYourPort</span>.<span class="hljs-title function_">postMes…
68035 …value.<span class="hljs-property">hereIsYourPort</span>.<span class="hljs-title function_">close</…
68063 <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
68067 When using a <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs"><…
68071 <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference…
68073 <li><code>argv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
68077 <li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
68082 <li><code>eval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
68085 <li><code>execArgv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
68086 V8 options (such as <code>--max-old-space-size</code>) and options that affect the
68087 process (such as <code>--title</code>) are not supported. If set, this will be provided
68090 <li><code>stdin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
68093 <li><code>stdout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
68095 <li><code>stderr</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
68097 <li><code>workerData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
68099 occur as described in the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API…
68102 <li><code>trackUnmanagedFds</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript…
68108 …illa.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object[]></…
68112 <li><code>resourceLimits</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re…
68116 may still abort if it encounters a global out-of-memory situation.
68118 <li><code>maxOldGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
68119 <li><code>maxYoungGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
68121 …/developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;n…
68123 <li><code>stackSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
68135 <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo…
68144 <li><code>exitCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
68156 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
68168 <li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/G…
68182 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
68201 <li><code>utilization1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
68203 <li><code>utilization2</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
68205 <li>Returns <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objec…
68207 <li><code>idle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
68208 <li><code>active</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struc…
68209 <li><code>utilization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
68222-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Worker</span>, isMa…
68224 <span class="hljs-keyword">if</span> (isMainThread) {
68225 …<span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class…
68226 <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> {
68227 …worker.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'hi'</span>…
68228-variable language_">console</span>.<span class="hljs-title function_">log</span>(worker.<span cla…
68229 }, <span class="hljs-number">100</span>).<span class="hljs-title function_">unref</span>();
68230 <span class="hljs-keyword">return</span>;
68233-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function…
68234 (<span class="hljs-keyword">function</span> <span class="hljs-title function_">r</span>(<span class…
68235 …<span class="hljs-keyword">if</span> (--n &#x3C; <span class="hljs-number">0</span>) <span class="…
68236 …<span class="hljs-keyword">const</span> t = <span class="hljs-title class_">Date</span>.<span clas…
68237 …hljs-keyword">while</span> (<span class="hljs-title class_">Date</span>.<span class="hljs-title fu…
68238 <span class="hljs-title function_">setImmediate</span>(r, n);
68239 })(<span class="hljs-number">10</span>);</code></pre>
68248 <li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
68249 <li><code>transferList</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe…
68267 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Objec…
68269 <li><code>maxYoungGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/Jav…
68270 <li><code>maxOldGenerationSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaS…
68271 <li><code>codeRangeSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
68272 <li><code>stackSizeMb</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
68296 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" cla…
68325 <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje…
68335 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
68339 This value is unique for each <code>Worker</code> instance inside a single process.</p>
68354 … class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><co…
68355 <span class="hljs-title class_">Worker</span>,
68357 } <span class="hljs-keyword">from</span> <span class="hljs-string">'worker_threads'</span>;
68359 <span class="hljs-keyword">if</span> (isMainThread) {
68360-keyword">new</span> <span class="hljs-title class_">Worker</span>(<span class="hljs-keyword">new<…
68361 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-num…
68362 } <span class="hljs-keyword">else</span> {
68363 …<span class="hljs-comment">// This output will be blocked by the for loop in the main thread.</spa…
68364 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
68365 }</code><code class="language-js cjs"><span class="hljs-meta">'use strict'</span>;
68367 <span class="hljs-keyword">const</span> {
68368 <span class="hljs-title class_">Worker</span>,
68370 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'worker_threads'</span>);
68372 <span class="hljs-keyword">if</span> (isMainThread) {
68373 <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(__filename);
68374 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> n = <span class="hljs-num…
68375 } <span class="hljs-keyword">else</span> {
68376 …<span class="hljs-comment">// This output will be blocked by the for loop in the main thread.</spa…
68377 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
68381 and run using the <code>-r</code> command line flag). Unless the <code>execArgv</code> option is
68389 …ty api_stability_2"><a href="#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
68394 …code class="language-js"><span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in…
68399 …class="language-js"><span class="hljs-keyword">const</span> { createGzip } = <span class="hljs-bui…
68400 <span class="hljs-keyword">const</span> { pipeline } = <span class="hljs-built_in">require</span>(<…
68401 <span class="hljs-keyword">const</span> {
68404 } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
68406 <span class="hljs-keyword">const</span> gzip = <span class="hljs-title function_">createGzip</span>…
68407 <span class="hljs-keyword">const</span> source = <span class="hljs-title function_">createReadStrea…
68408 <span class="hljs-keyword">const</span> destination = <span class="hljs-title function_">createWrit…
68410 <span class="hljs-title function_">pipeline</span>(source, gzip, destination, <span class="hljs-fun…
68411 <span class="hljs-keyword">if</span> (err) {
68412 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68413 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68417 <span class="hljs-comment">// Or, Promisified</span>
68419 <span class="hljs-keyword">const</span> { promisify } = <span class="hljs-built_in">require</span>(…
68420 <span class="hljs-keyword">const</span> pipe = <span class="hljs-title function_">promisify</span>(…
68422 …ass="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title…
68423 …<span class="hljs-keyword">const</span> gzip = <span class="hljs-title function_">createGzip</span…
68424 …<span class="hljs-keyword">const</span> source = <span class="hljs-title function_">createReadStre…
68425 …<span class="hljs-keyword">const</span> destination = <span class="hljs-title function_">createWri…
68426 …<span class="hljs-keyword">await</span> <span class="hljs-title function_">pipe</span>(source, gzi…
68429 <span class="hljs-title function_">do_gzip</span>(<span class="hljs-string">'input.txt'</span>, <sp…
68430 ….<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-pa…
68431 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68432 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68434 <p>It is also possible to compress or decompress data in a single step:</p>
68435 …ass="language-js"><span class="hljs-keyword">const</span> { deflate, unzip } = <span class="hljs-b…
68437 <span class="hljs-keyword">const</span> input = <span class="hljs-string">'........................…
68438 <span class="hljs-title function_">deflate</span>(input, <span class="hljs-function">(<span class="…
68439 <span class="hljs-keyword">if</span> (err) {
68440 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68441 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68443-variable language_">console</span>.<span class="hljs-title function_">log</span>(buffer.<span cla…
68446-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-ti…
68447 <span class="hljs-title function_">unzip</span>(buffer, <span class="hljs-function">(<span class="h…
68448 <span class="hljs-keyword">if</span> (err) {
68449 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68450 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68452 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
68455 <span class="hljs-comment">// Or, Promisified</span>
68457 <span class="hljs-keyword">const</span> { promisify } = <span class="hljs-built_in">require</span>(…
68458 <span class="hljs-keyword">const</span> do_unzip = <span class="hljs-title function_">promisify</sp…
68460 <span class="hljs-title function_">do_unzip</span>(buffer)
68461-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">buf</span>) =…
68462 ….<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-pa…
68463 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68464 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68472 …code class="language-js"><span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in…
68474 …hljs-keyword">const</span> payload = <span class="hljs-title class_">Buffer</span>.<span class="hl…
68476 <span class="hljs-comment">// WARNING: DO NOT DO THIS!</span>
68477 …n class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-num…
68478 …zlib.<span class="hljs-title function_">deflate</span>(payload, <span class="hljs-function">(<span…
68487 and <code>br</code> content-encoding mechanisms defined by
68488 <a href="https://tools.ietf.org/html/rfc7230#section-4.2">HTTP</a>.</p>
68489 <p>The HTTP <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3"><code>Accept-
68490 … The <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.11"><code>Content-Enco…
68497 <pre><code class="language-js"><span class="hljs-comment">// Client request example</span>
68498 <span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in">require</span>(<span cla…
68499 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
68500 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
68501 <span class="hljs-keyword">const</span> { pipeline } = <span class="hljs-built_in">require</span>(<…
68503 …="hljs-keyword">const</span> request = http.<span class="hljs-title function_">get</span>({ <span …
68504 … <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>,
68505 <span class="hljs-attr">port</span>: <span class="hljs-number">80</span>,
68506 …<span class="hljs-attr">headers</span>: { <span class="hljs-string">'Accept-Encoding'</span>: <spa…
68507 …span class="hljs-title function_">on</span>(<span class="hljs-string">'response'</span>, <span cla…
68508 …<span class="hljs-keyword">const</span> output = fs.<span class="hljs-title function_">createWrite…
68510 …<span class="hljs-keyword">const</span> <span class="hljs-title function_">onError</span> = (<span…
68511 <span class="hljs-keyword">if</span> (err) {
68512 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68513 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68517 …<span class="hljs-keyword">switch</span> (response.<span class="hljs-property">headers</span>[<spa…
68518 <span class="hljs-keyword">case</span> <span class="hljs-string">'br'</span>:
68519 …<span class="hljs-title function_">pipeline</span>(response, zlib.<span class="hljs-title function…
68520 <span class="hljs-keyword">break</span>;
68521 …<span class="hljs-comment">// Or, just use zlib.createUnzip() to handle both of the following case…
68522 <span class="hljs-keyword">case</span> <span class="hljs-string">'gzip'</span>:
68523 …<span class="hljs-title function_">pipeline</span>(response, zlib.<span class="hljs-title function…
68524 <span class="hljs-keyword">break</span>;
68525 <span class="hljs-keyword">case</span> <span class="hljs-string">'deflate'</span>:
68526 …<span class="hljs-title function_">pipeline</span>(response, zlib.<span class="hljs-title function…
68527 <span class="hljs-keyword">break</span>;
68528 <span class="hljs-attr">default</span>:
68529 <span class="hljs-title function_">pipeline</span>(response, output, onError);
68530 <span class="hljs-keyword">break</span>;
68533 <pre><code class="language-js"><span class="hljs-comment">// server example</span>
68534 <span class="hljs-comment">// Running a gzip operation on every request is quite expensive.</span>
68535 <span class="hljs-comment">// It would be much more efficient to cache the compressed buffer.</span>
68536 <span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in">require</span>(<span cla…
68537 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
68538 <span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class…
68539 <span class="hljs-keyword">const</span> { pipeline } = <span class="hljs-built_in">require</span>(<…
68541 http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span clas…
68542 …<span class="hljs-keyword">const</span> raw = fs.<span class="hljs-title function_">createReadStre…
68543 …<span class="hljs-comment">// Store both a compressed and an uncompressed version of the resource.…
68544 …nse.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Vary'</span>, <…
68545 …pan class="hljs-keyword">let</span> acceptEncoding = request.<span class="hljs-property">headers</…
68546 <span class="hljs-keyword">if</span> (!acceptEncoding) {
68547 acceptEncoding = <span class="hljs-string">''</span>;
68550 …<span class="hljs-keyword">const</span> <span class="hljs-title function_">onError</span> = (<span…
68551 <span class="hljs-keyword">if</span> (err) {
68552 <span class="hljs-comment">// If an error occurs, there's not much we can do because</span>
68553 <span class="hljs-comment">// the server has already sent the 200 response code and</span>
68554 <span class="hljs-comment">// some amount of data has already been sent to the client.</span>
68555 <span class="hljs-comment">// The best we can do is terminate the response immediately</span>
68556 <span class="hljs-comment">// and log the error.</span>
68557 response.<span class="hljs-title function_">end</span>();
68558 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68562 <span class="hljs-comment">// Note: This is not a conformant accept-encoding parser.</span>
68563 …<span class="hljs-comment">// See https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3<…
68564 …<span class="hljs-keyword">if</span> (<span class="hljs-regexp">/\bdeflate\b/</span>.<span class="…
68565 …ljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-st…
68566 …<span class="hljs-title function_">pipeline</span>(raw, zlib.<span class="hljs-title function_">cr…
68567 …pan class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-regex…
68568 …ljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-st…
68569 …<span class="hljs-title function_">pipeline</span>(raw, zlib.<span class="hljs-title function_">cr…
68570 …span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-rege…
68571 …ljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-st…
68572 …<span class="hljs-title function_">pipeline</span>(raw, zlib.<span class="hljs-title function_">cr…
68573 } <span class="hljs-keyword">else</span> {
68574 …response.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>,…
68575 <span class="hljs-title function_">pipeline</span>(raw, response, onError);
68577 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);</code><…
68583 <pre><code class="language-js"><span class="hljs-comment">// This is a truncated version of the buf…
68584-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-ti…
68586 zlib.<span class="hljs-title function_">unzip</span>(
68588 …<span class="hljs-comment">// For Brotli, the equivalent is zlib.constants.BROTLI_OPERATION_FLUSH.…
68589 …{ <span class="hljs-attr">finishFlush</span>: zlib.<span class="hljs-property">constants</span>.<s…
68590 <span class="hljs-function">(<span class="hljs-params">err, buffer</span>) =></span> {
68591 <span class="hljs-keyword">if</span> (err) {
68592 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68593 process.<span class="hljs-property">exitCode</span> = <span class="hljs-number">1</span>;
68595 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>…
68597 <p>This will not change the behavior in other error-throwing situations, e.g.
68604 <h4>For zlib-based streams<span><a class="mark" href="#zlib_for_zlib_based_streams" id="zlib_for_zl…
68607 <!-- eslint-disable semi -->
68608-js">(<span class="hljs-number">1</span> &#x3C;&#x3C; (windowBits + <span class="hljs-number">2</s…
68613-js"><span class="hljs-keyword">const</span> options = { <span class="hljs-attr">windowBits</span>…
68618 <p>This is in addition to a single internal output slab buffer of size
68628 <h4>For Brotli-based streams<span><a class="mark" href="#zlib_for_brotli_based_streams" id="zlib_fo…
68629 <p>There are equivalents to the zlib options for Brotli-based streams, although
68635 <p>See <a href="#zlib_brotli_constants">below</a> for more details on Brotli-specific options.</p>
68642 …code class="language-js"><span class="hljs-keyword">const</span> zlib = <span class="hljs-built_in…
68643 <span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span cla…
68644 <span class="hljs-keyword">const</span> { pipeline } = <span class="hljs-built_in">require</span>(<…
68646 http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span clas…
68647 …<span class="hljs-comment">// For the sake of simplicity, the Accept-Encoding checks are omitted.<…
68648 …ljs-title function_">writeHead</span>(<span class="hljs-number">200</span>, { <span class="hljs-st…
68649 …<span class="hljs-keyword">const</span> output = zlib.<span class="hljs-title function_">createGzi…
68650 <span class="hljs-keyword">let</span> i;
68652 …<span class="hljs-title function_">pipeline</span>(output, response, <span class="hljs-function">(…
68653 <span class="hljs-keyword">if</span> (err) {
68654 <span class="hljs-comment">// If an error occurs, there's not much we can do because</span>
68655 <span class="hljs-comment">// the server has already sent the 200 response code and</span>
68656 <span class="hljs-comment">// some amount of data has already been sent to the client.</span>
68657 <span class="hljs-comment">// The best we can do is terminate the response immediately</span>
68658 <span class="hljs-comment">// and log the error.</span>
68659 <span class="hljs-built_in">clearInterval</span>(i);
68660 response.<span class="hljs-title function_">end</span>();
68661 …<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</spa…
68665 i = <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> {
68666-title function_">write</span>(<span class="hljs-string">`The current time is <span class="hljs-su…
68667 …<span class="hljs-comment">// The data has been passed to zlib, but the compression algorithm may<…
68668 …<span class="hljs-comment">// have decided to buffer the data for more efficient compression.</spa…
68669 …<span class="hljs-comment">// Calling .flush() will make the data available as soon as the client<…
68670 <span class="hljs-comment">// is ready to receive it.</span>
68671 output.<span class="hljs-title function_">flush</span>();
68673 }, <span class="hljs-number">1000</span>);
68674 }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">1337</span>);</code><…
68732 <p>There are several options and other constants available for Brotli-based
68735 <p>The following values are valid flush operations for Brotli-based streams:</p>
68758 <li><code>BROTLI_MODE_TEXT</code>, adjusted for UTF-8 text</li>
68800 Brotli format as standardized in <a href="https://www.rfc-editor.org/rfc/rfc7932.txt">RFC 7932</a>)…
68825 Brotli format as standardized in <a href="https://www.rfc-editor.org/rfc/rfc7932.txt">RFC 7932</a>)…
68848 <p>Each zlib-based class takes an <code>options</code> object. No options are required.</p>
68852 <li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
68853 <li><code>finishFlush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
68854 <li><code>chunkSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
68855 <li><code>windowBits</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_s…
68856 <li><code>level</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
68857 <li><code>memLevel</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
68858 <li><code>strategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
68859-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
68861 <li><code>info</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structu…
68862 <li><code>maxOutputLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
68880 <p>Each Brotli-based class takes an <code>options</code> object. All options are optional.</p>
68882 <li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
68883 <li><code>finishFlush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_…
68884 <li><code>chunkSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_st…
68885 …tps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type…
68886 <li><code>maxOutputLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/D…
68890 <pre><code class="language-js"><span class="hljs-keyword">const</span> stream = zlib.<span class="h…
68891 …<span class="hljs-attr">chunkSize</span>: <span class="hljs-number">32</span> * <span class="hljs-
68892 <span class="hljs-attr">params</span>: {
68893 …s-property">constants</span>.<span class="hljs-property">BROTLI_PARAM_MODE</span>]: zlib.<span cla…
68894 …[zlib.<span class="hljs-property">constants</span>.<span class="hljs-property">BROTLI_PARAM_QUALIT…
68895-property">constants</span>.<span class="hljs-property">BROTLI_PARAM_SIZE_HINT</span>]: fs.<span c…
68972 <p>Decompress either a Gzip- or Deflate-compressed stream by auto-detecting
68994 … api_stability_0"><a href="#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a h…
68996 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
69007 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" c…
69017 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69025 <li><code>kind</code> <strong>Default:</strong> <code>zlib.constants.Z_FULL_FLUSH</code> for zlib-b…
69026 <code>zlib.constants.BROTLI_OPERATION_FLUSH</code> for Brotli-based streams.</li>
69027 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69040 <li><code>level</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_struct…
69041 <li><code>strategy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_str…
69042 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69044 <p>This function is only available for zlib-based streams, i.e. not Brotli.</p>
69057 <p>Provides an object enumerating Zlib-related constants.</p>
69095 that effectively uses an 8-bit window only.</p>
69139 …lla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>…
69140 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuff…
69150-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69152 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69159-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69168-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69170 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69177-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69198-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69200 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69219-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69238-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69240 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69259-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69280-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69282 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69301-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69322-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69324 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69343-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69364-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69366 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69385-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69406-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69408 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69427-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69448-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69450 <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc…
69469-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | …
69474 <!-- API END -->
69483 const themeToggleButton = document.getElementById('theme-toggle-btn');
69485 const mq = window.matchMedia('(prefers-color-scheme: dark)');
69488 document.body.classList.toggle('dark-mode', e.matches);
69498 document.body.classList.add('dark-mode');
69501 document.body.classList.add('dark-mode');
69508 document.body.classList.toggle('dark-mode')