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

231
Visualizações
how to override or extend ESM module in node js?

i want to add new function or override function into module in type esm.

But I do not know how ؟؟؟

in CommonJS Is as follows

add new function

// include the module that you like extend
var fs = require('fs');
 
// add a new function, printMessage(), to the module
fs.printMessage = function(str){
    console.log("Message from newly added function to the module");
    console.log(str);
}
 
// re-export the module for changes to take effect
module.exports = fs
 
// you may use the newly added function
fs.printMessage("Success");

override

// include the module whose functions are to be overridden
var fs = require('fs');
 
// delete the function you would like to override
delete fs['readFile'];
 
// add new functional with the same name as deleted function
fs.readFile = function(str){
    console.log("The functionality has been overridden.");
    console.log(str);
}
 
// re-export the module for changes to take effect
module.exports = fs
 
// you may use the newly overriden function
fs.readFile("sample.txt");

but in ESM i don't know ?

for example

test.mjs

const add = (a,b) => {
    return a+b;
}

const sub = (a,b) => {
     return a-b;
}

export {add,sub};

index.js

import * as m from './test.mjs';

now , how to add new function or override 'add' or 'sub' in test.mjs 
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