Skip to content

Instantly share code, notes, and snippets.

@jastisriradheshyam
Created April 17, 2020 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jastisriradheshyam/541ebae80adbd52e11b3eed0223da943 to your computer and use it in GitHub Desktop.
Save jastisriradheshyam/541ebae80adbd52e11b3eed0223da943 to your computer and use it in GitHub Desktop.
Change nodejs process current working directory
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