I am having trouble requiring from parent directories within NodeJS. I have read this post, but still couldn't figure it out.
node.js require from parent folder
This is my file structure:
-- components/
-- windows/
-- index.js
-- index.js
-- main.js
This is the code:
// /main.js
var components = require("./components")
components.windows.inner()
// /components/index.js
module.exports = {
windows: require("./windows"),
foo: "foo",
}
// /components/windows/index.js
var components = require("./..")
module.exports.inner = function() {
console.log(components.foo)
}
When I run main.js
, the inner()
function prints undefined
.
Why is it printing undefined? Shouldn't it print foo? Am I missing something about how Node works?