Skip to content

Repository files navigation

JavaScript Code Runner

NPM versionNPM downloads

JavaScript Code Runner allows the execution of JavaScript code. This package base on an awesome JS-Interpreter written by Google's Neil Fraser.

Execution is completely isolated from the global JavaScript environment. None of the DOM APIs are exposed. That is the point of a sandbox.

Installation

npm i javascript-code-runner

Usage

importJSrunnerfrom"javascript-code-runner";constfibonacciCode=`const result = [];const fibonacci = (n, result) => { var a = 1, b = 1, sum; for (var i = 0; i < n; i++) { result.push(a); sum = a + b; a = b; b = sum; }}fibonacci(12, result);result.join(', ');`;const{ result, message }=JSrunner(fibonacciCode);console.log(result);// 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144console.log(message);// null (error message)

Example with a web worker

worker.js

importJSrunnerfrom"javascript-code-runner";addEventListener("message",(e)=>{postMessage(JSrunner(e.data));});

About

Run your JavaScript code snippets in the browser. A sandboxed JavaScript interpreter.

Topics

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages