Skip to content

jasongin/node-addon-api

 
 

Repository files navigation

Node.js API (N-API) Package

This package contains header-only C++ wrapper classes for the ABI-stable Node.js API (N-API), along with library code that enables backward-compatibility with use with older versions of Node.js that do not have N-API built-in.

API Documentation

Getting Started

To use N-API in a native module:

  1. Add a dependency on this package to package.json:
  "dependencies": {
    "node-addon-api": "0.2.0",
  }
  1. Reference this package's include directory and gyp file in binding.gyp:
  'include_dirs': ["<!@(node -p \"require('node-addon-api').include\")"],
  'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
  1. Ensure C++ exceptions are enabled in binding.gyp. The N-API C++ wrapper classes use exceptions for error-handling; the base ABI-stable C APIs do not.
  'cflags!': [ '-fno-exceptions' ],
  'cflags_cc!': [ '-fno-exceptions' ],
  'xcode_settings': {
    'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
    'CLANG_CXX_LIBRARY': 'libc++',
    'MACOSX_DEPLOYMENT_TARGET': '10.7',
  },
  'msvs_settings': {
    'VCCLCompilerTool': { 'ExceptionHandling': 1 },
  },
  1. Include napi.h in the native module code. To ensure only ABI-stable APIs are used, DO NOT include node.h, nan.h, or v8.h.
#include "napi.h"

At build time, the N-API back-compat library code will be used only when the targeted node version does not have N-API built-in.

Conversion Tool

To make the migration to node-api easier, we have provided a script to help complete the steps listed above. To use the conversion script:

  1. Go to your module directory
cd [module_path]
  1. Install node-api module
npm install node-api
  1. Run node-api conversion script
node ./node_modules/node-api/tools/conversion.js ./
  1. While this script makes conversion easier, it still cannot fully convert the module. The next step is to try to build the module and complete the remaining conversions necessary to allow it to compile and pass all of the module's tests.

WG Members / Collaborators

Name GitHub link
Anna Henningsen addaleax
Arunesh Chandra aruneshchandra
Benjamin Byholm kkoopa
Gabriel Schulhof gabrielschulhof
Hitesh Kanwathirtha digitalinfinity
Jason Ginchereau jasongin
Michael Dawson mhdawson
Sampson Gao sampsongao
Taylor Woll boingoing

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 92.8%
  • JavaScript 5.9%
  • Other 1.3%