Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

297
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda