New Node.js API will shield modules from JavaScript engine changes

N-API should break the dependency on Google’s V8 engine and help developers avoid recompilation for new versions and engines

Help is on the way for developers building native modules for Node.js, as well for those who want to swap out the underlying JavaScript engine powering the platform.

Currently, modules must be recompiled to work with new versions of Node.js and the JavaScript engine, which traditionally has been Google’s V8 engine. But N-API is about to make things easier by providing an API for linking in native add-ons. Independent of the underlying JavaScript runtime, N-API will be stable for the application binary interface (ABI) across Node versions, to insulate add-ons from changes in the underlying JavaScript engine. Modules compiled for one version of Node.js would then run on later versions with no need to recompile.

The N-API effort also aims to ease support of multiple back-end JavaScript engines. Last year, Microsoft came up with a plan to swap out Google’s V8 engine with Microsoft’s ChakraCore engine. But Microsoft had to make ChakraCore look and act like V8 for it to work, and that required the development of a shim layer, said James Snell, a Node.js technical steering committee member and architect at software builder NearForm.

“What NAPI is allowing us to do is have an intermediary ABI that lives within Node that native module authors can write against,” said Myles Borins, a developer advocate at Google. To get the advantages of N-API, developers will have to migrate their NPMs to N-API. Once that's done, they should get its stability benefits.

N-API is in its early stages, having been introduced in May with Node.js 8. The goal is to make N-API a fully supported feature either in Node.js 9, which is due in October, or in Node.js 10, due six months later, Snell said.

N-API is expected to help users upgrade to the latest versions of Node.js. “Mission-critical native code dependencies is the No. 1 reason why people are still stuck with older versions of Node,” said Arunesh Chandra, Microsoft’s senior program manager for Chakra.

With native V8 modules, most NPMs are written in JavaScript. But there are some CPU-intensive modules, such as graphics engines or video encoders, written in C or C++. N-API should ensure those modules do not break with every new version of V8 or Node, said Joe McCann, CEO of Node distribution provider NodeSource.

N-API uses a C API, though some Node developers believe a C++ API would be easier to use. Thus, the builders of Node.js anticipate wrapper modules to provide an inline-able C++ API.