:: krowemoh

Thursday | 26 DEC 2024
Posts Links Other About Now

previous
next

Global in Node

2022-11-17

This is a bit of magic I didn't know about.

There is a global object that you can add things to and it becomes available in basically any file in the project.

This becomes handy when I need the root path of project so I can create directories from much deeper down the project.

In app.js I can do the following:

var path = require("path");
global.appRoot = path.resolve(__dirname);
...

In a route, much deeper down, I can then do:

let filePath = appRoot + "/some/file/path";

Frikken weird. It seems to be some sort of window analog that exists in browsers. I also imagine this very poor form as attaching to window is also bad form.