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

274
Views
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 answers
Answer question

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 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!