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

132
Views
How to link the imported dependencies of module created by vm.SourceTextModule to it?

Let's say we are creating a module called app by constructing a new vm.SourceTextModule object:

const context = {
  exports: {},
  console,  // custom console object
};
const sandbox = vm.createContext(context);

const app = new vm.SourceTextModule(
  `import path from 'path';
   console.log(path.resolve('./src'));`,
  {
    context: sandbox,
  }
);

According to the Node.js documentation to obtain the default export from path module we should "link" the imported dependencies of app module to it.
To achieve this we should pass linker callback to app.link method:

async function linker(specifier, referencingModule) {
    // the desired logic...
}

await app.link(linker);

How to implement linker function properly so that we could import path module in newly created app module and use it:

await app.evaluate();  // => /home/user/Documents/project/src

P.S. We are using TypeScript, so I checked if we have installed types for path package.

package.json:

"@types/node": "^17.0.31",
about 4 years ago · Juan Pablo Isaza
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!