A secure, isolated JavaScript VM for Node.js, built with TypeScript. Developed and maintained as an open source project by ChatFinAI.
Visit our official website:
π ChatFin β AI Finance Platform
Connect with us on LinkedIn:
π ChatFin LinkedIn
Explore our SuiteApp listing on NetSuite:
π ChatFin AI for NetSuite β SuiteApp
Read our latest press release:
π ChatFin Launches Next-Gen AI Releases to Revolutionize Finance
Book a Demo:
π Book Demo
- Run untrusted JavaScript code securely in an isolated VM
- TypeScript-first development
- Uses isolated-vm for sandboxing
- WebSocket inspector support for debugging
- Extensible sandbox API
- Node.js >= 18
- npm >= 9
npm install @chatfinai/javascript-vmClone the repository and install dependencies:
git clone https://github.com/ChatFinAI/javascript-vm.git
cd javascript-vm
npm installnpm run buildRun the sample calculator example:
npm run devYou can debug the TypeScript code using VS Code or any Node.js debugger:
node --inspect-brk -r ts-node/register src/index.tsThen attach your debugger to localhost:9229.
To enable the VM inspector, instantiate ExecSandbox with debug=true and specify a port (default: 3000):
const sandbox = new ExecSandbox({}, true, 3000);This starts a WebSocket inspector server. You can connect Chrome DevTools to:
devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:3000
for live debugging of the isolated VM context.
npm run type-checkimport { ExecSandbox, GenDictionary } from '@chatfinai/javascript-vm';
const calculatorCode = `
function add(a, b) { return a + b; }
function subtract(a, b) { return a - b; }
function multiply(a, b) { return a * b; }
function divide(a, b) { return a / b; }
result = {
add: add(5, 3),
subtract: subtract(5, 3),
multiply: multiply(5, 3),
divide: divide(5, 3)
};
`;
async function runCalculatorSandbox() {
const sandbox = new ExecSandbox(false);
try {
const output = await sandbox.run(calculatorCode, {} as GenDictionary);
console.log("Calculator Results:", output.result);
} catch (err) {
console.error("Sandbox error:", err);
} finally {
sandbox.dispose();
}
}
runCalculatorSandbox();const { ExecSandbox } = require('@chatfinai/javascript-vm');
const calculatorCode = `
function add(a, b) { return a + b; }
function subtract(a, b) { return a - b; }
function multiply(a, b) { return a * b; }
function divide(a, b) { return a / b; }
result = {
add: add(5, 3),
subtract: subtract(5, 3),
multiply: multiply(5, 3),
divide: divide(5, 3)
};
`;
async function runCalculatorSandbox() {
const sandbox = new ExecSandbox(false);
try {
const output = await sandbox.run(calculatorCode, {});
console.log("Calculator Results:", output.result);
} catch (err) {
console.error("Sandbox error:", err);
} finally {
sandbox.dispose();
}
}
runCalculatorSandbox();src/VM/β Core VM and sandbox classessrc/Models/β Type definitionssrc/index.tsβ Main exports for npm packageexamples/β Calculator usage examples (JavaScript and TypeScript)
The examples/ directory contains calculator examples showing how to use the VM:
calculator.jsβ JavaScript (CommonJS) examplecalculator.tsβ TypeScript example with proper types
npm install @chatfinai/javascript-vmThen update the import statements in the examples:
- In
calculator.js: Changerequire('../dist/index.js')torequire('@chatfinai/javascript-vm') - In
calculator.ts: Changefrom '../src/index'tofrom '@chatfinai/javascript-vm'
# Run JavaScript example
node examples/calculator.js
# Run TypeScript example
npx ts-node examples/calculator.ts# First, build the project
npm run build
# Run JavaScript example (uses dist/index.js)
node examples/calculator.js
# Run TypeScript example (uses src/index.ts)
npx ts-node examples/calculator.tsThis project is licensed under the MIT License. See the LICENSE file for details.
- ChatFinAI Open Source Team (support@chatfin.ai)
For questions or support, open an issue on GitHub or contact us at support@chatfin.ai.