On this page

    M

    process.chdir

    History
    process.chdir(directory): void
    Attributes
    directory:string

    The process.chdir() method changes the current working directory of the Node.js process or throws an exception if doing so fails (for instance, if the specified directory does not exist).

    import { chdir, cwd } from 'node:process';
    
    console.log(`Starting directory: ${cwd()}`);
    try {
      chdir('/tmp');
      console.log(`New directory: ${cwd()}`);
    } catch (err) {
      console.error(`chdir: ${err}`);
    }
    const { chdir, cwd } = require('node:process');
    
    console.log(`Starting directory: ${cwd()}`);
    try {
      chdir('/tmp');
      console.log(`New directory: ${cwd()}`);
    } catch (err) {
      console.error(`chdir: ${err}`);
    }

    This feature is not available in Worker threads.