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

137
Visualizações
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 à 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