A Neon project demonstrating Rust-Node.js interoperability. This project uses Neon to build native Node.js modules in Rust.
- Node.js (>= 20.x)
- Rust (>= 1.85.0)
- pnpm
.
├── lib/
│ └── index.js # Node.js entry point
├── native/
│ ├── Cargo.toml # Rust project configuration
│ ├── build.rs # Rust build script
│ └── src/
│ └── lib.rs # Rust native module implementation
├── package.json # Node.js project configuration
└── README.md
Install dependencies and build the native module:
pnpm installThe root package.json acts as a convenient Makefile, wrapping both Node.js and Rust commands:
Development build:
npm run build:devProduction build:
npm run buildRun tests (runs Rust tests, then Node.js test):
npm testLint code:
npm run lintFix linting issues:
npm run lint:fixCheck Rust code without building:
npm run checkYou can also use the tools directly for more control:
Rust commands (run from native/ directory):
| Command | Description |
|---|---|
cargo build |
Development build |
cargo build --release |
Production build |
cargo test |
Run Rust tests |
cargo check |
Check code without building |
cargo clippy |
Lint Rust code |
cargo fmt |
Format Rust code |
First navigate to the native directory:
cd nativeNode.js commands (run from project root):
neon build
node lib/index.jsneon build- Build native modulenode lib/index.js- Run the example
The project includes:
- Rust unit tests (currently none defined)
- Node.js integration test (runs the module and verifies output)
Run all tests:
npm testnpm run buildThis creates an optimized release build of the native module.
- The Rust code in
native/src/lib.rsdefines functions that can be called from JavaScript - Neon provides the bridge between Rust and Node.js, handling:
- Type conversions between Rust and JavaScript
- Memory management
- Function registration
- The built native module is loaded by
lib/index.jsand can be used like any other Node.js module
The current implementation exports a simple hello function:
const addon = require('./native');
console.log(addon.hello()); // outputs: "hello node"If you encounter build errors:
-
Ensure you have the correct Rust version:
rustc --version # Should be 1.85.0 or later -
Clean and rebuild:
npm run prebuild # Cleans Rust build artifacts npm run build:dev
Neon builds native modules that are platform-specific. If you're sharing this project:
- Add
native/target/to.gitignore - Add
native/index.nodeto.gitignore - Each developer/deployment needs to build locally
UNLICENSED (private use only)