One of Electron's most powerful features is the ability to combine web technologies with native code - both for compute-intensive logic as well as for the occasional native user interface, where desired.
Electron does so by building on top of "Native Node.js Addons". You've probably already come across a few of them - packages like the famous [sqlite](https://www.npmjs.com/package/sqlite3) use native code to combine JavaScript and native technologies. You can use this feature to extend your Electron application with anything a fully native application can do:
* Access native platform APIs not available in JavaScript. Any macOS, Windows, or Linux operating system API is available to you.
* Create UI components that interact with native desktop frameworks.
* Integrate with existing native libraries.
* Implement performance-critical code that runs faster than JavaScript.
Native Node.js addons are dynamically-linked shared objects (on Unix-like systems) or DLL files (on Windows) that can be loaded into Node.js or Electron using the `require()` or `import` functions. They behave just like regular JavaScript modules but provide an interface to code written in C++, Rust, or other languages that can compile to native code.
# Tutorial: Creating a Native Node.js Addon for Electron
This tutorial will walk you through building a basic Node.js native addon that can be used in Electron applications. We'll focus on concepts common to all platforms, using C++ as the implementation language. Once you complete this tutorial common to all native Node.js addons, you can move on to one of our platform-specific tutorials.
## Requirements
This tutorial assumes you have Node.js and npm installed, as well as the basic tools necessary for compiling code on your platform (like Visual Studio on Windows, Xcode on macOS, or GCC/Clang on Linux). You can find detailed instructions in the [`node-gyp` readme](https://github.com/nodejs/node-gyp?tab=readme-ov-file).
### Requirements: macOS
To build native Node.js addons on macOS, you'll need the Xcode Command Line Tools. These provide the necessary compilers and build tools (namely, `clang`, `clang++`, and `make`). The following command will prompt you to install the Command Line Tools if they aren't already installed.
```sh
xcode-select --install
```
### Requirements: Windows
The official Node.js installer offers the optional installation of "Tools for Native Modules", which installs everything required for the basic compilation of C++ modules - specifically, Python 3 and the "Visual Studio Desktop development with C++" workload. Alternatively, you can use `chocolatey`, `winget`, or the Windows Store.
### Requirements: Linux
* [A supported version of Python](https://devguide.python.org/versions/)
*`make`
* A proper C/C++ compiler toolchain, like [GCC](https://gcc.gnu.org)
## 1) Creating a package
First, create a new Node.js package that will contain your native addon:
```sh
mkdir my-native-addon
cd my-native-addon
npm init -y
```
This creates a basic `package.json` file. Next, we'll install the necessary dependencies:
```sh
npm install node-addon-api bindings
```
*`node-addon-api`: This is a C++ wrapper for the low-level Node.js API that makes it easier to build addons. It provides a C++ object-oriented API that's more convenient and safer to use than the raw C-style API.
*`bindings`: A helper module that simplifies the process of loading your compiled native addon. It handles finding your compiled `.node` file automatically.
Now, let's update our `package.json` to include the appropriate build scripts. We will explain what these specifically do further below.
*`clean`: Remove the build directory, allowing for a fresh build
*`build`: Run the standard node-gyp build process to compile your addon
## 2) Setting up the build system
Node.js addons use a build system called `node-gyp`, which is a cross-platform command-line tool written in Node.js. It compiles native addon modules for Node.js using platform-specific build tools behind the scenes:
* On Windows: Visual Studio
* On macOS: Xcode or command-line tools
* On Linux: GCC or similar compilers
### Configuring `node-gyp`
The `binding.gyp` file is a JSON-like configuration file that tells node-gyp how to build your native addon. It's similar to a make file or a project file but in a platform-independent format. Let's create a basic `binding.gyp` file:
*`target_name`: The name of your addon. This determines the filename of the compiled module (my_addon.node).
*`sources`: List of source files to compile. We'll have two files: the main addon file and our actual C++ implementation.
*`include_dirs`: Directories to search for header files. The cryptic-looking line `<!@(node -p \"require('node-addon-api').include\")` runs a Node.js command to get the path to the node-addon-api include directory.
*`dependencies`: The `node-addon-api` dependency. Similar to the include dirs, this executes a Node.js command to get the proper configuration.
*`defines`: Preprocessor definitions. Here we're enabling C++ exceptions for node-addon-api.
Platform-specific settings:
*`cflags`! and cflags_cc!: Compiler flags for Unix-like systems
*`xcode_settings`: Settings specific to macOS/Xcode compiler
*`msvs_settings`: Settings specific to Microsoft Visual Studio on Windows
Now, create the directory structure for our project:
```sh
mkdir src
mkdir include
mkdir js
```
This creates:
*`src/`: Where our source files will go
*`include/`: For header files
*`js/`: For our JavaScript wrapper
## 3) "Hello World" from C++
Let's start by defining our C++ interface in a header file. Create `include/cpp_code.h`:
```cpp
#pragma once
#include <string>
namespace cpp_code {
// A simple function that takes a string input and returns a string
The `#pragma once` directive is a header guard that prevents the file from being included multiple times in the same compilation unit. The actual function declaration is inside a namespace to avoid potential name conflicts.
Next, let's implement the function in `src/cpp_code.cc`:
console.warn('Native addon not supported on this platform')
module.exports = {
helloWorld: (input) => `Hello from JS! You said: ${input}`
}
}
```
This JavaScript wrapper:
1. Uses `bindings` to load our compiled native addon
1. Creates a class that extends EventEmitter (useful for future extensions that might emit events)
1. Instantiates our C++ class and provides a simpler API
1. Adds some input validation on the JavaScript side
1. Exports a singleton instance of our wrapper
1. Handles unsupported platforms gracefully
### Building and testing the addon
Now we can build our native addon:
```sh
npm run build
```
This will run `node-gyp configure` and `node-gyp build` to compile our C++ code into a `.node` file.
Let's create a simple test script to verify everything works. Create `test.js` in the project root:
```js title='test.js' @ts-expect-error=[2]
// Load our addon
const myAddon = require('./js')
// Try the helloWorld function
const result = myAddon.helloWorld('This is a test')
// Should print: "Hello from C++! You said: This is a test"
console.log(result)
```
Run the test:
```sh
node test.js
```
If everything works correctly, you should see:
```txt
Hello from C++! You said: This is a test
```
### Using the addon in Electron
To use this addon in an Electron application, you would:
1. Include it as a dependency in your Electron project
1. Build it targeting your specific Electron version. `electron-forge` handles this step automatically for you - for more details, see [Native Node Modules](./using-native-node-modules.md).
1. Import and use it just like any other module in a process that has Node.js enabled.
Native addon development can be written in several languages beyond C++. Rust can be used with crates like [`napi-rs`](https://github.com/napi-rs/napi-rs), [`neon`](https://neon-rs.dev/), or [`node-bindgen`](https://github.com/infinyon/node-bindgen). Objective-C/Swift can be used through Objective-C++ on macOS.
The specific implementation details differ significantly by platform, especially when accessing platform-specific APIs or UI frameworks, like Windows' Win32 API, COM components, UWP/WinRT - or macOS's Cocoa, AppKit, or ObjectiveC runtime.
This means that you'll likely use two groups of references for your native code: First, on the Node.js side, use the [N-API documentation](https://nodejs.org/api/n-api.html) to learn about creating and exposing complex structures to JavaScript - like asynchronous thread-safe function calls or creating JavaScript-native objects (`error`, `promise`, etc). Secondly, on the side of the technology you're working with, you'll likely be looking at their lower-level documentation:
* [Microsoft C++, C, and Assembler documentation](https://learn.microsoft.com/en-us/cpp/?view=msvc-170)
* [Programming with Objective-C](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011210)