Node.js C++ Embedder API: Running JavaScript from C++ Applications

Node.js provides a set of C++ APIs that allow developers to execute JavaScript inside a Node.js environment from C++ applications. This is useful when Node.js is embedded as a library within other software. Unlike typical Node.js code execution, embedding Node.js

C++ Embedder APInode::InitializeOncePerProcessMultiIsolatePlatformCommonEnvironmentSetupnode::Environment

~2 min read · Updated Dec 26, 2025

1. Introduction


The C++ Embedder API allows developers to run JavaScript inside C++ applications. These APIs are defined in src/node.h and rely on concepts from the V8 embedder API.

2. Setting up Per-Process State


  • Parse Node.js CLI arguments.
  • Initialize V8 requirements such as a v8::Platform instance.
  • node::InitializeOncePerProcess sets up Node.js globally.
  • MultiIsolatePlatform::Create() creates a V8 platform that supports Worker threads.
int main(int argc, char** argv) {
  argv = uv_setup_args(argc, argv);
  std::vector args(argv, argv + argc);

  auto result = node::InitializeOncePerProcess(args, {...});
  if (result->early_return() != 0) return result->exit_code();

  auto platform = MultiIsolatePlatform::Create(4);
  V8::InitializePlatform(platform.get());
  V8::Initialize();

  int ret = RunNodeInstance(platform.get(), result->args(), result->exec_args());

  V8::Dispose();
  V8::DisposePlatform();
  node::TearDownOncePerProcess();
  return ret;
}

3. Setting up Per-Instance State


  • Each node::Environment is tied to one v8::Isolate and one uv_loop_t.
  • An ArrayBuffer::Allocator must be provided (Node.js offers a default allocator).
  • node::NewIsolate() creates and registers a new Isolate with Node.js hooks.

4. Running JavaScript Code


  • node::LoadEnvironment: Loads the Node.js environment and executes code.
  • node::SpinEventLoop: Runs the event loop until completion.
  • node::Stop: Explicitly stops the event loop.
MaybeLocal loadenv_ret = node::LoadEnvironment(
    env,
    "const publicRequire = require('node:module').createRequire(process.cwd() + '/');"
    "globalThis.require = publicRequire;"
    "require('node:vm').runInThisContext(process.argv[1]);");

if (loadenv_ret.IsEmpty()) return 1;
exit_code = node::SpinEventLoop(env).FromMaybe(1);
node::Stop(env);

Conclusion


The C++ Embedder API in Node.js is a powerful tool for executing JavaScript inside C++ applications. By managing per-process and per-instance state, and using APIs like LoadEnvironment and SpinEventLoop, developers can build hybrid applications that leverage both Node.js and C++ together.

Written & researched by Dr. Shahin Siami

Related Articles

Comprehensive Guide to the Node.js VM Module (node:vm)

The node:vm module allows you to compile and execute JavaScript code inside isolated V8 contexts — essentially creating lightweight sandboxes within your Node.js application. These contexts have their own global scope and can run code independently from the main environment. However, vm is NOT a security sandbox. It is powerful for dynamic code execution, template engines, REPLs, plugin systems, and controlled module execution, but it must never be used to run untrusted code.

Continue

Comprehensive Guide to the V8 Module in Node.js (node:v8)

The node:v8 module exposes low-level APIs that interact directly with the V8 JavaScript engine embedded in Node.js. . These APIs provide access to heap statistics, heap snapshots, coverage tools, serialization mechanisms, V8 flags, object queries, and promise lifecycle hooks. The module is essential for performance analysis, memory debugging, tooling, and advanced Node.js internals work.

Continue

Comprehensive Guide to Node.js Worker Threads (node:worker_threads)

The node:worker_threads module enables true multithreading in Node.js by running JavaScript in separate threads. While Node.js is traditionally single‑threaded, worker threads allow CPU‑intensive tasks to run in parallel without blocking the event loop. They support shared memory, zero‑copy transfers, worker pools, resource limits, and advanced synchronization APIs. Worker threads are ideal for heavy computation, data processing, and parallel workloads—while async I/O remains best handled by the main thread.

Continue

Comprehensive Guide to the Node.js util Module (node:util)

The node:util module provides a powerful collection of helper functions used throughout Node.js core and extremely useful for application developers. These utilities support debugging, inspection, formatting, type checking, callback/Promise conversions, argument parsing, text encoding, MIME handling, and more. It is one of the most versatile and essential toolkits in the Node.js ecosystem.

Continue

Comprehensive Guide to the URL Module in Node.js

The node:url module provides tools for parsing, constructing, and manipulating URLs. Node.js supports two URL APIs: WHATWG URL API — modern, browser‑compatible, standards‑based. Legacy Node.js URL API — older, Node‑specific, now discouraged. The WHATWG API is the recommended approach for all modern applications. It provides a clean, consistent interface for working with URL components, query parameters, and structured URL patterns.

Continue

راهنمای جامع UDP / Datagram Sockets در Node.js

ماژول node:dgram پیاده‌سازی کامل سوکت‌های UDP را در Node.js فراهم می‌کند. UDP یک پروتکل سبک، بدون اتصال (connectionless) و مناسب برای برنامه‌های بلادرنگ مانند VoIP، بازی‌ها، IoT، سیستم‌های پخش (broadcast) و چندپخشی (multicast) است. این ماژول امکان ساخت سوکت، ارسال و دریافت دیتاگرام، مدیریت TTL، عضویت در گروه‌های multicast، کنترل بافرها، و مدیریت رفتار سطح پایین شبکه را فراهم می‌کند.

Continue