eval5: JavaScript ES5 Interpreter
raw JSON →eval5 is a JavaScript interpreter written in TypeScript, designed to execute full ES5 syntax within various JavaScript environments including browsers, Node.js, and mini-programs like WeChat and Taro. Currently at version 1.4.8, the project maintains an active release cadence, primarily focusing on bug fixes and minor improvements. Its key differentiators include providing a sandboxed execution environment, allowing control over script execution time, and enabling JavaScript execution in environments where native `eval` or `Function` constructors are unavailable or restricted. It's particularly useful for scenarios requiring isolated script execution or educational purposes, strictly adhering to the ES5 specification without support for newer ECMAScript features.
Common errors
error ReferenceError: <variable_name> is not defined ↓
Interpreter instance. If using the standalone evaluate function, pass all necessary variables and functions into its ctx parameter for each invocation. error TypeError: Illegal invocation ↓
new Interpreter({ alert: window.alert.bind(window) }). error TypeError: Cannot read properties of undefined (reading '<property>') ↓
globalContextInFunction option in the Interpreter constructor or globally via Interpreter.globalContextInFunction to set the desired this context for functions. Warnings
breaking The `rootContext` property on the `Interpreter` constructor options was removed in version 1.4.0. It was replaced by `globalContextInFunction`. ↓
gotcha By default, `this` inside functions evaluated by `eval5` will be `undefined`, unlike native non-strict JavaScript where it defaults to the global object. This can lead to unexpected behavior for code expecting `this` to point to `window` or `global`. ↓
gotcha When calling host environment methods (like `alert` or `console.log`) passed into the interpreter's context, you might encounter 'Illegal invocation' errors if the methods are not correctly bound. This happens because `eval5` might call them without the correct `this` context. ↓
gotcha eval5 only supports the ES5 specification. It does not parse or execute newer ECMAScript features (ES6+), such as `let`/`const`, arrow functions, classes, modules, or async/await. ↓
Install
npm install eval5 yarn add eval5 pnpm add eval5 Imports
- Interpreter wrong
const Interpreter = require('eval5').Interpretercorrectimport { Interpreter } from 'eval5' - evaluate wrong
const evaluate = require('eval5').evaluatecorrectimport { evaluate } from 'eval5' - Function wrong
const CustomFunction = require('eval5').Functioncorrectimport { Function } from 'eval5'
Quickstart
import { Interpreter } from "eval5";
// Define a custom context for the interpreter
const myContext = {
customVar: 42,
log: (...args: any[]) => console.log('INTERPRETER LOG:', ...args)
};
const interpreter = new Interpreter(myContext, {
timeout: 5000, // Set a timeout for execution (5 seconds)
// globalContextInFunction: window // For browser environments to set 'this' in functions
});
let result;
try {
interpreter.evaluate("var a = 100;");
interpreter.evaluate("var b = customVar * 2;");
result = interpreter.evaluate("a + b;");
myContext.log(`First evaluation result: ${result}`); // INTERPRETER LOG: First evaluation result: 184
interpreter.evaluate("function add(x, y) { return x + y; }");
result = interpreter.evaluate("add(a, b);");
myContext.log(`Second evaluation result (calling function): ${result}`); // INTERPRETER LOG: Second evaluation result (calling function): 184
// Demonstrate scope isolation
const outerScopeVar = 500;
interpreter.evaluate(`
var c = ${outerScopeVar};
log('Inside interpreter, c is:', c);
var d = 10;
`);
// Accessing 'd' outside the interpreter's scope would fail:
// console.log(interpreter.evaluate("d;")); // ReferenceError: d is not defined
myContext.log('Interpreter operations complete.');
} catch (e: any) {
console.error("Interpreter error:", e.message);
}