Server IP : 85.214.239.14 / Your IP : 18.119.123.10 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/2/root/proc/3/task/3/cwd/usr/share/doc/nodejs/api/ |
Upload File : |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <meta name="nodejs.org:node-version" content="v20.18.0"> <title>Node-API | Node.js v20.18.0 Documentation</title> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> <link rel="stylesheet" href="assets/style.css"> <link rel="stylesheet" href="assets/hljs.css"> <link rel="canonical" href="https://nodejs.org/api/n-api.html"> <script async defer src="assets/api.js" type="text/javascript"></script> <script> const storedTheme = localStorage.getItem('theme'); // Follow operating system theme preference if (storedTheme === null && window.matchMedia) { const mq = window.matchMedia('(prefers-color-scheme: dark)'); if (mq.matches) { document.documentElement.classList.add('dark-mode'); } } else if (storedTheme === 'dark') { document.documentElement.classList.add('dark-mode'); } </script> </head> <body class="alt apidoc" id="api-section-n-api"> <div id="content" class="clearfix"> <div role="navigation" id="column2" class="interior"> <div id="intro" class="interior"> <a href="/" title="Go back to the home page"> Node.js </a> </div> <ul> <li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> <li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> </ul> <hr class="line"> <ul> <li><a href="assert.html" class="nav-assert">Assertion testing</a></li> <li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> <li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> <li><a href="buffer.html" class="nav-buffer">Buffer</a></li> <li><a href="addons.html" class="nav-addons">C++ addons</a></li> <li><a href="n-api.html" class="nav-n-api active">C/C++ addons with Node-API</a></li> <li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> <li><a href="child_process.html" class="nav-child_process">Child processes</a></li> <li><a href="cluster.html" class="nav-cluster">Cluster</a></li> <li><a href="cli.html" class="nav-cli">Command-line options</a></li> <li><a href="console.html" class="nav-console">Console</a></li> <li><a href="corepack.html" class="nav-corepack">Corepack</a></li> <li><a href="crypto.html" class="nav-crypto">Crypto</a></li> <li><a href="debugger.html" class="nav-debugger">Debugger</a></li> <li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> <li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> <li><a href="dns.html" class="nav-dns">DNS</a></li> <li><a href="domain.html" class="nav-domain">Domain</a></li> <li><a href="errors.html" class="nav-errors">Errors</a></li> <li><a href="events.html" class="nav-events">Events</a></li> <li><a href="fs.html" class="nav-fs">File system</a></li> <li><a href="globals.html" class="nav-globals">Globals</a></li> <li><a href="http.html" class="nav-http">HTTP</a></li> <li><a href="http2.html" class="nav-http2">HTTP/2</a></li> <li><a href="https.html" class="nav-https">HTTPS</a></li> <li><a href="inspector.html" class="nav-inspector">Inspector</a></li> <li><a href="intl.html" class="nav-intl">Internationalization</a></li> <li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> <li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> <li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> <li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> <li><a href="net.html" class="nav-net">Net</a></li> <li><a href="os.html" class="nav-os">OS</a></li> <li><a href="path.html" class="nav-path">Path</a></li> <li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> <li><a href="permissions.html" class="nav-permissions">Permissions</a></li> <li><a href="process.html" class="nav-process">Process</a></li> <li><a href="punycode.html" class="nav-punycode">Punycode</a></li> <li><a href="querystring.html" class="nav-querystring">Query strings</a></li> <li><a href="readline.html" class="nav-readline">Readline</a></li> <li><a href="repl.html" class="nav-repl">REPL</a></li> <li><a href="report.html" class="nav-report">Report</a></li> <li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> <li><a href="stream.html" class="nav-stream">Stream</a></li> <li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> <li><a href="test.html" class="nav-test">Test runner</a></li> <li><a href="timers.html" class="nav-timers">Timers</a></li> <li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> <li><a href="tracing.html" class="nav-tracing">Trace events</a></li> <li><a href="tty.html" class="nav-tty">TTY</a></li> <li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> <li><a href="url.html" class="nav-url">URL</a></li> <li><a href="util.html" class="nav-util">Utilities</a></li> <li><a href="v8.html" class="nav-v8">V8</a></li> <li><a href="vm.html" class="nav-vm">VM</a></li> <li><a href="wasi.html" class="nav-wasi">WASI</a></li> <li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> <li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> <li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> <li><a href="zlib.html" class="nav-zlib">Zlib</a></li> </ul> <hr class="line"> <ul> <li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> </ul> </div> <div id="column1" data-id="n-api" class="interior"> <header class="header"> <div class="header-container"> <h1>Node.js v20.18.0 documentation</h1> <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> <path fill="none" d="M0 0h24v24H0z" /> <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> </svg> <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> <path d="M0 0h24v24H0z" fill="none" /> <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> </svg> </button> </div> <div id="gtoc"> <ul> <li class="pinned-header">Node.js v20.18.0</li> <li class="picker-header"> <a href="#"> <span class="picker-arrow"></span> Table of contents </a> <div class="picker"><div class="toc"><ul> <li><span class="stability_2"><a href="#node-api">Node-API</a></span> <ul> <li><a href="#implications-of-abi-stability">Implications of ABI stability</a></li> <li><a href="#building">Building</a> <ul> <li><a href="#build-tools">Build tools</a> <ul> <li><a href="#node-gyp">node-gyp</a></li> <li><a href="#cmakejs">CMake.js</a></li> </ul> </li> <li><a href="#uploading-precompiled-binaries">Uploading precompiled binaries</a> <ul> <li><a href="#node-pre-gyp">node-pre-gyp</a></li> <li><a href="#prebuild">prebuild</a></li> <li><a href="#prebuildify">prebuildify</a></li> </ul> </li> </ul> </li> <li><a href="#usage">Usage</a></li> <li><a href="#node-api-version-matrix">Node-API version matrix</a></li> <li><a href="#environment-life-cycle-apis">Environment life cycle APIs</a> <ul> <li><a href="#napi_set_instance_data"><code>napi_set_instance_data</code></a></li> <li><a href="#napi_get_instance_data"><code>napi_get_instance_data</code></a></li> </ul> </li> <li><a href="#basic-node-api-data-types">Basic Node-API data types</a> <ul> <li><a href="#napi_status"><code>napi_status</code></a></li> <li><a href="#napi_extended_error_info"><code>napi_extended_error_info</code></a></li> <li><a href="#napi_env"><code>napi_env</code></a></li> <li><span class="stability_1"><a href="#node_api_basic_env"><code>node_api_basic_env</code></a></span></li> <li><a href="#napi_value"><code>napi_value</code></a></li> <li><a href="#napi_threadsafe_function"><code>napi_threadsafe_function</code></a></li> <li><a href="#napi_threadsafe_function_release_mode"><code>napi_threadsafe_function_release_mode</code></a></li> <li><a href="#napi_threadsafe_function_call_mode"><code>napi_threadsafe_function_call_mode</code></a></li> <li><a href="#node-api-memory-management-types">Node-API memory management types</a> <ul> <li><a href="#napi_handle_scope"><code>napi_handle_scope</code></a></li> <li><a href="#napi_escapable_handle_scope"><code>napi_escapable_handle_scope</code></a></li> <li><a href="#napi_ref"><code>napi_ref</code></a></li> <li><a href="#napi_type_tag"><code>napi_type_tag</code></a></li> <li><a href="#napi_async_cleanup_hook_handle"><code>napi_async_cleanup_hook_handle</code></a></li> </ul> </li> <li><a href="#node-api-callback-types">Node-API callback types</a> <ul> <li><a href="#napi_callback_info"><code>napi_callback_info</code></a></li> <li><a href="#napi_callback"><code>napi_callback</code></a></li> <li><span class="stability_1"><a href="#node_api_basic_finalize"><code>node_api_basic_finalize</code></a></span></li> <li><a href="#napi_finalize"><code>napi_finalize</code></a></li> <li><a href="#napi_async_execute_callback"><code>napi_async_execute_callback</code></a></li> <li><a href="#napi_async_complete_callback"><code>napi_async_complete_callback</code></a></li> <li><a href="#napi_threadsafe_function_call_js"><code>napi_threadsafe_function_call_js</code></a></li> <li><a href="#napi_cleanup_hook"><code>napi_cleanup_hook</code></a></li> <li><a href="#napi_async_cleanup_hook"><code>napi_async_cleanup_hook</code></a></li> </ul> </li> </ul> </li> <li><a href="#error-handling">Error handling</a> <ul> <li><a href="#return-values">Return values</a> <ul> <li><a href="#napi_get_last_error_info"><code>napi_get_last_error_info</code></a></li> </ul> </li> <li><a href="#exceptions">Exceptions</a> <ul> <li><a href="#napi_throw"><code>napi_throw</code></a></li> <li><a href="#napi_throw_error"><code>napi_throw_error</code></a></li> <li><a href="#napi_throw_type_error"><code>napi_throw_type_error</code></a></li> <li><a href="#napi_throw_range_error"><code>napi_throw_range_error</code></a></li> <li><a href="#node_api_throw_syntax_error"><code>node_api_throw_syntax_error</code></a></li> <li><a href="#napi_is_error"><code>napi_is_error</code></a></li> <li><a href="#napi_create_error"><code>napi_create_error</code></a></li> <li><a href="#napi_create_type_error"><code>napi_create_type_error</code></a></li> <li><a href="#napi_create_range_error"><code>napi_create_range_error</code></a></li> <li><a href="#node_api_create_syntax_error"><code>node_api_create_syntax_error</code></a></li> <li><a href="#napi_get_and_clear_last_exception"><code>napi_get_and_clear_last_exception</code></a></li> <li><a href="#napi_is_exception_pending"><code>napi_is_exception_pending</code></a></li> <li><a href="#napi_fatal_exception"><code>napi_fatal_exception</code></a></li> </ul> </li> <li><a href="#fatal-errors">Fatal errors</a> <ul> <li><a href="#napi_fatal_error"><code>napi_fatal_error</code></a></li> </ul> </li> </ul> </li> <li><a href="#object-lifetime-management">Object lifetime management</a> <ul> <li><a href="#making-handle-lifespan-shorter-than-that-of-the-native-method">Making handle lifespan shorter than that of the native method</a> <ul> <li><a href="#napi_open_handle_scope"><code>napi_open_handle_scope</code></a></li> <li><a href="#napi_close_handle_scope"><code>napi_close_handle_scope</code></a></li> <li><a href="#napi_open_escapable_handle_scope"><code>napi_open_escapable_handle_scope</code></a></li> <li><a href="#napi_close_escapable_handle_scope"><code>napi_close_escapable_handle_scope</code></a></li> <li><a href="#napi_escape_handle"><code>napi_escape_handle</code></a></li> </ul> </li> <li><a href="#references-to-values-with-a-lifespan-longer-than-that-of-the-native-method">References to values with a lifespan longer than that of the native method</a> <ul> <li><a href="#napi_create_reference"><code>napi_create_reference</code></a></li> <li><a href="#napi_delete_reference"><code>napi_delete_reference</code></a></li> <li><a href="#napi_reference_ref"><code>napi_reference_ref</code></a></li> <li><a href="#napi_reference_unref"><code>napi_reference_unref</code></a></li> <li><a href="#napi_get_reference_value"><code>napi_get_reference_value</code></a></li> </ul> </li> <li><a href="#cleanup-on-exit-of-the-current-nodejs-environment">Cleanup on exit of the current Node.js environment</a> <ul> <li><a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a></li> <li><a href="#napi_remove_env_cleanup_hook"><code>napi_remove_env_cleanup_hook</code></a></li> <li><a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a></li> <li><a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a></li> </ul> </li> <li><a href="#finalization-on-the-exit-of-the-nodejs-environment">Finalization on the exit of the Node.js environment</a></li> </ul> </li> <li><a href="#module-registration">Module registration</a></li> <li><a href="#working-with-javascript-values">Working with JavaScript values</a> <ul> <li><a href="#enum-types">Enum types</a> <ul> <li><a href="#napi_key_collection_mode"><code>napi_key_collection_mode</code></a></li> <li><a href="#napi_key_filter"><code>napi_key_filter</code></a></li> <li><a href="#napi_key_conversion"><code>napi_key_conversion</code></a></li> <li><a href="#napi_valuetype"><code>napi_valuetype</code></a></li> <li><a href="#napi_typedarray_type"><code>napi_typedarray_type</code></a></li> </ul> </li> <li><a href="#object-creation-functions">Object creation functions</a> <ul> <li><a href="#napi_create_array"><code>napi_create_array</code></a></li> <li><a href="#napi_create_array_with_length"><code>napi_create_array_with_length</code></a></li> <li><a href="#napi_create_arraybuffer"><code>napi_create_arraybuffer</code></a></li> <li><a href="#napi_create_buffer"><code>napi_create_buffer</code></a></li> <li><a href="#napi_create_buffer_copy"><code>napi_create_buffer_copy</code></a></li> <li><a href="#napi_create_date"><code>napi_create_date</code></a></li> <li><a href="#napi_create_external"><code>napi_create_external</code></a></li> <li><a href="#napi_create_external_arraybuffer"><code>napi_create_external_arraybuffer</code></a></li> <li><a href="#napi_create_external_buffer"><code>napi_create_external_buffer</code></a></li> <li><a href="#napi_create_object"><code>napi_create_object</code></a></li> <li><a href="#napi_create_symbol"><code>napi_create_symbol</code></a></li> <li><a href="#node_api_symbol_for"><code>node_api_symbol_for</code></a></li> <li><a href="#napi_create_typedarray"><code>napi_create_typedarray</code></a></li> <li><a href="#napi_create_dataview"><code>napi_create_dataview</code></a></li> </ul> </li> <li><a href="#functions-to-convert-from-c-types-to-node-api">Functions to convert from C types to Node-API</a> <ul> <li><a href="#napi_create_int32"><code>napi_create_int32</code></a></li> <li><a href="#napi_create_uint32"><code>napi_create_uint32</code></a></li> <li><a href="#napi_create_int64"><code>napi_create_int64</code></a></li> <li><a href="#napi_create_double"><code>napi_create_double</code></a></li> <li><a href="#napi_create_bigint_int64"><code>napi_create_bigint_int64</code></a></li> <li><a href="#napi_create_bigint_uint64"><code>napi_create_bigint_uint64</code></a></li> <li><a href="#napi_create_bigint_words"><code>napi_create_bigint_words</code></a></li> <li><a href="#napi_create_string_latin1"><code>napi_create_string_latin1</code></a></li> <li><span class="stability_1"><a href="#node_api_create_external_string_latin1"><code>node_api_create_external_string_latin1</code></a></span></li> <li><a href="#napi_create_string_utf16"><code>napi_create_string_utf16</code></a></li> <li><span class="stability_1"><a href="#node_api_create_external_string_utf16"><code>node_api_create_external_string_utf16</code></a></span></li> <li><a href="#napi_create_string_utf8"><code>napi_create_string_utf8</code></a></li> </ul> </li> <li><a href="#functions-to-create-optimized-property-keys">Functions to create optimized property keys</a> <ul> <li><span class="stability_1"><a href="#node_api_create_property_key_latin1"><code>node_api_create_property_key_latin1</code></a></span></li> <li><span class="stability_1"><a href="#node_api_create_property_key_utf16"><code>node_api_create_property_key_utf16</code></a></span></li> <li><span class="stability_1"><a href="#node_api_create_property_key_utf8"><code>node_api_create_property_key_utf8</code></a></span></li> </ul> </li> <li><a href="#functions-to-convert-from-node-api-to-c-types">Functions to convert from Node-API to C types</a> <ul> <li><a href="#napi_get_array_length"><code>napi_get_array_length</code></a></li> <li><a href="#napi_get_arraybuffer_info"><code>napi_get_arraybuffer_info</code></a></li> <li><a href="#napi_get_buffer_info"><code>napi_get_buffer_info</code></a></li> <li><a href="#napi_get_prototype"><code>napi_get_prototype</code></a></li> <li><a href="#napi_get_typedarray_info"><code>napi_get_typedarray_info</code></a></li> <li><a href="#napi_get_dataview_info"><code>napi_get_dataview_info</code></a></li> <li><a href="#napi_get_date_value"><code>napi_get_date_value</code></a></li> <li><a href="#napi_get_value_bool"><code>napi_get_value_bool</code></a></li> <li><a href="#napi_get_value_double"><code>napi_get_value_double</code></a></li> <li><a href="#napi_get_value_bigint_int64"><code>napi_get_value_bigint_int64</code></a></li> <li><a href="#napi_get_value_bigint_uint64"><code>napi_get_value_bigint_uint64</code></a></li> <li><a href="#napi_get_value_bigint_words"><code>napi_get_value_bigint_words</code></a></li> <li><a href="#napi_get_value_external"><code>napi_get_value_external</code></a></li> <li><a href="#napi_get_value_int32"><code>napi_get_value_int32</code></a></li> <li><a href="#napi_get_value_int64"><code>napi_get_value_int64</code></a></li> <li><a href="#napi_get_value_string_latin1"><code>napi_get_value_string_latin1</code></a></li> <li><a href="#napi_get_value_string_utf8"><code>napi_get_value_string_utf8</code></a></li> <li><a href="#napi_get_value_string_utf16"><code>napi_get_value_string_utf16</code></a></li> <li><a href="#napi_get_value_uint32"><code>napi_get_value_uint32</code></a></li> </ul> </li> <li><a href="#functions-to-get-global-instances">Functions to get global instances</a> <ul> <li><a href="#napi_get_boolean"><code>napi_get_boolean</code></a></li> <li><a href="#napi_get_global"><code>napi_get_global</code></a></li> <li><a href="#napi_get_null"><code>napi_get_null</code></a></li> <li><a href="#napi_get_undefined"><code>napi_get_undefined</code></a></li> </ul> </li> </ul> </li> <li><a href="#working-with-javascript-values-and-abstract-operations">Working with JavaScript values and abstract operations</a> <ul> <li><a href="#napi_coerce_to_bool"><code>napi_coerce_to_bool</code></a></li> <li><a href="#napi_coerce_to_number"><code>napi_coerce_to_number</code></a></li> <li><a href="#napi_coerce_to_object"><code>napi_coerce_to_object</code></a></li> <li><a href="#napi_coerce_to_string"><code>napi_coerce_to_string</code></a></li> <li><a href="#napi_typeof"><code>napi_typeof</code></a></li> <li><a href="#napi_instanceof"><code>napi_instanceof</code></a></li> <li><a href="#napi_is_array"><code>napi_is_array</code></a></li> <li><a href="#napi_is_arraybuffer"><code>napi_is_arraybuffer</code></a></li> <li><a href="#napi_is_buffer"><code>napi_is_buffer</code></a></li> <li><a href="#napi_is_date"><code>napi_is_date</code></a></li> <li><a href="#napi_is_error_1"><code>napi_is_error</code></a></li> <li><a href="#napi_is_typedarray"><code>napi_is_typedarray</code></a></li> <li><a href="#napi_is_dataview"><code>napi_is_dataview</code></a></li> <li><a href="#napi_strict_equals"><code>napi_strict_equals</code></a></li> <li><a href="#napi_detach_arraybuffer"><code>napi_detach_arraybuffer</code></a></li> <li><a href="#napi_is_detached_arraybuffer"><code>napi_is_detached_arraybuffer</code></a></li> </ul> </li> <li><a href="#working-with-javascript-properties">Working with JavaScript properties</a> <ul> <li><a href="#structures">Structures</a> <ul> <li><a href="#napi_property_attributes"><code>napi_property_attributes</code></a></li> <li><a href="#napi_property_descriptor"><code>napi_property_descriptor</code></a></li> </ul> </li> <li><a href="#functions">Functions</a> <ul> <li><a href="#napi_get_property_names"><code>napi_get_property_names</code></a></li> <li><a href="#napi_get_all_property_names"><code>napi_get_all_property_names</code></a></li> <li><a href="#napi_set_property"><code>napi_set_property</code></a></li> <li><a href="#napi_get_property"><code>napi_get_property</code></a></li> <li><a href="#napi_has_property"><code>napi_has_property</code></a></li> <li><a href="#napi_delete_property"><code>napi_delete_property</code></a></li> <li><a href="#napi_has_own_property"><code>napi_has_own_property</code></a></li> <li><a href="#napi_set_named_property"><code>napi_set_named_property</code></a></li> <li><a href="#napi_get_named_property"><code>napi_get_named_property</code></a></li> <li><a href="#napi_has_named_property"><code>napi_has_named_property</code></a></li> <li><a href="#napi_set_element"><code>napi_set_element</code></a></li> <li><a href="#napi_get_element"><code>napi_get_element</code></a></li> <li><a href="#napi_has_element"><code>napi_has_element</code></a></li> <li><a href="#napi_delete_element"><code>napi_delete_element</code></a></li> <li><a href="#napi_define_properties"><code>napi_define_properties</code></a></li> <li><a href="#napi_object_freeze"><code>napi_object_freeze</code></a></li> <li><a href="#napi_object_seal"><code>napi_object_seal</code></a></li> </ul> </li> </ul> </li> <li><a href="#working-with-javascript-functions">Working with JavaScript functions</a> <ul> <li><a href="#napi_call_function"><code>napi_call_function</code></a></li> <li><a href="#napi_create_function"><code>napi_create_function</code></a></li> <li><a href="#napi_get_cb_info"><code>napi_get_cb_info</code></a></li> <li><a href="#napi_get_new_target"><code>napi_get_new_target</code></a></li> <li><a href="#napi_new_instance"><code>napi_new_instance</code></a></li> </ul> </li> <li><a href="#object-wrap">Object wrap</a> <ul> <li><a href="#napi_define_class"><code>napi_define_class</code></a></li> <li><a href="#napi_wrap"><code>napi_wrap</code></a></li> <li><a href="#napi_unwrap"><code>napi_unwrap</code></a></li> <li><a href="#napi_remove_wrap"><code>napi_remove_wrap</code></a></li> <li><a href="#napi_type_tag_object"><code>napi_type_tag_object</code></a></li> <li><a href="#napi_check_object_type_tag"><code>napi_check_object_type_tag</code></a></li> <li><a href="#napi_add_finalizer"><code>napi_add_finalizer</code></a> <ul> <li><span class="stability_1"><a href="#node_api_post_finalizer"><code>node_api_post_finalizer</code></a></span></li> </ul> </li> </ul> </li> <li><a href="#simple-asynchronous-operations">Simple asynchronous operations</a> <ul> <li><a href="#napi_create_async_work"><code>napi_create_async_work</code></a></li> <li><a href="#napi_delete_async_work"><code>napi_delete_async_work</code></a></li> <li><a href="#napi_queue_async_work"><code>napi_queue_async_work</code></a></li> <li><a href="#napi_cancel_async_work"><code>napi_cancel_async_work</code></a></li> </ul> </li> <li><a href="#custom-asynchronous-operations">Custom asynchronous operations</a> <ul> <li><a href="#napi_async_init"><code>napi_async_init</code></a></li> <li><a href="#napi_async_destroy"><code>napi_async_destroy</code></a></li> <li><a href="#napi_make_callback"><code>napi_make_callback</code></a></li> <li><a href="#napi_open_callback_scope"><code>napi_open_callback_scope</code></a></li> <li><a href="#napi_close_callback_scope"><code>napi_close_callback_scope</code></a></li> </ul> </li> <li><a href="#version-management">Version management</a> <ul> <li><a href="#napi_get_node_version"><code>napi_get_node_version</code></a></li> <li><a href="#napi_get_version"><code>napi_get_version</code></a></li> </ul> </li> <li><a href="#memory-management">Memory management</a> <ul> <li><a href="#napi_adjust_external_memory"><code>napi_adjust_external_memory</code></a></li> </ul> </li> <li><a href="#promises">Promises</a> <ul> <li><a href="#napi_create_promise"><code>napi_create_promise</code></a></li> <li><a href="#napi_resolve_deferred"><code>napi_resolve_deferred</code></a></li> <li><a href="#napi_reject_deferred"><code>napi_reject_deferred</code></a></li> <li><a href="#napi_is_promise"><code>napi_is_promise</code></a></li> </ul> </li> <li><a href="#script-execution">Script execution</a> <ul> <li><a href="#napi_run_script"><code>napi_run_script</code></a></li> </ul> </li> <li><a href="#libuv-event-loop">libuv event loop</a> <ul> <li><a href="#napi_get_uv_event_loop"><code>napi_get_uv_event_loop</code></a></li> </ul> </li> <li><a href="#asynchronous-thread-safe-function-calls">Asynchronous thread-safe function calls</a> <ul> <li><a href="#calling-a-thread-safe-function">Calling a thread-safe function</a></li> <li><a href="#reference-counting-of-thread-safe-functions">Reference counting of thread-safe functions</a></li> <li><a href="#deciding-whether-to-keep-the-process-running">Deciding whether to keep the process running</a></li> <li><a href="#napi_create_threadsafe_function"><code>napi_create_threadsafe_function</code></a></li> <li><a href="#napi_get_threadsafe_function_context"><code>napi_get_threadsafe_function_context</code></a></li> <li><a href="#napi_call_threadsafe_function"><code>napi_call_threadsafe_function</code></a></li> <li><a href="#napi_acquire_threadsafe_function"><code>napi_acquire_threadsafe_function</code></a></li> <li><a href="#napi_release_threadsafe_function"><code>napi_release_threadsafe_function</code></a></li> <li><a href="#napi_ref_threadsafe_function"><code>napi_ref_threadsafe_function</code></a></li> <li><a href="#napi_unref_threadsafe_function"><code>napi_unref_threadsafe_function</code></a></li> </ul> </li> <li><a href="#miscellaneous-utilities">Miscellaneous utilities</a> <ul> <li><a href="#node_api_get_module_file_name"><code>node_api_get_module_file_name</code></a></li> </ul> </li> </ul> </li> </ul></div></div> </li> <li class="picker-header"> <a href="#"> <span class="picker-arrow"></span> Index </a> <div class="picker"><ul> <li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> <li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> <li> <a href="index.html">Index</a> </li> </ul> <hr class="line"> <ul> <li><a href="assert.html" class="nav-assert">Assertion testing</a></li> <li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> <li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> <li><a href="buffer.html" class="nav-buffer">Buffer</a></li> <li><a href="addons.html" class="nav-addons">C++ addons</a></li> <li><a href="n-api.html" class="nav-n-api active">C/C++ addons with Node-API</a></li> <li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> <li><a href="child_process.html" class="nav-child_process">Child processes</a></li> <li><a href="cluster.html" class="nav-cluster">Cluster</a></li> <li><a href="cli.html" class="nav-cli">Command-line options</a></li> <li><a href="console.html" class="nav-console">Console</a></li> <li><a href="corepack.html" class="nav-corepack">Corepack</a></li> <li><a href="crypto.html" class="nav-crypto">Crypto</a></li> <li><a href="debugger.html" class="nav-debugger">Debugger</a></li> <li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> <li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> <li><a href="dns.html" class="nav-dns">DNS</a></li> <li><a href="domain.html" class="nav-domain">Domain</a></li> <li><a href="errors.html" class="nav-errors">Errors</a></li> <li><a href="events.html" class="nav-events">Events</a></li> <li><a href="fs.html" class="nav-fs">File system</a></li> <li><a href="globals.html" class="nav-globals">Globals</a></li> <li><a href="http.html" class="nav-http">HTTP</a></li> <li><a href="http2.html" class="nav-http2">HTTP/2</a></li> <li><a href="https.html" class="nav-https">HTTPS</a></li> <li><a href="inspector.html" class="nav-inspector">Inspector</a></li> <li><a href="intl.html" class="nav-intl">Internationalization</a></li> <li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> <li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> <li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> <li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> <li><a href="net.html" class="nav-net">Net</a></li> <li><a href="os.html" class="nav-os">OS</a></li> <li><a href="path.html" class="nav-path">Path</a></li> <li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> <li><a href="permissions.html" class="nav-permissions">Permissions</a></li> <li><a href="process.html" class="nav-process">Process</a></li> <li><a href="punycode.html" class="nav-punycode">Punycode</a></li> <li><a href="querystring.html" class="nav-querystring">Query strings</a></li> <li><a href="readline.html" class="nav-readline">Readline</a></li> <li><a href="repl.html" class="nav-repl">REPL</a></li> <li><a href="report.html" class="nav-report">Report</a></li> <li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> <li><a href="stream.html" class="nav-stream">Stream</a></li> <li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> <li><a href="test.html" class="nav-test">Test runner</a></li> <li><a href="timers.html" class="nav-timers">Timers</a></li> <li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> <li><a href="tracing.html" class="nav-tracing">Trace events</a></li> <li><a href="tty.html" class="nav-tty">TTY</a></li> <li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> <li><a href="url.html" class="nav-url">URL</a></li> <li><a href="util.html" class="nav-util">Utilities</a></li> <li><a href="v8.html" class="nav-v8">V8</a></li> <li><a href="vm.html" class="nav-vm">VM</a></li> <li><a href="wasi.html" class="nav-wasi">WASI</a></li> <li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> <li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> <li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> <li><a href="zlib.html" class="nav-zlib">Zlib</a></li> </ul> <hr class="line"> <ul> <li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> </ul></div> </li> <li class="picker-header"> <a href="#"> <span class="picker-arrow"></span> Other versions </a> <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v22.x/api/n-api.html">22.x</a></li> <li><a href="https://nodejs.org/docs/latest-v21.x/api/n-api.html">21.x</a></li> <li><a href="https://nodejs.org/docs/latest-v20.x/api/n-api.html">20.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v19.x/api/n-api.html">19.x</a></li> <li><a href="https://nodejs.org/docs/latest-v18.x/api/n-api.html">18.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v17.x/api/n-api.html">17.x</a></li> <li><a href="https://nodejs.org/docs/latest-v16.x/api/n-api.html">16.x</a></li> <li><a href="https://nodejs.org/docs/latest-v15.x/api/n-api.html">15.x</a></li> <li><a href="https://nodejs.org/docs/latest-v14.x/api/n-api.html">14.x</a></li> <li><a href="https://nodejs.org/docs/latest-v13.x/api/n-api.html">13.x</a></li> <li><a href="https://nodejs.org/docs/latest-v12.x/api/n-api.html">12.x</a></li> <li><a href="https://nodejs.org/docs/latest-v11.x/api/n-api.html">11.x</a></li> <li><a href="https://nodejs.org/docs/latest-v10.x/api/n-api.html">10.x</a></li> <li><a href="https://nodejs.org/docs/latest-v9.x/api/n-api.html">9.x</a></li> <li><a href="https://nodejs.org/docs/latest-v8.x/api/n-api.html">8.x</a></li></ol></div> </li> <li class="picker-header"> <a href="#"> <span class="picker-arrow"></span> Options </a> <div class="picker"> <ul> <li> <a href="all.html">View on single page</a> </li> <li> <a href="n-api.json">View as JSON</a> </li> <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/n-api.md">Edit on GitHub</a></li> </ul> </div> </li> </ul> </div> <hr> </header> <details role="navigation" id="toc" open><summary>Table of contents</summary><ul> <li><span class="stability_2"><a href="#node-api">Node-API</a></span> <ul> <li><a href="#implications-of-abi-stability">Implications of ABI stability</a></li> <li><a href="#building">Building</a> <ul> <li><a href="#build-tools">Build tools</a> <ul> <li><a href="#node-gyp">node-gyp</a></li> <li><a href="#cmakejs">CMake.js</a></li> </ul> </li> <li><a href="#uploading-precompiled-binaries">Uploading precompiled binaries</a> <ul> <li><a href="#node-pre-gyp">node-pre-gyp</a></li> <li><a href="#prebuild">prebuild</a></li> <li><a href="#prebuildify">prebuildify</a></li> </ul> </li> </ul> </li> <li><a href="#usage">Usage</a></li> <li><a href="#node-api-version-matrix">Node-API version matrix</a></li> <li><a href="#environment-life-cycle-apis">Environment life cycle APIs</a> <ul> <li><a href="#napi_set_instance_data"><code>napi_set_instance_data</code></a></li> <li><a href="#napi_get_instance_data"><code>napi_get_instance_data</code></a></li> </ul> </li> <li><a href="#basic-node-api-data-types">Basic Node-API data types</a> <ul> <li><a href="#napi_status"><code>napi_status</code></a></li> <li><a href="#napi_extended_error_info"><code>napi_extended_error_info</code></a></li> <li><a href="#napi_env"><code>napi_env</code></a></li> <li><span class="stability_1"><a href="#node_api_basic_env"><code>node_api_basic_env</code></a></span></li> <li><a href="#napi_value"><code>napi_value</code></a></li> <li><a href="#napi_threadsafe_function"><code>napi_threadsafe_function</code></a></li> <li><a href="#napi_threadsafe_function_release_mode"><code>napi_threadsafe_function_release_mode</code></a></li> <li><a href="#napi_threadsafe_function_call_mode"><code>napi_threadsafe_function_call_mode</code></a></li> <li><a href="#node-api-memory-management-types">Node-API memory management types</a> <ul> <li><a href="#napi_handle_scope"><code>napi_handle_scope</code></a></li> <li><a href="#napi_escapable_handle_scope"><code>napi_escapable_handle_scope</code></a></li> <li><a href="#napi_ref"><code>napi_ref</code></a></li> <li><a href="#napi_type_tag"><code>napi_type_tag</code></a></li> <li><a href="#napi_async_cleanup_hook_handle"><code>napi_async_cleanup_hook_handle</code></a></li> </ul> </li> <li><a href="#node-api-callback-types">Node-API callback types</a> <ul> <li><a href="#napi_callback_info"><code>napi_callback_info</code></a></li> <li><a href="#napi_callback"><code>napi_callback</code></a></li> <li><span class="stability_1"><a href="#node_api_basic_finalize"><code>node_api_basic_finalize</code></a></span></li> <li><a href="#napi_finalize"><code>napi_finalize</code></a></li> <li><a href="#napi_async_execute_callback"><code>napi_async_execute_callback</code></a></li> <li><a href="#napi_async_complete_callback"><code>napi_async_complete_callback</code></a></li> <li><a href="#napi_threadsafe_function_call_js"><code>napi_threadsafe_function_call_js</code></a></li> <li><a href="#napi_cleanup_hook"><code>napi_cleanup_hook</code></a></li> <li><a href="#napi_async_cleanup_hook"><code>napi_async_cleanup_hook</code></a></li> </ul> </li> </ul> </li> <li><a href="#error-handling">Error handling</a> <ul> <li><a href="#return-values">Return values</a> <ul> <li><a href="#napi_get_last_error_info"><code>napi_get_last_error_info</code></a></li> </ul> </li> <li><a href="#exceptions">Exceptions</a> <ul> <li><a href="#napi_throw"><code>napi_throw</code></a></li> <li><a href="#napi_throw_error"><code>napi_throw_error</code></a></li> <li><a href="#napi_throw_type_error"><code>napi_throw_type_error</code></a></li> <li><a href="#napi_throw_range_error"><code>napi_throw_range_error</code></a></li> <li><a href="#node_api_throw_syntax_error"><code>node_api_throw_syntax_error</code></a></li> <li><a href="#napi_is_error"><code>napi_is_error</code></a></li> <li><a href="#napi_create_error"><code>napi_create_error</code></a></li> <li><a href="#napi_create_type_error"><code>napi_create_type_error</code></a></li> <li><a href="#napi_create_range_error"><code>napi_create_range_error</code></a></li> <li><a href="#node_api_create_syntax_error"><code>node_api_create_syntax_error</code></a></li> <li><a href="#napi_get_and_clear_last_exception"><code>napi_get_and_clear_last_exception</code></a></li> <li><a href="#napi_is_exception_pending"><code>napi_is_exception_pending</code></a></li> <li><a href="#napi_fatal_exception"><code>napi_fatal_exception</code></a></li> </ul> </li> <li><a href="#fatal-errors">Fatal errors</a> <ul> <li><a href="#napi_fatal_error"><code>napi_fatal_error</code></a></li> </ul> </li> </ul> </li> <li><a href="#object-lifetime-management">Object lifetime management</a> <ul> <li><a href="#making-handle-lifespan-shorter-than-that-of-the-native-method">Making handle lifespan shorter than that of the native method</a> <ul> <li><a href="#napi_open_handle_scope"><code>napi_open_handle_scope</code></a></li> <li><a href="#napi_close_handle_scope"><code>napi_close_handle_scope</code></a></li> <li><a href="#napi_open_escapable_handle_scope"><code>napi_open_escapable_handle_scope</code></a></li> <li><a href="#napi_close_escapable_handle_scope"><code>napi_close_escapable_handle_scope</code></a></li> <li><a href="#napi_escape_handle"><code>napi_escape_handle</code></a></li> </ul> </li> <li><a href="#references-to-values-with-a-lifespan-longer-than-that-of-the-native-method">References to values with a lifespan longer than that of the native method</a> <ul> <li><a href="#napi_create_reference"><code>napi_create_reference</code></a></li> <li><a href="#napi_delete_reference"><code>napi_delete_reference</code></a></li> <li><a href="#napi_reference_ref"><code>napi_reference_ref</code></a></li> <li><a href="#napi_reference_unref"><code>napi_reference_unref</code></a></li> <li><a href="#napi_get_reference_value"><code>napi_get_reference_value</code></a></li> </ul> </li> <li><a href="#cleanup-on-exit-of-the-current-nodejs-environment">Cleanup on exit of the current Node.js environment</a> <ul> <li><a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a></li> <li><a href="#napi_remove_env_cleanup_hook"><code>napi_remove_env_cleanup_hook</code></a></li> <li><a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a></li> <li><a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a></li> </ul> </li> <li><a href="#finalization-on-the-exit-of-the-nodejs-environment">Finalization on the exit of the Node.js environment</a></li> </ul> </li> <li><a href="#module-registration">Module registration</a></li> <li><a href="#working-with-javascript-values">Working with JavaScript values</a> <ul> <li><a href="#enum-types">Enum types</a> <ul> <li><a href="#napi_key_collection_mode"><code>napi_key_collection_mode</code></a></li> <li><a href="#napi_key_filter"><code>napi_key_filter</code></a></li> <li><a href="#napi_key_conversion"><code>napi_key_conversion</code></a></li> <li><a href="#napi_valuetype"><code>napi_valuetype</code></a></li> <li><a href="#napi_typedarray_type"><code>napi_typedarray_type</code></a></li> </ul> </li> <li><a href="#object-creation-functions">Object creation functions</a> <ul> <li><a href="#napi_create_array"><code>napi_create_array</code></a></li> <li><a href="#napi_create_array_with_length"><code>napi_create_array_with_length</code></a></li> <li><a href="#napi_create_arraybuffer"><code>napi_create_arraybuffer</code></a></li> <li><a href="#napi_create_buffer"><code>napi_create_buffer</code></a></li> <li><a href="#napi_create_buffer_copy"><code>napi_create_buffer_copy</code></a></li> <li><a href="#napi_create_date"><code>napi_create_date</code></a></li> <li><a href="#napi_create_external"><code>napi_create_external</code></a></li> <li><a href="#napi_create_external_arraybuffer"><code>napi_create_external_arraybuffer</code></a></li> <li><a href="#napi_create_external_buffer"><code>napi_create_external_buffer</code></a></li> <li><a href="#napi_create_object"><code>napi_create_object</code></a></li> <li><a href="#napi_create_symbol"><code>napi_create_symbol</code></a></li> <li><a href="#node_api_symbol_for"><code>node_api_symbol_for</code></a></li> <li><a href="#napi_create_typedarray"><code>napi_create_typedarray</code></a></li> <li><a href="#napi_create_dataview"><code>napi_create_dataview</code></a></li> </ul> </li> <li><a href="#functions-to-convert-from-c-types-to-node-api">Functions to convert from C types to Node-API</a> <ul> <li><a href="#napi_create_int32"><code>napi_create_int32</code></a></li> <li><a href="#napi_create_uint32"><code>napi_create_uint32</code></a></li> <li><a href="#napi_create_int64"><code>napi_create_int64</code></a></li> <li><a href="#napi_create_double"><code>napi_create_double</code></a></li> <li><a href="#napi_create_bigint_int64"><code>napi_create_bigint_int64</code></a></li> <li><a href="#napi_create_bigint_uint64"><code>napi_create_bigint_uint64</code></a></li> <li><a href="#napi_create_bigint_words"><code>napi_create_bigint_words</code></a></li> <li><a href="#napi_create_string_latin1"><code>napi_create_string_latin1</code></a></li> <li><span class="stability_1"><a href="#node_api_create_external_string_latin1"><code>node_api_create_external_string_latin1</code></a></span></li> <li><a href="#napi_create_string_utf16"><code>napi_create_string_utf16</code></a></li> <li><span class="stability_1"><a href="#node_api_create_external_string_utf16"><code>node_api_create_external_string_utf16</code></a></span></li> <li><a href="#napi_create_string_utf8"><code>napi_create_string_utf8</code></a></li> </ul> </li> <li><a href="#functions-to-create-optimized-property-keys">Functions to create optimized property keys</a> <ul> <li><span class="stability_1"><a href="#node_api_create_property_key_latin1"><code>node_api_create_property_key_latin1</code></a></span></li> <li><span class="stability_1"><a href="#node_api_create_property_key_utf16"><code>node_api_create_property_key_utf16</code></a></span></li> <li><span class="stability_1"><a href="#node_api_create_property_key_utf8"><code>node_api_create_property_key_utf8</code></a></span></li> </ul> </li> <li><a href="#functions-to-convert-from-node-api-to-c-types">Functions to convert from Node-API to C types</a> <ul> <li><a href="#napi_get_array_length"><code>napi_get_array_length</code></a></li> <li><a href="#napi_get_arraybuffer_info"><code>napi_get_arraybuffer_info</code></a></li> <li><a href="#napi_get_buffer_info"><code>napi_get_buffer_info</code></a></li> <li><a href="#napi_get_prototype"><code>napi_get_prototype</code></a></li> <li><a href="#napi_get_typedarray_info"><code>napi_get_typedarray_info</code></a></li> <li><a href="#napi_get_dataview_info"><code>napi_get_dataview_info</code></a></li> <li><a href="#napi_get_date_value"><code>napi_get_date_value</code></a></li> <li><a href="#napi_get_value_bool"><code>napi_get_value_bool</code></a></li> <li><a href="#napi_get_value_double"><code>napi_get_value_double</code></a></li> <li><a href="#napi_get_value_bigint_int64"><code>napi_get_value_bigint_int64</code></a></li> <li><a href="#napi_get_value_bigint_uint64"><code>napi_get_value_bigint_uint64</code></a></li> <li><a href="#napi_get_value_bigint_words"><code>napi_get_value_bigint_words</code></a></li> <li><a href="#napi_get_value_external"><code>napi_get_value_external</code></a></li> <li><a href="#napi_get_value_int32"><code>napi_get_value_int32</code></a></li> <li><a href="#napi_get_value_int64"><code>napi_get_value_int64</code></a></li> <li><a href="#napi_get_value_string_latin1"><code>napi_get_value_string_latin1</code></a></li> <li><a href="#napi_get_value_string_utf8"><code>napi_get_value_string_utf8</code></a></li> <li><a href="#napi_get_value_string_utf16"><code>napi_get_value_string_utf16</code></a></li> <li><a href="#napi_get_value_uint32"><code>napi_get_value_uint32</code></a></li> </ul> </li> <li><a href="#functions-to-get-global-instances">Functions to get global instances</a> <ul> <li><a href="#napi_get_boolean"><code>napi_get_boolean</code></a></li> <li><a href="#napi_get_global"><code>napi_get_global</code></a></li> <li><a href="#napi_get_null"><code>napi_get_null</code></a></li> <li><a href="#napi_get_undefined"><code>napi_get_undefined</code></a></li> </ul> </li> </ul> </li> <li><a href="#working-with-javascript-values-and-abstract-operations">Working with JavaScript values and abstract operations</a> <ul> <li><a href="#napi_coerce_to_bool"><code>napi_coerce_to_bool</code></a></li> <li><a href="#napi_coerce_to_number"><code>napi_coerce_to_number</code></a></li> <li><a href="#napi_coerce_to_object"><code>napi_coerce_to_object</code></a></li> <li><a href="#napi_coerce_to_string"><code>napi_coerce_to_string</code></a></li> <li><a href="#napi_typeof"><code>napi_typeof</code></a></li> <li><a href="#napi_instanceof"><code>napi_instanceof</code></a></li> <li><a href="#napi_is_array"><code>napi_is_array</code></a></li> <li><a href="#napi_is_arraybuffer"><code>napi_is_arraybuffer</code></a></li> <li><a href="#napi_is_buffer"><code>napi_is_buffer</code></a></li> <li><a href="#napi_is_date"><code>napi_is_date</code></a></li> <li><a href="#napi_is_error_1"><code>napi_is_error</code></a></li> <li><a href="#napi_is_typedarray"><code>napi_is_typedarray</code></a></li> <li><a href="#napi_is_dataview"><code>napi_is_dataview</code></a></li> <li><a href="#napi_strict_equals"><code>napi_strict_equals</code></a></li> <li><a href="#napi_detach_arraybuffer"><code>napi_detach_arraybuffer</code></a></li> <li><a href="#napi_is_detached_arraybuffer"><code>napi_is_detached_arraybuffer</code></a></li> </ul> </li> <li><a href="#working-with-javascript-properties">Working with JavaScript properties</a> <ul> <li><a href="#structures">Structures</a> <ul> <li><a href="#napi_property_attributes"><code>napi_property_attributes</code></a></li> <li><a href="#napi_property_descriptor"><code>napi_property_descriptor</code></a></li> </ul> </li> <li><a href="#functions">Functions</a> <ul> <li><a href="#napi_get_property_names"><code>napi_get_property_names</code></a></li> <li><a href="#napi_get_all_property_names"><code>napi_get_all_property_names</code></a></li> <li><a href="#napi_set_property"><code>napi_set_property</code></a></li> <li><a href="#napi_get_property"><code>napi_get_property</code></a></li> <li><a href="#napi_has_property"><code>napi_has_property</code></a></li> <li><a href="#napi_delete_property"><code>napi_delete_property</code></a></li> <li><a href="#napi_has_own_property"><code>napi_has_own_property</code></a></li> <li><a href="#napi_set_named_property"><code>napi_set_named_property</code></a></li> <li><a href="#napi_get_named_property"><code>napi_get_named_property</code></a></li> <li><a href="#napi_has_named_property"><code>napi_has_named_property</code></a></li> <li><a href="#napi_set_element"><code>napi_set_element</code></a></li> <li><a href="#napi_get_element"><code>napi_get_element</code></a></li> <li><a href="#napi_has_element"><code>napi_has_element</code></a></li> <li><a href="#napi_delete_element"><code>napi_delete_element</code></a></li> <li><a href="#napi_define_properties"><code>napi_define_properties</code></a></li> <li><a href="#napi_object_freeze"><code>napi_object_freeze</code></a></li> <li><a href="#napi_object_seal"><code>napi_object_seal</code></a></li> </ul> </li> </ul> </li> <li><a href="#working-with-javascript-functions">Working with JavaScript functions</a> <ul> <li><a href="#napi_call_function"><code>napi_call_function</code></a></li> <li><a href="#napi_create_function"><code>napi_create_function</code></a></li> <li><a href="#napi_get_cb_info"><code>napi_get_cb_info</code></a></li> <li><a href="#napi_get_new_target"><code>napi_get_new_target</code></a></li> <li><a href="#napi_new_instance"><code>napi_new_instance</code></a></li> </ul> </li> <li><a href="#object-wrap">Object wrap</a> <ul> <li><a href="#napi_define_class"><code>napi_define_class</code></a></li> <li><a href="#napi_wrap"><code>napi_wrap</code></a></li> <li><a href="#napi_unwrap"><code>napi_unwrap</code></a></li> <li><a href="#napi_remove_wrap"><code>napi_remove_wrap</code></a></li> <li><a href="#napi_type_tag_object"><code>napi_type_tag_object</code></a></li> <li><a href="#napi_check_object_type_tag"><code>napi_check_object_type_tag</code></a></li> <li><a href="#napi_add_finalizer"><code>napi_add_finalizer</code></a> <ul> <li><span class="stability_1"><a href="#node_api_post_finalizer"><code>node_api_post_finalizer</code></a></span></li> </ul> </li> </ul> </li> <li><a href="#simple-asynchronous-operations">Simple asynchronous operations</a> <ul> <li><a href="#napi_create_async_work"><code>napi_create_async_work</code></a></li> <li><a href="#napi_delete_async_work"><code>napi_delete_async_work</code></a></li> <li><a href="#napi_queue_async_work"><code>napi_queue_async_work</code></a></li> <li><a href="#napi_cancel_async_work"><code>napi_cancel_async_work</code></a></li> </ul> </li> <li><a href="#custom-asynchronous-operations">Custom asynchronous operations</a> <ul> <li><a href="#napi_async_init"><code>napi_async_init</code></a></li> <li><a href="#napi_async_destroy"><code>napi_async_destroy</code></a></li> <li><a href="#napi_make_callback"><code>napi_make_callback</code></a></li> <li><a href="#napi_open_callback_scope"><code>napi_open_callback_scope</code></a></li> <li><a href="#napi_close_callback_scope"><code>napi_close_callback_scope</code></a></li> </ul> </li> <li><a href="#version-management">Version management</a> <ul> <li><a href="#napi_get_node_version"><code>napi_get_node_version</code></a></li> <li><a href="#napi_get_version"><code>napi_get_version</code></a></li> </ul> </li> <li><a href="#memory-management">Memory management</a> <ul> <li><a href="#napi_adjust_external_memory"><code>napi_adjust_external_memory</code></a></li> </ul> </li> <li><a href="#promises">Promises</a> <ul> <li><a href="#napi_create_promise"><code>napi_create_promise</code></a></li> <li><a href="#napi_resolve_deferred"><code>napi_resolve_deferred</code></a></li> <li><a href="#napi_reject_deferred"><code>napi_reject_deferred</code></a></li> <li><a href="#napi_is_promise"><code>napi_is_promise</code></a></li> </ul> </li> <li><a href="#script-execution">Script execution</a> <ul> <li><a href="#napi_run_script"><code>napi_run_script</code></a></li> </ul> </li> <li><a href="#libuv-event-loop">libuv event loop</a> <ul> <li><a href="#napi_get_uv_event_loop"><code>napi_get_uv_event_loop</code></a></li> </ul> </li> <li><a href="#asynchronous-thread-safe-function-calls">Asynchronous thread-safe function calls</a> <ul> <li><a href="#calling-a-thread-safe-function">Calling a thread-safe function</a></li> <li><a href="#reference-counting-of-thread-safe-functions">Reference counting of thread-safe functions</a></li> <li><a href="#deciding-whether-to-keep-the-process-running">Deciding whether to keep the process running</a></li> <li><a href="#napi_create_threadsafe_function"><code>napi_create_threadsafe_function</code></a></li> <li><a href="#napi_get_threadsafe_function_context"><code>napi_get_threadsafe_function_context</code></a></li> <li><a href="#napi_call_threadsafe_function"><code>napi_call_threadsafe_function</code></a></li> <li><a href="#napi_acquire_threadsafe_function"><code>napi_acquire_threadsafe_function</code></a></li> <li><a href="#napi_release_threadsafe_function"><code>napi_release_threadsafe_function</code></a></li> <li><a href="#napi_ref_threadsafe_function"><code>napi_ref_threadsafe_function</code></a></li> <li><a href="#napi_unref_threadsafe_function"><code>napi_unref_threadsafe_function</code></a></li> </ul> </li> <li><a href="#miscellaneous-utilities">Miscellaneous utilities</a> <ul> <li><a href="#node_api_get_module_file_name"><code>node_api_get_module_file_name</code></a></li> </ul> </li> </ul> </li> </ul></details> <div role="main" id="apicontent"> <h2>Node-API<span><a class="mark" href="#node-api" id="node-api">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api"></a></h2> <p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> <p>Node-API (formerly N-API) is an API for building native Addons. It is independent from the underlying JavaScript runtime (for example, V8) and is maintained as part of Node.js itself. This API will be Application Binary Interface (ABI) stable across versions of Node.js. It is intended to insulate addons from changes in the underlying JavaScript engine and allow modules compiled for one major version to run on later major versions of Node.js without recompilation. The <a href="https://nodejs.org/en/docs/guides/abi-stability/">ABI Stability</a> guide provides a more in-depth explanation.</p> <p>Addons are built/packaged with the same approach/tools outlined in the section titled <a href="addons.html">C++ Addons</a>. The only difference is the set of APIs that are used by the native code. Instead of using the V8 or <a href="https://github.com/nodejs/nan">Native Abstractions for Node.js</a> APIs, the functions available in Node-API are used.</p> <p>APIs exposed by Node-API are generally used to create and manipulate JavaScript values. Concepts and operations generally map to ideas specified in the ECMA-262 Language Specification. The APIs have the following properties:</p> <ul> <li>All Node-API calls return a status code of type <code>napi_status</code>. This status indicates whether the API call succeeded or failed.</li> <li>The API's return value is passed via an out parameter.</li> <li>All JavaScript values are abstracted behind an opaque type named <code>napi_value</code>.</li> <li>In case of an error status code, additional information can be obtained using <code>napi_get_last_error_info</code>. More information can be found in the error handling section <a href="#error-handling">Error handling</a>.</li> </ul> <p>Node-API is a C API that ensures ABI stability across Node.js versions and different compiler levels. A C++ API can be easier to use. To support using C++, the project maintains a C++ wrapper module called <a href="https://github.com/nodejs/node-addon-api"><code>node-addon-api</code></a>. This wrapper provides an inlinable C++ API. Binaries built with <code>node-addon-api</code> will depend on the symbols for the Node-API C-based functions exported by Node.js. <code>node-addon-api</code> is a more efficient way to write code that calls Node-API. Take, for example, the following <code>node-addon-api</code> code. The first section shows the <code>node-addon-api</code> code and the second section shows what actually gets used in the addon.</p> <pre><code class="language-cpp">Object obj = Object::<span class="hljs-built_in">New</span>(env); obj[<span class="hljs-string">"foo"</span>] = String::<span class="hljs-built_in">New</span>(env, <span class="hljs-string">"bar"</span>);</code> <button class="copy-button">copy</button></pre> <pre><code class="language-cpp">napi_status status; napi_value object, string; status = <span class="hljs-built_in">napi_create_object</span>(env, &object); <span class="hljs-keyword">if</span> (status != napi_ok) { <span class="hljs-built_in">napi_throw_error</span>(env, ...); <span class="hljs-keyword">return</span>; } status = <span class="hljs-built_in">napi_create_string_utf8</span>(env, <span class="hljs-string">"bar"</span>, NAPI_AUTO_LENGTH, &string); <span class="hljs-keyword">if</span> (status != napi_ok) { <span class="hljs-built_in">napi_throw_error</span>(env, ...); <span class="hljs-keyword">return</span>; } status = <span class="hljs-built_in">napi_set_named_property</span>(env, object, <span class="hljs-string">"foo"</span>, string); <span class="hljs-keyword">if</span> (status != napi_ok) { <span class="hljs-built_in">napi_throw_error</span>(env, ...); <span class="hljs-keyword">return</span>; }</code> <button class="copy-button">copy</button></pre> <p>The end result is that the addon only uses the exported C APIs. As a result, it still gets the benefits of the ABI stability provided by the C API.</p> <p>When using <code>node-addon-api</code> instead of the C APIs, start with the API <a href="https://github.com/nodejs/node-addon-api#api-documentation">docs</a> for <code>node-addon-api</code>.</p> <p>The <a href="https://nodejs.github.io/node-addon-examples/">Node-API Resource</a> offers an excellent orientation and tips for developers just getting started with Node-API and <code>node-addon-api</code>. Additional media resources can be found on the <a href="https://github.com/nodejs/abi-stable-node/blob/HEAD/node-api-media.md">Node-API Media</a> page.</p> <section><h3>Implications of ABI stability<span><a class="mark" href="#implications-of-abi-stability" id="implications-of-abi-stability">#</a></span><a aria-hidden="true" class="legacy" id="n_api_implications_of_abi_stability"></a></h3> <p>Although Node-API provides an ABI stability guarantee, other parts of Node.js do not, and any external libraries used from the addon may not. In particular, none of the following APIs provide an ABI stability guarantee across major versions:</p> <ul> <li> <p>the Node.js C++ APIs available via any of</p> <pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node.h></span></span> <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_buffer.h></span></span> <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_version.h></span></span> <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_object_wrap.h></span></span></code> <button class="copy-button">copy</button></pre> </li> <li> <p>the libuv APIs which are also included with Node.js and available via</p> <pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><uv.h></span></span></code> <button class="copy-button">copy</button></pre> </li> <li> <p>the V8 API available via</p> <pre><code class="language-cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><v8.h></span></span></code> <button class="copy-button">copy</button></pre> </li> </ul> <p>Thus, for an addon to remain ABI-compatible across Node.js major versions, it must use Node-API exclusively by restricting itself to using</p> <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_api.h></span></span></code> <button class="copy-button">copy</button></pre> <p>and by checking, for all external libraries that it uses, that the external library makes ABI stability guarantees similar to Node-API.</p> </section><section><h3>Building<span><a class="mark" href="#building" id="building">#</a></span><a aria-hidden="true" class="legacy" id="n_api_building"></a></h3> <p>Unlike modules written in JavaScript, developing and deploying Node.js native addons using Node-API requires an additional set of tools. Besides the basic tools required to develop for Node.js, the native addon developer requires a toolchain that can compile C and C++ code into a binary. In addition, depending upon how the native addon is deployed, the <em>user</em> of the native addon will also need to have a C/C++ toolchain installed.</p> <p>For Linux developers, the necessary C/C++ toolchain packages are readily available. <a href="https://gcc.gnu.org">GCC</a> is widely used in the Node.js community to build and test across a variety of platforms. For many developers, the <a href="https://llvm.org">LLVM</a> compiler infrastructure is also a good choice.</p> <p>For Mac developers, <a href="https://developer.apple.com/xcode/">Xcode</a> offers all the required compiler tools. However, it is not necessary to install the entire Xcode IDE. The following command installs the necessary toolchain:</p> <pre><code class="language-bash">xcode-select --install</code> <button class="copy-button">copy</button></pre> <p>For Windows developers, <a href="https://visualstudio.microsoft.com">Visual Studio</a> offers all the required compiler tools. However, it is not necessary to install the entire Visual Studio IDE. The following command installs the necessary toolchain:</p> <pre><code class="language-bash">npm install --global windows-build-tools</code> <button class="copy-button">copy</button></pre> <p>The sections below describe the additional tools available for developing and deploying Node.js native addons.</p> <h4>Build tools<span><a class="mark" href="#build-tools" id="build-tools">#</a></span><a aria-hidden="true" class="legacy" id="n_api_build_tools"></a></h4> <p>Both the tools listed here require that <em>users</em> of the native addon have a C/C++ toolchain installed in order to successfully install the native addon.</p> <h5>node-gyp<span><a class="mark" href="#node-gyp" id="node-gyp">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_gyp"></a></h5> <p><a href="https://github.com/nodejs/node-gyp">node-gyp</a> is a build system based on the <a href="https://github.com/nodejs/gyp-next">gyp-next</a> fork of Google's <a href="https://gyp.gsrc.io">GYP</a> tool and comes bundled with npm. GYP, and therefore node-gyp, requires that Python be installed.</p> <p>Historically, node-gyp has been the tool of choice for building native addons. It has widespread adoption and documentation. However, some developers have run into limitations in node-gyp.</p> <h5>CMake.js<span><a class="mark" href="#cmakejs" id="cmakejs">#</a></span><a aria-hidden="true" class="legacy" id="n_api_cmake_js"></a></h5> <p><a href="https://github.com/cmake-js/cmake-js">CMake.js</a> is an alternative build system based on <a href="https://cmake.org">CMake</a>.</p> <p>CMake.js is a good choice for projects that already use CMake or for developers affected by limitations in node-gyp. <a href="https://github.com/nodejs/node-addon-examples/tree/main/src/8-tooling/build_with_cmake"><code>build_with_cmake</code></a> is an example of a CMake-based native addon project.</p> <h4>Uploading precompiled binaries<span><a class="mark" href="#uploading-precompiled-binaries" id="uploading-precompiled-binaries">#</a></span><a aria-hidden="true" class="legacy" id="n_api_uploading_precompiled_binaries"></a></h4> <p>The three tools listed here permit native addon developers and maintainers to create and upload binaries to public or private servers. These tools are typically integrated with CI/CD build systems like <a href="https://travis-ci.org">Travis CI</a> and <a href="https://www.appveyor.com">AppVeyor</a> to build and upload binaries for a variety of platforms and architectures. These binaries are then available for download by users who do not need to have a C/C++ toolchain installed.</p> <h5>node-pre-gyp<span><a class="mark" href="#node-pre-gyp" id="node-pre-gyp">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_pre_gyp"></a></h5> <p><a href="https://github.com/mapbox/node-pre-gyp">node-pre-gyp</a> is a tool based on node-gyp that adds the ability to upload binaries to a server of the developer's choice. node-pre-gyp has particularly good support for uploading binaries to Amazon S3.</p> <h5>prebuild<span><a class="mark" href="#prebuild" id="prebuild">#</a></span><a aria-hidden="true" class="legacy" id="n_api_prebuild"></a></h5> <p><a href="https://github.com/prebuild/prebuild">prebuild</a> is a tool that supports builds using either node-gyp or CMake.js. Unlike node-pre-gyp which supports a variety of servers, prebuild uploads binaries only to <a href="https://help.github.com/en/github/administering-a-repository/about-releases">GitHub releases</a>. prebuild is a good choice for GitHub projects using CMake.js.</p> <h5>prebuildify<span><a class="mark" href="#prebuildify" id="prebuildify">#</a></span><a aria-hidden="true" class="legacy" id="n_api_prebuildify"></a></h5> <p><a href="https://github.com/prebuild/prebuildify">prebuildify</a> is a tool based on node-gyp. The advantage of prebuildify is that the built binaries are bundled with the native addon when it's uploaded to npm. The binaries are downloaded from npm and are immediately available to the module user when the native addon is installed.</p> </section><section><h3>Usage<span><a class="mark" href="#usage" id="usage">#</a></span><a aria-hidden="true" class="legacy" id="n_api_usage"></a></h3> <p>In order to use the Node-API functions, include the file <a href="https://github.com/nodejs/node/blob/HEAD/src/node_api.h"><code>node_api.h</code></a> which is located in the src directory in the node development tree:</p> <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_api.h></span></span></code> <button class="copy-button">copy</button></pre> <p>This will opt into the default <code>NAPI_VERSION</code> for the given release of Node.js. In order to ensure compatibility with specific versions of Node-API, the version can be specified explicitly when including the header:</p> <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAPI_VERSION 3</span> <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_api.h></span></span></code> <button class="copy-button">copy</button></pre> <p>This restricts the Node-API surface to just the functionality that was available in the specified (and earlier) versions.</p> <p>Some of the Node-API surface is experimental and requires explicit opt-in:</p> <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAPI_EXPERIMENTAL</span> <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_api.h></span></span></code> <button class="copy-button">copy</button></pre> <p>In this case the entire API surface, including any experimental APIs, will be available to the module code.</p> <p>Occasionally, experimental features are introduced that affect already-released and stable APIs. These features can be disabled by an opt-out:</p> <pre><code class="language-c"><span class="hljs-meta">#<span class="hljs-keyword">define</span> NAPI_EXPERIMENTAL</span> <span class="hljs-meta">#<span class="hljs-keyword">define</span> NODE_API_EXPERIMENTAL_<span class="hljs-string"><FEATURE_NAME></span>_OPT_OUT</span> <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_api.h></span></span></code> <button class="copy-button">copy</button></pre> <p>where <code><FEATURE_NAME></code> is the name of an experimental feature that affects both experimental and stable APIs.</p> </section><section><h3>Node-API version matrix<span><a class="mark" href="#node-api-version-matrix" id="node-api-version-matrix">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_version_matrix"></a></h3> <p>Up until version 9, Node-API versions were additive and versioned independently from Node.js. This meant that any version was an extension to the previous version in that it had all of the APIs from the previous version with some additions. Each Node.js version only supported a single Node-API version. For example v18.15.0 supports only Node-API version 8. ABI stability was achieved because 8 was a strict superset of all previous versions.</p> <p>As of version 9, while Node-API versions continue to be versioned independently an add-on that ran with Node-API version 9 may need code updates to run with Node-API version 10. ABI stability is maintained, however, because Node.js versions that support Node-API versions higher than 8 will support all versions between 8 and the highest version they support and will default to providing the version 8 APIs unless an add-on opts into a higher Node-API version. This approach provides the flexibility of better optimizing existing Node-API functions while maintaining ABI stability. Existing add-ons can continue to run without recompilation using an earlier version of Node-API. If an add-on needs functionality from a newer Node-API version, changes to existing code and recompilation will be needed to use those new functions anyway.</p> <p>In versions of Node.js that support Node-API version 9 and later, defining <code>NAPI_VERSION=X</code> and using the existing add-on initialization macros will bake in the requested Node-API version that will be used at runtime into the add-on. If <code>NAPI_VERSION</code> is not set it will default to 8.</p> <p>This table may not be up to date in older streams, the most up to date information is in the latest API documentation in: <a href="https://nodejs.org/docs/latest/api/n-api.html#node-api-version-matrix">Node-API version matrix</a></p> <!-- For accessibility purposes, this table needs row headers. That means we can't do it in markdown. Hence, the raw HTML. --> <table> <tbody><tr> <th>Node-API version</th> <th scope="col">Supported In</th> </tr> <tr> <th scope="row">9</th> <td>v18.17.0+, 20.3.0+, 21.0.0 and all later versions</td> </tr> <tr> <th scope="row">8</th> <td>v12.22.0+, v14.17.0+, v15.12.0+, 16.0.0 and all later versions</td> </tr> <tr> <th scope="row">7</th> <td>v10.23.0+, v12.19.0+, v14.12.0+, 15.0.0 and all later versions</td> </tr> <tr> <th scope="row">6</th> <td>v10.20.0+, v12.17.0+, 14.0.0 and all later versions</td> </tr> <tr> <th scope="row">5</th> <td>v10.17.0+, v12.11.0+, 13.0.0 and all later versions</td> </tr> <tr> <th scope="row">4</th> <td>v10.16.0+, v11.8.0+, 12.0.0 and all later versions</td> </tr> <tr> <th scope="row">3</th> <td>v6.14.2*, 8.11.2+, v9.11.0+*, 10.0.0 and all later versions</td> </tr> <tr> <th scope="row">2</th> <td>v8.10.0+*, v9.3.0+*, 10.0.0 and all later versions</td> </tr> <tr> <th scope="row">1</th> <td>v8.6.0+**, v9.0.0+*, 10.0.0 and all later versions</td> </tr> </tbody></table> <p>* Node-API was experimental.</p> <p>** Node.js 8.0.0 included Node-API as experimental. It was released as Node-API version 1 but continued to evolve until Node.js 8.6.0. The API is different in versions prior to Node.js 8.6.0. We recommend Node-API version 3 or later.</p> <p>Each API documented for Node-API will have a header named <code>added in:</code>, and APIs which are stable will have the additional header <code>Node-API version:</code>. APIs are directly usable when using a Node.js version which supports the Node-API version shown in <code>Node-API version:</code> or higher. When using a Node.js version that does not support the <code>Node-API version:</code> listed or if there is no <code>Node-API version:</code> listed, then the API will only be available if <code>#define NAPI_EXPERIMENTAL</code> precedes the inclusion of <code>node_api.h</code> or <code>js_native_api.h</code>. If an API appears not to be available on a version of Node.js which is later than the one shown in <code>added in:</code> then this is most likely the reason for the apparent absence.</p> <p>The Node-APIs associated strictly with accessing ECMAScript features from native code can be found separately in <code>js_native_api.h</code> and <code>js_native_api_types.h</code>. The APIs defined in these headers are included in <code>node_api.h</code> and <code>node_api_types.h</code>. The headers are structured in this way in order to allow implementations of Node-API outside of Node.js. For those implementations the Node.js specific APIs may not be applicable.</p> <p>The Node.js-specific parts of an addon can be separated from the code that exposes the actual functionality to the JavaScript environment so that the latter may be used with multiple implementations of Node-API. In the example below, <code>addon.c</code> and <code>addon.h</code> refer only to <code>js_native_api.h</code>. This ensures that <code>addon.c</code> can be reused to compile against either the Node.js implementation of Node-API or any implementation of Node-API outside of Node.js.</p> <p><code>addon_node.c</code> is a separate file that contains the Node.js specific entry point to the addon and which instantiates the addon by calling into <code>addon.c</code> when the addon is loaded into a Node.js environment.</p> <pre><code class="language-c"><span class="hljs-comment">// addon.h</span> <span class="hljs-meta">#<span class="hljs-keyword">ifndef</span> _ADDON_H_</span> <span class="hljs-meta">#<span class="hljs-keyword">define</span> _ADDON_H_</span> <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><js_native_api.h></span></span> napi_value <span class="hljs-title function_">create_addon</span><span class="hljs-params">(napi_env env)</span>; <span class="hljs-meta">#<span class="hljs-keyword">endif</span> <span class="hljs-comment">// _ADDON_H_</span></span></code> <button class="copy-button">copy</button></pre> <pre><code class="language-c"><span class="hljs-comment">// addon.c</span> <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"addon.h"</span></span> <span class="hljs-meta">#<span class="hljs-keyword">define</span> NODE_API_CALL(env, call) \ do { \ napi_status status = (call); \ <span class="hljs-keyword">if</span> (status != napi_ok) { \ const napi_extended_error_info* error_info = NULL; \ napi_get_last_error_info((env), &error_info); \ const char* err_message = error_info->error_message; \ bool is_pending; \ napi_is_exception_pending((env), &is_pending); \ <span class="hljs-comment">/* If an exception is already pending, don't rethrow it */</span> \ <span class="hljs-keyword">if</span> (!is_pending) { \ const char* message = (err_message == NULL) \ ? <span class="hljs-string">"empty error message"</span> \ : err_message; \ napi_throw_error((env), NULL, message); \ } \ return NULL; \ } \ } while(0)</span> <span class="hljs-type">static</span> napi_value <span class="hljs-title function_">DoSomethingUseful</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> { <span class="hljs-comment">// Do something useful.</span> <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; } napi_value <span class="hljs-title function_">create_addon</span><span class="hljs-params">(napi_env env)</span> { napi_value result; NODE_API_CALL(env, napi_create_object(env, &result)); napi_value exported_function; NODE_API_CALL(env, napi_create_function(env, <span class="hljs-string">"doSomethingUseful"</span>, NAPI_AUTO_LENGTH, DoSomethingUseful, <span class="hljs-literal">NULL</span>, &exported_function)); NODE_API_CALL(env, napi_set_named_property(env, result, <span class="hljs-string">"doSomethingUseful"</span>, exported_function)); <span class="hljs-keyword">return</span> result; }</code> <button class="copy-button">copy</button></pre> <pre><code class="language-c"><span class="hljs-comment">// addon_node.c</span> <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><node_api.h></span></span> <span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">"addon.h"</span></span> NAPI_MODULE_INIT(<span class="hljs-comment">/* napi_env env, napi_value exports */</span>) { <span class="hljs-comment">// This function body is expected to return a `napi_value`.</span> <span class="hljs-comment">// The variables `napi_env env` and `napi_value exports` may be used within</span> <span class="hljs-comment">// the body, as they are provided by the definition of `NAPI_MODULE_INIT()`.</span> <span class="hljs-keyword">return</span> create_addon(env); }</code> <button class="copy-button">copy</button></pre> </section><section><h3>Environment life cycle APIs<span><a class="mark" href="#environment-life-cycle-apis" id="environment-life-cycle-apis">#</a></span><a aria-hidden="true" class="legacy" id="n_api_environment_life_cycle_apis"></a></h3> <p><a href="https://tc39.es/ecma262/#sec-agents">Section 8.7</a> of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a> defines the concept of an "Agent" as a self-contained environment in which JavaScript code runs. Multiple such Agents may be started and terminated either concurrently or in sequence by the process.</p> <p>A Node.js environment corresponds to an ECMAScript Agent. In the main process, an environment is created at startup, and additional environments can be created on separate threads to serve as <a href="https://nodejs.org/api/worker_threads.html">worker threads</a>. When Node.js is embedded in another application, the main thread of the application may also construct and destroy a Node.js environment multiple times during the life cycle of the application process such that each Node.js environment created by the application may, in turn, during its life cycle create and destroy additional environments as worker threads.</p> <p>From the perspective of a native addon this means that the bindings it provides may be called multiple times, from multiple contexts, and even concurrently from multiple threads.</p> <p>Native addons may need to allocate global state which they use during their life cycle of an Node.js environment such that the state can be unique to each instance of the addon.</p> <p>To this end, Node-API provides a way to associate data such that its life cycle is tied to the life cycle of a Node.js environment.</p> <h4><code>napi_set_instance_data</code><span><a class="mark" href="#napi_set_instance_data" id="napi_set_instance_data">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_set_instance_data"></a></h4> <div class="api_metadata"> <span>Added in: v12.8.0, v10.20.0</span> <span>N-API version: 6</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_instance_data</span><span class="hljs-params">(node_api_basic_env env, <span class="hljs-type">void</span>* data, napi_finalize finalize_cb, <span class="hljs-type">void</span>* finalize_hint)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[in] data</code>: The data item to make available to bindings of this instance.</li> <li><code>[in] finalize_cb</code>: The function to call when the environment is being torn down. The function receives <code>data</code> so that it might free it. <a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li> <li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during collection.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API associates <code>data</code> with the currently running Node.js environment. <code>data</code> can later be retrieved using <code>napi_get_instance_data()</code>. Any existing data associated with the currently running Node.js environment which was set by means of a previous call to <code>napi_set_instance_data()</code> will be overwritten. If a <code>finalize_cb</code> was provided by the previous call, it will not be called.</p> <h4><code>napi_get_instance_data</code><span><a class="mark" href="#napi_get_instance_data" id="napi_get_instance_data">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_instance_data"></a></h4> <div class="api_metadata"> <span>Added in: v12.8.0, v10.20.0</span> <span>N-API version: 6</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_instance_data</span><span class="hljs-params">(node_api_basic_env env, <span class="hljs-type">void</span>** data)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[out] data</code>: The data item that was previously associated with the currently running Node.js environment by a call to <code>napi_set_instance_data()</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API retrieves data that was previously associated with the currently running Node.js environment via <code>napi_set_instance_data()</code>. If no data is set, the call will succeed and <code>data</code> will be set to <code>NULL</code>.</p> </section><section><h3>Basic Node-API data types<span><a class="mark" href="#basic-node-api-data-types" id="basic-node-api-data-types">#</a></span><a aria-hidden="true" class="legacy" id="n_api_basic_node_api_data_types"></a></h3> <p>Node-API exposes the following fundamental data types as abstractions that are consumed by the various APIs. These APIs should be treated as opaque, introspectable only with other Node-API calls.</p> <h4><code>napi_status</code><span><a class="mark" href="#napi_status" id="napi_status">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_status"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <p>Integral status code indicating the success or failure of a Node-API call. Currently, the following status codes are supported.</p> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span> napi_ok, napi_invalid_arg, napi_object_expected, napi_string_expected, napi_name_expected, napi_function_expected, napi_number_expected, napi_boolean_expected, napi_array_expected, napi_generic_failure, napi_pending_exception, napi_cancelled, napi_escape_called_twice, napi_handle_scope_mismatch, napi_callback_scope_mismatch, napi_queue_full, napi_closing, napi_bigint_expected, napi_date_expected, napi_arraybuffer_expected, napi_detachable_arraybuffer_expected, napi_would_deadlock, <span class="hljs-comment">/* unused */</span> napi_no_external_buffers_allowed, napi_cannot_run_js } napi_status;</code> <button class="copy-button">copy</button></pre> <p>If additional information is required upon an API returning a failed status, it can be obtained by calling <code>napi_get_last_error_info</code>.</p> <h4><code>napi_extended_error_info</code><span><a class="mark" href="#napi_extended_error_info" id="napi_extended_error_info">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_extended_error_info"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span> <span class="hljs-type">const</span> <span class="hljs-type">char</span>* error_message; <span class="hljs-type">void</span>* engine_reserved; <span class="hljs-type">uint32_t</span> engine_error_code; napi_status error_code; } napi_extended_error_info;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>error_message</code>: UTF8-encoded string containing a VM-neutral description of the error.</li> <li><code>engine_reserved</code>: Reserved for VM-specific error details. This is currently not implemented for any VM.</li> <li><code>engine_error_code</code>: VM-specific error code. This is currently not implemented for any VM.</li> <li><code>error_code</code>: The Node-API status code that originated with the last error.</li> </ul> <p>See the <a href="#error-handling">Error handling</a> section for additional information.</p> <h4><code>napi_env</code><span><a class="mark" href="#napi_env" id="napi_env">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_env"></a></h4> <p><code>napi_env</code> is used to represent a context that the underlying Node-API implementation can use to persist VM-specific state. This structure is passed to native functions when they're invoked, and it must be passed back when making Node-API calls. Specifically, the same <code>napi_env</code> that was passed in when the initial native function was called must be passed to any subsequent nested Node-API calls. Caching the <code>napi_env</code> for the purpose of general reuse, and passing the <code>napi_env</code> between instances of the same addon running on different <a href="worker_threads.html#class-worker"><code>Worker</code></a> threads is not allowed. The <code>napi_env</code> becomes invalid when an instance of a native addon is unloaded. Notification of this event is delivered through the callbacks given to <a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a> and <a href="#napi_set_instance_data"><code>napi_set_instance_data</code></a>.</p> <h4><code>node_api_basic_env</code><span><a class="mark" href="#node_api_basic_env" id="node_api_basic_env">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_basic_env"></a></h4> <p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> <p>This variant of <code>napi_env</code> is passed to synchronous finalizers (<a href="#node_api_basic_finalize"><code>node_api_basic_finalize</code></a>). There is a subset of Node-APIs which accept a parameter of type <code>node_api_basic_env</code> as their first argument. These APIs do not access the state of the JavaScript engine and are thus safe to call from synchronous finalizers. Passing a parameter of type <code>napi_env</code> to these APIs is allowed, however, passing a parameter of type <code>node_api_basic_env</code> to APIs that access the JavaScript engine state is not allowed. Attempting to do so without a cast will produce a compiler warning or an error when add-ons are compiled with flags which cause them to emit warnings and/or errors when incorrect pointer types are passed into a function. Calling such APIs from a synchronous finalizer will ultimately result in the termination of the application.</p> <h4><code>napi_value</code><span><a class="mark" href="#napi_value" id="napi_value">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_value"></a></h4> <p>This is an opaque pointer that is used to represent a JavaScript value.</p> <h4><code>napi_threadsafe_function</code><span><a class="mark" href="#napi_threadsafe_function" id="napi_threadsafe_function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_threadsafe_function"></a></h4> <div class="api_metadata"> <span>Added in: v10.6.0</span> <span>N-API version: 4</span> </div> <p>This is an opaque pointer that represents a JavaScript function which can be called asynchronously from multiple threads via <code>napi_call_threadsafe_function()</code>.</p> <h4><code>napi_threadsafe_function_release_mode</code><span><a class="mark" href="#napi_threadsafe_function_release_mode" id="napi_threadsafe_function_release_mode">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_threadsafe_function_release_mode"></a></h4> <div class="api_metadata"> <span>Added in: v10.6.0</span> <span>N-API version: 4</span> </div> <p>A value to be given to <code>napi_release_threadsafe_function()</code> to indicate whether the thread-safe function is to be closed immediately (<code>napi_tsfn_abort</code>) or merely released (<code>napi_tsfn_release</code>) and thus available for subsequent use via <code>napi_acquire_threadsafe_function()</code> and <code>napi_call_threadsafe_function()</code>.</p> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span> napi_tsfn_release, napi_tsfn_abort } napi_threadsafe_function_release_mode;</code> <button class="copy-button">copy</button></pre> <h4><code>napi_threadsafe_function_call_mode</code><span><a class="mark" href="#napi_threadsafe_function_call_mode" id="napi_threadsafe_function_call_mode">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_threadsafe_function_call_mode"></a></h4> <div class="api_metadata"> <span>Added in: v10.6.0</span> <span>N-API version: 4</span> </div> <p>A value to be given to <code>napi_call_threadsafe_function()</code> to indicate whether the call should block whenever the queue associated with the thread-safe function is full.</p> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span> napi_tsfn_nonblocking, napi_tsfn_blocking } napi_threadsafe_function_call_mode;</code> <button class="copy-button">copy</button></pre> <h4>Node-API memory management types<span><a class="mark" href="#node-api-memory-management-types" id="node-api-memory-management-types">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_memory_management_types"></a></h4> <h5><code>napi_handle_scope</code><span><a class="mark" href="#napi_handle_scope" id="napi_handle_scope">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_handle_scope"></a></h5> <p>This is an abstraction used to control and modify the lifetime of objects created within a particular scope. In general, Node-API values are created within the context of a handle scope. When a native method is called from JavaScript, a default handle scope will exist. If the user does not explicitly create a new handle scope, Node-API values will be created in the default handle scope. For any invocations of code outside the execution of a native method (for instance, during a libuv callback invocation), the module is required to create a scope before invoking any functions that can result in the creation of JavaScript values.</p> <p>Handle scopes are created using <a href="#napi_open_handle_scope"><code>napi_open_handle_scope</code></a> and are destroyed using <a href="#napi_close_handle_scope"><code>napi_close_handle_scope</code></a>. Closing the scope can indicate to the GC that all <code>napi_value</code>s created during the lifetime of the handle scope are no longer referenced from the current stack frame.</p> <p>For more details, review the <a href="#object-lifetime-management">Object lifetime management</a>.</p> <h5><code>napi_escapable_handle_scope</code><span><a class="mark" href="#napi_escapable_handle_scope" id="napi_escapable_handle_scope">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_escapable_handle_scope"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <p>Escapable handle scopes are a special type of handle scope to return values created within a particular handle scope to a parent scope.</p> <h5><code>napi_ref</code><span><a class="mark" href="#napi_ref" id="napi_ref">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_ref"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <p>This is the abstraction to use to reference a <code>napi_value</code>. This allows for users to manage the lifetimes of JavaScript values, including defining their minimum lifetimes explicitly.</p> <p>For more details, review the <a href="#object-lifetime-management">Object lifetime management</a>.</p> <h5><code>napi_type_tag</code><span><a class="mark" href="#napi_type_tag" id="napi_type_tag">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_type_tag"></a></h5> <div class="api_metadata"> <span>Added in: v14.8.0, v12.19.0</span> <span>N-API version: 8</span> </div> <p>A 128-bit value stored as two unsigned 64-bit integers. It serves as a UUID with which JavaScript objects or <a href="#napi_create_external">externals</a> can be "tagged" in order to ensure that they are of a certain type. This is a stronger check than <a href="#napi_instanceof"><code>napi_instanceof</code></a>, because the latter can report a false positive if the object's prototype has been manipulated. Type-tagging is most useful in conjunction with <a href="#napi_wrap"><code>napi_wrap</code></a> because it ensures that the pointer retrieved from a wrapped object can be safely cast to the native type corresponding to the type tag that had been previously applied to the JavaScript object.</p> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span> <span class="hljs-type">uint64_t</span> lower; <span class="hljs-type">uint64_t</span> upper; } napi_type_tag;</code> <button class="copy-button">copy</button></pre> <h5><code>napi_async_cleanup_hook_handle</code><span><a class="mark" href="#napi_async_cleanup_hook_handle" id="napi_async_cleanup_hook_handle">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_async_cleanup_hook_handle"></a></h5> <div class="api_metadata"> <span>Added in: v14.10.0, v12.19.0</span> </div> <p>An opaque value returned by <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>. It must be passed to <a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a> when the chain of asynchronous cleanup events completes.</p> <h4>Node-API callback types<span><a class="mark" href="#node-api-callback-types" id="node-api-callback-types">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_callback_types"></a></h4> <h5><code>napi_callback_info</code><span><a class="mark" href="#napi_callback_info" id="napi_callback_info">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_callback_info"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <p>Opaque datatype that is passed to a callback function. It can be used for getting additional information about the context in which the callback was invoked.</p> <h5><code>napi_callback</code><span><a class="mark" href="#napi_callback" id="napi_callback">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_callback"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <p>Function pointer type for user-provided native functions which are to be exposed to JavaScript via Node-API. Callback functions should satisfy the following signature:</p> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">napi_value</span> <span class="hljs-params">(*napi_callback)</span><span class="hljs-params">(napi_env, napi_callback_info)</span>;</code> <button class="copy-button">copy</button></pre> <p>Unless for reasons discussed in <a href="#object-lifetime-management">Object Lifetime Management</a>, creating a handle and/or callback scope inside a <code>napi_callback</code> is not necessary.</p> <h5><code>node_api_basic_finalize</code><span><a class="mark" href="#node_api_basic_finalize" id="node_api_basic_finalize">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_basic_finalize"></a></h5> <div class="api_metadata"> <span>Added in: v20.12.0</span> </div> <p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> <p>Function pointer type for add-on provided functions that allow the user to be notified when externally-owned data is ready to be cleaned up because the object it was associated with has been garbage-collected. The user must provide a function satisfying the following signature which would get called upon the object's collection. Currently, <code>node_api_basic_finalize</code> can be used for finding out when objects that have external data are collected.</p> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*node_api_basic_finalize)</span><span class="hljs-params">(node_api_basic_env env, <span class="hljs-type">void</span>* finalize_data, <span class="hljs-type">void</span>* finalize_hint)</span>;</code> <button class="copy-button">copy</button></pre> <p>Unless for reasons discussed in <a href="#object-lifetime-management">Object Lifetime Management</a>, creating a handle and/or callback scope inside the function body is not necessary.</p> <p>Since these functions may be called while the JavaScript engine is in a state where it cannot execute JavaScript code, only Node-APIs which accept a <code>node_api_basic_env</code> as their first parameter may be called. <a href="#node_api_post_finalizer"><code>node_api_post_finalizer</code></a> can be used to schedule Node-API calls that require access to the JavaScript engine's state to run after the current garbage collection cycle has completed.</p> <p>In the case of <a href="#node_api_create_external_string_latin1"><code>node_api_create_external_string_latin1</code></a> and <a href="#node_api_create_external_string_utf16"><code>node_api_create_external_string_utf16</code></a> the <code>env</code> parameter may be null, because external strings can be collected during the latter part of environment shutdown.</p> <p>Change History:</p> <ul> <li> <p>experimental (<code>NAPI_EXPERIMENTAL</code>):</p> <p>Only Node-API calls that accept a <code>node_api_basic_env</code> as their first parameter may be called, otherwise the application will be terminated with an appropriate error message. This feature can be turned off by defining <code>NODE_API_EXPERIMENTAL_BASIC_ENV_OPT_OUT</code>.</p> </li> </ul> <h5><code>napi_finalize</code><span><a class="mark" href="#napi_finalize" id="napi_finalize">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_finalize"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <p>Function pointer type for add-on provided function that allow the user to schedule a group of calls to Node-APIs in response to a garbage collection event, after the garbage collection cycle has completed. These function pointers can be used with <a href="#node_api_post_finalizer"><code>node_api_post_finalizer</code></a>.</p> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_finalize)</span><span class="hljs-params">(napi_env env, <span class="hljs-type">void</span>* finalize_data, <span class="hljs-type">void</span>* finalize_hint)</span>;</code> <button class="copy-button">copy</button></pre> <p>Change History:</p> <ul> <li> <p>experimental (<code>NAPI_EXPERIMENTAL</code> is defined):</p> <p>A function of this type may no longer be used as a finalizer, except with <a href="#node_api_post_finalizer"><code>node_api_post_finalizer</code></a>. <a href="#node_api_basic_finalize"><code>node_api_basic_finalize</code></a> must be used instead. This feature can be turned off by defining <code>NODE_API_EXPERIMENTAL_BASIC_ENV_OPT_OUT</code>.</p> </li> </ul> <h5><code>napi_async_execute_callback</code><span><a class="mark" href="#napi_async_execute_callback" id="napi_async_execute_callback">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_async_execute_callback"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <p>Function pointer used with functions that support asynchronous operations. Callback functions must satisfy the following signature:</p> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_execute_callback)</span><span class="hljs-params">(napi_env env, <span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button></pre> <p>Implementations of this function must avoid making Node-API calls that execute JavaScript or interact with JavaScript objects. Node-API calls should be in the <code>napi_async_complete_callback</code> instead. Do not use the <code>napi_env</code> parameter as it will likely result in execution of JavaScript.</p> <h5><code>napi_async_complete_callback</code><span><a class="mark" href="#napi_async_complete_callback" id="napi_async_complete_callback">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_async_complete_callback"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <p>Function pointer used with functions that support asynchronous operations. Callback functions must satisfy the following signature:</p> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_complete_callback)</span><span class="hljs-params">(napi_env env, napi_status status, <span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button></pre> <p>Unless for reasons discussed in <a href="#object-lifetime-management">Object Lifetime Management</a>, creating a handle and/or callback scope inside the function body is not necessary.</p> <h5><code>napi_threadsafe_function_call_js</code><span><a class="mark" href="#napi_threadsafe_function_call_js" id="napi_threadsafe_function_call_js">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_threadsafe_function_call_js"></a></h5> <div class="api_metadata"> <span>Added in: v10.6.0</span> <span>N-API version: 4</span> </div> <p>Function pointer used with asynchronous thread-safe function calls. The callback will be called on the main thread. Its purpose is to use a data item arriving via the queue from one of the secondary threads to construct the parameters necessary for a call into JavaScript, usually via <code>napi_call_function</code>, and then make the call into JavaScript.</p> <p>The data arriving from the secondary thread via the queue is given in the <code>data</code> parameter and the JavaScript function to call is given in the <code>js_callback</code> parameter.</p> <p>Node-API sets up the environment prior to calling this callback, so it is sufficient to call the JavaScript function via <code>napi_call_function</code> rather than via <code>napi_make_callback</code>.</p> <p>Callback functions must satisfy the following signature:</p> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_threadsafe_function_call_js)</span><span class="hljs-params">(napi_env env, napi_value js_callback, <span class="hljs-type">void</span>* context, <span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment to use for API calls, or <code>NULL</code> if the thread-safe function is being torn down and <code>data</code> may need to be freed.</li> <li><code>[in] js_callback</code>: The JavaScript function to call, or <code>NULL</code> if the thread-safe function is being torn down and <code>data</code> may need to be freed. It may also be <code>NULL</code> if the thread-safe function was created without <code>js_callback</code>.</li> <li><code>[in] context</code>: The optional data with which the thread-safe function was created.</li> <li><code>[in] data</code>: Data created by the secondary thread. It is the responsibility of the callback to convert this native data to JavaScript values (with Node-API functions) that can be passed as parameters when <code>js_callback</code> is invoked. This pointer is managed entirely by the threads and this callback. Thus this callback should free the data.</li> </ul> <p>Unless for reasons discussed in <a href="#object-lifetime-management">Object Lifetime Management</a>, creating a handle and/or callback scope inside the function body is not necessary.</p> <h5><code>napi_cleanup_hook</code><span><a class="mark" href="#napi_cleanup_hook" id="napi_cleanup_hook">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_cleanup_hook"></a></h5> <div class="api_metadata"> <span>Added in: v19.2.0, v18.13.0</span> <span>N-API version: 3</span> </div> <p>Function pointer used with <a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a>. It will be called when the environment is being torn down.</p> <p>Callback functions must satisfy the following signature:</p> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_cleanup_hook)</span><span class="hljs-params">(<span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] data</code>: The data that was passed to <a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a>.</li> </ul> <h5><code>napi_async_cleanup_hook</code><span><a class="mark" href="#napi_async_cleanup_hook" id="napi_async_cleanup_hook">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_async_cleanup_hook"></a></h5> <div class="api_metadata"> <span>Added in: v14.10.0, v12.19.0</span> </div> <p>Function pointer used with <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>. It will be called when the environment is being torn down.</p> <p>Callback functions must satisfy the following signature:</p> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_cleanup_hook)</span><span class="hljs-params">(napi_async_cleanup_hook_handle handle, <span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] handle</code>: The handle that must be passed to <a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a> after completion of the asynchronous cleanup.</li> <li><code>[in] data</code>: The data that was passed to <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>.</li> </ul> <p>The body of the function should initiate the asynchronous cleanup actions at the end of which <code>handle</code> must be passed in a call to <a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a>.</p> </section><section><h3>Error handling<span><a class="mark" href="#error-handling" id="error-handling">#</a></span><a aria-hidden="true" class="legacy" id="n_api_error_handling"></a></h3> <p>Node-API uses both return values and JavaScript exceptions for error handling. The following sections explain the approach for each case.</p> <h4>Return values<span><a class="mark" href="#return-values" id="return-values">#</a></span><a aria-hidden="true" class="legacy" id="n_api_return_values"></a></h4> <p>All of the Node-API functions share the same error handling pattern. The return type of all API functions is <code>napi_status</code>.</p> <p>The return value will be <code>napi_ok</code> if the request was successful and no uncaught JavaScript exception was thrown. If an error occurred AND an exception was thrown, the <code>napi_status</code> value for the error will be returned. If an exception was thrown, and no error occurred, <code>napi_pending_exception</code> will be returned.</p> <p>In cases where a return value other than <code>napi_ok</code> or <code>napi_pending_exception</code> is returned, <a href="#napi_is_exception_pending"><code>napi_is_exception_pending</code></a> must be called to check if an exception is pending. See the section on exceptions for more details.</p> <p>The full set of possible <code>napi_status</code> values is defined in <code>napi_api_types.h</code>.</p> <p>The <code>napi_status</code> return value provides a VM-independent representation of the error which occurred. In some cases it is useful to be able to get more detailed information, including a string representing the error as well as VM (engine)-specific information.</p> <p>In order to retrieve this information <a href="#napi_get_last_error_info"><code>napi_get_last_error_info</code></a> is provided which returns a <code>napi_extended_error_info</code> structure. The format of the <code>napi_extended_error_info</code> structure is as follows:</p> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">napi_extended_error_info</span> {</span> <span class="hljs-type">const</span> <span class="hljs-type">char</span>* error_message; <span class="hljs-type">void</span>* engine_reserved; <span class="hljs-type">uint32_t</span> engine_error_code; napi_status error_code; };</code> <button class="copy-button">copy</button></pre> <ul> <li><code>error_message</code>: Textual representation of the error that occurred.</li> <li><code>engine_reserved</code>: Opaque handle reserved for engine use only.</li> <li><code>engine_error_code</code>: VM specific error code.</li> <li><code>error_code</code>: Node-API status code for the last error.</li> </ul> <p><a href="#napi_get_last_error_info"><code>napi_get_last_error_info</code></a> returns the information for the last Node-API call that was made.</p> <p>Do not rely on the content or format of any of the extended information as it is not subject to SemVer and may change at any time. It is intended only for logging purposes.</p> <h5><code>napi_get_last_error_info</code><span><a class="mark" href="#napi_get_last_error_info" id="napi_get_last_error_info">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_last_error_info"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_last_error_info</span><span class="hljs-params">(node_api_basic_env env, <span class="hljs-type">const</span> napi_extended_error_info** result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[out] result</code>: The <code>napi_extended_error_info</code> structure with more information about the error.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API retrieves a <code>napi_extended_error_info</code> structure with information about the last error that occurred.</p> <p>The content of the <code>napi_extended_error_info</code> returned is only valid up until a Node-API function is called on the same <code>env</code>. This includes a call to <code>napi_is_exception_pending</code> so it may often be necessary to make a copy of the information so that it can be used later. The pointer returned in <code>error_message</code> points to a statically-defined string so it is safe to use that pointer if you have copied it out of the <code>error_message</code> field (which will be overwritten) before another Node-API function was called.</p> <p>Do not rely on the content or format of any of the extended information as it is not subject to SemVer and may change at any time. It is intended only for logging purposes.</p> <p>This API can be called even if there is a pending JavaScript exception.</p> <h4>Exceptions<span><a class="mark" href="#exceptions" id="exceptions">#</a></span><a aria-hidden="true" class="legacy" id="n_api_exceptions"></a></h4> <p>Any Node-API function call may result in a pending JavaScript exception. This is the case for any of the API functions, even those that may not cause the execution of JavaScript.</p> <p>If the <code>napi_status</code> returned by a function is <code>napi_ok</code> then no exception is pending and no additional action is required. If the <code>napi_status</code> returned is anything other than <code>napi_ok</code> or <code>napi_pending_exception</code>, in order to try to recover and continue instead of simply returning immediately, <a href="#napi_is_exception_pending"><code>napi_is_exception_pending</code></a> must be called in order to determine if an exception is pending or not.</p> <p>In many cases when a Node-API function is called and an exception is already pending, the function will return immediately with a <code>napi_status</code> of <code>napi_pending_exception</code>. However, this is not the case for all functions. Node-API allows a subset of the functions to be called to allow for some minimal cleanup before returning to JavaScript. In that case, <code>napi_status</code> will reflect the status for the function. It will not reflect previous pending exceptions. To avoid confusion, check the error status after every function call.</p> <p>When an exception is pending one of two approaches can be employed.</p> <p>The first approach is to do any appropriate cleanup and then return so that execution will return to JavaScript. As part of the transition back to JavaScript, the exception will be thrown at the point in the JavaScript code where the native method was invoked. The behavior of most Node-API calls is unspecified while an exception is pending, and many will simply return <code>napi_pending_exception</code>, so do as little as possible and then return to JavaScript where the exception can be handled.</p> <p>The second approach is to try to handle the exception. There will be cases where the native code can catch the exception, take the appropriate action, and then continue. This is only recommended in specific cases where it is known that the exception can be safely handled. In these cases <a href="#napi_get_and_clear_last_exception"><code>napi_get_and_clear_last_exception</code></a> can be used to get and clear the exception. On success, result will contain the handle to the last JavaScript <code>Object</code> thrown. If it is determined, after retrieving the exception, the exception cannot be handled after all it can be re-thrown it with <a href="#napi_throw"><code>napi_throw</code></a> where error is the JavaScript value to be thrown.</p> <p>The following utility functions are also available in case native code needs to throw an exception or determine if a <code>napi_value</code> is an instance of a JavaScript <code>Error</code> object: <a href="#napi_throw_error"><code>napi_throw_error</code></a>, <a href="#napi_throw_type_error"><code>napi_throw_type_error</code></a>, <a href="#napi_throw_range_error"><code>napi_throw_range_error</code></a>, <a href="#node_api_throw_syntax_error"><code>node_api_throw_syntax_error</code></a> and <a href="#napi_is_error"><code>napi_is_error</code></a>.</p> <p>The following utility functions are also available in case native code needs to create an <code>Error</code> object: <a href="#napi_create_error"><code>napi_create_error</code></a>, <a href="#napi_create_type_error"><code>napi_create_type_error</code></a>, <a href="#napi_create_range_error"><code>napi_create_range_error</code></a> and <a href="#node_api_create_syntax_error"><code>node_api_create_syntax_error</code></a>, where result is the <code>napi_value</code> that refers to the newly created JavaScript <code>Error</code> object.</p> <p>The Node.js project is adding error codes to all of the errors generated internally. The goal is for applications to use these error codes for all error checking. The associated error messages will remain, but will only be meant to be used for logging and display with the expectation that the message can change without SemVer applying. In order to support this model with Node-API, both in internal functionality and for module specific functionality (as its good practice), the <code>throw_</code> and <code>create_</code> functions take an optional code parameter which is the string for the code to be added to the error object. If the optional parameter is <code>NULL</code> then no code will be associated with the error. If a code is provided, the name associated with the error is also updated to be:</p> <pre><code class="language-text">originalName [code]</code> <button class="copy-button">copy</button></pre> <p>where <code>originalName</code> is the original name associated with the error and <code>code</code> is the code that was provided. For example, if the code is <code>'ERR_ERROR_1'</code> and a <code>TypeError</code> is being created the name will be:</p> <pre><code class="language-text">TypeError [ERR_ERROR_1]</code> <button class="copy-button">copy</button></pre> <h5><code>napi_throw</code><span><a class="mark" href="#napi_throw" id="napi_throw">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_throw"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw</span><span class="hljs-params">(napi_env env, napi_value error)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] error</code>: The JavaScript value to be thrown.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API throws the JavaScript value provided.</p> <h5><code>napi_throw_error</code><span><a class="mark" href="#napi_throw_error" id="napi_throw_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_throw_error"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_error</span><span class="hljs-params">(napi_env env, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* code, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* msg)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] code</code>: Optional error code to be set on the error.</li> <li><code>[in] msg</code>: C string representing the text to be associated with the error.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API throws a JavaScript <code>Error</code> with the text provided.</p> <h5><code>napi_throw_type_error</code><span><a class="mark" href="#napi_throw_type_error" id="napi_throw_type_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_throw_type_error"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_type_error</span><span class="hljs-params">(napi_env env, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* code, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* msg)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] code</code>: Optional error code to be set on the error.</li> <li><code>[in] msg</code>: C string representing the text to be associated with the error.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API throws a JavaScript <code>TypeError</code> with the text provided.</p> <h5><code>napi_throw_range_error</code><span><a class="mark" href="#napi_throw_range_error" id="napi_throw_range_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_throw_range_error"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_throw_range_error</span><span class="hljs-params">(napi_env env, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* code, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* msg)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] code</code>: Optional error code to be set on the error.</li> <li><code>[in] msg</code>: C string representing the text to be associated with the error.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API throws a JavaScript <code>RangeError</code> with the text provided.</p> <h5><code>node_api_throw_syntax_error</code><span><a class="mark" href="#node_api_throw_syntax_error" id="node_api_throw_syntax_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_throw_syntax_error"></a></h5> <div class="api_metadata"> <span>Added in: v17.2.0, v16.14.0</span> <span>N-API version: 9</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">node_api_throw_syntax_error</span><span class="hljs-params">(napi_env env, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* code, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* msg)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] code</code>: Optional error code to be set on the error.</li> <li><code>[in] msg</code>: C string representing the text to be associated with the error.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API throws a JavaScript <code>SyntaxError</code> with the text provided.</p> <h5><code>napi_is_error</code><span><a class="mark" href="#napi_is_error" id="napi_is_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_error"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_is_error</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: The <code>napi_value</code> to be checked.</li> <li><code>[out] result</code>: Boolean value that is set to true if <code>napi_value</code> represents an error, false otherwise.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API queries a <code>napi_value</code> to check if it represents an error object.</p> <h5><code>napi_create_error</code><span><a class="mark" href="#napi_create_error" id="napi_create_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_error"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_error</span><span class="hljs-params">(napi_env env, napi_value code, napi_value msg, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] code</code>: Optional <code>napi_value</code> with the string for the error code to be associated with the error.</li> <li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code> to be used as the message for the <code>Error</code>.</li> <li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API returns a JavaScript <code>Error</code> with the text provided.</p> <h5><code>napi_create_type_error</code><span><a class="mark" href="#napi_create_type_error" id="napi_create_type_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_type_error"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_type_error</span><span class="hljs-params">(napi_env env, napi_value code, napi_value msg, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] code</code>: Optional <code>napi_value</code> with the string for the error code to be associated with the error.</li> <li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code> to be used as the message for the <code>Error</code>.</li> <li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API returns a JavaScript <code>TypeError</code> with the text provided.</p> <h5><code>napi_create_range_error</code><span><a class="mark" href="#napi_create_range_error" id="napi_create_range_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_range_error"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_range_error</span><span class="hljs-params">(napi_env env, napi_value code, napi_value msg, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] code</code>: Optional <code>napi_value</code> with the string for the error code to be associated with the error.</li> <li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code> to be used as the message for the <code>Error</code>.</li> <li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API returns a JavaScript <code>RangeError</code> with the text provided.</p> <h5><code>node_api_create_syntax_error</code><span><a class="mark" href="#node_api_create_syntax_error" id="node_api_create_syntax_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_create_syntax_error"></a></h5> <div class="api_metadata"> <span>Added in: v17.2.0, v16.14.0</span> <span>N-API version: 9</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">node_api_create_syntax_error</span><span class="hljs-params">(napi_env env, napi_value code, napi_value msg, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] code</code>: Optional <code>napi_value</code> with the string for the error code to be associated with the error.</li> <li><code>[in] msg</code>: <code>napi_value</code> that references a JavaScript <code>string</code> to be used as the message for the <code>Error</code>.</li> <li><code>[out] result</code>: <code>napi_value</code> representing the error created.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API returns a JavaScript <code>SyntaxError</code> with the text provided.</p> <h5><code>napi_get_and_clear_last_exception</code><span><a class="mark" href="#napi_get_and_clear_last_exception" id="napi_get_and_clear_last_exception">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_and_clear_last_exception"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_and_clear_last_exception</span><span class="hljs-params">(napi_env env, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[out] result</code>: The exception if one is pending, <code>NULL</code> otherwise.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API can be called even if there is a pending JavaScript exception.</p> <h5><code>napi_is_exception_pending</code><span><a class="mark" href="#napi_is_exception_pending" id="napi_is_exception_pending">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_exception_pending"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_exception_pending</span><span class="hljs-params">(napi_env env, <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[out] result</code>: Boolean value that is set to true if an exception is pending.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API can be called even if there is a pending JavaScript exception.</p> <h5><code>napi_fatal_exception</code><span><a class="mark" href="#napi_fatal_exception" id="napi_fatal_exception">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_fatal_exception"></a></h5> <div class="api_metadata"> <span>Added in: v9.10.0</span> <span>N-API version: 3</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_fatal_exception</span><span class="hljs-params">(napi_env env, napi_value err)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] err</code>: The error that is passed to <code>'uncaughtException'</code>.</li> </ul> <p>Trigger an <code>'uncaughtException'</code> in JavaScript. Useful if an async callback throws an exception with no way to recover.</p> <h4>Fatal errors<span><a class="mark" href="#fatal-errors" id="fatal-errors">#</a></span><a aria-hidden="true" class="legacy" id="n_api_fatal_errors"></a></h4> <p>In the event of an unrecoverable error in a native addon, a fatal error can be thrown to immediately terminate the process.</p> <h5><code>napi_fatal_error</code><span><a class="mark" href="#napi_fatal_error" id="napi_fatal_error">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_fatal_error"></a></h5> <div class="api_metadata"> <span>Added in: v8.2.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_NO_RETURN <span class="hljs-type">void</span> <span class="hljs-title function_">napi_fatal_error</span><span class="hljs-params">(<span class="hljs-type">const</span> <span class="hljs-type">char</span>* location, <span class="hljs-type">size_t</span> location_len, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* message, <span class="hljs-type">size_t</span> message_len)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] location</code>: Optional location at which the error occurred.</li> <li><code>[in] location_len</code>: The length of the location in bytes, or <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li> <li><code>[in] message</code>: The message associated with the error.</li> <li><code>[in] message_len</code>: The length of the message in bytes, or <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li> </ul> <p>The function call does not return, the process will be terminated.</p> <p>This API can be called even if there is a pending JavaScript exception.</p> </section><section><h3>Object lifetime management<span><a class="mark" href="#object-lifetime-management" id="object-lifetime-management">#</a></span><a aria-hidden="true" class="legacy" id="n_api_object_lifetime_management"></a></h3> <p>As Node-API calls are made, handles to objects in the heap for the underlying VM may be returned as <code>napi_values</code>. These handles must hold the objects 'live' until they are no longer required by the native code, otherwise the objects could be collected before the native code was finished using them.</p> <p>As object handles are returned they are associated with a 'scope'. The lifespan for the default scope is tied to the lifespan of the native method call. The result is that, by default, handles remain valid and the objects associated with these handles will be held live for the lifespan of the native method call.</p> <p>In many cases, however, it is necessary that the handles remain valid for either a shorter or longer lifespan than that of the native method. The sections which follow describe the Node-API functions that can be used to change the handle lifespan from the default.</p> <h4>Making handle lifespan shorter than that of the native method<span><a class="mark" href="#making-handle-lifespan-shorter-than-that-of-the-native-method" id="making-handle-lifespan-shorter-than-that-of-the-native-method">#</a></span><a aria-hidden="true" class="legacy" id="n_api_making_handle_lifespan_shorter_than_that_of_the_native_method"></a></h4> <p>It is often necessary to make the lifespan of handles shorter than the lifespan of a native method. For example, consider a native method that has a loop which iterates through the elements in a large array:</p> <pre><code class="language-c"><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">1000000</span>; i++) { napi_value result; napi_status status = napi_get_element(env, object, i, &result); <span class="hljs-keyword">if</span> (status != napi_ok) { <span class="hljs-keyword">break</span>; } <span class="hljs-comment">// do something with element</span> }</code> <button class="copy-button">copy</button></pre> <p>This would result in a large number of handles being created, consuming substantial resources. In addition, even though the native code could only use the most recent handle, all of the associated objects would also be kept alive since they all share the same scope.</p> <p>To handle this case, Node-API provides the ability to establish a new 'scope' to which newly created handles will be associated. Once those handles are no longer required, the scope can be 'closed' and any handles associated with the scope are invalidated. The methods available to open/close scopes are <a href="#napi_open_handle_scope"><code>napi_open_handle_scope</code></a> and <a href="#napi_close_handle_scope"><code>napi_close_handle_scope</code></a>.</p> <p>Node-API only supports a single nested hierarchy of scopes. There is only one active scope at any time, and all new handles will be associated with that scope while it is active. Scopes must be closed in the reverse order from which they are opened. In addition, all scopes created within a native method must be closed before returning from that method.</p> <p>Taking the earlier example, adding calls to <a href="#napi_open_handle_scope"><code>napi_open_handle_scope</code></a> and <a href="#napi_close_handle_scope"><code>napi_close_handle_scope</code></a> would ensure that at most a single handle is valid throughout the execution of the loop:</p> <pre><code class="language-c"><span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">1000000</span>; i++) { napi_handle_scope scope; napi_status status = napi_open_handle_scope(env, &scope); <span class="hljs-keyword">if</span> (status != napi_ok) { <span class="hljs-keyword">break</span>; } napi_value result; status = napi_get_element(env, object, i, &result); <span class="hljs-keyword">if</span> (status != napi_ok) { <span class="hljs-keyword">break</span>; } <span class="hljs-comment">// do something with element</span> status = napi_close_handle_scope(env, scope); <span class="hljs-keyword">if</span> (status != napi_ok) { <span class="hljs-keyword">break</span>; } }</code> <button class="copy-button">copy</button></pre> <p>When nesting scopes, there are cases where a handle from an inner scope needs to live beyond the lifespan of that scope. Node-API supports an 'escapable scope' in order to support this case. An escapable scope allows one handle to be 'promoted' so that it 'escapes' the current scope and the lifespan of the handle changes from the current scope to that of the outer scope.</p> <p>The methods available to open/close escapable scopes are <a href="#napi_open_escapable_handle_scope"><code>napi_open_escapable_handle_scope</code></a> and <a href="#napi_close_escapable_handle_scope"><code>napi_close_escapable_handle_scope</code></a>.</p> <p>The request to promote a handle is made through <a href="#napi_escape_handle"><code>napi_escape_handle</code></a> which can only be called once.</p> <h5><code>napi_open_handle_scope</code><span><a class="mark" href="#napi_open_handle_scope" id="napi_open_handle_scope">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_open_handle_scope"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_open_handle_scope</span><span class="hljs-params">(napi_env env, napi_handle_scope* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[out] result</code>: <code>napi_value</code> representing the new scope.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API opens a new scope.</p> <h5><code>napi_close_handle_scope</code><span><a class="mark" href="#napi_close_handle_scope" id="napi_close_handle_scope">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_close_handle_scope"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_close_handle_scope</span><span class="hljs-params">(napi_env env, napi_handle_scope scope)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] scope</code>: <code>napi_value</code> representing the scope to be closed.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API closes the scope passed in. Scopes must be closed in the reverse order from which they were created.</p> <p>This API can be called even if there is a pending JavaScript exception.</p> <h5><code>napi_open_escapable_handle_scope</code><span><a class="mark" href="#napi_open_escapable_handle_scope" id="napi_open_escapable_handle_scope">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_open_escapable_handle_scope"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_open_escapable_handle_scope</span><span class="hljs-params">(napi_env env, napi_handle_scope* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[out] result</code>: <code>napi_value</code> representing the new scope.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API opens a new scope from which one object can be promoted to the outer scope.</p> <h5><code>napi_close_escapable_handle_scope</code><span><a class="mark" href="#napi_close_escapable_handle_scope" id="napi_close_escapable_handle_scope">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_close_escapable_handle_scope"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_close_escapable_handle_scope</span><span class="hljs-params">(napi_env env, napi_handle_scope scope)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] scope</code>: <code>napi_value</code> representing the scope to be closed.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API closes the scope passed in. Scopes must be closed in the reverse order from which they were created.</p> <p>This API can be called even if there is a pending JavaScript exception.</p> <h5><code>napi_escape_handle</code><span><a class="mark" href="#napi_escape_handle" id="napi_escape_handle">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_escape_handle"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_escape_handle</span><span class="hljs-params">(napi_env env, napi_escapable_handle_scope scope, napi_value escapee, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] scope</code>: <code>napi_value</code> representing the current scope.</li> <li><code>[in] escapee</code>: <code>napi_value</code> representing the JavaScript <code>Object</code> to be escaped.</li> <li><code>[out] result</code>: <code>napi_value</code> representing the handle to the escaped <code>Object</code> in the outer scope.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API promotes the handle to the JavaScript object so that it is valid for the lifetime of the outer scope. It can only be called once per scope. If it is called more than once an error will be returned.</p> <p>This API can be called even if there is a pending JavaScript exception.</p> <h4>References to values with a lifespan longer than that of the native method<span><a class="mark" href="#references-to-values-with-a-lifespan-longer-than-that-of-the-native-method" id="references-to-values-with-a-lifespan-longer-than-that-of-the-native-method">#</a></span><a aria-hidden="true" class="legacy" id="n_api_references_to_values_with_a_lifespan_longer_than_that_of_the_native_method"></a></h4> <p>In some cases, an addon will need to be able to create and reference values with a lifespan longer than that of a single native method invocation. For example, to create a constructor and later use that constructor in a request to create instances, it must be possible to reference the constructor object across many different instance creation requests. This would not be possible with a normal handle returned as a <code>napi_value</code> as described in the earlier section. The lifespan of a normal handle is managed by scopes and all scopes must be closed before the end of a native method.</p> <p>Node-API provides methods for creating persistent references to values. Currently Node-API only allows references to be created for a limited set of value types, including object, external, function, and symbol.</p> <p>Each reference has an associated count with a value of 0 or higher, which determines whether the reference will keep the corresponding value alive. References with a count of 0 do not prevent values from being collected. Values of object (object, function, external) and symbol types are becoming 'weak' references and can still be accessed while they are not collected. Any count greater than 0 will prevent the values from being collected.</p> <p>Symbol values have different flavors. The true weak reference behavior is only supported by local symbols created with the <code>napi_create_symbol</code> function or the JavaScript <code>Symbol()</code> constructor calls. Globally registered symbols created with the <code>node_api_symbol_for</code> function or JavaScript <code>Symbol.for()</code> function calls remain always strong references because the garbage collector does not collect them. The same is true for well-known symbols such as <code>Symbol.iterator</code>. They are also never collected by the garbage collector.</p> <p>References can be created with an initial reference count. The count can then be modified through <a href="#napi_reference_ref"><code>napi_reference_ref</code></a> and <a href="#napi_reference_unref"><code>napi_reference_unref</code></a>. If an object is collected while the count for a reference is 0, all subsequent calls to get the object associated with the reference <a href="#napi_get_reference_value"><code>napi_get_reference_value</code></a> will return <code>NULL</code> for the returned <code>napi_value</code>. An attempt to call <a href="#napi_reference_ref"><code>napi_reference_ref</code></a> for a reference whose object has been collected results in an error.</p> <p>References must be deleted once they are no longer required by the addon. When a reference is deleted, it will no longer prevent the corresponding object from being collected. Failure to delete a persistent reference results in a 'memory leak' with both the native memory for the persistent reference and the corresponding object on the heap being retained forever.</p> <p>There can be multiple persistent references created which refer to the same object, each of which will either keep the object live or not based on its individual count. Multiple persistent references to the same object can result in unexpectedly keeping alive native memory. The native structures for a persistent reference must be kept alive until finalizers for the referenced object are executed. If a new persistent reference is created for the same object, the finalizers for that object will not be run and the native memory pointed by the earlier persistent reference will not be freed. This can be avoided by calling <code>napi_delete_reference</code> in addition to <code>napi_reference_unref</code> when possible.</p> <p><strong>Change History:</strong></p> <ul> <li> <p>Experimental (<code>NAPI_EXPERIMENTAL</code> is defined):</p> <p>References can be created for all value types. The new supported value types do not support weak reference semantic and the values of these types are released when the reference count becomes 0 and cannot be accessed from the reference anymore.</p> </li> </ul> <h5><code>napi_create_reference</code><span><a class="mark" href="#napi_create_reference" id="napi_create_reference">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_reference"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_reference</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">uint32_t</span> initial_refcount, napi_ref* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: The <code>napi_value</code> for which a reference is being created.</li> <li><code>[in] initial_refcount</code>: Initial reference count for the new reference.</li> <li><code>[out] result</code>: <code>napi_ref</code> pointing to the new reference.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API creates a new reference with the specified reference count to the value passed in.</p> <h5><code>napi_delete_reference</code><span><a class="mark" href="#napi_delete_reference" id="napi_delete_reference">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_delete_reference"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_delete_reference</span><span class="hljs-params">(napi_env env, napi_ref ref)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] ref</code>: <code>napi_ref</code> to be deleted.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API deletes the reference passed in.</p> <p>This API can be called even if there is a pending JavaScript exception.</p> <h5><code>napi_reference_ref</code><span><a class="mark" href="#napi_reference_ref" id="napi_reference_ref">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_reference_ref"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_reference_ref</span><span class="hljs-params">(napi_env env, napi_ref ref, <span class="hljs-type">uint32_t</span>* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] ref</code>: <code>napi_ref</code> for which the reference count will be incremented.</li> <li><code>[out] result</code>: The new reference count.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API increments the reference count for the reference passed in and returns the resulting reference count.</p> <h5><code>napi_reference_unref</code><span><a class="mark" href="#napi_reference_unref" id="napi_reference_unref">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_reference_unref"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_reference_unref</span><span class="hljs-params">(napi_env env, napi_ref ref, <span class="hljs-type">uint32_t</span>* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] ref</code>: <code>napi_ref</code> for which the reference count will be decremented.</li> <li><code>[out] result</code>: The new reference count.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API decrements the reference count for the reference passed in and returns the resulting reference count.</p> <h5><code>napi_get_reference_value</code><span><a class="mark" href="#napi_get_reference_value" id="napi_get_reference_value">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_reference_value"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_get_reference_value</span><span class="hljs-params">(napi_env env, napi_ref ref, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] ref</code>: The <code>napi_ref</code> for which the corresponding value is being requested.</li> <li><code>[out] result</code>: The <code>napi_value</code> referenced by the <code>napi_ref</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>If still valid, this API returns the <code>napi_value</code> representing the JavaScript value associated with the <code>napi_ref</code>. Otherwise, result will be <code>NULL</code>.</p> <h4>Cleanup on exit of the current Node.js environment<span><a class="mark" href="#cleanup-on-exit-of-the-current-nodejs-environment" id="cleanup-on-exit-of-the-current-nodejs-environment">#</a></span><a aria-hidden="true" class="legacy" id="n_api_cleanup_on_exit_of_the_current_node_js_environment"></a></h4> <p>While a Node.js process typically releases all its resources when exiting, embedders of Node.js, or future Worker support, may require addons to register clean-up hooks that will be run once the current Node.js environment exits.</p> <p>Node-API provides functions for registering and un-registering such callbacks. When those callbacks are run, all resources that are being held by the addon should be freed up.</p> <h5><code>napi_add_env_cleanup_hook</code><span><a class="mark" href="#napi_add_env_cleanup_hook" id="napi_add_env_cleanup_hook">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_add_env_cleanup_hook"></a></h5> <div class="api_metadata"> <span>Added in: v10.2.0</span> <span>N-API version: 3</span> </div> <pre><code class="language-c">NODE_EXTERN napi_status <span class="hljs-title function_">napi_add_env_cleanup_hook</span><span class="hljs-params">(node_api_basic_env env, napi_cleanup_hook fun, <span class="hljs-type">void</span>* arg)</span>;</code> <button class="copy-button">copy</button></pre> <p>Registers <code>fun</code> as a function to be run with the <code>arg</code> parameter once the current Node.js environment exits.</p> <p>A function can safely be specified multiple times with different <code>arg</code> values. In that case, it will be called multiple times as well. Providing the same <code>fun</code> and <code>arg</code> values multiple times is not allowed and will lead the process to abort.</p> <p>The hooks will be called in reverse order, i.e. the most recently added one will be called first.</p> <p>Removing this hook can be done by using <a href="#napi_remove_env_cleanup_hook"><code>napi_remove_env_cleanup_hook</code></a>. Typically, that happens when the resource for which this hook was added is being torn down anyway.</p> <p>For asynchronous cleanup, <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a> is available.</p> <h5><code>napi_remove_env_cleanup_hook</code><span><a class="mark" href="#napi_remove_env_cleanup_hook" id="napi_remove_env_cleanup_hook">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_remove_env_cleanup_hook"></a></h5> <div class="api_metadata"> <span>Added in: v10.2.0</span> <span>N-API version: 3</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_remove_env_cleanup_hook</span><span class="hljs-params">(node_api_basic_env env, <span class="hljs-type">void</span> (*fun)(<span class="hljs-type">void</span>* arg), <span class="hljs-type">void</span>* arg)</span>;</code> <button class="copy-button">copy</button></pre> <p>Unregisters <code>fun</code> as a function to be run with the <code>arg</code> parameter once the current Node.js environment exits. Both the argument and the function value need to be exact matches.</p> <p>The function must have originally been registered with <code>napi_add_env_cleanup_hook</code>, otherwise the process will abort.</p> <h5><code>napi_add_async_cleanup_hook</code><span><a class="mark" href="#napi_add_async_cleanup_hook" id="napi_add_async_cleanup_hook">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_add_async_cleanup_hook"></a></h5> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.10.0, v12.19.0</td> <td><p>Changed signature of the <code>hook</code> callback.</p></td></tr> <tr><td>v14.8.0, v12.19.0</td> <td><p><span>Added in: v14.8.0, v12.19.0</span></p></td></tr> </tbody></table> </details> <span>N-API version: 8</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_add_async_cleanup_hook</span><span class="hljs-params">( node_api_basic_env env, napi_async_cleanup_hook hook, <span class="hljs-type">void</span>* arg, napi_async_cleanup_hook_handle* remove_handle)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] hook</code>: The function pointer to call at environment teardown.</li> <li><code>[in] arg</code>: The pointer to pass to <code>hook</code> when it gets called.</li> <li><code>[out] remove_handle</code>: Optional handle that refers to the asynchronous cleanup hook.</li> </ul> <p>Registers <code>hook</code>, which is a function of type <a href="#napi_async_cleanup_hook"><code>napi_async_cleanup_hook</code></a>, as a function to be run with the <code>remove_handle</code> and <code>arg</code> parameters once the current Node.js environment exits.</p> <p>Unlike <a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a>, the hook is allowed to be asynchronous.</p> <p>Otherwise, behavior generally matches that of <a href="#napi_add_env_cleanup_hook"><code>napi_add_env_cleanup_hook</code></a>.</p> <p>If <code>remove_handle</code> is not <code>NULL</code>, an opaque value will be stored in it that must later be passed to <a href="#napi_remove_async_cleanup_hook"><code>napi_remove_async_cleanup_hook</code></a>, regardless of whether the hook has already been invoked. Typically, that happens when the resource for which this hook was added is being torn down anyway.</p> <h5><code>napi_remove_async_cleanup_hook</code><span><a class="mark" href="#napi_remove_async_cleanup_hook" id="napi_remove_async_cleanup_hook">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_remove_async_cleanup_hook"></a></h5> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.10.0, v12.19.0</td> <td><p>Removed <code>env</code> parameter.</p></td></tr> <tr><td>v14.8.0, v12.19.0</td> <td><p><span>Added in: v14.8.0, v12.19.0</span></p></td></tr> </tbody></table> </details> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_remove_async_cleanup_hook</span><span class="hljs-params">( napi_async_cleanup_hook_handle remove_handle)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] remove_handle</code>: The handle to an asynchronous cleanup hook that was created with <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>.</li> </ul> <p>Unregisters the cleanup hook corresponding to <code>remove_handle</code>. This will prevent the hook from being executed, unless it has already started executing. This must be called on any <code>napi_async_cleanup_hook_handle</code> value obtained from <a href="#napi_add_async_cleanup_hook"><code>napi_add_async_cleanup_hook</code></a>.</p> <h4>Finalization on the exit of the Node.js environment<span><a class="mark" href="#finalization-on-the-exit-of-the-nodejs-environment" id="finalization-on-the-exit-of-the-nodejs-environment">#</a></span><a aria-hidden="true" class="legacy" id="n_api_finalization_on_the_exit_of_the_node_js_environment"></a></h4> <p>The Node.js environment may be torn down at an arbitrary time as soon as possible with JavaScript execution disallowed, like on the request of <a href="worker_threads.html#workerterminate"><code>worker.terminate()</code></a>. When the environment is being torn down, the registered <code>napi_finalize</code> callbacks of JavaScript objects, thread-safe functions and environment instance data are invoked immediately and independently.</p> <p>The invocation of <code>napi_finalize</code> callbacks is scheduled after the manually registered cleanup hooks. In order to ensure a proper order of addon finalization during environment shutdown to avoid use-after-free in the <code>napi_finalize</code> callback, addons should register a cleanup hook with <code>napi_add_env_cleanup_hook</code> and <code>napi_add_async_cleanup_hook</code> to manually release the allocated resource in a proper order.</p> </section><section><h3>Module registration<span><a class="mark" href="#module-registration" id="module-registration">#</a></span><a aria-hidden="true" class="legacy" id="n_api_module_registration"></a></h3> <p>Node-API modules are registered in a manner similar to other modules except that instead of using the <code>NODE_MODULE</code> macro the following is used:</p> <pre><code class="language-c">NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)</code> <button class="copy-button">copy</button></pre> <p>The next difference is the signature for the <code>Init</code> method. For a Node-API module it is as follows:</p> <pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span>;</code> <button class="copy-button">copy</button></pre> <p>The return value from <code>Init</code> is treated as the <code>exports</code> object for the module. The <code>Init</code> method is passed an empty object via the <code>exports</code> parameter as a convenience. If <code>Init</code> returns <code>NULL</code>, the parameter passed as <code>exports</code> is exported by the module. Node-API modules cannot modify the <code>module</code> object but can specify anything as the <code>exports</code> property of the module.</p> <p>To add the method <code>hello</code> as a function so that it can be called as a method provided by the addon:</p> <pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> { napi_status status; napi_property_descriptor desc = { <span class="hljs-string">"hello"</span>, <span class="hljs-literal">NULL</span>, Method, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, napi_writable | napi_enumerable | napi_configurable, <span class="hljs-literal">NULL</span> }; status = napi_define_properties(env, exports, <span class="hljs-number">1</span>, &desc); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; <span class="hljs-keyword">return</span> exports; }</code> <button class="copy-button">copy</button></pre> <p>To set a function to be returned by the <code>require()</code> for the addon:</p> <pre><code class="language-c">napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> { napi_value method; napi_status status; status = napi_create_function(env, <span class="hljs-string">"exports"</span>, NAPI_AUTO_LENGTH, Method, <span class="hljs-literal">NULL</span>, &method); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; <span class="hljs-keyword">return</span> method; }</code> <button class="copy-button">copy</button></pre> <p>To define a class so that new instances can be created (often used with <a href="#object-wrap">Object wrap</a>):</p> <pre><code class="language-c"><span class="hljs-comment">// <span class="hljs-doctag">NOTE:</span> partial example, not all referenced code is included</span> napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> { napi_status status; napi_property_descriptor properties[] = { { <span class="hljs-string">"value"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, GetValue, SetValue, <span class="hljs-literal">NULL</span>, napi_writable | napi_configurable, <span class="hljs-literal">NULL</span> }, DECLARE_NAPI_METHOD(<span class="hljs-string">"plusOne"</span>, PlusOne), DECLARE_NAPI_METHOD(<span class="hljs-string">"multiply"</span>, Multiply), }; napi_value cons; status = napi_define_class(env, <span class="hljs-string">"MyObject"</span>, New, <span class="hljs-literal">NULL</span>, <span class="hljs-number">3</span>, properties, &cons); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; status = napi_create_reference(env, cons, <span class="hljs-number">1</span>, &constructor); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; status = napi_set_named_property(env, exports, <span class="hljs-string">"MyObject"</span>, cons); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; <span class="hljs-keyword">return</span> exports; }</code> <button class="copy-button">copy</button></pre> <p>You can also use the <code>NAPI_MODULE_INIT</code> macro, which acts as a shorthand for <code>NAPI_MODULE</code> and defining an <code>Init</code> function:</p> <pre><code class="language-c">NAPI_MODULE_INIT(<span class="hljs-comment">/* napi_env env, napi_value exports */</span>) { napi_value answer; napi_status result; status = napi_create_int64(env, <span class="hljs-number">42</span>, &answer); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; status = napi_set_named_property(env, exports, <span class="hljs-string">"answer"</span>, answer); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; <span class="hljs-keyword">return</span> exports; }</code> <button class="copy-button">copy</button></pre> <p>The parameters <code>env</code> and <code>exports</code> are provided to the body of the <code>NAPI_MODULE_INIT</code> macro.</p> <p>All Node-API addons are context-aware, meaning they may be loaded multiple times. There are a few design considerations when declaring such a module. The documentation on <a href="addons.html#context-aware-addons">context-aware addons</a> provides more details.</p> <p>The variables <code>env</code> and <code>exports</code> will be available inside the function body following the macro invocation.</p> <p>For more details on setting properties on objects, see the section on <a href="#working-with-javascript-properties">Working with JavaScript properties</a>.</p> <p>For more details on building addon modules in general, refer to the existing API.</p> </section><section><h3>Working with JavaScript values<span><a class="mark" href="#working-with-javascript-values" id="working-with-javascript-values">#</a></span><a aria-hidden="true" class="legacy" id="n_api_working_with_javascript_values"></a></h3> <p>Node-API exposes a set of APIs to create all types of JavaScript values. Some of these types are documented under <a href="https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values">Section 6</a> of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p> <p>Fundamentally, these APIs are used to do one of the following:</p> <ol> <li>Create a new JavaScript object</li> <li>Convert from a primitive C type to a Node-API value</li> <li>Convert from Node-API value to a primitive C type</li> <li>Get global instances including <code>undefined</code> and <code>null</code></li> </ol> <p>Node-API values are represented by the type <code>napi_value</code>. Any Node-API call that requires a JavaScript value takes in a <code>napi_value</code>. In some cases, the API does check the type of the <code>napi_value</code> up-front. However, for better performance, it's better for the caller to make sure that the <code>napi_value</code> in question is of the JavaScript type expected by the API.</p> <h4>Enum types<span><a class="mark" href="#enum-types" id="enum-types">#</a></span><a aria-hidden="true" class="legacy" id="n_api_enum_types"></a></h4> <h5><code>napi_key_collection_mode</code><span><a class="mark" href="#napi_key_collection_mode" id="napi_key_collection_mode">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_key_collection_mode"></a></h5> <div class="api_metadata"> <span>Added in: v13.7.0, v12.17.0, v10.20.0</span> <span>N-API version: 6</span> </div> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span> napi_key_include_prototypes, napi_key_own_only } napi_key_collection_mode;</code> <button class="copy-button">copy</button></pre> <p>Describes the <code>Keys/Properties</code> filter enums:</p> <p><code>napi_key_collection_mode</code> limits the range of collected properties.</p> <p><code>napi_key_own_only</code> limits the collected properties to the given object only. <code>napi_key_include_prototypes</code> will include all keys of the objects's prototype chain as well.</p> <h5><code>napi_key_filter</code><span><a class="mark" href="#napi_key_filter" id="napi_key_filter">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_key_filter"></a></h5> <div class="api_metadata"> <span>Added in: v13.7.0, v12.17.0, v10.20.0</span> <span>N-API version: 6</span> </div> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span> napi_key_all_properties = <span class="hljs-number">0</span>, napi_key_writable = <span class="hljs-number">1</span>, napi_key_enumerable = <span class="hljs-number">1</span> << <span class="hljs-number">1</span>, napi_key_configurable = <span class="hljs-number">1</span> << <span class="hljs-number">2</span>, napi_key_skip_strings = <span class="hljs-number">1</span> << <span class="hljs-number">3</span>, napi_key_skip_symbols = <span class="hljs-number">1</span> << <span class="hljs-number">4</span> } napi_key_filter;</code> <button class="copy-button">copy</button></pre> <p>Property filter bits. They can be or'ed to build a composite filter.</p> <h5><code>napi_key_conversion</code><span><a class="mark" href="#napi_key_conversion" id="napi_key_conversion">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_key_conversion"></a></h5> <div class="api_metadata"> <span>Added in: v13.7.0, v12.17.0, v10.20.0</span> <span>N-API version: 6</span> </div> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span> napi_key_keep_numbers, napi_key_numbers_to_strings } napi_key_conversion;</code> <button class="copy-button">copy</button></pre> <p><code>napi_key_numbers_to_strings</code> will convert integer indexes to strings. <code>napi_key_keep_numbers</code> will return numbers for integer indexes.</p> <h5><code>napi_valuetype</code><span><a class="mark" href="#napi_valuetype" id="napi_valuetype">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_valuetype"></a></h5> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span> <span class="hljs-comment">// ES6 types (corresponds to typeof)</span> napi_undefined, napi_null, napi_boolean, napi_number, napi_string, napi_symbol, napi_object, napi_function, napi_external, napi_bigint, } napi_valuetype;</code> <button class="copy-button">copy</button></pre> <p>Describes the type of a <code>napi_value</code>. This generally corresponds to the types described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types">Section 6.1</a> of the ECMAScript Language Specification. In addition to types in that section, <code>napi_valuetype</code> can also represent <code>Function</code>s and <code>Object</code>s with external data.</p> <p>A JavaScript value of type <code>napi_external</code> appears in JavaScript as a plain object such that no properties can be set on it, and no prototype.</p> <h5><code>napi_typedarray_type</code><span><a class="mark" href="#napi_typedarray_type" id="napi_typedarray_type">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_typedarray_type"></a></h5> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span> napi_int8_array, napi_uint8_array, napi_uint8_clamped_array, napi_int16_array, napi_uint16_array, napi_int32_array, napi_uint32_array, napi_float32_array, napi_float64_array, napi_bigint64_array, napi_biguint64_array, } napi_typedarray_type;</code> <button class="copy-button">copy</button></pre> <p>This represents the underlying binary scalar datatype of the <code>TypedArray</code>. Elements of this enum correspond to <a href="https://tc39.github.io/ecma262/#sec-typedarray-objects">Section 22.2</a> of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p> <h4>Object creation functions<span><a class="mark" href="#object-creation-functions" id="object-creation-functions">#</a></span><a aria-hidden="true" class="legacy" id="n_api_object_creation_functions"></a></h4> <h5><code>napi_create_array</code><span><a class="mark" href="#napi_create_array" id="napi_create_array">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_array"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_array</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Array</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API returns a Node-API value corresponding to a JavaScript <code>Array</code> type. JavaScript arrays are described in <a href="https://tc39.github.io/ecma262/#sec-array-objects">Section 22.1</a> of the ECMAScript Language Specification.</p> <h5><code>napi_create_array_with_length</code><span><a class="mark" href="#napi_create_array_with_length" id="napi_create_array_with_length">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_array_with_length"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_array_with_length</span><span class="hljs-params">(napi_env env, <span class="hljs-type">size_t</span> length, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] length</code>: The initial length of the <code>Array</code>.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Array</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API returns a Node-API value corresponding to a JavaScript <code>Array</code> type. The <code>Array</code>'s length property is set to the passed-in length parameter. However, the underlying buffer is not guaranteed to be pre-allocated by the VM when the array is created. That behavior is left to the underlying VM implementation. If the buffer must be a contiguous block of memory that can be directly read and/or written via C, consider using <a href="#napi_create_external_arraybuffer"><code>napi_create_external_arraybuffer</code></a>.</p> <p>JavaScript arrays are described in <a href="https://tc39.github.io/ecma262/#sec-array-objects">Section 22.1</a> of the ECMAScript Language Specification.</p> <h5><code>napi_create_arraybuffer</code><span><a class="mark" href="#napi_create_arraybuffer" id="napi_create_arraybuffer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_arraybuffer"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_arraybuffer</span><span class="hljs-params">(napi_env env, <span class="hljs-type">size_t</span> byte_length, <span class="hljs-type">void</span>** data, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] length</code>: The length in bytes of the array buffer to create.</li> <li><code>[out] data</code>: Pointer to the underlying byte buffer of the <code>ArrayBuffer</code>. <code>data</code> can optionally be ignored by passing <code>NULL</code>.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>ArrayBuffer</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API returns a Node-API value corresponding to a JavaScript <code>ArrayBuffer</code>. <code>ArrayBuffer</code>s are used to represent fixed-length binary data buffers. They are normally used as a backing-buffer for <code>TypedArray</code> objects. The <code>ArrayBuffer</code> allocated will have an underlying byte buffer whose size is determined by the <code>length</code> parameter that's passed in. The underlying buffer is optionally returned back to the caller in case the caller wants to directly manipulate the buffer. This buffer can only be written to directly from native code. To write to this buffer from JavaScript, a typed array or <code>DataView</code> object would need to be created.</p> <p>JavaScript <code>ArrayBuffer</code> objects are described in <a href="https://tc39.github.io/ecma262/#sec-arraybuffer-objects">Section 24.1</a> of the ECMAScript Language Specification.</p> <h5><code>napi_create_buffer</code><span><a class="mark" href="#napi_create_buffer" id="napi_create_buffer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_buffer"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_buffer</span><span class="hljs-params">(napi_env env, <span class="hljs-type">size_t</span> size, <span class="hljs-type">void</span>** data, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] size</code>: Size in bytes of the underlying buffer.</li> <li><code>[out] data</code>: Raw pointer to the underlying buffer. <code>data</code> can optionally be ignored by passing <code>NULL</code>.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a <code>node::Buffer</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API allocates a <code>node::Buffer</code> object. While this is still a fully-supported data structure, in most cases using a <code>TypedArray</code> will suffice.</p> <h5><code>napi_create_buffer_copy</code><span><a class="mark" href="#napi_create_buffer_copy" id="napi_create_buffer_copy">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_buffer_copy"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_buffer_copy</span><span class="hljs-params">(napi_env env, <span class="hljs-type">size_t</span> length, <span class="hljs-type">const</span> <span class="hljs-type">void</span>* data, <span class="hljs-type">void</span>** result_data, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] size</code>: Size in bytes of the input buffer (should be the same as the size of the new buffer).</li> <li><code>[in] data</code>: Raw pointer to the underlying buffer to copy from.</li> <li><code>[out] result_data</code>: Pointer to the new <code>Buffer</code>'s underlying data buffer. <code>result_data</code> can optionally be ignored by passing <code>NULL</code>.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a <code>node::Buffer</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API allocates a <code>node::Buffer</code> object and initializes it with data copied from the passed-in buffer. While this is still a fully-supported data structure, in most cases using a <code>TypedArray</code> will suffice.</p> <h5><code>napi_create_date</code><span><a class="mark" href="#napi_create_date" id="napi_create_date">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_date"></a></h5> <div class="api_metadata"> <span>Added in: v11.11.0, v10.17.0</span> <span>N-API version: 5</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_date</span><span class="hljs-params">(napi_env env, <span class="hljs-type">double</span> time, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] time</code>: ECMAScript time value in milliseconds since 01 January, 1970 UTC.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Date</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API does not observe leap seconds; they are ignored, as ECMAScript aligns with POSIX time specification.</p> <p>This API allocates a JavaScript <code>Date</code> object.</p> <p>JavaScript <code>Date</code> objects are described in <a href="https://tc39.github.io/ecma262/#sec-date-objects">Section 20.3</a> of the ECMAScript Language Specification.</p> <h5><code>napi_create_external</code><span><a class="mark" href="#napi_create_external" id="napi_create_external">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_external"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external</span><span class="hljs-params">(napi_env env, <span class="hljs-type">void</span>* data, napi_finalize finalize_cb, <span class="hljs-type">void</span>* finalize_hint, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] data</code>: Raw pointer to the external data.</li> <li><code>[in] finalize_cb</code>: Optional callback to call when the external value is being collected. <a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li> <li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during collection.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing an external value.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API allocates a JavaScript value with external data attached to it. This is used to pass external data through JavaScript code, so it can be retrieved later by native code using <a href="#napi_get_value_external"><code>napi_get_value_external</code></a>.</p> <p>The API adds a <code>napi_finalize</code> callback which will be called when the JavaScript object just created has been garbage collected.</p> <p>The created value is not an object, and therefore does not support additional properties. It is considered a distinct value type: calling <code>napi_typeof()</code> with an external value yields <code>napi_external</code>.</p> <h5><code>napi_create_external_arraybuffer</code><span><a class="mark" href="#napi_create_external_arraybuffer" id="napi_create_external_arraybuffer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_external_arraybuffer"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external_arraybuffer</span><span class="hljs-params">(napi_env env, <span class="hljs-type">void</span>* external_data, <span class="hljs-type">size_t</span> byte_length, napi_finalize finalize_cb, <span class="hljs-type">void</span>* finalize_hint, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] external_data</code>: Pointer to the underlying byte buffer of the <code>ArrayBuffer</code>.</li> <li><code>[in] byte_length</code>: The length in bytes of the underlying buffer.</li> <li><code>[in] finalize_cb</code>: Optional callback to call when the <code>ArrayBuffer</code> is being collected. <a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li> <li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during collection.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>ArrayBuffer</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p><strong>Some runtimes other than Node.js have dropped support for external buffers</strong>. On runtimes other than Node.js this method may return <code>napi_no_external_buffers_allowed</code> to indicate that external buffers are not supported. One such runtime is Electron as described in this issue <a href="https://github.com/electron/electron/issues/35801">electron/issues/35801</a>.</p> <p>In order to maintain broadest compatibility with all runtimes you may define <code>NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED</code> in your addon before includes for the node-api headers. Doing so will hide the 2 functions that create external buffers. This will ensure a compilation error occurs if you accidentally use one of these methods.</p> <p>This API returns a Node-API value corresponding to a JavaScript <code>ArrayBuffer</code>. The underlying byte buffer of the <code>ArrayBuffer</code> is externally allocated and managed. The caller must ensure that the byte buffer remains valid until the finalize callback is called.</p> <p>The API adds a <code>napi_finalize</code> callback which will be called when the JavaScript object just created has been garbage collected.</p> <p>JavaScript <code>ArrayBuffer</code>s are described in <a href="https://tc39.github.io/ecma262/#sec-arraybuffer-objects">Section 24.1</a> of the ECMAScript Language Specification.</p> <h5><code>napi_create_external_buffer</code><span><a class="mark" href="#napi_create_external_buffer" id="napi_create_external_buffer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_external_buffer"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_external_buffer</span><span class="hljs-params">(napi_env env, <span class="hljs-type">size_t</span> length, <span class="hljs-type">void</span>* data, napi_finalize finalize_cb, <span class="hljs-type">void</span>* finalize_hint, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] length</code>: Size in bytes of the input buffer (should be the same as the size of the new buffer).</li> <li><code>[in] data</code>: Raw pointer to the underlying buffer to expose to JavaScript.</li> <li><code>[in] finalize_cb</code>: Optional callback to call when the <code>ArrayBuffer</code> is being collected. <a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li> <li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during collection.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a <code>node::Buffer</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p><strong>Some runtimes other than Node.js have dropped support for external buffers</strong>. On runtimes other than Node.js this method may return <code>napi_no_external_buffers_allowed</code> to indicate that external buffers are not supported. One such runtime is Electron as described in this issue <a href="https://github.com/electron/electron/issues/35801">electron/issues/35801</a>.</p> <p>In order to maintain broadest compatibility with all runtimes you may define <code>NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED</code> in your addon before includes for the node-api headers. Doing so will hide the 2 functions that create external buffers. This will ensure a compilation error occurs if you accidentally use one of these methods.</p> <p>This API allocates a <code>node::Buffer</code> object and initializes it with data backed by the passed in buffer. While this is still a fully-supported data structure, in most cases using a <code>TypedArray</code> will suffice.</p> <p>The API adds a <code>napi_finalize</code> callback which will be called when the JavaScript object just created has been garbage collected.</p> <p>For Node.js >=4 <code>Buffers</code> are <code>Uint8Array</code>s.</p> <h5><code>napi_create_object</code><span><a class="mark" href="#napi_create_object" id="napi_create_object">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_object"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_object</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>Object</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API allocates a default JavaScript <code>Object</code>. It is the equivalent of doing <code>new Object()</code> in JavaScript.</p> <p>The JavaScript <code>Object</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-object-type">Section 6.1.7</a> of the ECMAScript Language Specification.</p> <h5><code>napi_create_symbol</code><span><a class="mark" href="#napi_create_symbol" id="napi_create_symbol">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_symbol"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_symbol</span><span class="hljs-params">(napi_env env, napi_value description, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] description</code>: Optional <code>napi_value</code> which refers to a JavaScript <code>string</code> to be set as the description for the symbol.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>symbol</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API creates a JavaScript <code>symbol</code> value from a UTF8-encoded C string.</p> <p>The JavaScript <code>symbol</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-symbol-objects">Section 19.4</a> of the ECMAScript Language Specification.</p> <h5><code>node_api_symbol_for</code><span><a class="mark" href="#node_api_symbol_for" id="node_api_symbol_for">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_symbol_for"></a></h5> <div class="api_metadata"> <span>Added in: v17.5.0, v16.15.0</span> <span>N-API version: 9</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">node_api_symbol_for</span><span class="hljs-params">(napi_env env, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8description, <span class="hljs-type">size_t</span> length, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] utf8description</code>: UTF-8 C string representing the text to be used as the description for the symbol.</li> <li><code>[in] length</code>: The length of the description string in bytes, or <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>symbol</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API searches in the global registry for an existing symbol with the given description. If the symbol already exists it will be returned, otherwise a new symbol will be created in the registry.</p> <p>The JavaScript <code>symbol</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-symbol-objects">Section 19.4</a> of the ECMAScript Language Specification.</p> <h5><code>napi_create_typedarray</code><span><a class="mark" href="#napi_create_typedarray" id="napi_create_typedarray">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_typedarray"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_typedarray</span><span class="hljs-params">(napi_env env, napi_typedarray_type type, <span class="hljs-type">size_t</span> length, napi_value arraybuffer, <span class="hljs-type">size_t</span> byte_offset, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] type</code>: Scalar datatype of the elements within the <code>TypedArray</code>.</li> <li><code>[in] length</code>: Number of elements in the <code>TypedArray</code>.</li> <li><code>[in] arraybuffer</code>: <code>ArrayBuffer</code> underlying the typed array.</li> <li><code>[in] byte_offset</code>: The byte offset within the <code>ArrayBuffer</code> from which to start projecting the <code>TypedArray</code>.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>TypedArray</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API creates a JavaScript <code>TypedArray</code> object over an existing <code>ArrayBuffer</code>. <code>TypedArray</code> objects provide an array-like view over an underlying data buffer where each element has the same underlying binary scalar datatype.</p> <p>It's required that <code>(length * size_of_element) + byte_offset</code> should be <= the size in bytes of the array passed in. If not, a <code>RangeError</code> exception is raised.</p> <p>JavaScript <code>TypedArray</code> objects are described in <a href="https://tc39.github.io/ecma262/#sec-typedarray-objects">Section 22.2</a> of the ECMAScript Language Specification.</p> <h5><code>napi_create_dataview</code><span><a class="mark" href="#napi_create_dataview" id="napi_create_dataview">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_dataview"></a></h5> <div class="api_metadata"> <span>Added in: v8.3.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_dataview</span><span class="hljs-params">(napi_env env, <span class="hljs-type">size_t</span> byte_length, napi_value arraybuffer, <span class="hljs-type">size_t</span> byte_offset, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] length</code>: Number of elements in the <code>DataView</code>.</li> <li><code>[in] arraybuffer</code>: <code>ArrayBuffer</code> underlying the <code>DataView</code>.</li> <li><code>[in] byte_offset</code>: The byte offset within the <code>ArrayBuffer</code> from which to start projecting the <code>DataView</code>.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>DataView</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API creates a JavaScript <code>DataView</code> object over an existing <code>ArrayBuffer</code>. <code>DataView</code> objects provide an array-like view over an underlying data buffer, but one which allows items of different size and type in the <code>ArrayBuffer</code>.</p> <p>It is required that <code>byte_length + byte_offset</code> is less than or equal to the size in bytes of the array passed in. If not, a <code>RangeError</code> exception is raised.</p> <p>JavaScript <code>DataView</code> objects are described in <a href="https://tc39.github.io/ecma262/#sec-dataview-objects">Section 24.3</a> of the ECMAScript Language Specification.</p> <h4>Functions to convert from C types to Node-API<span><a class="mark" href="#functions-to-convert-from-c-types-to-node-api" id="functions-to-convert-from-c-types-to-node-api">#</a></span><a aria-hidden="true" class="legacy" id="n_api_functions_to_convert_from_c_types_to_node_api"></a></h4> <h5><code>napi_create_int32</code><span><a class="mark" href="#napi_create_int32" id="napi_create_int32">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_int32"></a></h5> <div class="api_metadata"> <span>Added in: v8.4.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_int32</span><span class="hljs-params">(napi_env env, <span class="hljs-type">int32_t</span> value, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: Integer value to be represented in JavaScript.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API is used to convert from the C <code>int32_t</code> type to the JavaScript <code>number</code> type.</p> <p>The JavaScript <code>number</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a> of the ECMAScript Language Specification.</p> <h5><code>napi_create_uint32</code><span><a class="mark" href="#napi_create_uint32" id="napi_create_uint32">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_uint32"></a></h5> <div class="api_metadata"> <span>Added in: v8.4.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_uint32</span><span class="hljs-params">(napi_env env, <span class="hljs-type">uint32_t</span> value, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: Unsigned integer value to be represented in JavaScript.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API is used to convert from the C <code>uint32_t</code> type to the JavaScript <code>number</code> type.</p> <p>The JavaScript <code>number</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a> of the ECMAScript Language Specification.</p> <h5><code>napi_create_int64</code><span><a class="mark" href="#napi_create_int64" id="napi_create_int64">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_int64"></a></h5> <div class="api_metadata"> <span>Added in: v8.4.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_int64</span><span class="hljs-params">(napi_env env, <span class="hljs-type">int64_t</span> value, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: Integer value to be represented in JavaScript.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API is used to convert from the C <code>int64_t</code> type to the JavaScript <code>number</code> type.</p> <p>The JavaScript <code>number</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a> of the ECMAScript Language Specification. Note the complete range of <code>int64_t</code> cannot be represented with full precision in JavaScript. Integer values outside the range of <a href="https://tc39.github.io/ecma262/#sec-number.min_safe_integer"><code>Number.MIN_SAFE_INTEGER</code></a> <code>-(2**53 - 1)</code> - <a href="https://tc39.github.io/ecma262/#sec-number.max_safe_integer"><code>Number.MAX_SAFE_INTEGER</code></a> <code>(2**53 - 1)</code> will lose precision.</p> <h5><code>napi_create_double</code><span><a class="mark" href="#napi_create_double" id="napi_create_double">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_double"></a></h5> <div class="api_metadata"> <span>Added in: v8.4.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_double</span><span class="hljs-params">(napi_env env, <span class="hljs-type">double</span> value, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: Double-precision value to be represented in JavaScript.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>number</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API is used to convert from the C <code>double</code> type to the JavaScript <code>number</code> type.</p> <p>The JavaScript <code>number</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">Section 6.1.6</a> of the ECMAScript Language Specification.</p> <h5><code>napi_create_bigint_int64</code><span><a class="mark" href="#napi_create_bigint_int64" id="napi_create_bigint_int64">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_bigint_int64"></a></h5> <div class="api_metadata"> <span>Added in: v10.7.0</span> <span>N-API version: 6</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_int64</span><span class="hljs-params">(napi_env env, <span class="hljs-type">int64_t</span> value, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: Integer value to be represented in JavaScript.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>BigInt</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API converts the C <code>int64_t</code> type to the JavaScript <code>BigInt</code> type.</p> <h5><code>napi_create_bigint_uint64</code><span><a class="mark" href="#napi_create_bigint_uint64" id="napi_create_bigint_uint64">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_bigint_uint64"></a></h5> <div class="api_metadata"> <span>Added in: v10.7.0</span> <span>N-API version: 6</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_uint64</span><span class="hljs-params">(napi_env env, <span class="hljs-type">uint64_t</span> value, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: Unsigned integer value to be represented in JavaScript.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>BigInt</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API converts the C <code>uint64_t</code> type to the JavaScript <code>BigInt</code> type.</p> <h5><code>napi_create_bigint_words</code><span><a class="mark" href="#napi_create_bigint_words" id="napi_create_bigint_words">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_bigint_words"></a></h5> <div class="api_metadata"> <span>Added in: v10.7.0</span> <span>N-API version: 6</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_bigint_words</span><span class="hljs-params">(napi_env env, <span class="hljs-type">int</span> sign_bit, <span class="hljs-type">size_t</span> word_count, <span class="hljs-type">const</span> <span class="hljs-type">uint64_t</span>* words, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] sign_bit</code>: Determines if the resulting <code>BigInt</code> will be positive or negative.</li> <li><code>[in] word_count</code>: The length of the <code>words</code> array.</li> <li><code>[in] words</code>: An array of <code>uint64_t</code> little-endian 64-bit words.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>BigInt</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API converts an array of unsigned 64-bit words into a single <code>BigInt</code> value.</p> <p>The resulting <code>BigInt</code> is calculated as: (–1)<sup><code>sign_bit</code></sup> (<code>words[0]</code> × (2<sup>64</sup>)<sup>0</sup> + <code>words[1]</code> × (2<sup>64</sup>)<sup>1</sup> + …)</p> <h5><code>napi_create_string_latin1</code><span><a class="mark" href="#napi_create_string_latin1" id="napi_create_string_latin1">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_string_latin1"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_latin1</span><span class="hljs-params">(napi_env env, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* str, <span class="hljs-type">size_t</span> length, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] str</code>: Character buffer representing an ISO-8859-1-encoded string.</li> <li><code>[in] length</code>: The length of the string in bytes, or <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API creates a JavaScript <code>string</code> value from an ISO-8859-1-encoded C string. The native string is copied.</p> <p>The JavaScript <code>string</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p> <h5><code>node_api_create_external_string_latin1</code><span><a class="mark" href="#node_api_create_external_string_latin1" id="node_api_create_external_string_latin1">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_create_external_string_latin1"></a></h5> <div class="api_metadata"> <span>Added in: v20.4.0</span> </div> <p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> <pre><code class="language-c">napi_status <span class="hljs-title function_">node_api_create_external_string_latin1</span><span class="hljs-params">(napi_env env, <span class="hljs-type">char</span>* str, <span class="hljs-type">size_t</span> length, napi_finalize finalize_callback, <span class="hljs-type">void</span>* finalize_hint, napi_value* result, <span class="hljs-type">bool</span>* copied)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] str</code>: Character buffer representing an ISO-8859-1-encoded string.</li> <li><code>[in] length</code>: The length of the string in bytes, or <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li> <li><code>[in] finalize_callback</code>: The function to call when the string is being collected. The function will be called with the following parameters: <ul> <li><code>[in] env</code>: The environment in which the add-on is running. This value may be null if the string is being collected as part of the termination of the worker or the main Node.js instance.</li> <li><code>[in] data</code>: This is the value <code>str</code> as a <code>void*</code> pointer.</li> <li><code>[in] finalize_hint</code>: This is the value <code>finalize_hint</code> that was given to the API. <a href="#napi_finalize"><code>napi_finalize</code></a> provides more details. This parameter is optional. Passing a null value means that the add-on doesn't need to be notified when the corresponding JavaScript string is collected.</li> </ul> </li> <li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during collection.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li> <li><code>[out] copied</code>: Whether the string was copied. If it was, the finalizer will already have been invoked to destroy <code>str</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API creates a JavaScript <code>string</code> value from an ISO-8859-1-encoded C string. The native string may not be copied and must thus exist for the entire life cycle of the JavaScript value.</p> <p>The JavaScript <code>string</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p> <h5><code>napi_create_string_utf16</code><span><a class="mark" href="#napi_create_string_utf16" id="napi_create_string_utf16">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_string_utf16"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_utf16</span><span class="hljs-params">(napi_env env, <span class="hljs-type">const</span> <span class="hljs-type">char16_t</span>* str, <span class="hljs-type">size_t</span> length, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] str</code>: Character buffer representing a UTF16-LE-encoded string.</li> <li><code>[in] length</code>: The length of the string in two-byte code units, or <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API creates a JavaScript <code>string</code> value from a UTF16-LE-encoded C string. The native string is copied.</p> <p>The JavaScript <code>string</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p> <h5><code>node_api_create_external_string_utf16</code><span><a class="mark" href="#node_api_create_external_string_utf16" id="node_api_create_external_string_utf16">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_create_external_string_utf16"></a></h5> <div class="api_metadata"> <span>Added in: v20.4.0</span> </div> <p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> <pre><code class="language-c">napi_status <span class="hljs-title function_">node_api_create_external_string_utf16</span><span class="hljs-params">(napi_env env, <span class="hljs-type">char16_t</span>* str, <span class="hljs-type">size_t</span> length, napi_finalize finalize_callback, <span class="hljs-type">void</span>* finalize_hint, napi_value* result, <span class="hljs-type">bool</span>* copied)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] str</code>: Character buffer representing a UTF16-LE-encoded string.</li> <li><code>[in] length</code>: The length of the string in two-byte code units, or <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li> <li><code>[in] finalize_callback</code>: The function to call when the string is being collected. The function will be called with the following parameters: <ul> <li><code>[in] env</code>: The environment in which the add-on is running. This value may be null if the string is being collected as part of the termination of the worker or the main Node.js instance.</li> <li><code>[in] data</code>: This is the value <code>str</code> as a <code>void*</code> pointer.</li> <li><code>[in] finalize_hint</code>: This is the value <code>finalize_hint</code> that was given to the API. <a href="#napi_finalize"><code>napi_finalize</code></a> provides more details. This parameter is optional. Passing a null value means that the add-on doesn't need to be notified when the corresponding JavaScript string is collected.</li> </ul> </li> <li><code>[in] finalize_hint</code>: Optional hint to pass to the finalize callback during collection.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li> <li><code>[out] copied</code>: Whether the string was copied. If it was, the finalizer will already have been invoked to destroy <code>str</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API creates a JavaScript <code>string</code> value from a UTF16-LE-encoded C string. The native string may not be copied and must thus exist for the entire life cycle of the JavaScript value.</p> <p>The JavaScript <code>string</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p> <h5><code>napi_create_string_utf8</code><span><a class="mark" href="#napi_create_string_utf8" id="napi_create_string_utf8">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_string_utf8"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_string_utf8</span><span class="hljs-params">(napi_env env, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* str, <span class="hljs-type">size_t</span> length, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] str</code>: Character buffer representing a UTF8-encoded string.</li> <li><code>[in] length</code>: The length of the string in bytes, or <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing a JavaScript <code>string</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API creates a JavaScript <code>string</code> value from a UTF8-encoded C string. The native string is copied.</p> <p>The JavaScript <code>string</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p> <h4>Functions to create optimized property keys<span><a class="mark" href="#functions-to-create-optimized-property-keys" id="functions-to-create-optimized-property-keys">#</a></span><a aria-hidden="true" class="legacy" id="n_api_functions_to_create_optimized_property_keys"></a></h4> <p>Many JavaScript engines including V8 use internalized strings as keys to set and get property values. They typically use a hash table to create and lookup such strings. While it adds some cost per key creation, it improves the performance after that by enabling comparison of string pointers instead of the whole strings.</p> <p>If a new JavaScript string is intended to be used as a property key, then for some JavaScript engines it will be more efficient to use the functions in this section. Otherwise, use the <code>napi_create_string_utf8</code> or <code>node_api_create_external_string_utf8</code> series functions as there may be additional overhead in creating/storing strings with the property key creation methods.</p> <h5><code>node_api_create_property_key_latin1</code><span><a class="mark" href="#node_api_create_property_key_latin1" id="node_api_create_property_key_latin1">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_create_property_key_latin1"></a></h5> <div class="api_metadata"> <span>Added in: v20.18.0</span> </div> <p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> <pre><code class="language-c">napi_status NAPI_CDECL <span class="hljs-title function_">node_api_create_property_key_latin1</span><span class="hljs-params">(napi_env env, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* str, <span class="hljs-type">size_t</span> length, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] str</code>: Character buffer representing an ISO-8859-1-encoded string.</li> <li><code>[in] length</code>: The length of the string in bytes, or <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing an optimized JavaScript <code>string</code> to be used as a property key for objects.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API creates an optimized JavaScript <code>string</code> value from an ISO-8859-1-encoded C string to be used as a property key for objects. The native string is copied. In contrast with <code>napi_create_string_latin1</code>, subsequent calls to this function with the same <code>str</code> pointer may benefit from a speedup in the creation of the requested <code>napi_value</code>, depending on the engine.</p> <p>The JavaScript <code>string</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p> <h5><code>node_api_create_property_key_utf16</code><span><a class="mark" href="#node_api_create_property_key_utf16" id="node_api_create_property_key_utf16">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_create_property_key_utf16"></a></h5> <div class="api_metadata"> <span>Added in: v20.12.0</span> </div> <p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> <pre><code class="language-c">napi_status NAPI_CDECL <span class="hljs-title function_">node_api_create_property_key_utf16</span><span class="hljs-params">(napi_env env, <span class="hljs-type">const</span> <span class="hljs-type">char16_t</span>* str, <span class="hljs-type">size_t</span> length, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] str</code>: Character buffer representing a UTF16-LE-encoded string.</li> <li><code>[in] length</code>: The length of the string in two-byte code units, or <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing an optimized JavaScript <code>string</code> to be used as a property key for objects.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API creates an optimized JavaScript <code>string</code> value from a UTF16-LE-encoded C string to be used as a property key for objects. The native string is copied.</p> <p>The JavaScript <code>string</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p> <h5><code>node_api_create_property_key_utf8</code><span><a class="mark" href="#node_api_create_property_key_utf8" id="node_api_create_property_key_utf8">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_create_property_key_utf8"></a></h5> <div class="api_metadata"> <span>Added in: v20.18.0</span> </div> <p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> <pre><code class="language-c">napi_status NAPI_CDECL <span class="hljs-title function_">node_api_create_property_key_utf8</span><span class="hljs-params">(napi_env env, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* str, <span class="hljs-type">size_t</span> length, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] str</code>: Character buffer representing a UTF8-encoded string.</li> <li><code>[in] length</code>: The length of the string in two-byte code units, or <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing an optimized JavaScript <code>string</code> to be used as a property key for objects.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API creates an optimized JavaScript <code>string</code> value from a UTF8-encoded C string to be used as a property key for objects. The native string is copied.</p> <p>The JavaScript <code>string</code> type is described in <a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type">Section 6.1.4</a> of the ECMAScript Language Specification.</p> <h4>Functions to convert from Node-API to C types<span><a class="mark" href="#functions-to-convert-from-node-api-to-c-types" id="functions-to-convert-from-node-api-to-c-types">#</a></span><a aria-hidden="true" class="legacy" id="n_api_functions_to_convert_from_node_api_to_c_types"></a></h4> <h5><code>napi_get_array_length</code><span><a class="mark" href="#napi_get_array_length" id="napi_get_array_length">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_array_length"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_array_length</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">uint32_t</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: <code>napi_value</code> representing the JavaScript <code>Array</code> whose length is being queried.</li> <li><code>[out] result</code>: <code>uint32</code> representing length of the array.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API returns the length of an array.</p> <p><code>Array</code> length is described in <a href="https://tc39.github.io/ecma262/#sec-properties-of-array-instances-length">Section 22.1.4.1</a> of the ECMAScript Language Specification.</p> <h5><code>napi_get_arraybuffer_info</code><span><a class="mark" href="#napi_get_arraybuffer_info" id="napi_get_arraybuffer_info">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_arraybuffer_info"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_arraybuffer_info</span><span class="hljs-params">(napi_env env, napi_value arraybuffer, <span class="hljs-type">void</span>** data, <span class="hljs-type">size_t</span>* byte_length)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] arraybuffer</code>: <code>napi_value</code> representing the <code>ArrayBuffer</code> being queried.</li> <li><code>[out] data</code>: The underlying data buffer of the <code>ArrayBuffer</code>. If byte_length is <code>0</code>, this may be <code>NULL</code> or any other pointer value.</li> <li><code>[out] byte_length</code>: Length in bytes of the underlying data buffer.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API is used to retrieve the underlying data buffer of an <code>ArrayBuffer</code> and its length.</p> <p><em>WARNING</em>: Use caution while using this API. The lifetime of the underlying data buffer is managed by the <code>ArrayBuffer</code> even after it's returned. A possible safe way to use this API is in conjunction with <a href="#napi_create_reference"><code>napi_create_reference</code></a>, which can be used to guarantee control over the lifetime of the <code>ArrayBuffer</code>. It's also safe to use the returned data buffer within the same callback as long as there are no calls to other APIs that might trigger a GC.</p> <h5><code>napi_get_buffer_info</code><span><a class="mark" href="#napi_get_buffer_info" id="napi_get_buffer_info">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_buffer_info"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_buffer_info</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">void</span>** data, <span class="hljs-type">size_t</span>* length)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: <code>napi_value</code> representing the <code>node::Buffer</code> or <code>Uint8Array</code> being queried.</li> <li><code>[out] data</code>: The underlying data buffer of the <code>node::Buffer</code> or <code>Uint8Array</code>. If length is <code>0</code>, this may be <code>NULL</code> or any other pointer value.</li> <li><code>[out] length</code>: Length in bytes of the underlying data buffer.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This method returns the identical <code>data</code> and <code>byte_length</code> as <a href="#napi_get_typedarray_info"><code>napi_get_typedarray_info</code></a>. And <code>napi_get_typedarray_info</code> accepts a <code>node::Buffer</code> (a Uint8Array) as the value too.</p> <p>This API is used to retrieve the underlying data buffer of a <code>node::Buffer</code> and its length.</p> <p><em>Warning</em>: Use caution while using this API since the underlying data buffer's lifetime is not guaranteed if it's managed by the VM.</p> <h5><code>napi_get_prototype</code><span><a class="mark" href="#napi_get_prototype" id="napi_get_prototype">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_prototype"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_prototype</span><span class="hljs-params">(napi_env env, napi_value object, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] object</code>: <code>napi_value</code> representing JavaScript <code>Object</code> whose prototype to return. This returns the equivalent of <code>Object.getPrototypeOf</code> (which is not the same as the function's <code>prototype</code> property).</li> <li><code>[out] result</code>: <code>napi_value</code> representing prototype of the given object.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <h5><code>napi_get_typedarray_info</code><span><a class="mark" href="#napi_get_typedarray_info" id="napi_get_typedarray_info">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_typedarray_info"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_typedarray_info</span><span class="hljs-params">(napi_env env, napi_value typedarray, napi_typedarray_type* type, <span class="hljs-type">size_t</span>* length, <span class="hljs-type">void</span>** data, napi_value* arraybuffer, <span class="hljs-type">size_t</span>* byte_offset)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] typedarray</code>: <code>napi_value</code> representing the <code>TypedArray</code> whose properties to query.</li> <li><code>[out] type</code>: Scalar datatype of the elements within the <code>TypedArray</code>.</li> <li><code>[out] length</code>: The number of elements in the <code>TypedArray</code>.</li> <li><code>[out] data</code>: The data buffer underlying the <code>TypedArray</code> adjusted by the <code>byte_offset</code> value so that it points to the first element in the <code>TypedArray</code>. If the length of the array is <code>0</code>, this may be <code>NULL</code> or any other pointer value.</li> <li><code>[out] arraybuffer</code>: The <code>ArrayBuffer</code> underlying the <code>TypedArray</code>.</li> <li><code>[out] byte_offset</code>: The byte offset within the underlying native array at which the first element of the arrays is located. The value for the data parameter has already been adjusted so that data points to the first element in the array. Therefore, the first byte of the native array would be at <code>data - byte_offset</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API returns various properties of a typed array.</p> <p>Any of the out parameters may be <code>NULL</code> if that property is unneeded.</p> <p><em>Warning</em>: Use caution while using this API since the underlying data buffer is managed by the VM.</p> <h5><code>napi_get_dataview_info</code><span><a class="mark" href="#napi_get_dataview_info" id="napi_get_dataview_info">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_dataview_info"></a></h5> <div class="api_metadata"> <span>Added in: v8.3.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_dataview_info</span><span class="hljs-params">(napi_env env, napi_value dataview, <span class="hljs-type">size_t</span>* byte_length, <span class="hljs-type">void</span>** data, napi_value* arraybuffer, <span class="hljs-type">size_t</span>* byte_offset)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] dataview</code>: <code>napi_value</code> representing the <code>DataView</code> whose properties to query.</li> <li><code>[out] byte_length</code>: Number of bytes in the <code>DataView</code>.</li> <li><code>[out] data</code>: The data buffer underlying the <code>DataView</code>. If byte_length is <code>0</code>, this may be <code>NULL</code> or any other pointer value.</li> <li><code>[out] arraybuffer</code>: <code>ArrayBuffer</code> underlying the <code>DataView</code>.</li> <li><code>[out] byte_offset</code>: The byte offset within the data buffer from which to start projecting the <code>DataView</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>Any of the out parameters may be <code>NULL</code> if that property is unneeded.</p> <p>This API returns various properties of a <code>DataView</code>.</p> <h5><code>napi_get_date_value</code><span><a class="mark" href="#napi_get_date_value" id="napi_get_date_value">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_date_value"></a></h5> <div class="api_metadata"> <span>Added in: v11.11.0, v10.17.0</span> <span>N-API version: 5</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_date_value</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">double</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: <code>napi_value</code> representing a JavaScript <code>Date</code>.</li> <li><code>[out] result</code>: Time value as a <code>double</code> represented as milliseconds since midnight at the beginning of 01 January, 1970 UTC.</li> </ul> <p>This API does not observe leap seconds; they are ignored, as ECMAScript aligns with POSIX time specification.</p> <p>Returns <code>napi_ok</code> if the API succeeded. If a non-date <code>napi_value</code> is passed in it returns <code>napi_date_expected</code>.</p> <p>This API returns the C double primitive of time value for the given JavaScript <code>Date</code>.</p> <h5><code>napi_get_value_bool</code><span><a class="mark" href="#napi_get_value_bool" id="napi_get_value_bool">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_bool"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bool</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>Boolean</code>.</li> <li><code>[out] result</code>: C boolean primitive equivalent of the given JavaScript <code>Boolean</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded. If a non-boolean <code>napi_value</code> is passed in it returns <code>napi_boolean_expected</code>.</p> <p>This API returns the C boolean primitive equivalent of the given JavaScript <code>Boolean</code>.</p> <h5><code>napi_get_value_double</code><span><a class="mark" href="#napi_get_value_double" id="napi_get_value_double">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_double"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_double</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">double</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li> <li><code>[out] result</code>: C double primitive equivalent of the given JavaScript <code>number</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code> is passed in it returns <code>napi_number_expected</code>.</p> <p>This API returns the C double primitive equivalent of the given JavaScript <code>number</code>.</p> <h5><code>napi_get_value_bigint_int64</code><span><a class="mark" href="#napi_get_value_bigint_int64" id="napi_get_value_bigint_int64">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_bigint_int64"></a></h5> <div class="api_metadata"> <span>Added in: v10.7.0</span> <span>N-API version: 6</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_int64</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">int64_t</span>* result, <span class="hljs-type">bool</span>* lossless)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under</li> <li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>BigInt</code>.</li> <li><code>[out] result</code>: C <code>int64_t</code> primitive equivalent of the given JavaScript <code>BigInt</code>.</li> <li><code>[out] lossless</code>: Indicates whether the <code>BigInt</code> value was converted losslessly.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>BigInt</code> is passed in it returns <code>napi_bigint_expected</code>.</p> <p>This API returns the C <code>int64_t</code> primitive equivalent of the given JavaScript <code>BigInt</code>. If needed it will truncate the value, setting <code>lossless</code> to <code>false</code>.</p> <h5><code>napi_get_value_bigint_uint64</code><span><a class="mark" href="#napi_get_value_bigint_uint64" id="napi_get_value_bigint_uint64">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_bigint_uint64"></a></h5> <div class="api_metadata"> <span>Added in: v10.7.0</span> <span>N-API version: 6</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_uint64</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">uint64_t</span>* result, <span class="hljs-type">bool</span>* lossless)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>BigInt</code>.</li> <li><code>[out] result</code>: C <code>uint64_t</code> primitive equivalent of the given JavaScript <code>BigInt</code>.</li> <li><code>[out] lossless</code>: Indicates whether the <code>BigInt</code> value was converted losslessly.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>BigInt</code> is passed in it returns <code>napi_bigint_expected</code>.</p> <p>This API returns the C <code>uint64_t</code> primitive equivalent of the given JavaScript <code>BigInt</code>. If needed it will truncate the value, setting <code>lossless</code> to <code>false</code>.</p> <h5><code>napi_get_value_bigint_words</code><span><a class="mark" href="#napi_get_value_bigint_words" id="napi_get_value_bigint_words">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_bigint_words"></a></h5> <div class="api_metadata"> <span>Added in: v10.7.0</span> <span>N-API version: 6</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_bigint_words</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">int</span>* sign_bit, <span class="hljs-type">size_t</span>* word_count, <span class="hljs-type">uint64_t</span>* words)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>BigInt</code>.</li> <li><code>[out] sign_bit</code>: Integer representing if the JavaScript <code>BigInt</code> is positive or negative.</li> <li><code>[in/out] word_count</code>: Must be initialized to the length of the <code>words</code> array. Upon return, it will be set to the actual number of words that would be needed to store this <code>BigInt</code>.</li> <li><code>[out] words</code>: Pointer to a pre-allocated 64-bit word array.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API converts a single <code>BigInt</code> value into a sign bit, 64-bit little-endian array, and the number of elements in the array. <code>sign_bit</code> and <code>words</code> may be both set to <code>NULL</code>, in order to get only <code>word_count</code>.</p> <h5><code>napi_get_value_external</code><span><a class="mark" href="#napi_get_value_external" id="napi_get_value_external">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_external"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_external</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">void</span>** result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: <code>napi_value</code> representing JavaScript external value.</li> <li><code>[out] result</code>: Pointer to the data wrapped by the JavaScript external value.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded. If a non-external <code>napi_value</code> is passed in it returns <code>napi_invalid_arg</code>.</p> <p>This API retrieves the external data pointer that was previously passed to <code>napi_create_external()</code>.</p> <h5><code>napi_get_value_int32</code><span><a class="mark" href="#napi_get_value_int32" id="napi_get_value_int32">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_int32"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_int32</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">int32_t</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li> <li><code>[out] result</code>: C <code>int32</code> primitive equivalent of the given JavaScript <code>number</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code> is passed in <code>napi_number_expected</code>.</p> <p>This API returns the C <code>int32</code> primitive equivalent of the given JavaScript <code>number</code>.</p> <p>If the number exceeds the range of the 32 bit integer, then the result is truncated to the equivalent of the bottom 32 bits. This can result in a large positive number becoming a negative number if the value is > 2<sup>31</sup> - 1.</p> <p>Non-finite number values (<code>NaN</code>, <code>+Infinity</code>, or <code>-Infinity</code>) set the result to zero.</p> <h5><code>napi_get_value_int64</code><span><a class="mark" href="#napi_get_value_int64" id="napi_get_value_int64">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_int64"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_int64</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">int64_t</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li> <li><code>[out] result</code>: C <code>int64</code> primitive equivalent of the given JavaScript <code>number</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code> is passed in it returns <code>napi_number_expected</code>.</p> <p>This API returns the C <code>int64</code> primitive equivalent of the given JavaScript <code>number</code>.</p> <p><code>number</code> values outside the range of <a href="https://tc39.github.io/ecma262/#sec-number.min_safe_integer"><code>Number.MIN_SAFE_INTEGER</code></a> <code>-(2**53 - 1)</code> - <a href="https://tc39.github.io/ecma262/#sec-number.max_safe_integer"><code>Number.MAX_SAFE_INTEGER</code></a> <code>(2**53 - 1)</code> will lose precision.</p> <p>Non-finite number values (<code>NaN</code>, <code>+Infinity</code>, or <code>-Infinity</code>) set the result to zero.</p> <h5><code>napi_get_value_string_latin1</code><span><a class="mark" href="#napi_get_value_string_latin1" id="napi_get_value_string_latin1">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_string_latin1"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_latin1</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">char</span>* buf, <span class="hljs-type">size_t</span> bufsize, <span class="hljs-type">size_t</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: <code>napi_value</code> representing JavaScript string.</li> <li><code>[in] buf</code>: Buffer to write the ISO-8859-1-encoded string into. If <code>NULL</code> is passed in, the length of the string in bytes and excluding the null terminator is returned in <code>result</code>.</li> <li><code>[in] bufsize</code>: Size of the destination buffer. When this value is insufficient, the returned string is truncated and null-terminated.</li> <li><code>[out] result</code>: Number of bytes copied into the buffer, excluding the null terminator.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value</code> is passed in it returns <code>napi_string_expected</code>.</p> <p>This API returns the ISO-8859-1-encoded string corresponding the value passed in.</p> <h5><code>napi_get_value_string_utf8</code><span><a class="mark" href="#napi_get_value_string_utf8" id="napi_get_value_string_utf8">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_string_utf8"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_utf8</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">char</span>* buf, <span class="hljs-type">size_t</span> bufsize, <span class="hljs-type">size_t</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: <code>napi_value</code> representing JavaScript string.</li> <li><code>[in] buf</code>: Buffer to write the UTF8-encoded string into. If <code>NULL</code> is passed in, the length of the string in bytes and excluding the null terminator is returned in <code>result</code>.</li> <li><code>[in] bufsize</code>: Size of the destination buffer. When this value is insufficient, the returned string is truncated and null-terminated.</li> <li><code>[out] result</code>: Number of bytes copied into the buffer, excluding the null terminator.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value</code> is passed in it returns <code>napi_string_expected</code>.</p> <p>This API returns the UTF8-encoded string corresponding the value passed in.</p> <h5><code>napi_get_value_string_utf16</code><span><a class="mark" href="#napi_get_value_string_utf16" id="napi_get_value_string_utf16">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_string_utf16"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_string_utf16</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">char16_t</span>* buf, <span class="hljs-type">size_t</span> bufsize, <span class="hljs-type">size_t</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: <code>napi_value</code> representing JavaScript string.</li> <li><code>[in] buf</code>: Buffer to write the UTF16-LE-encoded string into. If <code>NULL</code> is passed in, the length of the string in 2-byte code units and excluding the null terminator is returned.</li> <li><code>[in] bufsize</code>: Size of the destination buffer. When this value is insufficient, the returned string is truncated and null-terminated.</li> <li><code>[out] result</code>: Number of 2-byte code units copied into the buffer, excluding the null terminator.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded. If a non-<code>string</code> <code>napi_value</code> is passed in it returns <code>napi_string_expected</code>.</p> <p>This API returns the UTF16-encoded string corresponding the value passed in.</p> <h5><code>napi_get_value_uint32</code><span><a class="mark" href="#napi_get_value_uint32" id="napi_get_value_uint32">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_value_uint32"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_value_uint32</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">uint32_t</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: <code>napi_value</code> representing JavaScript <code>number</code>.</li> <li><code>[out] result</code>: C primitive equivalent of the given <code>napi_value</code> as a <code>uint32_t</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded. If a non-number <code>napi_value</code> is passed in it returns <code>napi_number_expected</code>.</p> <p>This API returns the C primitive equivalent of the given <code>napi_value</code> as a <code>uint32_t</code>.</p> <h4>Functions to get global instances<span><a class="mark" href="#functions-to-get-global-instances" id="functions-to-get-global-instances">#</a></span><a aria-hidden="true" class="legacy" id="n_api_functions_to_get_global_instances"></a></h4> <h5><code>napi_get_boolean</code><span><a class="mark" href="#napi_get_boolean" id="napi_get_boolean">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_boolean"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_boolean</span><span class="hljs-params">(napi_env env, <span class="hljs-type">bool</span> value, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: The value of the boolean to retrieve.</li> <li><code>[out] result</code>: <code>napi_value</code> representing JavaScript <code>Boolean</code> singleton to retrieve.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API is used to return the JavaScript singleton object that is used to represent the given boolean value.</p> <h5><code>napi_get_global</code><span><a class="mark" href="#napi_get_global" id="napi_get_global">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_global"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_global</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[out] result</code>: <code>napi_value</code> representing JavaScript <code>global</code> object.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API returns the <code>global</code> object.</p> <h5><code>napi_get_null</code><span><a class="mark" href="#napi_get_null" id="napi_get_null">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_null"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_null</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[out] result</code>: <code>napi_value</code> representing JavaScript <code>null</code> object.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API returns the <code>null</code> object.</p> <h5><code>napi_get_undefined</code><span><a class="mark" href="#napi_get_undefined" id="napi_get_undefined">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_undefined"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_undefined</span><span class="hljs-params">(napi_env env, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[out] result</code>: <code>napi_value</code> representing JavaScript Undefined value.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API returns the Undefined object.</p> </section><section><h3>Working with JavaScript values and abstract operations<span><a class="mark" href="#working-with-javascript-values-and-abstract-operations" id="working-with-javascript-values-and-abstract-operations">#</a></span><a aria-hidden="true" class="legacy" id="n_api_working_with_javascript_values_and_abstract_operations"></a></h3> <p>Node-API exposes a set of APIs to perform some abstract operations on JavaScript values. Some of these operations are documented under <a href="https://tc39.github.io/ecma262/#sec-abstract-operations">Section 7</a> of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p> <p>These APIs support doing one of the following:</p> <ol> <li>Coerce JavaScript values to specific JavaScript types (such as <code>number</code> or <code>string</code>).</li> <li>Check the type of a JavaScript value.</li> <li>Check for equality between two JavaScript values.</li> </ol> <h4><code>napi_coerce_to_bool</code><span><a class="mark" href="#napi_coerce_to_bool" id="napi_coerce_to_bool">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_coerce_to_bool"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_bool</span><span class="hljs-params">(napi_env env, napi_value value, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: The JavaScript value to coerce.</li> <li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>Boolean</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API implements the abstract operation <code>ToBoolean()</code> as defined in <a href="https://tc39.github.io/ecma262/#sec-toboolean">Section 7.1.2</a> of the ECMAScript Language Specification.</p> <h4><code>napi_coerce_to_number</code><span><a class="mark" href="#napi_coerce_to_number" id="napi_coerce_to_number">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_coerce_to_number"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_number</span><span class="hljs-params">(napi_env env, napi_value value, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: The JavaScript value to coerce.</li> <li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>number</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API implements the abstract operation <code>ToNumber()</code> as defined in <a href="https://tc39.github.io/ecma262/#sec-tonumber">Section 7.1.3</a> of the ECMAScript Language Specification. This function potentially runs JS code if the passed-in value is an object.</p> <h4><code>napi_coerce_to_object</code><span><a class="mark" href="#napi_coerce_to_object" id="napi_coerce_to_object">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_coerce_to_object"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_object</span><span class="hljs-params">(napi_env env, napi_value value, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: The JavaScript value to coerce.</li> <li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>Object</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API implements the abstract operation <code>ToObject()</code> as defined in <a href="https://tc39.github.io/ecma262/#sec-toobject">Section 7.1.13</a> of the ECMAScript Language Specification.</p> <h4><code>napi_coerce_to_string</code><span><a class="mark" href="#napi_coerce_to_string" id="napi_coerce_to_string">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_coerce_to_string"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_coerce_to_string</span><span class="hljs-params">(napi_env env, napi_value value, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: The JavaScript value to coerce.</li> <li><code>[out] result</code>: <code>napi_value</code> representing the coerced JavaScript <code>string</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API implements the abstract operation <code>ToString()</code> as defined in <a href="https://tc39.github.io/ecma262/#sec-toobject">Section 7.1.13</a> of the ECMAScript Language Specification. This function potentially runs JS code if the passed-in value is an object.</p> <h4><code>napi_typeof</code><span><a class="mark" href="#napi_typeof" id="napi_typeof">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_typeof"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_typeof</span><span class="hljs-params">(napi_env env, napi_value value, napi_valuetype* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: The JavaScript value whose type to query.</li> <li><code>[out] result</code>: The type of the JavaScript value.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <ul> <li><code>napi_invalid_arg</code> if the type of <code>value</code> is not a known ECMAScript type and <code>value</code> is not an External value.</li> </ul> <p>This API represents behavior similar to invoking the <code>typeof</code> Operator on the object as defined in <a href="https://tc39.github.io/ecma262/#sec-typeof-operator">Section 12.5.5</a> of the ECMAScript Language Specification. However, there are some differences:</p> <ol> <li>It has support for detecting an External value.</li> <li>It detects <code>null</code> as a separate type, while ECMAScript <code>typeof</code> would detect <code>object</code>.</li> </ol> <p>If <code>value</code> has a type that is invalid, an error is returned.</p> <h4><code>napi_instanceof</code><span><a class="mark" href="#napi_instanceof" id="napi_instanceof">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_instanceof"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_instanceof</span><span class="hljs-params">(napi_env env, napi_value object, napi_value constructor, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] object</code>: The JavaScript value to check.</li> <li><code>[in] constructor</code>: The JavaScript function object of the constructor function to check against.</li> <li><code>[out] result</code>: Boolean that is set to true if <code>object instanceof constructor</code> is true.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API represents invoking the <code>instanceof</code> Operator on the object as defined in <a href="https://tc39.github.io/ecma262/#sec-instanceofoperator">Section 12.10.4</a> of the ECMAScript Language Specification.</p> <h4><code>napi_is_array</code><span><a class="mark" href="#napi_is_array" id="napi_is_array">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_array"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_array</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: The JavaScript value to check.</li> <li><code>[out] result</code>: Whether the given object is an array.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API represents invoking the <code>IsArray</code> operation on the object as defined in <a href="https://tc39.github.io/ecma262/#sec-isarray">Section 7.2.2</a> of the ECMAScript Language Specification.</p> <h4><code>napi_is_arraybuffer</code><span><a class="mark" href="#napi_is_arraybuffer" id="napi_is_arraybuffer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_arraybuffer"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_arraybuffer</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: The JavaScript value to check.</li> <li><code>[out] result</code>: Whether the given object is an <code>ArrayBuffer</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API checks if the <code>Object</code> passed in is an array buffer.</p> <h4><code>napi_is_buffer</code><span><a class="mark" href="#napi_is_buffer" id="napi_is_buffer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_buffer"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_buffer</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: The JavaScript value to check.</li> <li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a <code>node::Buffer</code> or <code>Uint8Array</code> object.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API checks if the <code>Object</code> passed in is a buffer or Uint8Array. <a href="#napi_is_typedarray"><code>napi_is_typedarray</code></a> should be preferred if the caller needs to check if the value is a Uint8Array.</p> <h4><code>napi_is_date</code><span><a class="mark" href="#napi_is_date" id="napi_is_date">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_date"></a></h4> <div class="api_metadata"> <span>Added in: v11.11.0, v10.17.0</span> <span>N-API version: 5</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_date</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: The JavaScript value to check.</li> <li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a JavaScript <code>Date</code> object.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API checks if the <code>Object</code> passed in is a date.</p> <h4><code>napi_is_error</code><span><a class="mark" href="#napi_is_error_1" id="napi_is_error_1">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_error_1"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_error</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: The JavaScript value to check.</li> <li><code>[out] result</code>: Whether the given <code>napi_value</code> represents an <code>Error</code> object.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API checks if the <code>Object</code> passed in is an <code>Error</code>.</p> <h4><code>napi_is_typedarray</code><span><a class="mark" href="#napi_is_typedarray" id="napi_is_typedarray">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_typedarray"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_typedarray</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: The JavaScript value to check.</li> <li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a <code>TypedArray</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API checks if the <code>Object</code> passed in is a typed array.</p> <h4><code>napi_is_dataview</code><span><a class="mark" href="#napi_is_dataview" id="napi_is_dataview">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_dataview"></a></h4> <div class="api_metadata"> <span>Added in: v8.3.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_dataview</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: The JavaScript value to check.</li> <li><code>[out] result</code>: Whether the given <code>napi_value</code> represents a <code>DataView</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API checks if the <code>Object</code> passed in is a <code>DataView</code>.</p> <h4><code>napi_strict_equals</code><span><a class="mark" href="#napi_strict_equals" id="napi_strict_equals">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_strict_equals"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_strict_equals</span><span class="hljs-params">(napi_env env, napi_value lhs, napi_value rhs, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] lhs</code>: The JavaScript value to check.</li> <li><code>[in] rhs</code>: The JavaScript value to check against.</li> <li><code>[out] result</code>: Whether the two <code>napi_value</code> objects are equal.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API represents the invocation of the Strict Equality algorithm as defined in <a href="https://tc39.github.io/ecma262/#sec-strict-equality-comparison">Section 7.2.14</a> of the ECMAScript Language Specification.</p> <h4><code>napi_detach_arraybuffer</code><span><a class="mark" href="#napi_detach_arraybuffer" id="napi_detach_arraybuffer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_detach_arraybuffer"></a></h4> <div class="api_metadata"> <span>Added in: v13.0.0, v12.16.0, v10.22.0</span> <span>N-API version: 7</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_detach_arraybuffer</span><span class="hljs-params">(napi_env env, napi_value arraybuffer)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] arraybuffer</code>: The JavaScript <code>ArrayBuffer</code> to be detached.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded. If a non-detachable <code>ArrayBuffer</code> is passed in it returns <code>napi_detachable_arraybuffer_expected</code>.</p> <p>Generally, an <code>ArrayBuffer</code> is non-detachable if it has been detached before. The engine may impose additional conditions on whether an <code>ArrayBuffer</code> is detachable. For example, V8 requires that the <code>ArrayBuffer</code> be external, that is, created with <a href="#napi_create_external_arraybuffer"><code>napi_create_external_arraybuffer</code></a>.</p> <p>This API represents the invocation of the <code>ArrayBuffer</code> detach operation as defined in <a href="https://tc39.es/ecma262/#sec-detacharraybuffer">Section 24.1.1.3</a> of the ECMAScript Language Specification.</p> <h4><code>napi_is_detached_arraybuffer</code><span><a class="mark" href="#napi_is_detached_arraybuffer" id="napi_is_detached_arraybuffer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_detached_arraybuffer"></a></h4> <div class="api_metadata"> <span>Added in: v13.3.0, v12.16.0, v10.22.0</span> <span>N-API version: 7</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_detached_arraybuffer</span><span class="hljs-params">(napi_env env, napi_value arraybuffer, <span class="hljs-type">bool</span>* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] arraybuffer</code>: The JavaScript <code>ArrayBuffer</code> to be checked.</li> <li><code>[out] result</code>: Whether the <code>arraybuffer</code> is detached.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>The <code>ArrayBuffer</code> is considered detached if its internal data is <code>null</code>.</p> <p>This API represents the invocation of the <code>ArrayBuffer</code> <code>IsDetachedBuffer</code> operation as defined in <a href="https://tc39.es/ecma262/#sec-isdetachedbuffer">Section 24.1.1.2</a> of the ECMAScript Language Specification.</p> </section><section><h3>Working with JavaScript properties<span><a class="mark" href="#working-with-javascript-properties" id="working-with-javascript-properties">#</a></span><a aria-hidden="true" class="legacy" id="n_api_working_with_javascript_properties"></a></h3> <p>Node-API exposes a set of APIs to get and set properties on JavaScript objects. Some of these types are documented under <a href="https://tc39.github.io/ecma262/#sec-abstract-operations">Section 7</a> of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</p> <p>Properties in JavaScript are represented as a tuple of a key and a value. Fundamentally, all property keys in Node-API can be represented in one of the following forms:</p> <ul> <li>Named: a simple UTF8-encoded string</li> <li>Integer-Indexed: an index value represented by <code>uint32_t</code></li> <li>JavaScript value: these are represented in Node-API by <code>napi_value</code>. This can be a <code>napi_value</code> representing a <code>string</code>, <code>number</code>, or <code>symbol</code>.</li> </ul> <p>Node-API values are represented by the type <code>napi_value</code>. Any Node-API call that requires a JavaScript value takes in a <code>napi_value</code>. However, it's the caller's responsibility to make sure that the <code>napi_value</code> in question is of the JavaScript type expected by the API.</p> <p>The APIs documented in this section provide a simple interface to get and set properties on arbitrary JavaScript objects represented by <code>napi_value</code>.</p> <p>For instance, consider the following JavaScript code snippet:</p> <pre><code class="language-js"><span class="hljs-keyword">const</span> obj = {}; obj.<span class="hljs-property">myProp</span> = <span class="hljs-number">123</span>;</code> <button class="copy-button">copy</button></pre> <p>The equivalent can be done using Node-API values with the following snippet:</p> <pre><code class="language-c">napi_status status = napi_generic_failure; <span class="hljs-comment">// const obj = {}</span> napi_value obj, value; status = napi_create_object(env, &obj); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status; <span class="hljs-comment">// Create a napi_value for 123</span> status = napi_create_int32(env, <span class="hljs-number">123</span>, &value); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status; <span class="hljs-comment">// obj.myProp = 123</span> status = napi_set_named_property(env, obj, <span class="hljs-string">"myProp"</span>, value); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code> <button class="copy-button">copy</button></pre> <p>Indexed properties can be set in a similar manner. Consider the following JavaScript snippet:</p> <pre><code class="language-js"><span class="hljs-keyword">const</span> arr = []; arr[<span class="hljs-number">123</span>] = <span class="hljs-string">'hello'</span>;</code> <button class="copy-button">copy</button></pre> <p>The equivalent can be done using Node-API values with the following snippet:</p> <pre><code class="language-c">napi_status status = napi_generic_failure; <span class="hljs-comment">// const arr = [];</span> napi_value arr, value; status = napi_create_array(env, &arr); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status; <span class="hljs-comment">// Create a napi_value for 'hello'</span> status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &value); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status; <span class="hljs-comment">// arr[123] = 'hello';</span> status = napi_set_element(env, arr, <span class="hljs-number">123</span>, value); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code> <button class="copy-button">copy</button></pre> <p>Properties can be retrieved using the APIs described in this section. Consider the following JavaScript snippet:</p> <pre><code class="language-js"><span class="hljs-keyword">const</span> arr = []; <span class="hljs-keyword">const</span> value = arr[<span class="hljs-number">123</span>];</code> <button class="copy-button">copy</button></pre> <p>The following is the approximate equivalent of the Node-API counterpart:</p> <pre><code class="language-c">napi_status status = napi_generic_failure; <span class="hljs-comment">// const arr = []</span> napi_value arr, value; status = napi_create_array(env, &arr); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status; <span class="hljs-comment">// const value = arr[123]</span> status = napi_get_element(env, arr, <span class="hljs-number">123</span>, &value); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code> <button class="copy-button">copy</button></pre> <p>Finally, multiple properties can also be defined on an object for performance reasons. Consider the following JavaScript:</p> <pre><code class="language-js"><span class="hljs-keyword">const</span> obj = {}; <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">defineProperties</span>(obj, { <span class="hljs-string">'foo'</span>: { <span class="hljs-attr">value</span>: <span class="hljs-number">123</span>, <span class="hljs-attr">writable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">configurable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">enumerable</span>: <span class="hljs-literal">true</span> }, <span class="hljs-string">'bar'</span>: { <span class="hljs-attr">value</span>: <span class="hljs-number">456</span>, <span class="hljs-attr">writable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">configurable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">enumerable</span>: <span class="hljs-literal">true</span> }, });</code> <button class="copy-button">copy</button></pre> <p>The following is the approximate equivalent of the Node-API counterpart:</p> <pre><code class="language-c">napi_status status = napi_status_generic_failure; <span class="hljs-comment">// const obj = {};</span> napi_value obj; status = napi_create_object(env, &obj); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status; <span class="hljs-comment">// Create napi_values for 123 and 456</span> napi_value fooValue, barValue; status = napi_create_int32(env, <span class="hljs-number">123</span>, &fooValue); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status; status = napi_create_int32(env, <span class="hljs-number">456</span>, &barValue); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status; <span class="hljs-comment">// Set the properties</span> napi_property_descriptor descriptors[] = { { <span class="hljs-string">"foo"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, fooValue, napi_writable | napi_configurable, <span class="hljs-literal">NULL</span> }, { <span class="hljs-string">"bar"</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, barValue, napi_writable | napi_configurable, <span class="hljs-literal">NULL</span> } } status = napi_define_properties(env, obj, <span class="hljs-keyword">sizeof</span>(descriptors) / <span class="hljs-keyword">sizeof</span>(descriptors[<span class="hljs-number">0</span>]), descriptors); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> status;</code> <button class="copy-button">copy</button></pre> <h4>Structures<span><a class="mark" href="#structures" id="structures">#</a></span><a aria-hidden="true" class="legacy" id="n_api_structures"></a></h4> <h5><code>napi_property_attributes</code><span><a class="mark" href="#napi_property_attributes" id="napi_property_attributes">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_property_attributes"></a></h5> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.12.0</td> <td><p>added <code>napi_default_method</code> and <code>napi_default_property</code>.</p></td></tr> </tbody></table> </details> </div> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> {</span> napi_default = <span class="hljs-number">0</span>, napi_writable = <span class="hljs-number">1</span> << <span class="hljs-number">0</span>, napi_enumerable = <span class="hljs-number">1</span> << <span class="hljs-number">1</span>, napi_configurable = <span class="hljs-number">1</span> << <span class="hljs-number">2</span>, <span class="hljs-comment">// Used with napi_define_class to distinguish static properties</span> <span class="hljs-comment">// from instance properties. Ignored by napi_define_properties.</span> napi_static = <span class="hljs-number">1</span> << <span class="hljs-number">10</span>, <span class="hljs-comment">// Default for class methods.</span> napi_default_method = napi_writable | napi_configurable, <span class="hljs-comment">// Default for object properties, like in JS obj[prop].</span> napi_default_jsproperty = napi_writable | napi_enumerable | napi_configurable, } napi_property_attributes;</code> <button class="copy-button">copy</button></pre> <p><code>napi_property_attributes</code> are flags used to control the behavior of properties set on a JavaScript object. Other than <code>napi_static</code> they correspond to the attributes listed in <a href="https://tc39.github.io/ecma262/#table-2">Section 6.1.7.1</a> of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>. They can be one or more of the following bitflags:</p> <ul> <li><code>napi_default</code>: No explicit attributes are set on the property. By default, a property is read only, not enumerable and not configurable.</li> <li><code>napi_writable</code>: The property is writable.</li> <li><code>napi_enumerable</code>: The property is enumerable.</li> <li><code>napi_configurable</code>: The property is configurable as defined in <a href="https://tc39.github.io/ecma262/#table-2">Section 6.1.7.1</a> of the <a href="https://tc39.github.io/ecma262/">ECMAScript Language Specification</a>.</li> <li><code>napi_static</code>: The property will be defined as a static property on a class as opposed to an instance property, which is the default. This is used only by <a href="#napi_define_class"><code>napi_define_class</code></a>. It is ignored by <code>napi_define_properties</code>.</li> <li><code>napi_default_method</code>: Like a method in a JS class, the property is configurable and writeable, but not enumerable.</li> <li><code>napi_default_jsproperty</code>: Like a property set via assignment in JavaScript, the property is writable, enumerable, and configurable.</li> </ul> <h5><code>napi_property_descriptor</code><span><a class="mark" href="#napi_property_descriptor" id="napi_property_descriptor">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_property_descriptor"></a></h5> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span> <span class="hljs-comment">// One of utf8name or name should be NULL.</span> <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8name; napi_value name; napi_callback method; napi_callback getter; napi_callback setter; napi_value value; napi_property_attributes attributes; <span class="hljs-type">void</span>* data; } napi_property_descriptor;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>utf8name</code>: Optional string describing the key for the property, encoded as UTF8. One of <code>utf8name</code> or <code>name</code> must be provided for the property.</li> <li><code>name</code>: Optional <code>napi_value</code> that points to a JavaScript string or symbol to be used as the key for the property. One of <code>utf8name</code> or <code>name</code> must be provided for the property.</li> <li><code>value</code>: The value that's retrieved by a get access of the property if the property is a data property. If this is passed in, set <code>getter</code>, <code>setter</code>, <code>method</code> and <code>data</code> to <code>NULL</code> (since these members won't be used).</li> <li><code>getter</code>: A function to call when a get access of the property is performed. If this is passed in, set <code>value</code> and <code>method</code> to <code>NULL</code> (since these members won't be used). The given function is called implicitly by the runtime when the property is accessed from JavaScript code (or if a get on the property is performed using a Node-API call). <a href="#napi_callback"><code>napi_callback</code></a> provides more details.</li> <li><code>setter</code>: A function to call when a set access of the property is performed. If this is passed in, set <code>value</code> and <code>method</code> to <code>NULL</code> (since these members won't be used). The given function is called implicitly by the runtime when the property is set from JavaScript code (or if a set on the property is performed using a Node-API call). <a href="#napi_callback"><code>napi_callback</code></a> provides more details.</li> <li><code>method</code>: Set this to make the property descriptor object's <code>value</code> property to be a JavaScript function represented by <code>method</code>. If this is passed in, set <code>value</code>, <code>getter</code> and <code>setter</code> to <code>NULL</code> (since these members won't be used). <a href="#napi_callback"><code>napi_callback</code></a> provides more details.</li> <li><code>attributes</code>: The attributes associated with the particular property. See <a href="#napi_property_attributes"><code>napi_property_attributes</code></a>.</li> <li><code>data</code>: The callback data passed into <code>method</code>, <code>getter</code> and <code>setter</code> if this function is invoked.</li> </ul> <h4>Functions<span><a class="mark" href="#functions" id="functions">#</a></span><a aria-hidden="true" class="legacy" id="n_api_functions"></a></h4> <h5><code>napi_get_property_names</code><span><a class="mark" href="#napi_get_property_names" id="napi_get_property_names">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_property_names"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_property_names</span><span class="hljs-params">(napi_env env, napi_value object, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[in] object</code>: The object from which to retrieve the properties.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing an array of JavaScript values that represent the property names of the object. The API can be used to iterate over <code>result</code> using <a href="#napi_get_array_length"><code>napi_get_array_length</code></a> and <a href="#napi_get_element"><code>napi_get_element</code></a>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API returns the names of the enumerable properties of <code>object</code> as an array of strings. The properties of <code>object</code> whose key is a symbol will not be included.</p> <h5><code>napi_get_all_property_names</code><span><a class="mark" href="#napi_get_all_property_names" id="napi_get_all_property_names">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_all_property_names"></a></h5> <div class="api_metadata"> <span>Added in: v13.7.0, v12.17.0, v10.20.0</span> <span>N-API version: 6</span> </div> <pre><code class="language-c">napi_get_all_property_names(napi_env env, napi_value object, napi_key_collection_mode key_mode, napi_key_filter key_filter, napi_key_conversion key_conversion, napi_value* result);</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[in] object</code>: The object from which to retrieve the properties.</li> <li><code>[in] key_mode</code>: Whether to retrieve prototype properties as well.</li> <li><code>[in] key_filter</code>: Which properties to retrieve (enumerable/readable/writable).</li> <li><code>[in] key_conversion</code>: Whether to convert numbered property keys to strings.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing an array of JavaScript values that represent the property names of the object. <a href="#napi_get_array_length"><code>napi_get_array_length</code></a> and <a href="#napi_get_element"><code>napi_get_element</code></a> can be used to iterate over <code>result</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API returns an array containing the names of the available properties of this object.</p> <h5><code>napi_set_property</code><span><a class="mark" href="#napi_set_property" id="napi_set_property">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_set_property"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_property</span><span class="hljs-params">(napi_env env, napi_value object, napi_value key, napi_value value)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[in] object</code>: The object on which to set the property.</li> <li><code>[in] key</code>: The name of the property to set.</li> <li><code>[in] value</code>: The property value.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API set a property on the <code>Object</code> passed in.</p> <h5><code>napi_get_property</code><span><a class="mark" href="#napi_get_property" id="napi_get_property">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_property"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_property</span><span class="hljs-params">(napi_env env, napi_value object, napi_value key, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[in] object</code>: The object from which to retrieve the property.</li> <li><code>[in] key</code>: The name of the property to retrieve.</li> <li><code>[out] result</code>: The value of the property.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API gets the requested property from the <code>Object</code> passed in.</p> <h5><code>napi_has_property</code><span><a class="mark" href="#napi_has_property" id="napi_has_property">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_has_property"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_property</span><span class="hljs-params">(napi_env env, napi_value object, napi_value key, <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[in] object</code>: The object to query.</li> <li><code>[in] key</code>: The name of the property whose existence to check.</li> <li><code>[out] result</code>: Whether the property exists on the object or not.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API checks if the <code>Object</code> passed in has the named property.</p> <h5><code>napi_delete_property</code><span><a class="mark" href="#napi_delete_property" id="napi_delete_property">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_delete_property"></a></h5> <div class="api_metadata"> <span>Added in: v8.2.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_property</span><span class="hljs-params">(napi_env env, napi_value object, napi_value key, <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[in] object</code>: The object to query.</li> <li><code>[in] key</code>: The name of the property to delete.</li> <li><code>[out] result</code>: Whether the property deletion succeeded or not. <code>result</code> can optionally be ignored by passing <code>NULL</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API attempts to delete the <code>key</code> own property from <code>object</code>.</p> <h5><code>napi_has_own_property</code><span><a class="mark" href="#napi_has_own_property" id="napi_has_own_property">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_has_own_property"></a></h5> <div class="api_metadata"> <span>Added in: v8.2.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_own_property</span><span class="hljs-params">(napi_env env, napi_value object, napi_value key, <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[in] object</code>: The object to query.</li> <li><code>[in] key</code>: The name of the own property whose existence to check.</li> <li><code>[out] result</code>: Whether the own property exists on the object or not.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API checks if the <code>Object</code> passed in has the named own property. <code>key</code> must be a <code>string</code> or a <code>symbol</code>, or an error will be thrown. Node-API will not perform any conversion between data types.</p> <h5><code>napi_set_named_property</code><span><a class="mark" href="#napi_set_named_property" id="napi_set_named_property">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_set_named_property"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_named_property</span><span class="hljs-params">(napi_env env, napi_value object, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8Name, napi_value value)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[in] object</code>: The object on which to set the property.</li> <li><code>[in] utf8Name</code>: The name of the property to set.</li> <li><code>[in] value</code>: The property value.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This method is equivalent to calling <a href="#napi_set_property"><code>napi_set_property</code></a> with a <code>napi_value</code> created from the string passed in as <code>utf8Name</code>.</p> <h5><code>napi_get_named_property</code><span><a class="mark" href="#napi_get_named_property" id="napi_get_named_property">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_named_property"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_named_property</span><span class="hljs-params">(napi_env env, napi_value object, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8Name, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[in] object</code>: The object from which to retrieve the property.</li> <li><code>[in] utf8Name</code>: The name of the property to get.</li> <li><code>[out] result</code>: The value of the property.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This method is equivalent to calling <a href="#napi_get_property"><code>napi_get_property</code></a> with a <code>napi_value</code> created from the string passed in as <code>utf8Name</code>.</p> <h5><code>napi_has_named_property</code><span><a class="mark" href="#napi_has_named_property" id="napi_has_named_property">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_has_named_property"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_named_property</span><span class="hljs-params">(napi_env env, napi_value object, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8Name, <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[in] object</code>: The object to query.</li> <li><code>[in] utf8Name</code>: The name of the property whose existence to check.</li> <li><code>[out] result</code>: Whether the property exists on the object or not.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This method is equivalent to calling <a href="#napi_has_property"><code>napi_has_property</code></a> with a <code>napi_value</code> created from the string passed in as <code>utf8Name</code>.</p> <h5><code>napi_set_element</code><span><a class="mark" href="#napi_set_element" id="napi_set_element">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_set_element"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_set_element</span><span class="hljs-params">(napi_env env, napi_value object, <span class="hljs-type">uint32_t</span> index, napi_value value)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[in] object</code>: The object from which to set the properties.</li> <li><code>[in] index</code>: The index of the property to set.</li> <li><code>[in] value</code>: The property value.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API sets an element on the <code>Object</code> passed in.</p> <h5><code>napi_get_element</code><span><a class="mark" href="#napi_get_element" id="napi_get_element">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_element"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_element</span><span class="hljs-params">(napi_env env, napi_value object, <span class="hljs-type">uint32_t</span> index, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[in] object</code>: The object from which to retrieve the property.</li> <li><code>[in] index</code>: The index of the property to get.</li> <li><code>[out] result</code>: The value of the property.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API gets the element at the requested index.</p> <h5><code>napi_has_element</code><span><a class="mark" href="#napi_has_element" id="napi_has_element">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_has_element"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_has_element</span><span class="hljs-params">(napi_env env, napi_value object, <span class="hljs-type">uint32_t</span> index, <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[in] object</code>: The object to query.</li> <li><code>[in] index</code>: The index of the property whose existence to check.</li> <li><code>[out] result</code>: Whether the property exists on the object or not.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API returns if the <code>Object</code> passed in has an element at the requested index.</p> <h5><code>napi_delete_element</code><span><a class="mark" href="#napi_delete_element" id="napi_delete_element">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_delete_element"></a></h5> <div class="api_metadata"> <span>Added in: v8.2.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_element</span><span class="hljs-params">(napi_env env, napi_value object, <span class="hljs-type">uint32_t</span> index, <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[in] object</code>: The object to query.</li> <li><code>[in] index</code>: The index of the property to delete.</li> <li><code>[out] result</code>: Whether the element deletion succeeded or not. <code>result</code> can optionally be ignored by passing <code>NULL</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API attempts to delete the specified <code>index</code> from <code>object</code>.</p> <h5><code>napi_define_properties</code><span><a class="mark" href="#napi_define_properties" id="napi_define_properties">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_define_properties"></a></h5> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_define_properties</span><span class="hljs-params">(napi_env env, napi_value object, <span class="hljs-type">size_t</span> property_count, <span class="hljs-type">const</span> napi_property_descriptor* properties)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[in] object</code>: The object from which to retrieve the properties.</li> <li><code>[in] property_count</code>: The number of elements in the <code>properties</code> array.</li> <li><code>[in] properties</code>: The array of property descriptors.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This method allows the efficient definition of multiple properties on a given object. The properties are defined using property descriptors (see <a href="#napi_property_descriptor"><code>napi_property_descriptor</code></a>). Given an array of such property descriptors, this API will set the properties on the object one at a time, as defined by <code>DefineOwnProperty()</code> (described in <a href="https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc">Section 9.1.6</a> of the ECMA-262 specification).</p> <h5><code>napi_object_freeze</code><span><a class="mark" href="#napi_object_freeze" id="napi_object_freeze">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_object_freeze"></a></h5> <div class="api_metadata"> <span>Added in: v14.14.0, v12.20.0</span> <span>N-API version: 8</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_object_freeze</span><span class="hljs-params">(napi_env env, napi_value object)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[in] object</code>: The object to freeze.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This method freezes a given object. This prevents new properties from being added to it, existing properties from being removed, prevents changing the enumerability, configurability, or writability of existing properties, and prevents the values of existing properties from being changed. It also prevents the object's prototype from being changed. This is described in <a href="https://tc39.es/ecma262/#sec-object.freeze">Section 19.1.2.6</a> of the ECMA-262 specification.</p> <h5><code>napi_object_seal</code><span><a class="mark" href="#napi_object_seal" id="napi_object_seal">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_object_seal"></a></h5> <div class="api_metadata"> <span>Added in: v14.14.0, v12.20.0</span> <span>N-API version: 8</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_object_seal</span><span class="hljs-params">(napi_env env, napi_value object)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the Node-API call is invoked under.</li> <li><code>[in] object</code>: The object to seal.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This method seals a given object. This prevents new properties from being added to it, as well as marking all existing properties as non-configurable. This is described in <a href="https://tc39.es/ecma262/#sec-object.seal">Section 19.1.2.20</a> of the ECMA-262 specification.</p> </section><section><h3>Working with JavaScript functions<span><a class="mark" href="#working-with-javascript-functions" id="working-with-javascript-functions">#</a></span><a aria-hidden="true" class="legacy" id="n_api_working_with_javascript_functions"></a></h3> <p>Node-API provides a set of APIs that allow JavaScript code to call back into native code. Node-APIs that support calling back into native code take in a callback functions represented by the <code>napi_callback</code> type. When the JavaScript VM calls back to native code, the <code>napi_callback</code> function provided is invoked. The APIs documented in this section allow the callback function to do the following:</p> <ul> <li>Get information about the context in which the callback was invoked.</li> <li>Get the arguments passed into the callback.</li> <li>Return a <code>napi_value</code> back from the callback.</li> </ul> <p>Additionally, Node-API provides a set of functions which allow calling JavaScript functions from native code. One can either call a function like a regular JavaScript function call, or as a constructor function.</p> <p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> field of the <code>napi_property_descriptor</code> items can be associated with <code>object</code> and freed whenever <code>object</code> is garbage-collected by passing both <code>object</code> and the data to <a href="#napi_add_finalizer"><code>napi_add_finalizer</code></a>.</p> <h4><code>napi_call_function</code><span><a class="mark" href="#napi_call_function" id="napi_call_function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_call_function"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_call_function</span><span class="hljs-params">(napi_env env, napi_value recv, napi_value func, <span class="hljs-type">size_t</span> argc, <span class="hljs-type">const</span> napi_value* argv, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] recv</code>: The <code>this</code> value passed to the called function.</li> <li><code>[in] func</code>: <code>napi_value</code> representing the JavaScript function to be invoked.</li> <li><code>[in] argc</code>: The count of elements in the <code>argv</code> array.</li> <li><code>[in] argv</code>: Array of <code>napi_values</code> representing JavaScript values passed in as arguments to the function.</li> <li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript object returned.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This method allows a JavaScript function object to be called from a native add-on. This is the primary mechanism of calling back <em>from</em> the add-on's native code <em>into</em> JavaScript. For the special case of calling into JavaScript after an async operation, see <a href="#napi_make_callback"><code>napi_make_callback</code></a>.</p> <p>A sample use case might look as follows. Consider the following JavaScript snippet:</p> <pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">AddTwo</span>(<span class="hljs-params">num</span>) { <span class="hljs-keyword">return</span> num + <span class="hljs-number">2</span>; } <span class="hljs-variable language_">global</span>.<span class="hljs-property">AddTwo</span> = <span class="hljs-title class_">AddTwo</span>;</code> <button class="copy-button">copy</button></pre> <p>Then, the above function can be invoked from a native add-on using the following code:</p> <pre><code class="language-c"><span class="hljs-comment">// Get the function named "AddTwo" on the global object</span> napi_value global, add_two, arg; napi_status status = napi_get_global(env, &global); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>; status = napi_get_named_property(env, global, <span class="hljs-string">"AddTwo"</span>, &add_two); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>; <span class="hljs-comment">// const arg = 1337</span> status = napi_create_int32(env, <span class="hljs-number">1337</span>, &arg); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>; napi_value* argv = &arg; <span class="hljs-type">size_t</span> argc = <span class="hljs-number">1</span>; <span class="hljs-comment">// AddTwo(arg);</span> napi_value return_val; status = napi_call_function(env, global, add_two, argc, argv, &return_val); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>; <span class="hljs-comment">// Convert the result back to a native type</span> <span class="hljs-type">int32_t</span> result; status = napi_get_value_int32(env, return_val, &result); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>;</code> <button class="copy-button">copy</button></pre> <h4><code>napi_create_function</code><span><a class="mark" href="#napi_create_function" id="napi_create_function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_function"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_function</span><span class="hljs-params">(napi_env env, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8name, <span class="hljs-type">size_t</span> length, napi_callback cb, <span class="hljs-type">void</span>* data, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] utf8Name</code>: Optional name of the function encoded as UTF8. This is visible within JavaScript as the new function object's <code>name</code> property.</li> <li><code>[in] length</code>: The length of the <code>utf8name</code> in bytes, or <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li> <li><code>[in] cb</code>: The native function which should be called when this function object is invoked. <a href="#napi_callback"><code>napi_callback</code></a> provides more details.</li> <li><code>[in] data</code>: User-provided data context. This will be passed back into the function when invoked later.</li> <li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript function object for the newly created function.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API allows an add-on author to create a function object in native code. This is the primary mechanism to allow calling <em>into</em> the add-on's native code <em>from</em> JavaScript.</p> <p>The newly created function is not automatically visible from script after this call. Instead, a property must be explicitly set on any object that is visible to JavaScript, in order for the function to be accessible from script.</p> <p>In order to expose a function as part of the add-on's module exports, set the newly created function on the exports object. A sample module might look as follows:</p> <pre><code class="language-c">napi_value <span class="hljs-title function_">SayHello</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> { <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello\n"</span>); <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; } napi_value <span class="hljs-title function_">Init</span><span class="hljs-params">(napi_env env, napi_value exports)</span> { napi_status status; napi_value fn; status = napi_create_function(env, <span class="hljs-literal">NULL</span>, <span class="hljs-number">0</span>, SayHello, <span class="hljs-literal">NULL</span>, &fn); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; status = napi_set_named_property(env, exports, <span class="hljs-string">"sayHello"</span>, fn); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; <span class="hljs-keyword">return</span> exports; } NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)</code> <button class="copy-button">copy</button></pre> <p>Given the above code, the add-on can be used from JavaScript as follows:</p> <pre><code class="language-js"><span class="hljs-keyword">const</span> myaddon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./addon'</span>); myaddon.<span class="hljs-title function_">sayHello</span>();</code> <button class="copy-button">copy</button></pre> <p>The string passed to <code>require()</code> is the name of the target in <code>binding.gyp</code> responsible for creating the <code>.node</code> file.</p> <p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> parameter can be associated with the resulting JavaScript function (which is returned in the <code>result</code> parameter) and freed whenever the function is garbage-collected by passing both the JavaScript function and the data to <a href="#napi_add_finalizer"><code>napi_add_finalizer</code></a>.</p> <p>JavaScript <code>Function</code>s are described in <a href="https://tc39.github.io/ecma262/#sec-function-objects">Section 19.2</a> of the ECMAScript Language Specification.</p> <h4><code>napi_get_cb_info</code><span><a class="mark" href="#napi_get_cb_info" id="napi_get_cb_info">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_cb_info"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_cb_info</span><span class="hljs-params">(napi_env env, napi_callback_info cbinfo, <span class="hljs-type">size_t</span>* argc, napi_value* argv, napi_value* thisArg, <span class="hljs-type">void</span>** data)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] cbinfo</code>: The callback info passed into the callback function.</li> <li><code>[in-out] argc</code>: Specifies the length of the provided <code>argv</code> array and receives the actual count of arguments. <code>argc</code> can optionally be ignored by passing <code>NULL</code>.</li> <li><code>[out] argv</code>: C array of <code>napi_value</code>s to which the arguments will be copied. If there are more arguments than the provided count, only the requested number of arguments are copied. If there are fewer arguments provided than claimed, the rest of <code>argv</code> is filled with <code>napi_value</code> values that represent <code>undefined</code>. <code>argv</code> can optionally be ignored by passing <code>NULL</code>.</li> <li><code>[out] thisArg</code>: Receives the JavaScript <code>this</code> argument for the call. <code>thisArg</code> can optionally be ignored by passing <code>NULL</code>.</li> <li><code>[out] data</code>: Receives the data pointer for the callback. <code>data</code> can optionally be ignored by passing <code>NULL</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This method is used within a callback function to retrieve details about the call like the arguments and the <code>this</code> pointer from a given callback info.</p> <h4><code>napi_get_new_target</code><span><a class="mark" href="#napi_get_new_target" id="napi_get_new_target">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_new_target"></a></h4> <div class="api_metadata"> <span>Added in: v8.6.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_new_target</span><span class="hljs-params">(napi_env env, napi_callback_info cbinfo, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] cbinfo</code>: The callback info passed into the callback function.</li> <li><code>[out] result</code>: The <code>new.target</code> of the constructor call.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API returns the <code>new.target</code> of the constructor call. If the current callback is not a constructor call, the result is <code>NULL</code>.</p> <h4><code>napi_new_instance</code><span><a class="mark" href="#napi_new_instance" id="napi_new_instance">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_new_instance"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_new_instance</span><span class="hljs-params">(napi_env env, napi_value cons, <span class="hljs-type">size_t</span> argc, napi_value* argv, napi_value* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] cons</code>: <code>napi_value</code> representing the JavaScript function to be invoked as a constructor.</li> <li><code>[in] argc</code>: The count of elements in the <code>argv</code> array.</li> <li><code>[in] argv</code>: Array of JavaScript values as <code>napi_value</code> representing the arguments to the constructor. If <code>argc</code> is zero this parameter may be omitted by passing in <code>NULL</code>.</li> <li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript object returned, which in this case is the constructed object.</li> </ul> <p>This method is used to instantiate a new JavaScript value using a given <code>napi_value</code> that represents the constructor for the object. For example, consider the following snippet:</p> <pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">MyObject</span>(<span class="hljs-params">param</span>) { <span class="hljs-variable language_">this</span>.<span class="hljs-property">param</span> = param; } <span class="hljs-keyword">const</span> arg = <span class="hljs-string">'hello'</span>; <span class="hljs-keyword">const</span> value = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyObject</span>(arg);</code> <button class="copy-button">copy</button></pre> <p>The following can be approximated in Node-API using the following snippet:</p> <pre><code class="language-c"><span class="hljs-comment">// Get the constructor function MyObject</span> napi_value global, constructor, arg, value; napi_status status = napi_get_global(env, &global); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>; status = napi_get_named_property(env, global, <span class="hljs-string">"MyObject"</span>, &constructor); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>; <span class="hljs-comment">// const arg = "hello"</span> status = napi_create_string_utf8(env, <span class="hljs-string">"hello"</span>, NAPI_AUTO_LENGTH, &arg); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span>; napi_value* argv = &arg; <span class="hljs-type">size_t</span> argc = <span class="hljs-number">1</span>; <span class="hljs-comment">// const value = new MyObject(arg)</span> status = napi_new_instance(env, constructor, argc, argv, &value);</code> <button class="copy-button">copy</button></pre> <p>Returns <code>napi_ok</code> if the API succeeded.</p> </section><section><h3>Object wrap<span><a class="mark" href="#object-wrap" id="object-wrap">#</a></span><a aria-hidden="true" class="legacy" id="n_api_object_wrap"></a></h3> <p>Node-API offers a way to "wrap" C++ classes and instances so that the class constructor and methods can be called from JavaScript.</p> <ol> <li>The <a href="#napi_define_class"><code>napi_define_class</code></a> API defines a JavaScript class with constructor, static properties and methods, and instance properties and methods that correspond to the C++ class.</li> <li>When JavaScript code invokes the constructor, the constructor callback uses <a href="#napi_wrap"><code>napi_wrap</code></a> to wrap a new C++ instance in a JavaScript object, then returns the wrapper object.</li> <li>When JavaScript code invokes a method or property accessor on the class, the corresponding <code>napi_callback</code> C++ function is invoked. For an instance callback, <a href="#napi_unwrap"><code>napi_unwrap</code></a> obtains the C++ instance that is the target of the call.</li> </ol> <p>For wrapped objects it may be difficult to distinguish between a function called on a class prototype and a function called on an instance of a class. A common pattern used to address this problem is to save a persistent reference to the class constructor for later <code>instanceof</code> checks.</p> <pre><code class="language-c">napi_value MyClass_constructor = <span class="hljs-literal">NULL</span>; status = napi_get_reference_value(env, MyClass::es_constructor, &MyClass_constructor); assert(napi_ok == status); <span class="hljs-type">bool</span> is_instance = <span class="hljs-literal">false</span>; status = napi_instanceof(env, es_this, MyClass_constructor, &is_instance); assert(napi_ok == status); <span class="hljs-keyword">if</span> (is_instance) { <span class="hljs-comment">// napi_unwrap() ...</span> } <span class="hljs-keyword">else</span> { <span class="hljs-comment">// otherwise...</span> }</code> <button class="copy-button">copy</button></pre> <p>The reference must be freed once it is no longer needed.</p> <p>There are occasions where <code>napi_instanceof()</code> is insufficient for ensuring that a JavaScript object is a wrapper for a certain native type. This is the case especially when wrapped JavaScript objects are passed back into the addon via static methods rather than as the <code>this</code> value of prototype methods. In such cases there is a chance that they may be unwrapped incorrectly.</p> <pre><code class="language-js"><span class="hljs-keyword">const</span> myAddon = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./build/Release/my_addon.node'</span>); <span class="hljs-comment">// `openDatabase()` returns a JavaScript object that wraps a native database</span> <span class="hljs-comment">// handle.</span> <span class="hljs-keyword">const</span> dbHandle = myAddon.<span class="hljs-title function_">openDatabase</span>(); <span class="hljs-comment">// `query()` returns a JavaScript object that wraps a native query handle.</span> <span class="hljs-keyword">const</span> queryHandle = myAddon.<span class="hljs-title function_">query</span>(dbHandle, <span class="hljs-string">'Gimme ALL the things!'</span>); <span class="hljs-comment">// There is an accidental error in the line below. The first parameter to</span> <span class="hljs-comment">// `myAddon.queryHasRecords()` should be the database handle (`dbHandle`), not</span> <span class="hljs-comment">// the query handle (`query`), so the correct condition for the while-loop</span> <span class="hljs-comment">// should be</span> <span class="hljs-comment">//</span> <span class="hljs-comment">// myAddon.queryHasRecords(dbHandle, queryHandle)</span> <span class="hljs-comment">//</span> <span class="hljs-keyword">while</span> (myAddon.<span class="hljs-title function_">queryHasRecords</span>(queryHandle, dbHandle)) { <span class="hljs-comment">// retrieve records</span> }</code> <button class="copy-button">copy</button></pre> <p>In the above example <code>myAddon.queryHasRecords()</code> is a method that accepts two arguments. The first is a database handle and the second is a query handle. Internally, it unwraps the first argument and casts the resulting pointer to a native database handle. It then unwraps the second argument and casts the resulting pointer to a query handle. If the arguments are passed in the wrong order, the casts will work, however, there is a good chance that the underlying database operation will fail, or will even cause an invalid memory access.</p> <p>To ensure that the pointer retrieved from the first argument is indeed a pointer to a database handle and, similarly, that the pointer retrieved from the second argument is indeed a pointer to a query handle, the implementation of <code>queryHasRecords()</code> has to perform a type validation. Retaining the JavaScript class constructor from which the database handle was instantiated and the constructor from which the query handle was instantiated in <code>napi_ref</code>s can help, because <code>napi_instanceof()</code> can then be used to ensure that the instances passed into <code>queryHashRecords()</code> are indeed of the correct type.</p> <p>Unfortunately, <code>napi_instanceof()</code> does not protect against prototype manipulation. For example, the prototype of the database handle instance can be set to the prototype of the constructor for query handle instances. In this case, the database handle instance can appear as a query handle instance, and it will pass the <code>napi_instanceof()</code> test for a query handle instance, while still containing a pointer to a database handle.</p> <p>To this end, Node-API provides type-tagging capabilities.</p> <p>A type tag is a 128-bit integer unique to the addon. Node-API provides the <code>napi_type_tag</code> structure for storing a type tag. When such a value is passed along with a JavaScript object or <a href="#napi_create_external">external</a> stored in a <code>napi_value</code> to <code>napi_type_tag_object()</code>, the JavaScript object will be "marked" with the type tag. The "mark" is invisible on the JavaScript side. When a JavaScript object arrives into a native binding, <code>napi_check_object_type_tag()</code> can be used along with the original type tag to determine whether the JavaScript object was previously "marked" with the type tag. This creates a type-checking capability of a higher fidelity than <code>napi_instanceof()</code> can provide, because such type- tagging survives prototype manipulation and addon unloading/reloading.</p> <p>Continuing the above example, the following skeleton addon implementation illustrates the use of <code>napi_type_tag_object()</code> and <code>napi_check_object_type_tag()</code>.</p> <pre><code class="language-c"><span class="hljs-comment">// This value is the type tag for a database handle. The command</span> <span class="hljs-comment">//</span> <span class="hljs-comment">// uuidgen | sed -r -e 's/-//g' -e 's/(.{16})(.*)/0x\1, 0x\2/'</span> <span class="hljs-comment">//</span> <span class="hljs-comment">// can be used to obtain the two values with which to initialize the structure.</span> <span class="hljs-type">static</span> <span class="hljs-type">const</span> napi_type_tag DatabaseHandleTypeTag = { <span class="hljs-number">0x1edf75a38336451d</span>, <span class="hljs-number">0xa5ed9ce2e4c00c38</span> }; <span class="hljs-comment">// This value is the type tag for a query handle.</span> <span class="hljs-type">static</span> <span class="hljs-type">const</span> napi_type_tag QueryHandleTypeTag = { <span class="hljs-number">0x9c73317f9fad44a3</span>, <span class="hljs-number">0x93c3920bf3b0ad6a</span> }; <span class="hljs-type">static</span> napi_value <span class="hljs-title function_">openDatabase</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> { napi_status status; napi_value result; <span class="hljs-comment">// Perform the underlying action which results in a database handle.</span> DatabaseHandle* dbHandle = open_database(); <span class="hljs-comment">// Create a new, empty JS object.</span> status = napi_create_object(env, &result); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; <span class="hljs-comment">// Tag the object to indicate that it holds a pointer to a `DatabaseHandle`.</span> status = napi_type_tag_object(env, result, &DatabaseHandleTypeTag); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; <span class="hljs-comment">// Store the pointer to the `DatabaseHandle` structure inside the JS object.</span> status = napi_wrap(env, result, dbHandle, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; <span class="hljs-keyword">return</span> result; } <span class="hljs-comment">// Later when we receive a JavaScript object purporting to be a database handle</span> <span class="hljs-comment">// we can use `napi_check_object_type_tag()` to ensure that it is indeed such a</span> <span class="hljs-comment">// handle.</span> <span class="hljs-type">static</span> napi_value <span class="hljs-title function_">query</span><span class="hljs-params">(napi_env env, napi_callback_info info)</span> { napi_status status; <span class="hljs-type">size_t</span> argc = <span class="hljs-number">2</span>; napi_value argv[<span class="hljs-number">2</span>]; <span class="hljs-type">bool</span> is_db_handle; status = napi_get_cb_info(env, info, &argc, argv, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; <span class="hljs-comment">// Check that the object passed as the first parameter has the previously</span> <span class="hljs-comment">// applied tag.</span> status = napi_check_object_type_tag(env, argv[<span class="hljs-number">0</span>], &DatabaseHandleTypeTag, &is_db_handle); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; <span class="hljs-comment">// Throw a `TypeError` if it doesn't.</span> <span class="hljs-keyword">if</span> (!is_db_handle) { <span class="hljs-comment">// Throw a TypeError.</span> <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; } }</code> <button class="copy-button">copy</button></pre> <h4><code>napi_define_class</code><span><a class="mark" href="#napi_define_class" id="napi_define_class">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_define_class"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_define_class</span><span class="hljs-params">(napi_env env, <span class="hljs-type">const</span> <span class="hljs-type">char</span>* utf8name, <span class="hljs-type">size_t</span> length, napi_callback constructor, <span class="hljs-type">void</span>* data, <span class="hljs-type">size_t</span> property_count, <span class="hljs-type">const</span> napi_property_descriptor* properties, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] utf8name</code>: Name of the JavaScript constructor function. For clarity, it is recommended to use the C++ class name when wrapping a C++ class.</li> <li><code>[in] length</code>: The length of the <code>utf8name</code> in bytes, or <code>NAPI_AUTO_LENGTH</code> if it is null-terminated.</li> <li><code>[in] constructor</code>: Callback function that handles constructing instances of the class. When wrapping a C++ class, this method must be a static member with the <a href="#napi_callback"><code>napi_callback</code></a> signature. A C++ class constructor cannot be used. <a href="#napi_callback"><code>napi_callback</code></a> provides more details.</li> <li><code>[in] data</code>: Optional data to be passed to the constructor callback as the <code>data</code> property of the callback info.</li> <li><code>[in] property_count</code>: Number of items in the <code>properties</code> array argument.</li> <li><code>[in] properties</code>: Array of property descriptors describing static and instance data properties, accessors, and methods on the class See <code>napi_property_descriptor</code>.</li> <li><code>[out] result</code>: A <code>napi_value</code> representing the constructor function for the class.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>Defines a JavaScript class, including:</p> <ul> <li>A JavaScript constructor function that has the class name. When wrapping a corresponding C++ class, the callback passed via <code>constructor</code> can be used to instantiate a new C++ class instance, which can then be placed inside the JavaScript object instance being constructed using <a href="#napi_wrap"><code>napi_wrap</code></a>.</li> <li>Properties on the constructor function whose implementation can call corresponding <em>static</em> data properties, accessors, and methods of the C++ class (defined by property descriptors with the <code>napi_static</code> attribute).</li> <li>Properties on the constructor function's <code>prototype</code> object. When wrapping a C++ class, <em>non-static</em> data properties, accessors, and methods of the C++ class can be called from the static functions given in the property descriptors without the <code>napi_static</code> attribute after retrieving the C++ class instance placed inside the JavaScript object instance by using <a href="#napi_unwrap"><code>napi_unwrap</code></a>.</li> </ul> <p>When wrapping a C++ class, the C++ constructor callback passed via <code>constructor</code> should be a static method on the class that calls the actual class constructor, then wraps the new C++ instance in a JavaScript object, and returns the wrapper object. See <a href="#napi_wrap"><code>napi_wrap</code></a> for details.</p> <p>The JavaScript constructor function returned from <a href="#napi_define_class"><code>napi_define_class</code></a> is often saved and used later to construct new instances of the class from native code, and/or to check whether provided values are instances of the class. In that case, to prevent the function value from being garbage-collected, a strong persistent reference to it can be created using <a href="#napi_create_reference"><code>napi_create_reference</code></a>, ensuring that the reference count is kept >= 1.</p> <p>Any non-<code>NULL</code> data which is passed to this API via the <code>data</code> parameter or via the <code>data</code> field of the <code>napi_property_descriptor</code> array items can be associated with the resulting JavaScript constructor (which is returned in the <code>result</code> parameter) and freed whenever the class is garbage-collected by passing both the JavaScript function and the data to <a href="#napi_add_finalizer"><code>napi_add_finalizer</code></a>.</p> <h4><code>napi_wrap</code><span><a class="mark" href="#napi_wrap" id="napi_wrap">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_wrap"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_wrap</span><span class="hljs-params">(napi_env env, napi_value js_object, <span class="hljs-type">void</span>* native_object, napi_finalize finalize_cb, <span class="hljs-type">void</span>* finalize_hint, napi_ref* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] js_object</code>: The JavaScript object that will be the wrapper for the native object.</li> <li><code>[in] native_object</code>: The native instance that will be wrapped in the JavaScript object.</li> <li><code>[in] finalize_cb</code>: Optional native callback that can be used to free the native instance when the JavaScript object has been garbage-collected. <a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li> <li><code>[in] finalize_hint</code>: Optional contextual hint that is passed to the finalize callback.</li> <li><code>[out] result</code>: Optional reference to the wrapped object.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>Wraps a native instance in a JavaScript object. The native instance can be retrieved later using <code>napi_unwrap()</code>.</p> <p>When JavaScript code invokes a constructor for a class that was defined using <code>napi_define_class()</code>, the <code>napi_callback</code> for the constructor is invoked. After constructing an instance of the native class, the callback must then call <code>napi_wrap()</code> to wrap the newly constructed instance in the already-created JavaScript object that is the <code>this</code> argument to the constructor callback. (That <code>this</code> object was created from the constructor function's <code>prototype</code>, so it already has definitions of all the instance properties and methods.)</p> <p>Typically when wrapping a class instance, a finalize callback should be provided that simply deletes the native instance that is received as the <code>data</code> argument to the finalize callback.</p> <p>The optional returned reference is initially a weak reference, meaning it has a reference count of 0. Typically this reference count would be incremented temporarily during async operations that require the instance to remain valid.</p> <p><em>Caution</em>: The optional returned reference (if obtained) should be deleted via <a href="#napi_delete_reference"><code>napi_delete_reference</code></a> ONLY in response to the finalize callback invocation. If it is deleted before then, then the finalize callback may never be invoked. Therefore, when obtaining a reference a finalize callback is also required in order to enable correct disposal of the reference.</p> <p>Finalizer callbacks may be deferred, leaving a window where the object has been garbage collected (and the weak reference is invalid) but the finalizer hasn't been called yet. When using <code>napi_get_reference_value()</code> on weak references returned by <code>napi_wrap()</code>, you should still handle an empty result.</p> <p>Calling <code>napi_wrap()</code> a second time on an object will return an error. To associate another native instance with the object, use <code>napi_remove_wrap()</code> first.</p> <h4><code>napi_unwrap</code><span><a class="mark" href="#napi_unwrap" id="napi_unwrap">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_unwrap"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_unwrap</span><span class="hljs-params">(napi_env env, napi_value js_object, <span class="hljs-type">void</span>** result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] js_object</code>: The object associated with the native instance.</li> <li><code>[out] result</code>: Pointer to the wrapped native instance.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>Retrieves a native instance that was previously wrapped in a JavaScript object using <code>napi_wrap()</code>.</p> <p>When JavaScript code invokes a method or property accessor on the class, the corresponding <code>napi_callback</code> is invoked. If the callback is for an instance method or accessor, then the <code>this</code> argument to the callback is the wrapper object; the wrapped C++ instance that is the target of the call can be obtained then by calling <code>napi_unwrap()</code> on the wrapper object.</p> <h4><code>napi_remove_wrap</code><span><a class="mark" href="#napi_remove_wrap" id="napi_remove_wrap">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_remove_wrap"></a></h4> <div class="api_metadata"> <span>Added in: v8.5.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_remove_wrap</span><span class="hljs-params">(napi_env env, napi_value js_object, <span class="hljs-type">void</span>** result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] js_object</code>: The object associated with the native instance.</li> <li><code>[out] result</code>: Pointer to the wrapped native instance.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>Retrieves a native instance that was previously wrapped in the JavaScript object <code>js_object</code> using <code>napi_wrap()</code> and removes the wrapping. If a finalize callback was associated with the wrapping, it will no longer be called when the JavaScript object becomes garbage-collected.</p> <h4><code>napi_type_tag_object</code><span><a class="mark" href="#napi_type_tag_object" id="napi_type_tag_object">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_type_tag_object"></a></h4> <div class="api_metadata"> <span>Added in: v14.8.0, v12.19.0</span> <span>N-API version: 8</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_type_tag_object</span><span class="hljs-params">(napi_env env, napi_value js_object, <span class="hljs-type">const</span> napi_type_tag* type_tag)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] js_object</code>: The JavaScript object or <a href="#napi_create_external">external</a> to be marked.</li> <li><code>[in] type_tag</code>: The tag with which the object is to be marked.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>Associates the value of the <code>type_tag</code> pointer with the JavaScript object or <a href="#napi_create_external">external</a>. <code>napi_check_object_type_tag()</code> can then be used to compare the tag that was attached to the object with one owned by the addon to ensure that the object has the right type.</p> <p>If the object already has an associated type tag, this API will return <code>napi_invalid_arg</code>.</p> <h4><code>napi_check_object_type_tag</code><span><a class="mark" href="#napi_check_object_type_tag" id="napi_check_object_type_tag">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_check_object_type_tag"></a></h4> <div class="api_metadata"> <span>Added in: v14.8.0, v12.19.0</span> <span>N-API version: 8</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_check_object_type_tag</span><span class="hljs-params">(napi_env env, napi_value js_object, <span class="hljs-type">const</span> napi_type_tag* type_tag, <span class="hljs-type">bool</span>* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] js_object</code>: The JavaScript object or <a href="#napi_create_external">external</a> whose type tag to examine.</li> <li><code>[in] type_tag</code>: The tag with which to compare any tag found on the object.</li> <li><code>[out] result</code>: Whether the type tag given matched the type tag on the object. <code>false</code> is also returned if no type tag was found on the object.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>Compares the pointer given as <code>type_tag</code> with any that can be found on <code>js_object</code>. If no tag is found on <code>js_object</code> or, if a tag is found but it does not match <code>type_tag</code>, then <code>result</code> is set to <code>false</code>. If a tag is found and it matches <code>type_tag</code>, then <code>result</code> is set to <code>true</code>.</p> <h4><code>napi_add_finalizer</code><span><a class="mark" href="#napi_add_finalizer" id="napi_add_finalizer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_add_finalizer"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 5</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_add_finalizer</span><span class="hljs-params">(napi_env env, napi_value js_object, <span class="hljs-type">void</span>* finalize_data, node_api_basic_finalize finalize_cb, <span class="hljs-type">void</span>* finalize_hint, napi_ref* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] js_object</code>: The JavaScript object to which the native data will be attached.</li> <li><code>[in] finalize_data</code>: Optional data to be passed to <code>finalize_cb</code>.</li> <li><code>[in] finalize_cb</code>: Native callback that will be used to free the native data when the JavaScript object has been garbage-collected. <a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li> <li><code>[in] finalize_hint</code>: Optional contextual hint that is passed to the finalize callback.</li> <li><code>[out] result</code>: Optional reference to the JavaScript object.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>Adds a <code>napi_finalize</code> callback which will be called when the JavaScript object in <code>js_object</code> has been garbage-collected.</p> <p>This API can be called multiple times on a single JavaScript object.</p> <p><em>Caution</em>: The optional returned reference (if obtained) should be deleted via <a href="#napi_delete_reference"><code>napi_delete_reference</code></a> ONLY in response to the finalize callback invocation. If it is deleted before then, then the finalize callback may never be invoked. Therefore, when obtaining a reference a finalize callback is also required in order to enable correct disposal of the reference.</p> <h5><code>node_api_post_finalizer</code><span><a class="mark" href="#node_api_post_finalizer" id="node_api_post_finalizer">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_post_finalizer"></a></h5> <div class="api_metadata"> <span>Added in: v20.10.0</span> </div> <p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> <pre><code class="language-c">napi_status <span class="hljs-title function_">node_api_post_finalizer</span><span class="hljs-params">(node_api_basic_env env, napi_finalize finalize_cb, <span class="hljs-type">void</span>* finalize_data, <span class="hljs-type">void</span>* finalize_hint)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] finalize_cb</code>: Native callback that will be used to free the native data when the JavaScript object has been garbage-collected. <a href="#napi_finalize"><code>napi_finalize</code></a> provides more details.</li> <li><code>[in] finalize_data</code>: Optional data to be passed to <code>finalize_cb</code>.</li> <li><code>[in] finalize_hint</code>: Optional contextual hint that is passed to the finalize callback.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>Schedules a <code>napi_finalize</code> callback to be called asynchronously in the event loop.</p> <p>Normally, finalizers are called while the GC (garbage collector) collects objects. At that point calling any Node-API that may cause changes in the GC state will be disabled and will crash Node.js.</p> <p><code>node_api_post_finalizer</code> helps to work around this limitation by allowing the add-on to defer calls to such Node-APIs to a point in time outside of the GC finalization.</p> </section><section><h3>Simple asynchronous operations<span><a class="mark" href="#simple-asynchronous-operations" id="simple-asynchronous-operations">#</a></span><a aria-hidden="true" class="legacy" id="n_api_simple_asynchronous_operations"></a></h3> <p>Addon modules often need to leverage async helpers from libuv as part of their implementation. This allows them to schedule work to be executed asynchronously so that their methods can return in advance of the work being completed. This allows them to avoid blocking overall execution of the Node.js application.</p> <p>Node-API provides an ABI-stable interface for these supporting functions which covers the most common asynchronous use cases.</p> <p>Node-API defines the <code>napi_async_work</code> structure which is used to manage asynchronous workers. Instances are created/deleted with <a href="#napi_create_async_work"><code>napi_create_async_work</code></a> and <a href="#napi_delete_async_work"><code>napi_delete_async_work</code></a>.</p> <p>The <code>execute</code> and <code>complete</code> callbacks are functions that will be invoked when the executor is ready to execute and when it completes its task respectively.</p> <p>The <code>execute</code> function should avoid making any Node-API calls that could result in the execution of JavaScript or interaction with JavaScript objects. Most often, any code that needs to make Node-API calls should be made in <code>complete</code> callback instead. Avoid using the <code>napi_env</code> parameter in the execute callback as it will likely execute JavaScript.</p> <p>These functions implement the following interfaces:</p> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_execute_callback)</span><span class="hljs-params">(napi_env env, <span class="hljs-type">void</span>* data)</span>; <span class="hljs-keyword">typedef</span> <span class="hljs-title function_">void</span> <span class="hljs-params">(*napi_async_complete_callback)</span><span class="hljs-params">(napi_env env, napi_status status, <span class="hljs-type">void</span>* data)</span>;</code> <button class="copy-button">copy</button></pre> <p>When these methods are invoked, the <code>data</code> parameter passed will be the addon-provided <code>void*</code> data that was passed into the <code>napi_create_async_work</code> call.</p> <p>Once created the async worker can be queued for execution using the <a href="#napi_queue_async_work"><code>napi_queue_async_work</code></a> function:</p> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_queue_async_work</span><span class="hljs-params">(node_api_basic_env env, napi_async_work work)</span>;</code> <button class="copy-button">copy</button></pre> <p><a href="#napi_cancel_async_work"><code>napi_cancel_async_work</code></a> can be used if the work needs to be cancelled before the work has started execution.</p> <p>After calling <a href="#napi_cancel_async_work"><code>napi_cancel_async_work</code></a>, the <code>complete</code> callback will be invoked with a status value of <code>napi_cancelled</code>. The work should not be deleted before the <code>complete</code> callback invocation, even when it was cancelled.</p> <h4><code>napi_create_async_work</code><span><a class="mark" href="#napi_create_async_work" id="napi_create_async_work">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_async_work"></a></h4> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v8.6.0</td> <td><p>Added <code>async_resource</code> and <code>async_resource_name</code> parameters.</p></td></tr> <tr><td>v8.0.0</td> <td><p><span>Added in: v8.0.0</span></p></td></tr> </tbody></table> </details> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_async_work</span><span class="hljs-params">(napi_env env, napi_value async_resource, napi_value async_resource_name, napi_async_execute_callback execute, napi_async_complete_callback complete, <span class="hljs-type">void</span>* data, napi_async_work* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] async_resource</code>: An optional object associated with the async work that will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#initasyncid-type-triggerasyncid-resource"><code>init</code> hooks</a>.</li> <li><code>[in] async_resource_name</code>: Identifier for the kind of resource that is being provided for diagnostic information exposed by the <code>async_hooks</code> API.</li> <li><code>[in] execute</code>: The native function which should be called to execute the logic asynchronously. The given function is called from a worker pool thread and can execute in parallel with the main event loop thread.</li> <li><code>[in] complete</code>: The native function which will be called when the asynchronous logic is completed or is cancelled. The given function is called from the main event loop thread. <a href="#napi_async_complete_callback"><code>napi_async_complete_callback</code></a> provides more details.</li> <li><code>[in] data</code>: User-provided data context. This will be passed back into the execute and complete functions.</li> <li><code>[out] result</code>: <code>napi_async_work*</code> which is the handle to the newly created async work.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API allocates a work object that is used to execute logic asynchronously. It should be freed using <a href="#napi_delete_async_work"><code>napi_delete_async_work</code></a> once the work is no longer required.</p> <p><code>async_resource_name</code> should be a null-terminated, UTF-8-encoded string.</p> <p>The <code>async_resource_name</code> identifier is provided by the user and should be representative of the type of async work being performed. It is also recommended to apply namespacing to the identifier, e.g. by including the module name. See the <a href="async_hooks.html#type"><code>async_hooks</code> documentation</a> for more information.</p> <h4><code>napi_delete_async_work</code><span><a class="mark" href="#napi_delete_async_work" id="napi_delete_async_work">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_delete_async_work"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_delete_async_work</span><span class="hljs-params">(napi_env env, napi_async_work work)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] work</code>: The handle returned by the call to <code>napi_create_async_work</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API frees a previously allocated work object.</p> <p>This API can be called even if there is a pending JavaScript exception.</p> <h4><code>napi_queue_async_work</code><span><a class="mark" href="#napi_queue_async_work" id="napi_queue_async_work">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_queue_async_work"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_queue_async_work</span><span class="hljs-params">(node_api_basic_env env, napi_async_work work)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] work</code>: The handle returned by the call to <code>napi_create_async_work</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API requests that the previously allocated work be scheduled for execution. Once it returns successfully, this API must not be called again with the same <code>napi_async_work</code> item or the result will be undefined.</p> <h4><code>napi_cancel_async_work</code><span><a class="mark" href="#napi_cancel_async_work" id="napi_cancel_async_work">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_cancel_async_work"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_cancel_async_work</span><span class="hljs-params">(node_api_basic_env env, napi_async_work work)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] work</code>: The handle returned by the call to <code>napi_create_async_work</code>.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API cancels queued work if it has not yet been started. If it has already started executing, it cannot be cancelled and <code>napi_generic_failure</code> will be returned. If successful, the <code>complete</code> callback will be invoked with a status value of <code>napi_cancelled</code>. The work should not be deleted before the <code>complete</code> callback invocation, even if it has been successfully cancelled.</p> <p>This API can be called even if there is a pending JavaScript exception.</p> </section><section><h3>Custom asynchronous operations<span><a class="mark" href="#custom-asynchronous-operations" id="custom-asynchronous-operations">#</a></span><a aria-hidden="true" class="legacy" id="n_api_custom_asynchronous_operations"></a></h3> <p>The simple asynchronous work APIs above may not be appropriate for every scenario. When using any other asynchronous mechanism, the following APIs are necessary to ensure an asynchronous operation is properly tracked by the runtime.</p> <h4><code>napi_async_init</code><span><a class="mark" href="#napi_async_init" id="napi_async_init">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_async_init"></a></h4> <div class="api_metadata"> <span>Added in: v8.6.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_async_init</span><span class="hljs-params">(napi_env env, napi_value async_resource, napi_value async_resource_name, napi_async_context* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] async_resource</code>: Object associated with the async work that will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#initasyncid-type-triggerasyncid-resource"><code>init</code> hooks</a> and can be accessed by <a href="async_hooks.html#async_hooksexecutionasyncresource"><code>async_hooks.executionAsyncResource()</code></a>.</li> <li><code>[in] async_resource_name</code>: Identifier for the kind of resource that is being provided for diagnostic information exposed by the <code>async_hooks</code> API.</li> <li><code>[out] result</code>: The initialized async context.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>The <code>async_resource</code> object needs to be kept alive until <a href="#napi_async_destroy"><code>napi_async_destroy</code></a> to keep <code>async_hooks</code> related API acts correctly. In order to retain ABI compatibility with previous versions, <code>napi_async_context</code>s are not maintaining the strong reference to the <code>async_resource</code> objects to avoid introducing causing memory leaks. However, if the <code>async_resource</code> is garbage collected by JavaScript engine before the <code>napi_async_context</code> was destroyed by <code>napi_async_destroy</code>, calling <code>napi_async_context</code> related APIs like <a href="#napi_open_callback_scope"><code>napi_open_callback_scope</code></a> and <a href="#napi_make_callback"><code>napi_make_callback</code></a> can cause problems like loss of async context when using the <code>AsyncLocalStorage</code> API.</p> <p>In order to retain ABI compatibility with previous versions, passing <code>NULL</code> for <code>async_resource</code> does not result in an error. However, this is not recommended as this will result in undesirable behavior with <code>async_hooks</code> <a href="async_hooks.html#initasyncid-type-triggerasyncid-resource"><code>init</code> hooks</a> and <code>async_hooks.executionAsyncResource()</code> as the resource is now required by the underlying <code>async_hooks</code> implementation in order to provide the linkage between async callbacks.</p> <h4><code>napi_async_destroy</code><span><a class="mark" href="#napi_async_destroy" id="napi_async_destroy">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_async_destroy"></a></h4> <div class="api_metadata"> <span>Added in: v8.6.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_async_destroy</span><span class="hljs-params">(napi_env env, napi_async_context async_context)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] async_context</code>: The async context to be destroyed.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API can be called even if there is a pending JavaScript exception.</p> <h4><code>napi_make_callback</code><span><a class="mark" href="#napi_make_callback" id="napi_make_callback">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_make_callback"></a></h4> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v8.6.0</td> <td><p>Added <code>async_context</code> parameter.</p></td></tr> <tr><td>v8.0.0</td> <td><p><span>Added in: v8.0.0</span></p></td></tr> </tbody></table> </details> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_make_callback</span><span class="hljs-params">(napi_env env, napi_async_context async_context, napi_value recv, napi_value func, <span class="hljs-type">size_t</span> argc, <span class="hljs-type">const</span> napi_value* argv, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] async_context</code>: Context for the async operation that is invoking the callback. This should normally be a value previously obtained from <a href="#napi_async_init"><code>napi_async_init</code></a>. In order to retain ABI compatibility with previous versions, passing <code>NULL</code> for <code>async_context</code> does not result in an error. However, this results in incorrect operation of async hooks. Potential issues include loss of async context when using the <code>AsyncLocalStorage</code> API.</li> <li><code>[in] recv</code>: The <code>this</code> value passed to the called function.</li> <li><code>[in] func</code>: <code>napi_value</code> representing the JavaScript function to be invoked.</li> <li><code>[in] argc</code>: The count of elements in the <code>argv</code> array.</li> <li><code>[in] argv</code>: Array of JavaScript values as <code>napi_value</code> representing the arguments to the function. If <code>argc</code> is zero this parameter may be omitted by passing in <code>NULL</code>.</li> <li><code>[out] result</code>: <code>napi_value</code> representing the JavaScript object returned.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This method allows a JavaScript function object to be called from a native add-on. This API is similar to <code>napi_call_function</code>. However, it is used to call <em>from</em> native code back <em>into</em> JavaScript <em>after</em> returning from an async operation (when there is no other script on the stack). It is a fairly simple wrapper around <code>node::MakeCallback</code>.</p> <p>Note it is <em>not</em> necessary to use <code>napi_make_callback</code> from within a <code>napi_async_complete_callback</code>; in that situation the callback's async context has already been set up, so a direct call to <code>napi_call_function</code> is sufficient and appropriate. Use of the <code>napi_make_callback</code> function may be required when implementing custom async behavior that does not use <code>napi_create_async_work</code>.</p> <p>Any <code>process.nextTick</code>s or Promises scheduled on the microtask queue by JavaScript during the callback are ran before returning back to C/C++.</p> <h4><code>napi_open_callback_scope</code><span><a class="mark" href="#napi_open_callback_scope" id="napi_open_callback_scope">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_open_callback_scope"></a></h4> <div class="api_metadata"> <span>Added in: v9.6.0</span> <span>N-API version: 3</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_open_callback_scope</span><span class="hljs-params">(napi_env env, napi_value resource_object, napi_async_context context, napi_callback_scope* result)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] resource_object</code>: An object associated with the async work that will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#initasyncid-type-triggerasyncid-resource"><code>init</code> hooks</a>. This parameter has been deprecated and is ignored at runtime. Use the <code>async_resource</code> parameter in <a href="#napi_async_init"><code>napi_async_init</code></a> instead.</li> <li><code>[in] context</code>: Context for the async operation that is invoking the callback. This should be a value previously obtained from <a href="#napi_async_init"><code>napi_async_init</code></a>.</li> <li><code>[out] result</code>: The newly created scope.</li> </ul> <p>There are cases (for example, resolving promises) where it is necessary to have the equivalent of the scope associated with a callback in place when making certain Node-API calls. If there is no other script on the stack the <a href="#napi_open_callback_scope"><code>napi_open_callback_scope</code></a> and <a href="#napi_close_callback_scope"><code>napi_close_callback_scope</code></a> functions can be used to open/close the required scope.</p> <h4><code>napi_close_callback_scope</code><span><a class="mark" href="#napi_close_callback_scope" id="napi_close_callback_scope">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_close_callback_scope"></a></h4> <div class="api_metadata"> <span>Added in: v9.6.0</span> <span>N-API version: 3</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_close_callback_scope</span><span class="hljs-params">(napi_env env, napi_callback_scope scope)</span></code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] scope</code>: The scope to be closed.</li> </ul> <p>This API can be called even if there is a pending JavaScript exception.</p> </section><section><h3>Version management<span><a class="mark" href="#version-management" id="version-management">#</a></span><a aria-hidden="true" class="legacy" id="n_api_version_management"></a></h3> <h4><code>napi_get_node_version</code><span><a class="mark" href="#napi_get_node_version" id="napi_get_node_version">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_node_version"></a></h4> <div class="api_metadata"> <span>Added in: v8.4.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> {</span> <span class="hljs-type">uint32_t</span> major; <span class="hljs-type">uint32_t</span> minor; <span class="hljs-type">uint32_t</span> patch; <span class="hljs-type">const</span> <span class="hljs-type">char</span>* release; } napi_node_version; napi_status <span class="hljs-title function_">napi_get_node_version</span><span class="hljs-params">(node_api_basic_env env, <span class="hljs-type">const</span> napi_node_version** version)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[out] version</code>: A pointer to version information for Node.js itself.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This function fills the <code>version</code> struct with the major, minor, and patch version of Node.js that is currently running, and the <code>release</code> field with the value of <a href="process.html#processrelease"><code>process.release.name</code></a>.</p> <p>The returned buffer is statically allocated and does not need to be freed.</p> <h4><code>napi_get_version</code><span><a class="mark" href="#napi_get_version" id="napi_get_version">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_version"></a></h4> <div class="api_metadata"> <span>Added in: v8.0.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_get_version</span><span class="hljs-params">(node_api_basic_env env, <span class="hljs-type">uint32_t</span>* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[out] result</code>: The highest version of Node-API supported.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API returns the highest Node-API version supported by the Node.js runtime. Node-API is planned to be additive such that newer releases of Node.js may support additional API functions. In order to allow an addon to use a newer function when running with versions of Node.js that support it, while providing fallback behavior when running with Node.js versions that don't support it:</p> <ul> <li>Call <code>napi_get_version()</code> to determine if the API is available.</li> <li>If available, dynamically load a pointer to the function using <code>uv_dlsym()</code>.</li> <li>Use the dynamically loaded pointer to invoke the function.</li> <li>If the function is not available, provide an alternate implementation that does not use the function.</li> </ul> </section><section><h3>Memory management<span><a class="mark" href="#memory-management" id="memory-management">#</a></span><a aria-hidden="true" class="legacy" id="n_api_memory_management"></a></h3> <h4><code>napi_adjust_external_memory</code><span><a class="mark" href="#napi_adjust_external_memory" id="napi_adjust_external_memory">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_adjust_external_memory"></a></h4> <div class="api_metadata"> <span>Added in: v8.5.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_adjust_external_memory</span><span class="hljs-params">(node_api_basic_env env, <span class="hljs-type">int64_t</span> change_in_bytes, <span class="hljs-type">int64_t</span>* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] change_in_bytes</code>: The change in externally allocated memory that is kept alive by JavaScript objects.</li> <li><code>[out] result</code>: The adjusted value</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This function gives V8 an indication of the amount of externally allocated memory that is kept alive by JavaScript objects (i.e. a JavaScript object that points to its own memory allocated by a native addon). Registering externally allocated memory will trigger global garbage collections more often than it would otherwise.</p> </section><section><h3>Promises<span><a class="mark" href="#promises" id="promises">#</a></span><a aria-hidden="true" class="legacy" id="n_api_promises"></a></h3> <p>Node-API provides facilities for creating <code>Promise</code> objects as described in <a href="https://tc39.github.io/ecma262/#sec-promise-objects">Section 25.4</a> of the ECMA specification. It implements promises as a pair of objects. When a promise is created by <code>napi_create_promise()</code>, a "deferred" object is created and returned alongside the <code>Promise</code>. The deferred object is bound to the created <code>Promise</code> and is the only means to resolve or reject the <code>Promise</code> using <code>napi_resolve_deferred()</code> or <code>napi_reject_deferred()</code>. The deferred object that is created by <code>napi_create_promise()</code> is freed by <code>napi_resolve_deferred()</code> or <code>napi_reject_deferred()</code>. The <code>Promise</code> object may be returned to JavaScript where it can be used in the usual fashion.</p> <p>For example, to create a promise and pass it to an asynchronous worker:</p> <pre><code class="language-c">napi_deferred deferred; napi_value promise; napi_status status; <span class="hljs-comment">// Create the promise.</span> status = napi_create_promise(env, &deferred, &promise); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; <span class="hljs-comment">// Pass the deferred to a function that performs an asynchronous action.</span> do_something_asynchronous(deferred); <span class="hljs-comment">// Return the promise to JS</span> <span class="hljs-keyword">return</span> promise;</code> <button class="copy-button">copy</button></pre> <p>The above function <code>do_something_asynchronous()</code> would perform its asynchronous action and then it would resolve or reject the deferred, thereby concluding the promise and freeing the deferred:</p> <pre><code class="language-c">napi_deferred deferred; napi_value undefined; napi_status status; <span class="hljs-comment">// Create a value with which to conclude the deferred.</span> status = napi_get_undefined(env, &undefined); <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; <span class="hljs-comment">// Resolve or reject the promise associated with the deferred depending on</span> <span class="hljs-comment">// whether the asynchronous action succeeded.</span> <span class="hljs-keyword">if</span> (asynchronous_action_succeeded) { status = napi_resolve_deferred(env, deferred, undefined); } <span class="hljs-keyword">else</span> { status = napi_reject_deferred(env, deferred, undefined); } <span class="hljs-keyword">if</span> (status != napi_ok) <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>; <span class="hljs-comment">// At this point the deferred has been freed, so we should assign NULL to it.</span> deferred = <span class="hljs-literal">NULL</span>;</code> <button class="copy-button">copy</button></pre> <h4><code>napi_create_promise</code><span><a class="mark" href="#napi_create_promise" id="napi_create_promise">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_promise"></a></h4> <div class="api_metadata"> <span>Added in: v8.5.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_create_promise</span><span class="hljs-params">(napi_env env, napi_deferred* deferred, napi_value* promise)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[out] deferred</code>: A newly created deferred object which can later be passed to <code>napi_resolve_deferred()</code> or <code>napi_reject_deferred()</code> to resolve resp. reject the associated promise.</li> <li><code>[out] promise</code>: The JavaScript promise associated with the deferred object.</li> </ul> <p>Returns <code>napi_ok</code> if the API succeeded.</p> <p>This API creates a deferred object and a JavaScript promise.</p> <h4><code>napi_resolve_deferred</code><span><a class="mark" href="#napi_resolve_deferred" id="napi_resolve_deferred">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_resolve_deferred"></a></h4> <div class="api_metadata"> <span>Added in: v8.5.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_resolve_deferred</span><span class="hljs-params">(napi_env env, napi_deferred deferred, napi_value resolution)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] deferred</code>: The deferred object whose associated promise to resolve.</li> <li><code>[in] resolution</code>: The value with which to resolve the promise.</li> </ul> <p>This API resolves a JavaScript promise by way of the deferred object with which it is associated. Thus, it can only be used to resolve JavaScript promises for which the corresponding deferred object is available. This effectively means that the promise must have been created using <code>napi_create_promise()</code> and the deferred object returned from that call must have been retained in order to be passed to this API.</p> <p>The deferred object is freed upon successful completion.</p> <h4><code>napi_reject_deferred</code><span><a class="mark" href="#napi_reject_deferred" id="napi_reject_deferred">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_reject_deferred"></a></h4> <div class="api_metadata"> <span>Added in: v8.5.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_reject_deferred</span><span class="hljs-params">(napi_env env, napi_deferred deferred, napi_value rejection)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] deferred</code>: The deferred object whose associated promise to resolve.</li> <li><code>[in] rejection</code>: The value with which to reject the promise.</li> </ul> <p>This API rejects a JavaScript promise by way of the deferred object with which it is associated. Thus, it can only be used to reject JavaScript promises for which the corresponding deferred object is available. This effectively means that the promise must have been created using <code>napi_create_promise()</code> and the deferred object returned from that call must have been retained in order to be passed to this API.</p> <p>The deferred object is freed upon successful completion.</p> <h4><code>napi_is_promise</code><span><a class="mark" href="#napi_is_promise" id="napi_is_promise">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_is_promise"></a></h4> <div class="api_metadata"> <span>Added in: v8.5.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">napi_status <span class="hljs-title function_">napi_is_promise</span><span class="hljs-params">(napi_env env, napi_value value, <span class="hljs-type">bool</span>* is_promise)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] value</code>: The value to examine</li> <li><code>[out] is_promise</code>: Flag indicating whether <code>promise</code> is a native promise object (that is, a promise object created by the underlying engine).</li> </ul> </section><section><h3>Script execution<span><a class="mark" href="#script-execution" id="script-execution">#</a></span><a aria-hidden="true" class="legacy" id="n_api_script_execution"></a></h3> <p>Node-API provides an API for executing a string containing JavaScript using the underlying JavaScript engine.</p> <h4><code>napi_run_script</code><span><a class="mark" href="#napi_run_script" id="napi_run_script">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_run_script"></a></h4> <div class="api_metadata"> <span>Added in: v8.5.0</span> <span>N-API version: 1</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_run_script</span><span class="hljs-params">(napi_env env, napi_value script, napi_value* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] script</code>: A JavaScript string containing the script to execute.</li> <li><code>[out] result</code>: The value resulting from having executed the script.</li> </ul> <p>This function executes a string of JavaScript code and returns its result with the following caveats:</p> <ul> <li>Unlike <code>eval</code>, this function does not allow the script to access the current lexical scope, and therefore also does not allow to access the <a href="modules.html#the-module-scope">module scope</a>, meaning that pseudo-globals such as <code>require</code> will not be available.</li> <li>The script can access the <a href="globals.html">global scope</a>. Function and <code>var</code> declarations in the script will be added to the <a href="globals.html#global"><code>global</code></a> object. Variable declarations made using <code>let</code> and <code>const</code> will be visible globally, but will not be added to the <a href="globals.html#global"><code>global</code></a> object.</li> <li>The value of <code>this</code> is <a href="globals.html#global"><code>global</code></a> within the script.</li> </ul> </section><section><h3>libuv event loop<span><a class="mark" href="#libuv-event-loop" id="libuv-event-loop">#</a></span><a aria-hidden="true" class="legacy" id="n_api_libuv_event_loop"></a></h3> <p>Node-API provides a function for getting the current event loop associated with a specific <code>napi_env</code>.</p> <h4><code>napi_get_uv_event_loop</code><span><a class="mark" href="#napi_get_uv_event_loop" id="napi_get_uv_event_loop">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_uv_event_loop"></a></h4> <div class="api_metadata"> <span>Added in: v9.3.0, v8.10.0</span> <span>N-API version: 2</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_get_uv_event_loop</span><span class="hljs-params">(node_api_basic_env env, <span class="hljs-keyword">struct</span> uv_loop_s** loop)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[out] loop</code>: The current libuv loop instance.</li> </ul> <p>Note: While libuv has been relatively stable over time, it does not provide an ABI stability guarantee. Use of this function should be avoided. Its use may result in an addon that does not work across Node.js versions. <a href="https://nodejs.org/docs/latest/api/n-api.html#asynchronous-thread-safe-function-calls">asynchronous-thread-safe-function-calls</a> are an alternative for many use cases.</p> </section><section><h3>Asynchronous thread-safe function calls<span><a class="mark" href="#asynchronous-thread-safe-function-calls" id="asynchronous-thread-safe-function-calls">#</a></span><a aria-hidden="true" class="legacy" id="n_api_asynchronous_thread_safe_function_calls"></a></h3> <p>JavaScript functions can normally only be called from a native addon's main thread. If an addon creates additional threads, then Node-API functions that require a <code>napi_env</code>, <code>napi_value</code>, or <code>napi_ref</code> must not be called from those threads.</p> <p>When an addon has additional threads and JavaScript functions need to be invoked based on the processing completed by those threads, those threads must communicate with the addon's main thread so that the main thread can invoke the JavaScript function on their behalf. The thread-safe function APIs provide an easy way to do this.</p> <p>These APIs provide the type <code>napi_threadsafe_function</code> as well as APIs to create, destroy, and call objects of this type. <code>napi_create_threadsafe_function()</code> creates a persistent reference to a <code>napi_value</code> that holds a JavaScript function which can be called from multiple threads. The calls happen asynchronously. This means that values with which the JavaScript callback is to be called will be placed in a queue, and, for each value in the queue, a call will eventually be made to the JavaScript function.</p> <p>Upon creation of a <code>napi_threadsafe_function</code> a <code>napi_finalize</code> callback can be provided. This callback will be invoked on the main thread when the thread-safe function is about to be destroyed. It receives the context and the finalize data given during construction, and provides an opportunity for cleaning up after the threads e.g. by calling <code>uv_thread_join()</code>. <strong>Aside from the main loop thread, no threads should be using the thread-safe function after the finalize callback completes.</strong></p> <p>The <code>context</code> given during the call to <code>napi_create_threadsafe_function()</code> can be retrieved from any thread with a call to <code>napi_get_threadsafe_function_context()</code>.</p> <h4>Calling a thread-safe function<span><a class="mark" href="#calling-a-thread-safe-function" id="calling-a-thread-safe-function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_calling_a_thread_safe_function"></a></h4> <p><code>napi_call_threadsafe_function()</code> can be used for initiating a call into JavaScript. <code>napi_call_threadsafe_function()</code> accepts a parameter which controls whether the API behaves blockingly. If set to <code>napi_tsfn_nonblocking</code>, the API behaves non-blockingly, returning <code>napi_queue_full</code> if the queue was full, preventing data from being successfully added to the queue. If set to <code>napi_tsfn_blocking</code>, the API blocks until space becomes available in the queue. <code>napi_call_threadsafe_function()</code> never blocks if the thread-safe function was created with a maximum queue size of 0.</p> <p><code>napi_call_threadsafe_function()</code> should not be called with <code>napi_tsfn_blocking</code> from a JavaScript thread, because, if the queue is full, it may cause the JavaScript thread to deadlock.</p> <p>The actual call into JavaScript is controlled by the callback given via the <code>call_js_cb</code> parameter. <code>call_js_cb</code> is invoked on the main thread once for each value that was placed into the queue by a successful call to <code>napi_call_threadsafe_function()</code>. If such a callback is not given, a default callback will be used, and the resulting JavaScript call will have no arguments. The <code>call_js_cb</code> callback receives the JavaScript function to call as a <code>napi_value</code> in its parameters, as well as the <code>void*</code> context pointer used when creating the <code>napi_threadsafe_function</code>, and the next data pointer that was created by one of the secondary threads. The callback can then use an API such as <code>napi_call_function()</code> to call into JavaScript.</p> <p>The callback may also be invoked with <code>env</code> and <code>call_js_cb</code> both set to <code>NULL</code> to indicate that calls into JavaScript are no longer possible, while items remain in the queue that may need to be freed. This normally occurs when the Node.js process exits while there is a thread-safe function still active.</p> <p>It is not necessary to call into JavaScript via <code>napi_make_callback()</code> because Node-API runs <code>call_js_cb</code> in a context appropriate for callbacks.</p> <p>Zero or more queued items may be invoked in each tick of the event loop. Applications should not depend on a specific behavior other than progress in invoking callbacks will be made and events will be invoked as time moves forward.</p> <h4>Reference counting of thread-safe functions<span><a class="mark" href="#reference-counting-of-thread-safe-functions" id="reference-counting-of-thread-safe-functions">#</a></span><a aria-hidden="true" class="legacy" id="n_api_reference_counting_of_thread_safe_functions"></a></h4> <p>Threads can be added to and removed from a <code>napi_threadsafe_function</code> object during its existence. Thus, in addition to specifying an initial number of threads upon creation, <code>napi_acquire_threadsafe_function</code> can be called to indicate that a new thread will start making use of the thread-safe function. Similarly, <code>napi_release_threadsafe_function</code> can be called to indicate that an existing thread will stop making use of the thread-safe function.</p> <p><code>napi_threadsafe_function</code> objects are destroyed when every thread which uses the object has called <code>napi_release_threadsafe_function()</code> or has received a return status of <code>napi_closing</code> in response to a call to <code>napi_call_threadsafe_function</code>. The queue is emptied before the <code>napi_threadsafe_function</code> is destroyed. <code>napi_release_threadsafe_function()</code> should be the last API call made in conjunction with a given <code>napi_threadsafe_function</code>, because after the call completes, there is no guarantee that the <code>napi_threadsafe_function</code> is still allocated. For the same reason, do not use a thread-safe function after receiving a return value of <code>napi_closing</code> in response to a call to <code>napi_call_threadsafe_function</code>. Data associated with the <code>napi_threadsafe_function</code> can be freed in its <code>napi_finalize</code> callback which was passed to <code>napi_create_threadsafe_function()</code>. The parameter <code>initial_thread_count</code> of <code>napi_create_threadsafe_function</code> marks the initial number of acquisitions of the thread-safe functions, instead of calling <code>napi_acquire_threadsafe_function</code> multiple times at creation.</p> <p>Once the number of threads making use of a <code>napi_threadsafe_function</code> reaches zero, no further threads can start making use of it by calling <code>napi_acquire_threadsafe_function()</code>. In fact, all subsequent API calls associated with it, except <code>napi_release_threadsafe_function()</code>, will return an error value of <code>napi_closing</code>.</p> <p>The thread-safe function can be "aborted" by giving a value of <code>napi_tsfn_abort</code> to <code>napi_release_threadsafe_function()</code>. This will cause all subsequent APIs associated with the thread-safe function except <code>napi_release_threadsafe_function()</code> to return <code>napi_closing</code> even before its reference count reaches zero. In particular, <code>napi_call_threadsafe_function()</code> will return <code>napi_closing</code>, thus informing the threads that it is no longer possible to make asynchronous calls to the thread-safe function. This can be used as a criterion for terminating the thread. <strong>Upon receiving a return value of <code>napi_closing</code> from <code>napi_call_threadsafe_function()</code> a thread must not use the thread-safe function anymore because it is no longer guaranteed to be allocated.</strong></p> <h4>Deciding whether to keep the process running<span><a class="mark" href="#deciding-whether-to-keep-the-process-running" id="deciding-whether-to-keep-the-process-running">#</a></span><a aria-hidden="true" class="legacy" id="n_api_deciding_whether_to_keep_the_process_running"></a></h4> <p>Similarly to libuv handles, thread-safe functions can be "referenced" and "unreferenced". A "referenced" thread-safe function will cause the event loop on the thread on which it is created to remain alive until the thread-safe function is destroyed. In contrast, an "unreferenced" thread-safe function will not prevent the event loop from exiting. The APIs <code>napi_ref_threadsafe_function</code> and <code>napi_unref_threadsafe_function</code> exist for this purpose.</p> <p>Neither does <code>napi_unref_threadsafe_function</code> mark the thread-safe functions as able to be destroyed nor does <code>napi_ref_threadsafe_function</code> prevent it from being destroyed.</p> <h4><code>napi_create_threadsafe_function</code><span><a class="mark" href="#napi_create_threadsafe_function" id="napi_create_threadsafe_function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_create_threadsafe_function"></a></h4> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v12.6.0, v10.17.0</td> <td><p>Made <code>func</code> parameter optional with custom <code>call_js_cb</code>.</p></td></tr> <tr><td>v10.6.0</td> <td><p><span>Added in: v10.6.0</span></p></td></tr> </tbody></table> </details> <span>N-API version: 4</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_create_threadsafe_function</span><span class="hljs-params">(napi_env env, napi_value func, napi_value async_resource, napi_value async_resource_name, <span class="hljs-type">size_t</span> max_queue_size, <span class="hljs-type">size_t</span> initial_thread_count, <span class="hljs-type">void</span>* thread_finalize_data, napi_finalize thread_finalize_cb, <span class="hljs-type">void</span>* context, napi_threadsafe_function_call_js call_js_cb, napi_threadsafe_function* result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] func</code>: An optional JavaScript function to call from another thread. It must be provided if <code>NULL</code> is passed to <code>call_js_cb</code>.</li> <li><code>[in] async_resource</code>: An optional object associated with the async work that will be passed to possible <code>async_hooks</code> <a href="async_hooks.html#initasyncid-type-triggerasyncid-resource"><code>init</code> hooks</a>.</li> <li><code>[in] async_resource_name</code>: A JavaScript string to provide an identifier for the kind of resource that is being provided for diagnostic information exposed by the <code>async_hooks</code> API.</li> <li><code>[in] max_queue_size</code>: Maximum size of the queue. <code>0</code> for no limit.</li> <li><code>[in] initial_thread_count</code>: The initial number of acquisitions, i.e. the initial number of threads, including the main thread, which will be making use of this function.</li> <li><code>[in] thread_finalize_data</code>: Optional data to be passed to <code>thread_finalize_cb</code>.</li> <li><code>[in] thread_finalize_cb</code>: Optional function to call when the <code>napi_threadsafe_function</code> is being destroyed.</li> <li><code>[in] context</code>: Optional data to attach to the resulting <code>napi_threadsafe_function</code>.</li> <li><code>[in] call_js_cb</code>: Optional callback which calls the JavaScript function in response to a call on a different thread. This callback will be called on the main thread. If not given, the JavaScript function will be called with no parameters and with <code>undefined</code> as its <code>this</code> value. <a href="#napi_threadsafe_function_call_js"><code>napi_threadsafe_function_call_js</code></a> provides more details.</li> <li><code>[out] result</code>: The asynchronous thread-safe JavaScript function.</li> </ul> <p><strong>Change History:</strong></p> <ul> <li> <p>Experimental (<code>NAPI_EXPERIMENTAL</code> is defined):</p> <p>Uncaught exceptions thrown in <code>call_js_cb</code> are handled with the <a href="process.html#event-uncaughtexception"><code>'uncaughtException'</code></a> event, instead of being ignored.</p> </li> </ul> <h4><code>napi_get_threadsafe_function_context</code><span><a class="mark" href="#napi_get_threadsafe_function_context" id="napi_get_threadsafe_function_context">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_get_threadsafe_function_context"></a></h4> <div class="api_metadata"> <span>Added in: v10.6.0</span> <span>N-API version: 4</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_get_threadsafe_function_context</span><span class="hljs-params">(napi_threadsafe_function func, <span class="hljs-type">void</span>** result)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] func</code>: The thread-safe function for which to retrieve the context.</li> <li><code>[out] result</code>: The location where to store the context.</li> </ul> <p>This API may be called from any thread which makes use of <code>func</code>.</p> <h4><code>napi_call_threadsafe_function</code><span><a class="mark" href="#napi_call_threadsafe_function" id="napi_call_threadsafe_function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_call_threadsafe_function"></a></h4> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v14.5.0</td> <td><p>Support for <code>napi_would_deadlock</code> has been reverted.</p></td></tr> <tr><td>v14.1.0</td> <td><p>Return <code>napi_would_deadlock</code> when called with <code>napi_tsfn_blocking</code> from the main thread or a worker thread and the queue is full.</p></td></tr> <tr><td>v10.6.0</td> <td><p><span>Added in: v10.6.0</span></p></td></tr> </tbody></table> </details> <span>N-API version: 4</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_call_threadsafe_function</span><span class="hljs-params">(napi_threadsafe_function func, <span class="hljs-type">void</span>* data, napi_threadsafe_function_call_mode is_blocking)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] func</code>: The asynchronous thread-safe JavaScript function to invoke.</li> <li><code>[in] data</code>: Data to send into JavaScript via the callback <code>call_js_cb</code> provided during the creation of the thread-safe JavaScript function.</li> <li><code>[in] is_blocking</code>: Flag whose value can be either <code>napi_tsfn_blocking</code> to indicate that the call should block if the queue is full or <code>napi_tsfn_nonblocking</code> to indicate that the call should return immediately with a status of <code>napi_queue_full</code> whenever the queue is full.</li> </ul> <p>This API should not be called with <code>napi_tsfn_blocking</code> from a JavaScript thread, because, if the queue is full, it may cause the JavaScript thread to deadlock.</p> <p>This API will return <code>napi_closing</code> if <code>napi_release_threadsafe_function()</code> was called with <code>abort</code> set to <code>napi_tsfn_abort</code> from any thread. The value is only added to the queue if the API returns <code>napi_ok</code>.</p> <p>This API may be called from any thread which makes use of <code>func</code>.</p> <h4><code>napi_acquire_threadsafe_function</code><span><a class="mark" href="#napi_acquire_threadsafe_function" id="napi_acquire_threadsafe_function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_acquire_threadsafe_function"></a></h4> <div class="api_metadata"> <span>Added in: v10.6.0</span> <span>N-API version: 4</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_acquire_threadsafe_function</span><span class="hljs-params">(napi_threadsafe_function func)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] func</code>: The asynchronous thread-safe JavaScript function to start making use of.</li> </ul> <p>A thread should call this API before passing <code>func</code> to any other thread-safe function APIs to indicate that it will be making use of <code>func</code>. This prevents <code>func</code> from being destroyed when all other threads have stopped making use of it.</p> <p>This API may be called from any thread which will start making use of <code>func</code>.</p> <h4><code>napi_release_threadsafe_function</code><span><a class="mark" href="#napi_release_threadsafe_function" id="napi_release_threadsafe_function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_release_threadsafe_function"></a></h4> <div class="api_metadata"> <span>Added in: v10.6.0</span> <span>N-API version: 4</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_release_threadsafe_function</span><span class="hljs-params">(napi_threadsafe_function func, napi_threadsafe_function_release_mode mode)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] func</code>: The asynchronous thread-safe JavaScript function whose reference count to decrement.</li> <li><code>[in] mode</code>: Flag whose value can be either <code>napi_tsfn_release</code> to indicate that the current thread will make no further calls to the thread-safe function, or <code>napi_tsfn_abort</code> to indicate that in addition to the current thread, no other thread should make any further calls to the thread-safe function. If set to <code>napi_tsfn_abort</code>, further calls to <code>napi_call_threadsafe_function()</code> will return <code>napi_closing</code>, and no further values will be placed in the queue.</li> </ul> <p>A thread should call this API when it stops making use of <code>func</code>. Passing <code>func</code> to any thread-safe APIs after having called this API has undefined results, as <code>func</code> may have been destroyed.</p> <p>This API may be called from any thread which will stop making use of <code>func</code>.</p> <h4><code>napi_ref_threadsafe_function</code><span><a class="mark" href="#napi_ref_threadsafe_function" id="napi_ref_threadsafe_function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_ref_threadsafe_function"></a></h4> <div class="api_metadata"> <span>Added in: v10.6.0</span> <span>N-API version: 4</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_ref_threadsafe_function</span><span class="hljs-params">(node_api_basic_env env, napi_threadsafe_function func)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] func</code>: The thread-safe function to reference.</li> </ul> <p>This API is used to indicate that the event loop running on the main thread should not exit until <code>func</code> has been destroyed. Similar to <a href="https://docs.libuv.org/en/v1.x/handle.html#c.uv_ref"><code>uv_ref</code></a> it is also idempotent.</p> <p>Neither does <code>napi_unref_threadsafe_function</code> mark the thread-safe functions as able to be destroyed nor does <code>napi_ref_threadsafe_function</code> prevent it from being destroyed. <code>napi_acquire_threadsafe_function</code> and <code>napi_release_threadsafe_function</code> are available for that purpose.</p> <p>This API may only be called from the main thread.</p> <h4><code>napi_unref_threadsafe_function</code><span><a class="mark" href="#napi_unref_threadsafe_function" id="napi_unref_threadsafe_function">#</a></span><a aria-hidden="true" class="legacy" id="n_api_napi_unref_threadsafe_function"></a></h4> <div class="api_metadata"> <span>Added in: v10.6.0</span> <span>N-API version: 4</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">napi_unref_threadsafe_function</span><span class="hljs-params">(node_api_basic_env env, napi_threadsafe_function func)</span>;</code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[in] func</code>: The thread-safe function to unreference.</li> </ul> <p>This API is used to indicate that the event loop running on the main thread may exit before <code>func</code> is destroyed. Similar to <a href="https://docs.libuv.org/en/v1.x/handle.html#c.uv_unref"><code>uv_unref</code></a> it is also idempotent.</p> <p>This API may only be called from the main thread.</p> </section><section><h3>Miscellaneous utilities<span><a class="mark" href="#miscellaneous-utilities" id="miscellaneous-utilities">#</a></span><a aria-hidden="true" class="legacy" id="n_api_miscellaneous_utilities"></a></h3> <h4><code>node_api_get_module_file_name</code><span><a class="mark" href="#node_api_get_module_file_name" id="node_api_get_module_file_name">#</a></span><a aria-hidden="true" class="legacy" id="n_api_node_api_get_module_file_name"></a></h4> <div class="api_metadata"> <span>Added in: v15.9.0, v14.18.0, v12.22.0</span> <span>N-API version: 9</span> </div> <pre><code class="language-c">NAPI_EXTERN napi_status <span class="hljs-title function_">node_api_get_module_file_name</span><span class="hljs-params">(node_api_basic_env env, <span class="hljs-type">const</span> <span class="hljs-type">char</span>** result)</span>; </code> <button class="copy-button">copy</button></pre> <ul> <li><code>[in] env</code>: The environment that the API is invoked under.</li> <li><code>[out] result</code>: A URL containing the absolute path of the location from which the add-on was loaded. For a file on the local file system it will start with <code>file://</code>. The string is null-terminated and owned by <code>env</code> and must thus not be modified or freed.</li> </ul> <p><code>result</code> may be an empty string if the add-on loading process fails to establish the add-on's file name during loading.</p></section> <!-- API END --> </div> </div> </div> </body> </html>