Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node JS addons - NAN vs N-API? [closed]

Tags:

I am looking to working on a project using node js addons with C++. I came across two abstract library NAN and N-API that I can use. However I am unable to decide which one I should use. I was not able to find proper comparison between these two libraries.

What are the pros, cons and differences of both? How to choose between them?

So far I have found that NAN has more online tutorials/articles regarding async calls. But N-API is officially supported by Node (and was created after NAN as a better alternative, although not sure.)

like image 529
The Prenx Avatar asked Feb 18 '19 05:02

The Prenx


People also ask

What is N API?

N-API (pronounced N as in the letter, followed by API) is an API for building native Addons. It is independent from the underlying JavaScript runtime (for example, V8) and is maintained as part of Node. js itself. This API will be Application Binary Interface (ABI) stable across versions of Node.

How many types of API functions are there in node JS?

The two types of API functions in Node.js are: Asynchronous, non-blocking functions. Synchronous, blocking functions.

What is Node JS native?

js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on a JavaScript Engine (i.e. V8 engine) and executes JavaScript code outside a web browser, which was designed to build scalable network applications. Node.


1 Answers

My understanding is this:

The N-API was added to the core node.js interface in v8.0.0. "It is intended to insulate Addons from changes in the underlying JavaScript engine…" to quote the documentation. It also provides some other wrappers around things like buffers and asynchronous work (which should help avoid some of the underlying non-stable APIs noted in their Implications of ABI stability section).

nan (Native Abstractions for Node) is indeed older and so also supports older versions of node.js — back to node.js 0.8! Now despite its author claiming back in 2017:

As I mentioned somewhere else, N-API is not meant to be directly used for anything. Where has this notion come from? It is an (effectively internal) low-level infrastructure layer meant to offer ABI stability. There will be another layer on top.

…I do not see much warning to that effect in the official Node.js add-on documentation. Perhaps this other comment is a bit more insightful:

Yes, you should still use NAN for production use. It covers every relevant version of Node.js. Also note that N-API is not intended for end users. You should eventually use https://github.com/nodejs/node-addon-api.

Again, that was in June of 2017 by the maintainer of nan at the time. It seems that node-addon-api has matured in the meantime and remains active. In fact, I found a comment in the -addon-api repo that is only a month old at present:

…part of the goal was to make it easy to transition from nan.

So I think the answer is:

  • use nan if you want something mature and very backwards-compatible
  • use node-addon-api if you want something forwards-looking in C++
  • use N-API if you are comfortable working in C and dealing with possible lower-level concerns
like image 109
natevw Avatar answered Sep 21 '22 05:09

natevw