Writings
Blog posts and articles I've written, here and elsewhere.
Blog posts
Articles
Guides and documentation I've written or co-written.
Node.js Learn › Asynchronous Work
Node.js Learn › Test Runner
Node.js Learn › Node-API
- What is Node-API?
Node-API is a stable C API built into Node.js that lets C/C++ code create, read, and manipulate JavaScript values as if they were created by JavaScript itself. It was introduced experimentally in Node.js 8.0.0 and became stable (no longer behind a flag) in the Node.js 8/10 timeframe.
- OverviewGetting Started
This section walks you through everything you need to begin building native Node.js addons with Node-API. It is designed for developers who are new to native addon development and want a guided path from zero to a working module.
- Pre-requisitesGetting Started
The purpose of Node-API is to enable you to use your existing or newly written C/C++ code from JavaScript. This assumes at least a passing familiarity with both JavaScript and C/C++. The level of familiarity necessary is dependent upon the complexity of your project and your involvement with the JavaScript and the C/C++.
- ToolsGetting Started
Node.js is the runtime that executes JavaScript on your machine. It bundles the V8 JavaScript engine together with a set of built-in modules that let you run JavaScript outside of a browser.
- Anatomy of a Node-API projectGetting Started
Before diving into code, it helps to understand the files and layout that every node-addon-api project shares. All N-API examples in these guides follow this same structure, so this page explains it once so those tutorials can focus on what makes each one unique.
- Your First ProjectGetting Started
Before you start, make sure you've got all the necessary prerequisites and tools installed, and read Anatomy of a Node-API project to understand the common layout and configuration files shared by all node-addon-api projects.
- Object WrapGetting Started
This tutorial is an alternative to Your First Project. Instead of exporting a plain function, it demonstrates how to expose a C++ object as a JavaScript object using node-addon-api's ObjectWrap class.
- Migrating to Node-APIGetting Started
The objective of this tutorial is to give you a good idea of the steps necessary and the tools available to migrate an existing NAN Node native add-on module to Node-API using the node-addon-api package.
- OverviewBuild Tools
Building a native Node.js addon requires compiling C/C++ source code into a binary .node file that Node.js can load at runtime. This section covers the build tools available to you and the trade-offs between them.
- node-gypBuild Tools
node-gyp is the standard build tool for native Node.js addons and is used by the vast majority of packages in the npm ecosystem. It is actively maintained by the Node.js team. Most of the examples on this site use node-gyp to build binaries.
- CMake.jsBuild Tools
CMake.js is good build tool alternative to node-gyp. CMake.js is based on the CMake tool which must be installed.
- node-pre-gypBuild Tools
One of the limitations of native addons is that they must be compiled for each target platform and architecture. Without pre-built binaries, every user who installs your package must have a working C/C++ toolchain on their machine.
- OverviewSpecial Topics
This section covers advanced Node-API patterns that you will encounter once your native addon grows beyond a simple synchronous function call.
- Context AwarenessSpecial Topics
Node.js has historically run as a single-threaded process. This all changed with the introduction of Worker Threads in Node 10. Worker Threads add a JavaScript-friendly concurrency abstraction that native add-on developers need to be aware of. What this means practically is that your native add-on may be loaded and unloaded more than once and its code may be executed concurrently in multiple threads. There are specific steps you must take to ensure your native add-on code runs correctly.
- Object and Function ReferencesSpecial Topics
JavaScript implements a dynamic memory model. When objects are no longer reachable, they become eligible for reclamation by the garbage collector running in the background.
- AsyncWorkerSpecial Topics
You may have a project in which you have a piece of long-running C/C++ code that you want to run in the background instead of on Node's main event loop. Node-API's AsyncWorker class is designed specifically for this case.
- Thread-Safe FunctionsSpecial Topics
JavaScript functions can normally only be called from a native addon's main thread. If an addon creates additional threads, then node-addon-api functions that require a Napi::Env, Napi::Value, or Napi::Reference must not be called from those threads.