Node.js
Node.js
Jul 18, 2018 · 6 min read

N-API: Next generation APIs for Node.js native addons available across all LTS release lines

This blog was written by Arunesh Chandra, Senior Program Manager of Microsoft Edge, and Michael Dawson, IBM Community Lead for Node.js. It covers the current status of N-API, how you can use this technology, and modules that are adopting N-API. If you are attending Node Summit, there are multiple ways to learn more about N-API (more information on this in the post below).

You may have heard that Node.js has experimental support for N-API, a new API for native modules. Not anymore! N-API is now a stable and fully supported feature of Node.js as of version 10. It is also available as an experimental feature in Node.js 8 and 6.

As we discussed previously, N-API provides an ABI-stable abstraction over native JavaScript APIs to enable a more robust native module ecosystem. Why? The lack of an ABI-stable interface for Node.js modules creates a fragile ecosystem that gets impacted with every new Node.js release. This fragility creates friction in the growth of Node.js as a mature platform.

It not only adds to the maintenance burden for native module maintainers, but it also presents a major barrier to upgrading Node.js versions in production for module consumers, who have mission critical native code dependencies in their production deployments.

With N-API, native module developers can compile their module once per platform and architecture, and make it available for any version of Node.js that implements N-API. This holds true even for versions of Node.js that are built with a different VM, e.g. Node-ChakraCore.

N-API was put in as an experimental feature in Node.js 8.0, and after meeting rigorous exit criteria defined by the community, it has been promoted to a supported feature in Node.js 10. It has also been backported to all LTS release lines. Having N-API with the same API across all LTS releases is a great milestone and we believe this is a significant step towards enabling its adoption. Many thanks to Gabriel Schulhof who put in a tremendous amount of effort to backport this feature across these versions.

Using N-API across versions

As of version 8.11.2 all of the LTS release lines provide the same set of APIs for N-API. The status of each is as follows:

Node-pre-gyp

For native module authors, Node-pre-gyp is a great tool to publish binary packages that end-users can install. Since it’s such a crucial part of the existing Node.js native module ecosystem, the N-API team worked with the maintainer of node-pre-gyp to integrate support for building and publishing N-API modules. Many thanks to Jim Schlight and Dane Springmeyer who made this happen.

Having Node-pre-gyp ready to support the N-API addons is another great milestone that we believe is a significant step towards enabling adoption.

If you would like to learn more about using node-pre-gyp with N-API, check out the node-pre-gyp documentation here: https://github.com/mapbox/node-pre-gyp#n-api-considerations

Node-addon-api

While N-API provides a C interface, many module developers use C++. The node-addon-api module provides an inlined wrapper that allows developers to use C++ and easily migrate from nan. The node-addon-api module was updated to use the N-API built-into 6.X such that it uses the built-in version of N-API for the latest versions of 6.x, 8.x and 10.x. There is a growing list of dependents on this module.

Image for post
Image for post

The N-API team continues to focus on filling out the documentation for node-addon-api and addressing reported issues as we continue to see increased usage.

N-API in use — Module Showcase

We are pleased to see that developers embracing this feature and have started publishing N-API based native addons on npm. In addition to the N-API addon dependencies shown above, there are also a number of packages tagged as N-API.

Image for post
Image for post

As adoption grows, we’d like to take the opportunity to regularly provide updates on some of the modules, which are adopting N-API. If you have a module that uses N-API in a unique or interesting way, or that you are just very proud of, let us know on the N-API repo so that we can highlight it for future updates.

Today, we are highlighting Log++, which demonstrates N-API being used for logging into an application with high performance requirements.

Image for post
Image for post

Fast logging is crucial to getting great performance for any application that depends on reliable diagnostic telemetry. Loggers in the past have been written in JavaScript because of the development and maintenance overhead of native modules, but N-API allows this (often critical) component of an app to be written as native code, and also runs the same binary across multiple versions of Node.js. Additionally, it has the following benefits:

The result is a logger that is 4–7 times faster than existing logging solutions.

Get Involved

Want to get more involved? Check out these documentation links below:

While we have made great progress on N-API, we can always use more help. Here are some ways you can help out:

In addition, you are always welcome to attend the weekly N-API team meeting on Thursdays at 1:30 EST.

If you’re attending the upcoming Node Summit conference, then there are additional resources that you can leverage to learn more about N-API. There are two N-API talks that will give you an overview of N-API and also a technical deep dive. In addition, there will be an N-API workshop on Day Zero to get some hands-on experience writing or porting native modules to N-API. It will be a great way to kick off your N-API development.

We hope to see you there!

Node.js Collection

Community-curated content for the millions of Node.js

Node.js

Written by

Node.js

Node.js is a collaborative open source project dedicated to building and supporting the Node.js platform. https://nodejs.org/en/

Node.js Collection

Community-curated content for the millions of Node.js users.

Node.js

Written by

Node.js

Node.js is a collaborative open source project dedicated to building and supporting the Node.js platform. https://nodejs.org/en/

Node.js Collection

Community-curated content for the millions of Node.js users.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store