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

295
Views
node vm runInContext Unexpected token 'if'

I've faced a weird error while playing with a VM in node.js

Lemme share a piece of the code, that produces an error:

  const vm = require("VM");

  const context = vm.createContext({result: null});

  const someEvalCode = "if(0==0){0}else{0}";

  const script = new vm.Script(`this.result = ${someEvalCode}`);

  try {
    script.runInContext(context);
  } catch (e) {
    console.log(e.message);
  }

The output:

SyntaxError: Unexpected token 'if'
   at new Script (vm.js:100:7)
   at Object.<anonymous> (/Users/ypetri/Desktop/my files/apps/nodejsApps/helpMeBear/server.js:1995:16)
   at Module._compile (internal/modules/cjs/loader.js:1063:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
   at Module.load (internal/modules/cjs/loader.js:928:32)
   at Function.Module._load (internal/modules/cjs/loader.js:769:14)
   at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
   at internal/main/run_main_module.js:17:47

Any help is appreciated!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Because you can't assign if as statement (this.result = if(0=0){0} else{0} is invalid syntax). Maybe try ternary operator instead:

  const someEvalCode = "0==0 ? 0 : 0";

  const script = new vm.Script(`this.result = ${someEvalCode}`);
about 4 years ago · Juan Pablo Isaza Report
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!