I need to be able to use require() on a dynamic relative path - meaning that the relative path needs to change depending on the current environment.
What is the best practice for this type of situation?
I thought of something like this:
var module = require(process.env.MY_MODULES_PATH + '/my-module');
However environment variables are not very convenient.
Are there other possibilities?
- Maybe use package.json post-install script to set the environment variable for me?
- Maybe there's a built in solution in node I don't know about?
EDIT
I just realized that this is a special case of require()
"mocking". Is there a best practice for how to mock require()
for unit-tests for example?