A unified API to execute different programming languages in the browser using web workers. This package provides a seamless way to run JavaScript, Python, and Lua code directly in the browser without blocking the main thread.
- 🚀 Web Worker Support: All code execution runs in separate threads to keep your main thread responsive
- 🐍 Python Support: Execute Python code using Pyodide (CPython compiled to WebAssembly)
- 🔧 JavaScript Support: Execute JavaScript/TypeScript code in a sandboxed environment
- 🎯 Lua Support: Execute Lua code using Fengari (Lua VM in JavaScript)
- ⏱️ Timeout Protection: Configurable timeouts to prevent infinite loops
- 📦 TypeScript Support: Full TypeScript definitions included
- 🧪 Comprehensive Testing: Thorough test coverage for all features
npm install browser-code-runnerimport { runCode } from 'browser-code-runner';
// Execute Python code
const result = await runCode({
language: 'python',
code: 'print("Hello from Python!")',
timeout: 2000
});
console.log(result);
// {
// stdout: "Hello from Python!\n",
// stderr: "",
// exitCode: 0,
// timeMs: 15
// }Experience the Browser Code Runner in action with our interactive demo:
- 🌐 Live Demo: View Demo
- 📱 Mobile Responsive: Works perfectly on all devices
- 🎨 Monaco Editor: Professional code editing experience
- 🚀 Real-time Execution: See results immediately
Run locally:
npm install
npm run demo-
JavaScript/TypeScript
- Runs natively in the browser
- Sandboxed execution environment
- Full access to JavaScript APIs
-
Python
- Powered by Pyodide (CPython → WebAssembly)
- ~7-10 MB initial load (cached after first use)
- Full Python standard library support
-
Lua
- Powered by Fengari (Lua VM in JavaScript)
- ~1 MB lightweight interpreter
- Easy to embed and fast execution
The main function to execute code in any supported language.
interface RunCodeOptions {
language: 'javascript' | 'python' | 'lua';
code: string;
stdin?: string;
timeout?: number; // Default: 5000ms
}interface RunCodeResult {
stdout: string;
stderr: string;
exitCode: number; // 0 for success, non-zero for errors
timeMs: number; // Execution time in milliseconds
}Create a new CodeRunner instance for advanced usage.
Advanced API for managing multiple code executions.
const runner = createCodeRunner();
// Execute code
const result = await runner.runCode({
language: 'javascript',
code: 'console.log("Hello World");'
});
// Clean up resources
runner.terminate();import { runCode } from 'browser-code-runner';
// Simple JavaScript
const jsResult = await runCode({
language: 'javascript',
code: `
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((a, b) => a + b, 0);
console.log('Sum:', sum);
`
});
console.log(jsResult.stdout); // "Sum: 15\n"import { runCode } from 'browser-code-runner';
// Python with libraries
const pythonResult = await runCode({
language: 'python',
code: `
import math
import random
numbers = [random.randint(1, 100) for _ in range(5)]
print(f"Numbers: {numbers}")
print(f"Average: {sum(numbers) / len(numbers):.2f}")
print(f"Square root of 16: {math.sqrt(16)}")
`,
timeout: 3000
});
console.log(pythonResult.stdout);import { runCode } from 'browser-code-runner';
// Lua programming
const luaResult = await runCode({
language: 'lua',
code: `
function fibonacci(n)
if n <= 1 then
return n
end
return fibonacci(n-1) + fibonacci(n-2)
end
for i = 0, 10 do
print("fib(" .. i .. ") = " .. fibonacci(i))
end
`,
timeout: 2000
});
console.log(luaResult.stdout);import { runCode } from 'browser-code-runner';
try {
const result = await runCode({
language: 'javascript',
code: 'undefined.method();', // This will cause an error
timeout: 1000
});
} catch (error) {
console.error('Execution failed:', error.message);
}import { createCodeRunner } from 'browser-code-runner';
// Create a custom runner with specific worker URL
const runner = createCodeRunner('/path/to/custom-worker.js');
try {
const result = await runner.runCode({
language: 'python',
code: 'print("Custom worker execution")'
});
console.log(result);
} finally {
// Always clean up resources
runner.terminate();
}- All code execution runs in separate threads
- Main thread remains responsive during execution
- Automatic cleanup of worker resources
- Python (Pyodide): ~7-10 MB initial download, cached after first use
- Lua (Fengari): ~1 MB, very lightweight
- JavaScript: No additional downloads required
- Default timeout: 5000ms (5 seconds)
- Configurable per execution
- Automatic cleanup on timeout
- Modern Browsers: Chrome 80+, Firefox 75+, Safari 13+, Edge 80+
- Web Worker Support: Required
- ES2020 Features: Required
- WebAssembly: Required for Python support
git clone <repository-url>
cd browser-code-runner
npm installnpm run buildnpm test
npm run test:watchnpm run lint
npm run lint:fixnpm run format- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
MIT License - see LICENSE file for details.
- C/C++ support via Emscripten
- Ruby support via Opal or WASM MRI builds
- Go support via WebAssembly
- Rust support via WebAssembly
- Real-time collaboration features
- Code sharing and execution
- Advanced debugging capabilities
- Performance profiling
- Issues: GitHub Issues
- Documentation: GitHub Wiki
- Discussions: GitHub Discussions
- Pyodide - Python in the browser
- Fengari - Lua VM in JavaScript
- Web Workers API - Browser threading