{"id":18041,"library":"eval5","title":"eval5: JavaScript ES5 Interpreter","description":"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.","status":"active","version":"1.4.8","language":"javascript","source_language":"en","source_url":"https://github.com/bplok20010/eval5","tags":["javascript","interpreter","js-interpreter","eval","Function","vm","eval5","typescript"],"install":[{"cmd":"npm install eval5","lang":"bash","label":"npm"},{"cmd":"yarn add eval5","lang":"bash","label":"yarn"},{"cmd":"pnpm add eval5","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This is the primary class for creating an interpreter with a persistent context. Use for scenarios requiring stateful execution across multiple calls.","wrong":"const Interpreter = require('eval5').Interpreter","symbol":"Interpreter","correct":"import { Interpreter } from 'eval5'"},{"note":"A utility function for one-off code execution. It creates a new Interpreter instance for each call, meaning no state is shared between invocations.","wrong":"const evaluate = require('eval5').evaluate","symbol":"evaluate","correct":"import { evaluate } from 'eval5'"},{"note":"This is `eval5`'s wrapped `Function` constructor, distinct from the native JavaScript `Function`. It creates functions within the interpreter's context, respecting its scope and `globalContextInFunction` settings.","wrong":"const CustomFunction = require('eval5').Function","symbol":"Function","correct":"import { Function } from 'eval5'"}],"quickstart":{"code":"import { Interpreter } from \"eval5\";\n\n// Define a custom context for the interpreter\nconst myContext = {\n  customVar: 42,\n  log: (...args: any[]) => console.log('INTERPRETER LOG:', ...args)\n};\n\nconst interpreter = new Interpreter(myContext, {\n  timeout: 5000, // Set a timeout for execution (5 seconds)\n  // globalContextInFunction: window // For browser environments to set 'this' in functions\n});\n\nlet result;\n\ntry {\n  interpreter.evaluate(\"var a = 100;\");\n  interpreter.evaluate(\"var b = customVar * 2;\");\n  result = interpreter.evaluate(\"a + b;\");\n  myContext.log(`First evaluation result: ${result}`); // INTERPRETER LOG: First evaluation result: 184\n\n  interpreter.evaluate(\"function add(x, y) { return x + y; }\");\n  result = interpreter.evaluate(\"add(a, b);\");\n  myContext.log(`Second evaluation result (calling function): ${result}`); // INTERPRETER LOG: Second evaluation result (calling function): 184\n\n  // Demonstrate scope isolation\n  const outerScopeVar = 500;\n  interpreter.evaluate(`\n    var c = ${outerScopeVar};\n    log('Inside interpreter, c is:', c);\n    var d = 10;\n  `);\n\n  // Accessing 'd' outside the interpreter's scope would fail:\n  // console.log(interpreter.evaluate(\"d;\")); // ReferenceError: d is not defined\n\n  myContext.log('Interpreter operations complete.');\n} catch (e: any) {\n  console.error(\"Interpreter error:\", e.message);\n}","lang":"typescript","description":"Demonstrates how to create an `Interpreter` instance with a custom global context and options, then execute multiple JavaScript code snippets sequentially, maintaining state across calls and logging results."},"warnings":[{"fix":"Use `globalContextInFunction` option in the `Interpreter` constructor instead of `rootContext` to control the 'this' context within functions.","message":"The `rootContext` property on the `Interpreter` constructor options was removed in version 1.4.0. It was replaced by `globalContextInFunction`.","severity":"breaking","affected_versions":">=1.4.0"},{"fix":"Set the `globalContextInFunction` option in the `Interpreter` constructor or globally via `Interpreter.globalContextInFunction` to the desired global object (e.g., `window` or `globalThis`) to control `this` behavior in functions.","message":"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`.","severity":"gotcha","affected_versions":"All"},{"fix":"Bind host functions explicitly when passing them to the interpreter's context. For example: `const ctx = { alert: alert.bind(window) };` or `const ctx = { console: { log: console.log.bind(console) } };`.","message":"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.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure that all JavaScript code provided to `eval5` is transpiled down to ES5 syntax using tools like Babel if it originates from a newer ECMAScript version.","message":"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.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"If you need shared state across multiple code snippets, use an `Interpreter` instance. If using the standalone `evaluate` function, pass all necessary variables and functions into its `ctx` parameter for each invocation.","cause":"Attempting to access a variable that was defined in a previous `evaluate` call using the standalone `evaluate` function, or accessing a variable not in the current interpreter's scope.","error":"ReferenceError: <variable_name> is not defined"},{"fix":"Bind the host function to its original object before passing it into the interpreter's context. Example: `new Interpreter({ alert: window.alert.bind(window) })`.","cause":"A host environment function (like `alert` or `console.log`) was called from within the `eval5` interpreter without its original `this` context.","error":"TypeError: Illegal invocation"},{"fix":"Configure the `globalContextInFunction` option in the `Interpreter` constructor or globally via `Interpreter.globalContextInFunction` to set the desired `this` context for functions.","cause":"Attempting to access a property on `this` inside a function executed by `eval5` when `this` is `undefined` (which is the default behavior if `globalContextInFunction` is not set).","error":"TypeError: Cannot read properties of undefined (reading '<property>')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}