C/C++ Addons for Node.js
πŸ“’

C/C++ Addons for Node.js

Tags
Published
July 5, 2024
Creating C/C++ addons for Node.js can be achieved using different methods, each involving different headers and approaches. Here's an overview of the differences between using node.h, node_api.h, and nan.h:

Using node.h

  • Description: This approach involves directly using the V8 and Node.js APIs. It provides full control over the V8 engine and the ability to use all of its features.
  • Pros:
    • Full access to the V8 API.
    • Ability to use the latest features of V8 and Node.js.
    • Suitable for performance-critical applications.
  • Cons:
    • Can be complex and error-prone.
    • Requires keeping up with changes in the V8 API, which can be frequent.
    • Tied closely to specific versions of Node.js and V8.
Example:
cppCopy code #include <node.h>void Method(const v8::FunctionCallbackInfo<v8::Value>& args) { args.GetReturnValue().Set(v8::String::NewFromUtf8(args.GetIsolate(), "Hello World")); } void Init(v8::Local<v8::Object> exports) { NODE_SET_METHOD(exports, "hello", Method); } NODE_MODULE(NODE_GYP_MODULE_NAME, Init)

Using node_api.h (N-API)

  • Description: Node-API (N-API) provides a stable API for native addons. It abstracts the underlying JavaScript engine, making addons compatible across different Node.js versions.
  • Pros:
    • Stability: The API is maintained and stable across Node.js versions.
    • Compatibility: Addons work with multiple versions of Node.js without modification.
    • Simplicity: Easier to use compared to direct V8 API.
  • Cons:
    • Slightly less control compared to using the V8 API directly.
    • Might lag in exposing new features of the underlying engine.
Example:
cppCopy code #include <node_api.h>napi_value Method(napi_env env, napi_callback_info info) { napi_value result; napi_create_string_utf8(env, "Hello World", NAPI_AUTO_LENGTH, &result); return result; } napi_value Init(napi_env env, napi_value exports) { napi_value fn; napi_create_function(env, nullptr, 0, Method, nullptr, &fn); napi_set_named_property(env, exports, "hello", fn); return exports; } NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)

Using nan.h (Native Abstractions for Node.js)

  • Description: NAN is a wrapper around V8 and Node.js APIs that aims to provide compatibility and ease the transition between different versions of Node.js and V8.
  • Pros:
    • Simplifies the use of V8 and Node.js APIs.
    • Provides backward and forward compatibility across Node.js versions.
    • Rich set of helper functions and macros.
  • Cons:
    • Adds an additional layer of abstraction, which might introduce overhead.
    • Dependency on an additional library (NAN).
Example:
cppCopy code #include <nan.h>void Method(const Nan::FunctionCallbackInfo<v8::Value>& info) { info.GetReturnValue().Set(Nan::New("Hello World").ToLocalChecked()); } void Init(v8::Local<v8::Object> exports) { Nan::SetMethod(exports, "hello", Method); } NODE_MODULE(NODE_GYP_MODULE_NAME, Init)

Summary

  • node.h:
    • Use when you need full control over V8 and are comfortable with potential breaking changes.
    • Best for performance-critical applications that need the latest V8 features.
  • node_api.h (N-API):
    • Use when you need stability and compatibility across different Node.js versions.
    • Suitable for most addons, especially those that do not need the latest V8 features.
  • nan.h (NAN):
    • Use when you need compatibility across Node.js versions and prefer a simpler API than the direct V8 and Node.js APIs.
    • Good middle ground, though it introduces an additional dependency.