Lua2JS
raw JSON →A Lua parser and transpiler to JavaScript that generates Mozilla Parser API ASTs. The current version is 0.0.11, with no recent releases or active development evident. It targets Lua 5.1 with partial support and aims to run unmodified Lua programs in JavaScript environments. Unlike other Lua transpilers, it provides options for Lua-JS interop such as automatic conversion of LuaTable to arrays/objects and method call syntax remapping. It requires no runtime dependencies and is distributed as a single minified file. The project has significant gaps: many standard library functions (e.g., string.format, pattern matching, coroutines, debug) are missing, and certain Lua syntax features (expression table keys, goto) are unsupported. The repository appears to be in maintenance or abandoned state.
Common errors
error Error: Lua parse error at line 1: unexpected symbol near '[' ↓
error TypeError: __lua is not defined ↓
error lua2js.parse is not a function ↓
Warnings
breaking Lua standard library functions like string.format, pattern matching, coroutines, and debug are missing. ↓
gotcha Expression table keys (e.g., {[expr]=value}) are not supported. ↓
breaking Long form strings with internal ]] may cause parse errors even with [==[ syntax. ↓
deprecated The project has no recent releases since 2014 and may be abandoned. ↓
gotcha The global environment _ENV and _G are not supported; globals are defined as properties of the global object. ↓
Install
npm install lua2js yarn add lua2js pnpm add lua2js Imports
- lua2js wrong
const lua2js = require('lua2js')correctimport lua2js from 'lua2js' - parse wrong
const parse = require('lua2js').parsecorrectimport { parse } from 'lua2js' - translate wrong
const lua2js = require('lua2js'); lua2js.translate(code)correctimport { translate } from 'lua2js'
Quickstart
import lua2js from 'lua2js';
const luaCode = `
function fib(n)
if n < 2 then
return n
else
return fib(n-1) + fib(n-2)
end
end
print(fib(10))
`;
const ast = lua2js.parse(luaCode);
const jsCode = lua2js.translate(ast);
console.log(jsCode);
// Output:
// var fib = function(n) {
// if (n < 2) {
// return n;
// } else {
// return fib(n - 1) + fib(n - 2);
// }
// };
// console.log(fib(10));