A native Node.js module for embedding Lua in your applications. This module provides seamless integration between JavaScript and Lua, allowing you to execute Lua scripts, pass functions between environments, and handle complex data structures.
- Node.js
- Bun
- Deno
- Execute Lua scripts from Node.js
- Pass JavaScript functions to Lua as callbacks
- Bidirectional data exchange (numbers, strings, booleans, objects, arrays)
- Global variable management
- Comprehensive error handling
- Cross-platform support (Windows, macOS, Linux)
- TypeScript support with full type definitions
npm install lua-native
NOTE: Windows and MacOS are supported as a prebuilt binary. Linux prebuilt binaries are coming soon.
If you want to build the module from source:
VCPKG is required to build the module. Once installed, you can install Lua via
vcpkg install lua
.
VCPKG is available at vcpkg
NOTE: There are two ways to build this module. You can use traditional
bindings.gyp
along with node-gyp
or you can use cmake
on
Windows and MacOS. I have not configured the build for Linux yet.
# Debug build
npm run build-debug
# Release build
npm run build-release
Hello World:
import lua_native from "lua-native";
// Create a new Lua context
const lua = new lua_native.init({
print: (msg: string) => {
console.log(msg);
},
});
// Execute a simple script
lua.execute_script('print("Hello, World!")');
Return a value:
import lua_native from "lua-native";
// Create a new Lua context
const lua = new lua_native.init({});
// Execute a simple script
const result = lua.execute_script("return 42");
console.log(result); // 42
import lua_native from "lua-native";
// Create context with JavaScript function
const lua = new lua_native.init({
add: (a, b) => a + b,
});
// Call the JavaScript function from Lua
const result = lua.execute_script("return add(2, 3)");
console.log(result); // 5
import lua_native from "lua-native";
const lua = new lua_native.init({});
// Set global variables
lua.set_global("x", 7);
lua.set_global("times2", (n) => n * 2);
// Use globals in Lua script
const [a, b] = lua.execute_script("return x, times2(x)");
console.log(a, b); // 7, 14
The module supports converting complex Lua tables to JavaScript objects:
import lua_native from "lua-native";
const lua = new lua_native.init({
greet: (name) => `Hello, ${name}!`,
});
const result = lua.execute_script(`
local t = {
numbers = {1, 2, 3},
flags = { on = true, off = false },
msg = greet('World')
}
return t
`);
console.log(result);
// {
// numbers: [1, 2, 3],
// flags: { on: true, off: false },
// msg: 'Hello, World!'
// }
Lua errors are automatically converted to JavaScript exceptions:
import lua_native from "lua-native";
const lua = new lua_native.init({});
try {
lua.execute_script("error('Something went wrong')");
} catch (error) {
console.error("Lua error:", error.message);
}
The module includes comprehensive TypeScript definitions:
import lua_native from "lua-native";
import type { LuaCallbacks, LuaContext } from "lua-native/types";
// Type-safe callback definition
const callbacks: LuaCallbacks = {
add: (a: number, b: number): number => a + b,
greet: (name: string): string => `Hello, ${name}!`,
};
const lua: LuaContext = new lua_native.init(callbacks);
const result: number = lua.execute_script("return add(10, 20)");
Creates a new Lua execution context.
Parameters:
callbacks
(optional): Object containing JavaScript functions and values to make available in Lua
Returns: LuaContext
instance
Executes a Lua script and returns the result.
Parameters:
script
: String containing Lua code to execute
Returns: The result of the script execution (converted to appropriate JavaScript type)
Sets a global variable or function in the Lua environment.
Parameters:
name
: Name of the global variablevalue
: Value to set (function, number, string, boolean, or object)
Lua Type | JavaScript Type | Notes |
---|---|---|
nil |
null |
|
boolean |
boolean |
|
number |
number |
|
string |
string |
|
table (array-like) |
Array |
Sequential numeric indices starting from 1 |
table (object-like) |
Object |
String or mixed keys |
function |
Function |
JavaScript functions passed to Lua |
npm test
src/
- C++ source codelua-native.cpp
- Main module implementationcore/lua-runtime.cpp
- Lua runtime wrapper
tests/
- Test filests/lua-native.spec.ts
- TypeScript/JavaScript testscpp/lua-native-test.cpp
- C++ unit tests
types.d.ts
- TypeScript type definitions
https://github.com/frankhale/lua-native.git
MIT
Frank Hale
21 August 2025