{"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,"search_vec":"'1.4.8':37 'activ':42 'adher':98 'allow':62 'browser':24 'bug':48 'cadenc':44 'constructor':79 'control':63 'current':34 'design':13 'differenti':55 'ecmascript':107 'educ':95 'enabl':69 'environ':22,61,73 'es5':3,17,101 'eval':76,114 'eval5':1,5,117 'execut':15,60,66,71,93 'featur':108 'fix':49 'focus':46 'full':16 'function':78,115 'improv':52 'includ':23,56 'interpret':4,9,110,113 'isol':91 'javascript':2,8,21,70,109 'js':112 'js-interpret':111 'key':54 'like':30 'maintain':40 'mini':28 'mini-program':27 'minor':51 'nativ':75 'newer':106 'node.js':25 'particular':86 'primarili':45 'program':29 'project':39 'provid':57 'purpos':96 'releas':43 'requir':90 'restrict':83 'sandbox':59 'scenario':89 'script':65,92 'specif':102 'strict':97 'support':104 'syntax':18 'taro':33 'time':67 'typescript':12,118 'unavail':81 'use':87 'various':20 'version':36 'vm':116 'wechat':31 'within':19 'without':103 'written':10","created_at":"2026-04-25T04:59:24.920231+00:00","updated_at":"2026-04-25T04:59:24.920231+00:00","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,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/bplok20010/eval5","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/eval5","openapi_spec":null,"status_page":null,"smithery":null,"categories":["web-framework"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-24","install_tag":null}}