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

183
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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