Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
N

nan

> 编程语言
Open source

Native Abstractions for Node.js

3.4K stars0 likes0 views
WebsiteGitHub

About

Native Abstractions for Node.js

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.29.0

(See CHANGELOG.md for complete ChangeLog) 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
  • Example
  • API
  • Tests
  • Known issues
  • Governance & Contributing

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:

  • The V8 Getting Started * Guide
  • V8 API Documentation
  • Node Add-on Documentation

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.

  • Method argument types
  • Nan::FunctionCallbackInfo
  • Nan::PropertyCallbackInfo
  • Nan::ReturnValue
  • Method declarations
  • Method declaration
  • Getter declaration
  • Setter declaration
  • Property getter declaration
  • Property setter declaration
  • Property enumerator declaration
  • Property deleter declaration
  • Property query declaration
  • Index getter declaration
  • Index setter declaration
  • Index enumerator declaration
  • Index deleter declaration
  • Index query declaration
  • Method and template helpers
  • Nan::SetMethod()
  • Nan::SetPrototypeMethod()
  • Nan::SetAccessor()
  • Nan::SetNamedPropertyHandler()
  • Nan::SetIndexedPropertyHandler()
  • Nan::SetTemplate()
  • Nan::SetPrototypeTemplate()
  • Nan::SetInstanceTemplate()
  • Nan::SetCallHandler()
  • Nan::SetCallAsFunctionHandler()

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.

  • Nan::HandleScope
  • Nan::EscapableHandleScope

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.

  • Nan::PersistentBase & v8::PersistentBase
  • Nan::NonCopyablePersistentTraits & v8::NonCopyablePersistentTraits
  • Nan::CopyablePersistentTraits & v8::CopyablePersistentTraits
  • Nan::Persistent
  • Nan::Global
  • Nan::WeakCallbackInfo
  • Nan::WeakCallbackType

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.

  • Nan::New()
  • Nan::Undefined()
  • Nan::Null()
  • Nan::True()
  • Nan::False()
  • Nan::EmptyString()

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.

  • Nan::To()

Maybe Types

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

  • Maybe Types
    • Nan::MaybeLocal
    • Nan::Maybe
    • Nan::Nothing
    • Nan::Just
  • Maybe Helpers
    • Nan::Call()
    • Nan::ToDetailString()
    • Nan::ToArrayIndex()
    • Nan::Equals()
    • Nan::NewInstance()
    • Nan::GetFunction()
    • Nan::Set()
    • Nan::DefineOwnProperty()
    • Nan::ForceSet()
    • Nan::Get()
    • Nan::GetPropertyAttributes()
    • Nan::Has()
    • Nan::Delete()
    • Nan::GetPropertyNames()
    • Nan::GetOwnPropertyNames()
    • Nan::SetPrototype()
    • Nan::ObjectProtoToString()
    • Nan::HasOwnProperty()
    • <a href="doc/maybe_

GitHub Issues· 72 open

View all on GitHub
  • #1020

    Under 8 bott

    Updated May 27, 2026
  • #1014

    PropertyCallbackInfo::This() deleted in v8 14.6

    Updated May 6, 2026
  • #1012

    v2.26.0 broken `nan.h` line 843 compiler error (missing closing parenthesis)

    Updated Mar 17, 2026
  • #1006

    NAN_GETTER and NAN_SETTER not called in Node 23+

    Updated Jan 10, 2026
  • #995

    Weak tests incompatible with V8 change

    Updated Oct 30, 2025
  • #958

    Use GitHub Actions and delete Travis / AppVeyor configuration

    enhancementUpdated Feb 13, 2025
  • #962

    can this node-gyp warning be dealt with?

    Updated Feb 8, 2025
  • #892

    Electron-rebuild canvas fails with nan 2.14.1, works with 2.14.0

    Updated Jul 18, 2024
  • #627

    sigsegv in test/js/returnvalue-test.js

    Updated Jun 15, 2024

Highlights

  • •News & Updates
  • •Known issues
  • •Governance & Contributing
  • •The V8 Getting Started * Guide
  • •V8 API Documentation
  • •Node Add-on Documentation
  • •Method argument types
  • •<a href="doc/methods.md#api_nan_function_callback_info"><b><code>Nan::FunctionCallbackInfo</code></b></a>
  • •<a href="doc/methods.md#api_nan_property_callback_info"><b><code>Nan::PropertyCallbackInfo</code></b></a>
  • •<a href="doc/methods.md#api_nan_return_value"><b><code>Nan::ReturnValue</code></b></a>

> Tags

C++nodenodejs

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言