Thanks to a collaborative open source effort across multiple companies, Node.js developers will no longer have to recompile their “native” modules each time Node is upgraded, which is the current headache-inducing practice.
“Node.js growth depends on this ecosystem. The more reliable this ecosystem is, the more Node.js developers will be relying on this platform,” Chandra said.
To date, native modules have been difficult to maintain, both for those who maintain them and those who use them. Every time a new version of Node.js is released, all the native modules must be recompiled for that new version. As a result, package maintainers must keep different versions of their modules for each version of Node, and developers trying out a new module have been endlessly frustrated by a message that the module “was compiled against a different Node.js version.”
Pretty big deal, new native layer for Node.js has landed https://t.co/pTtQvqAm5J
— Mikeal Rogers (@mikeal) April 3, 2017
“This is not how a mature platform should work,” Chandra said.
The N-API layer (often pronounced as “napi”) is designed to eliminate this fluctuation, by providing a stable API that developers can always write to, without worrying about the underlying version of Node. Module developers simply add the “node_api.h” header to their C code, and compile as usual against the methods provided. Every function returns a “NAPI” status, and state is captured within the module with a token provided as an argument.
As of last week, the LTS (long-term support) Node.js version 10, 8 and 6 (in “experimental mode”) all include the N-API layer.
Much work now must be done to get port the native modules to N-API. Developers should encourage the owners of the modules they use to port their modules to this API, or perhaps the developers could even do so themselves. Both would be motivated by no longer having to worry about compiling modules to each version. Developers would also enjoy a faster testing process, now that modules are no longer version-specific. The API will also make it easier to use the modules across different Node implementations, such as Microsoft’s Chakracore.
Standardizing the API layer for native modules could not only make life easier for Node developers, but even enable a whole new class of applications, thanks to its ease of use, Dawson noted. For instance, one party was able to put together a logging application, which would have been prohibitively difficult to do before even. Writing the modules in C would the app sufficient performance, but maintaining modules for each version of Node would not have been feasible. The code became much easier to manage after moving to the API.
“Because you can potentially write a single binary, and have that binary run across different versions, people are going to start doing things they couldn’t do before,” Dawson said.
The Linux Foundation provided travel assistance for the reporter to attend this conference.
The Linux Foundation is a sponsor of The New Stack.