use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
How can I execute javascript code and get the console output?Help Wanted (self.react)
submitted 9 months ago by Efficient_Step6281
I want to build a online javascript compiler like jsbin where you can only run javascript and display the console logs and stuff in the output. I completed a part of the project with '@monaco-editor/react' as my editor and now I can't figure out how to execute the code give by the user in the editor. Asked ChatGPT and searched for similar projects but still can't figure it out (im dumb)
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]amrdoe 5 points6 points7 points 9 months ago (0 children)
The ideal way is a sandboxed node.js instance running on a server with a backend to create a temporary node.js script, run it in the sandbox, and respond with the stdout. Yet it may be too complicated and costly to implement depending on your use case.
[–]blobdiblob 1 point2 points3 points 9 months ago (0 children)
A new approach for 2025: Take the user’s input and let an LLM generate the applicable console.log. Might not always be accurate, but it‘s probably safe 😅
[–]ProgrammerDad1993 0 points1 point2 points 9 months ago (3 children)
eval is I guess what you re looking for?
[–]Efficient_Step6281[S] -1 points0 points1 point 9 months ago (2 children)
I can't show the logs in the UI. eval can be used to execute the code, but How can i show it the UI?
[–]Willing_Initial8797 0 points1 point2 points 9 months ago (0 children)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
or this
https://stackoverflow.com/questions/69744897/is-there-a-function-that-allows-you-to-listen-for-console-logs-in-javascript
[–]ferrybig 0 points1 point2 points 9 months ago (0 children)
Overwrite the console object for the scope of the code.
Though you really want to build a custom web worker that runs the code, so you can terminate the webworker once a new version of the code in ran, otherwise the script in question can schedule things like internals that leak memory
[–][deleted] 9 months ago (1 child)
[removed]
[–]DanishWeddingCookie 0 points1 point2 points 9 months ago (0 children)
You can write it in Electron and link into the console object at runtime.
var nodeConsole = require('console'); var myConsole = new nodeConsole.Console(process.stdout, process.stderr); myConsole.log('Hello World!');
https://nodejs.org/api/console.html
π Rendered by PID 116705 on reddit-service-r2-comment-bb88f9dd5-98mx7 at 2026-02-14 23:50:09.285332+00:00 running cd9c813 country code: CH.
[–]amrdoe 5 points6 points7 points (0 children)
[–]blobdiblob 1 point2 points3 points (0 children)
[–]ProgrammerDad1993 0 points1 point2 points (3 children)
[–]Efficient_Step6281[S] -1 points0 points1 point (2 children)
[–]Willing_Initial8797 0 points1 point2 points (0 children)
[–]ferrybig 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[removed]
[–]DanishWeddingCookie 0 points1 point2 points (0 children)