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

185
Vistas
Unable to use 'require' in downstream node_modules

I am developing an express api, where routes are installed as a dependency of the main project. Inside the main project I have a config folder with index.js. index.js is exporting an object that serves as the route configuration.

Inside the main project, I am able to get inside this exported object from all of the files, however, when trying to require from this config object inside of the node_modules dependency, it is coming back as undefined. I have tried with both relative and absolute pathnames in the require statement and neither seem to retrieve the js object.

using commonJs. Confirmed that I am using module.exports and require statements on the receiving end. Tried require statement from app.js and include a module.exports statement there as well. No luck.

Is there some magical way to get objects exported from the main project into the dependency packages? Wondering if there is something simple I am missing.

Structure:
-app.js (main)
-package.json
--routes (folder)
    |
    index.js ('imports' route express modules from node_modules)

--config (folder)
    |
    index.js (where module.exports.objectname = {stuff:stuff} exists)

--node_modules
    |
    packagefolder (project scope)
          |
          packagename (route scope)
          |
          functions (folder)
              bunchofjs files to complete i/o of requests
          routes (folder)
              index.js (package.json main. Handles require from ../../..)
          config (folder)
              index.js (where the config require statement is.)

Sample statement from main application in the config/index.js:

const globalConfig = {
    port : 4443,
    otherinfo: "stuff"
}
module.exports.globalConfig = globalConfig

Sample code from inside node_modules/packagefolder/packagename/routes/index.js

const port = require('../../../../../config').globalConfig.port

Sample error:

TypeError: Cannot read properties of undefined

The carrot( ^ ) symbol is on the dot between globalConfig and port.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Answer attributed from @bigless in comments. Having dependency packages reach outside of the node_modules folder would be considered a security risk and is blocked by design. The strategy of the app using configs needs to be redesigned to keep this in mind.

about 4 years ago · Juan Pablo Isaza 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