Created
April 17, 2020 20:59
-
-
Save jastisriradheshyam/541ebae80adbd52e11b3eed0223da943 to your computer and use it in GitHub Desktop.
Change nodejs process current working directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path'); | |
// here after the dirname just make it to the root path of your choice | |
// all the path.resolve() will resolve from the process current directrory | |
// and chdir change that directory | |
const val = path.resolve(path.dirname(require.main.filename)+"/../..") | |
process.chdir(val); | |
/* | |
use case: | |
when use root index file is somewhere else and your testing file at another path, | |
sometimes this root path or application execution path causes app to fail reading files | |
at relative paths. So, we can change the directory of the process to root app path | |
and also test from testing directory | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment