js_exec
Execute JavaScript code in Node.js to manipulate JSON, use npm packages, and maintain persistent state across invocations.
Instructions
Execute JavaScript code in a full Node.js environment. Returns an object with keys: success (bool), output (combined stdout/stderr), stdout, stderr, exitCode, and outputTruncated (bool if output exceeded 100KB). Use this tool when: (1) manipulating JSON data where JavaScript's native handling is cleaner than Python, (2) testing algorithms where JS syntax is preferred, (3) using npm packages not available in Python, (4) working with Node.js-specific APIs. Do NOT use this tool when: (1) generating visualizations - Node.js has no DOM, canvas, or browser APIs (use python_exec with matplotlib instead), (2) the task is equally achievable in Python (prefer python_exec for consistency). The tool provides persistent state across calls via a state object that is automatically loaded from and saved to .shell0_js_state.json in the working directory. Assign values to state.myKey to persist them between invocations. Limitations: 30-second default timeout (configurable up to 600s), 100K character source limit, 2000 line limit, 100KB output cap. On timeout the process is killed. Syntax errors return in stderr with line numbers. Requires Node.js on PATH.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | JavaScript source code to execute. Supports require(), async/await, and ES6+ syntax. Use console.log() for output. Access persistent state via the `state` object (auto-loaded/saved). Example: `const data = require('fs').readFileSync('file.json'); console.log(JSON.parse(data));` | |
| timeout | No | Execution timeout in seconds. Default: 30. Maximum: 600. |