Node.js Collection
Published in

Node.js Collection

Native Extensions for Node.js

A step by step guide to understand the core concepts and build a first extension

THE BASICS TO GET STARTED WITH NATIVE EXTENSIONS

  • JavaScript: it’s the coding language.
  • V8: it’s the engine that runs our JavaScript code.
  • Libuv: it’s a C library that provides us with asynchronous execution.

BASIC TOOLING TO BUILD A NATIVE EXTENSION

BINDING.GYP FILE

NODE-GYP

BINDINGS

N-API

NODE ADDON API

FIRST STEPS IN THE NATIVE EXTENSION WORLD

  • NODE_API_MODULE (Line 14): The first argument is the native extension name and the second one is the name of the function that initializes our extension.
  • Init (Line 10): This is the function that will initialize our native extension. In this function we must export the functions that will be invoked from JavaScript code. To do this, we need to set the name of the function to the exports object and the function itself that will be invoked. This init function must return the exports object.
  • SayHi (Line 3): This function is what will be executed when we invoke our native extension from our JavaScript.

WHAT WE HAD BEFORE N-API?

FURTHER STEPS TO DEVELOP YOUR NATIVE EXTENSIONS

  • NAPI examples can be found in here.
  • Node-addon-api examples can be found in here.
  • Nan examples can be found in here.
  • Another good source are tests here.
  • To learn more about native extensions here.

CONCLUSION

TAKING PART IN THE COMMUNITY

--

--

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
Node.js

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