devinivy / mo-walk

walk directory for esm and cjs modules
MIT License
0 stars 1 forks source link

File not found issues when using custom node loader #4

Open Toktik opened 1 year ago

Toktik commented 1 year ago

We're using TestDouble.js's replaceEsm for mocking.

To use TestDouble.js with ESM, we have to use their node loader. When using their loader, mo-walk fails when trying to load a number of files used by different hapi libraries. Such as .hc.mjs, caches.mjs, and so on.

It turns out that TestDouble returns ENOENT instead of ERR_MODULE_NOT_FOUND. I'm not sure whether that is TestDouble issue (throwing wrong error) or mo-walk's issue, not handling ENOENT. But I have forked mo-walk and solved it like this: https://github.com/devinivy/mo-walk/commit/e2d20b9862985e4b90bdd024dc6c03c07fa38199

Btw, this is the command I'm invoking when running tests: cross-env NODE_OPTIONS=--loader=testdouble node lab.cjs -a @hapi/code

devinivy commented 1 year ago

Ah, interesting! Thanks for finding that case. In my view ideally that loader would maintain node's behavior and rethrow the ERR_MODULE_NOT_FOUND error.

I think the starting point is to see if testdouble has a differing perspective on it. I am hoping to keep this module constrained to node's behaviors as much as possible, but I would be open to hearing from the author of a loader such as testdouble, in case there are some practical reasons why throwing ERR_MODULE_NOT_FOUND isn't practical/possible for them.