node js find directory change directory

const fs = require('fs');
const path = require('path');

// Step 1: Get the current working directory
const currentDirectory = process.cwd();
console.log('Current Directory:', currentDirectory);

// Step 2: List the contents of the current directory
const currentDirectoryContents = fs.readdirSync(currentDirectory);
console.log('Current Directory Contents:', currentDirectoryContents);

// Step 3: Specify the target directory
const targetDirectory = 'path/to/target/directory';

// Step 4: Check if the target directory exists
const isTargetDirectoryExists = fs.existsSync(targetDirectory);
console.log('Is Target Directory Exists:', isTargetDirectoryExists);

// Step 5: Change the current working directory to the target directory
if (isTargetDirectoryExists) {
  process.chdir(targetDirectory);
  console.log('Changed to Target Directory:', process.cwd());
} else {
  console.error('Target Directory does not exist.');
}