I have an express project where I have a directory structure like so,
- my-app
- routes
- index.js
- views
- public
- components
- toolbar
- test
- components
- toolbar
- routes
Now let’s say from my routes I want to require the component toolbar I do it like this
toolbar = require(__dirname + ‘/../components/toolbar’)
Now when I run my test for routes I need to require routes. When I do this I get an error at runtime that toolbar file could not be found.
Is there some global available like say __express_home that I could use in my require so that I would not run into this issue? I would then use it as so,
toolbar = require(__express_home + ‘/components/toolbar’)
You can just do:
Here is an example from express’s github repo.