K the Kelvin
K the Kelvin

Reputation: 102

How to require module from parent folder in Node/Express JS?

-controller
---user_controller.js

-router
---user
------user_router.js

I have a file structure as above in my Express Js project, but I am facing difficulties to require user_controller from user_router. The only way I can required that is by using this method :

const user_controller = require(process.mainModule.path + '/controller/user_controller')

But something tells me that this might not be the best practice to do so. Anyone hove any idea to do so?

Upvotes: 0

Views: 102

Answers (2)

K the Kelvin
K the Kelvin

Reputation: 102

require('./../../controller/user_controller') do solved the problem. Sorry for being silly, and Thanks.

Upvotes: 0

David Vicente
David Vicente

Reputation: 3131

Try this:

const user_controller = require('../controller/user_controller);

Upvotes: 1

Related Questions