MCPcopy Create free account
hub / github.com/nodejs/nan

github.com/nodejs/nan @v2.28.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.28.0 ↗ · + Follow
710 symbols 1,840 edges 129 files 27 documented · 4% updated 19d ago★ 3,35458 open issues

Browse by type

Functions 598 Types & classes 112
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Native Abstractions for Node.js

A header file filled with macro and utility goodness for making add-on development for Node.js easier across versions 8, 10, 12, 14, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 and 26.

Current version: 2.28.0

(See CHANGELOG.md for complete ChangeLog)

NPM NPM

Build status

Thanks to the crazy changes in V8 (and some in Node core), keeping native addons compiling happily across versions, particularly 0.10 to 0.12 to 4.0, is a minor nightmare. The goal of this project is to store all logic necessary to develop native Node.js addons without having to inspect NODE_MODULE_VERSION and get yourself into a macro-tangle.

This project also contains some helper utilities that make addon development a bit more pleasant.

News & Updates

Usage

Simply add NAN as a dependency using a package manager like npm, yarn, or bun:

$ npm install nan

Pull in the path to NAN in your binding.gyp so that you can use #include <nan.h> in your .cpp files:

"include_dirs" : [
    "<!(node -e \"require('nan')\")"
]

This works like a -I<path-to-NAN> when compiling your addon.

Example

Just getting started with Nan? Take a look at the Node Add-on Examples.

Refer to a quick-start Nan Boilerplate for a ready-to-go project that utilizes basic Nan functionality.

For a simpler example, see the async pi estimation example in the examples directory for full code and an explanation of what this Monte Carlo Pi estimation example does. Below are just some parts of the full example that illustrate the use of NAN.

Yet another example is nan-example-eol. It shows newline detection implemented as a native addon.

Also take a look at our comprehensive C++ test suite which has a plethora of code snippets for your pasting pleasure.

API

Additional to the NAN documentation below, please consult:

JavaScript-accessible methods

A template is a blueprint for JavaScript functions and objects in a context. You can use a template to wrap C++ functions and data structures within JavaScript objects so that they can be manipulated from JavaScript. See the V8 Embedders Guide section on Templates for further information.

In order to expose functionality to JavaScript via a template, you must provide it to V8 in a form that it understands. Across the versions of V8 supported by NAN, JavaScript-accessible method signatures vary widely, NAN fully abstracts method declaration and provides you with an interface that is similar to the most recent V8 API but is backward-compatible with older versions that still use the now-deceased v8::Argument type.

Scopes

A local handle is a pointer to an object. All V8 objects are accessed using handles, they are necessary because of the way the V8 garbage collector works.

A handle scope can be thought of as a container for any number of handles. When you've finished with your handles, instead of deleting each one individually you can simply delete their scope.

The creation of HandleScope objects is different across the supported versions of V8. Therefore, NAN provides its own implementations that can be used safely across these.

Also see the V8 Embedders Guide section on Handles and Garbage Collection.

Persistent references

An object reference that is independent of any HandleScope is a persistent reference. Where a Local handle only lives as long as the HandleScope in which it was allocated, a Persistent handle remains valid until it is explicitly disposed.

Due to the evolution of the V8 API, it is necessary for NAN to provide a wrapper implementation of the Persistent classes to supply compatibility across the V8 versions supported.

Also see the V8 Embedders Guide section on Handles and Garbage Collection.

New

NAN provides a Nan::New() helper for the creation of new JavaScript objects in a way that's compatible across the supported versions of V8.

Converters

NAN contains functions that convert v8::Values to other v8::Value types and native types. Since type conversion is not guaranteed to succeed, they return Nan::Maybe types. These converters can be used in place of value->ToX() and value->XValue() (where X is one of the types, e.g. Boolean) in a way that provides a consistent interface across V8 versions. Newer versions of V8 use the new v8::Maybe and v8::MaybeLocal types for these conversions, older versions don't have this functionality so it is provided by NAN.

Maybe Types

The Nan::MaybeLocal and Nan::Maybe types are monads that encapsulate v8::Local handles that may be empty.

Core symbols most depended-on inside this repo

Shape

Function 328
Method 270
Class 108
Enum 4

Languages

C++98%
TypeScript2%

Modules by API surface

nan.h187 symbols
nan_callbacks_pre_12_inl.h51 symbols
nan_maybe_pre_43_inl.h36 symbols
nan_callbacks_12_inl.h32 symbols
nan_maybe_43_inl.h30 symbols
nan_new.h29 symbols
nan_persistent_pre_12_inl.h28 symbols
test/cpp/wrappedobjectfactory.cpp12 symbols
nan_weak.h12 symbols
test/cpp/namedinterceptors.cpp10 symbols
test/cpp/morenews.cpp10 symbols
test/cpp/indexedinterceptors.cpp10 symbols

For agents

$ claude mcp add nan \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page