Node.js Native Modules
Lynxtron's support for Node.js native modules is aligned with Electron's: both can use npm native modules in the Node.js environment that runs with the app, and both can load .node binary artifacts from those modules.
The main difference is the rebuild target. In Electron, people usually use @electron/rebuild to rebuild native modules for the Electron runtime. In Lynxtron, use @lynx-js/lynxtron-rebuild to rebuild native modules for the Lynxtron runtime.
What is a Node.js native module?
A Node.js native module is an npm package that contains not only JavaScript code, but also locally compiled artifacts, usually .node files. They are commonly implemented in C/C++ and exposed to JavaScript through Node-API or node-gyp, and are suitable for scenarios such as:
- high-performance capabilities provided by C/C++ extensions
- npm packages that depend on
.nodebinary artifacts - local system capabilities that are only accessible in the Node.js environment
Lynxtron supports these modules, but there is one important thing to note: native modules must be rebuilt for the Lynxtron runtime.
If a module was compiled only for your regular local Node.js environment, you may run into ABI mismatch errors when loading it in Lynxtron. A typical error looks like this:
In Electron, people usually solve this with @electron/rebuild. In Lynxtron, use @lynx-js/lynxtron-rebuild instead.
Rebuild and Require Native Modules
When using an npm native module in Lynxtron, install the module and run @lynx-js/lynxtron-rebuild first, then import or require the module in the Node.js environment.
Install the npm native module you need, and install @lynx-js/lynxtron-rebuild:
Then run @lynx-js/lynxtron-rebuild to rebuild native modules for Lynxtron:
After rebuild is complete, import the native module in the Node.js environment:
If you need to expose Node.js capabilities to Lynx UI, use bridge messaging or preload scripts as described in Communication Between Node.js and Lynx.
Example: Todo List Application
✅ You have already completed Getting Started with Lynxtron
✅ Your machine already has the local build toolchain required by node-gyp, such as Python, C/C++ Build Tools, or Xcode Command Line Tools
This example uses better-sqlite3 to build a Todo List application:
- Node.js side: use
better-sqlite3to persist to-do items in a local SQLite database - Lynx side: call Node.js logic through communication APIs, display the to-do list, and support adding, toggling completion state, and deleting items
The Node.js main process in this example imports better-sqlite3 and initializes the database and schema:
Related docs
Next steps
Once you are comfortable with Node.js native modules, you can try:
- Add more local capabilities: for example, use
sharpfor thumbnail generation, ornode-ptyfor an embedded terminal - Combine with Lynx native libraries: use Node.js native modules together with Lynx native libraries to build more complex desktop applications
