Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
How to compile and run a javascript code from string using node server

The scenario

I am planning to develop a small online judge site for hiring purpose, where I can create contests and participants can solve and submit their solutions.

For that I will provide them an online code editor, where they write their codes and submit. After the submission the source code will send to a node server using a POST request and the source code will send as a string.

Now the problem is how to compile and run the javascript code from string and get the result.

What have I tried

function runCode(code) {
    let input = 5;
    const wrap = source => `{ return ${source} };`;
    const fn = new Function(wrap(code));
    const output = fn.call(null).call(null, input); // The output is n * n = 5 * 5 = 25
  console.log(output);
}

runCode(`function myFunction(n) { return n * n; }`);

What am I seeking for-

The code is working perfectly, but I don't understand is this a good way to implement the scenario? Is there any other better & secure way? I've heard about the node's vm package.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!