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.
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.