65 lines
1.4 KiB
Markdown
65 lines
1.4 KiB
Markdown
# Node.js Native Addon Example
|
|
|
|
This project is a minimal example of a Node.js native addon written in C++ and built with `node-gyp`, exposing a simple function to JavaScript.
|
|
|
|
## Overview
|
|
|
|
The addon compiles a C++ binding into a native `.node` module that can be required from Node.js and invoked like a normal JavaScript function.
|
|
|
|
## Project Structure
|
|
|
|
* `src/binding.cc`
|
|
Main C++ source file defining the native addon.
|
|
|
|
* `binding.gyp`
|
|
`node-gyp` build configuration.
|
|
|
|
* `build/`
|
|
Generated build artifacts (created by `node-gyp`).
|
|
|
|
* `test.js` / `test-worker.js`
|
|
Simple test scripts to verify the addon.
|
|
|
|
* `package.json`
|
|
Project metadata and dependencies.
|
|
|
|
## Prerequisites
|
|
|
|
* Node.js (with headers available)
|
|
* Python (required by `node-gyp`)
|
|
* C/C++ build tools
|
|
|
|
* Linux: `build-essential`
|
|
* macOS: Xcode Command Line Tools
|
|
* Windows: Visual Studio Build Tools
|
|
|
|
## Build Instructions
|
|
|
|
Run the following commands in the project root:
|
|
|
|
```
|
|
node-gyp configure
|
|
node-gyp build
|
|
```
|
|
|
|
This will generate the native module under `build/Release/`.
|
|
|
|
## Usage
|
|
|
|
Example JavaScript usage:
|
|
|
|
```
|
|
const binding = require("./build/Release/binding");
|
|
|
|
console.log(binding.hello()); // outputs: "hello world"
|
|
```
|
|
|
|
## Notes
|
|
|
|
* This repository is intended as a learning or reference project.
|
|
* The build output should not be edited manually.
|
|
* For distribution, consider ignoring `build/` in version control and rebuilding per environment.
|
|
|
|
## License
|
|
|
|
MIT |