The currently accepted self-answer's explanation falls short. It says:
Apearently fs looks at the root of the project, the package.json folder, and not realative to the file calling it, which I did not know.
Sure, but it's not happenstance or a quirk of the fs
module. The fs
module uses process.cwd()
to interpret relative paths passed to it. Unless it is changed with process.chdir()
, process.cwd()
is the directory in which the current process has been started. Since mocha
is usually launched from the root of a project, then in a mocha
process, process.cwd()
is usually going to be the root of the project. If mocha
were launched from a different directory, then process.cwd()
would have a different value.
The confusion may come from how the module system handles relative paths. A module name with a relative path will be interpreted relative to the __dirname
of the current file. This may be a value different from process.cwd()
.