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

279
Visualizações
Best way to evaluate javascript source code module with node.js

With node, I transpile a typescript tsx file int javascript. Then I need to call a function that is described in that javascript code. ES6 modules are used.

I use

writeFileSync('./temp/page1.js', js) // js is the module source code
import page from '../temp/page1.js'

page is here the default export which is a function that I want to call.

That works fine but I wonder if there is a better way to call the function inside the module source code? A way that avoids saving the source code into a temporary file.

eval would not be a security concern, but I think it would not give me the default export, modules are not considered in this old function.

Maybe something node related?

I found https://www.npmjs.com/package/node-eval which would avoid the file saving, although it still requires a filename, which causes some brittleness in my code.

node:module node:vm

are maybe promising, looking at https://github.com/node-eval/node-eval/blob/master/index.js but I have not yet figured out if and how.

Similar, but more related to require: Load node.js module from string in memory

https://github.com/floatdrop/require-from-string did not work either

all the variants with node:vm like

import vm from 'node:vm'
let context = vm.createContext({}, {name:"temp1.mjs"})
vm.runInContext(js, context, {filename: "temp2.mjs"})

result in "Cannot use import statement outside a module"

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The best way would be nodes vm.Module class

https://nodejs.org/api/vm.html#vm_module_evaluate_options

but that is in draft stage and behind feature flags.

over 4 years ago · Santiago Trujillo 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