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

134
Vistas
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
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