Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!