Skip to content

Paths and require

Talysson de Oliveira Cassiano edited this page Mar 30, 2017 · 4 revisions

You'll notice that in the files from the codebase most of the time we don't use ../ on the requires but instead we require files from the application as the root of the project was a Node package, like the way we require User from the domain layer.

It's important to warn you that it's not the default behavior of Node's require, but we change that defining a environment variable called NODE_PATH with the path of the root of the project. Doing that we're defining that the root of the project may be used as any other package, making our life easier when it comes to requiring files, but it will not prevent you from using relative paths normally when requiring files!

This variable should be set on every entry point of execution of your project for it to work properly, that's why we use it in almost all of our npm scripts on package.json file, we don't do that for the production mode (the start script) because it's done on the cluster.js file directly.

Setting this variable to make your life easier will never bite you, but be aware that you have to remember to set it in case you add new npm scripts that will interact with files that use non-relative paths on require.

Clone this wiki locally