Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

280
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda