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

368
Vistas
Overwrite or Overload a node_module name path

Assume i have a installed node_module called magicalModule and I have installed it via npm i magicalModule.

I import it into my code via const magic = require('magicalModule').

Is there a way for me to overwrite that specific path so that I can use my own module instead?

For example I have a modified version of that module that I want to use instead of the installed package. Maybe place something in package.json or something like:

dependencies:{
    Alias:{
         'magicalModule': process.env.local ? './myPath' : 'magicalModule'
    }
}

Something like above...

I know that there is a module called Module-alias but I'm not sure if it fits my use case.

As to why I would need to do something like this is because there is an NPM package that is used alot in multiple files and I want to replace it with another NPM package that I'm currently developing. Wanna test it locally before publishing that NPM package...

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

If you need dynamic import then put require in an if condition

let myModule = require(process.env.local ? './myPath' : 'myModule'); // make sure there is index.js inside

You cannot do it in package.json as that file is static. However if you can somehow edit package json then local dependencies can be added like

"dependencies": {
    "bar": "file:./myPath" // package.json is read inside of ./myPath
  }
over 4 years ago · Santiago Trujillo Denunciar

0

Yup, after some tinkering the solution was to use module-alias after all. In my main file I added to the top of it before any code:

if(process.env.useLocal) require('module-alias/register')

Then in my package.json file I added:

 "_moduleAliases": { "magicalModule" : "path_to_file" }

after my dependencies section, it overwrites path to node_modules and my project will use my local module instead.

Hope this helps anyone else out there who needs to do something similar to me.

Cheers

over 4 years ago · Santiago Trujillo Denunciar
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