Skip to content

Instantly share code, notes, and snippets.

@RB-Lab
Created October 6, 2025 06:44
Show Gist options
  • Select an option

  • Save RB-Lab/fe32223fd69a0c0cfb922b69457f20e5 to your computer and use it in GitHub Desktop.

Select an option

Save RB-Lab/fe32223fd69a0c0cfb922b69457f20e5 to your computer and use it in GitHub Desktop.
Function to debug paths in AWS lambda (but not only)
const fs = require('fs');
const path = require('path');
function debugPath(targetPath) {
console.log(`Debugging path: ${targetPath}`);
console.log('='.repeat(50));
const segments = targetPath.split('/').filter(segment => segment !== '');
let currentPath = '';
// Start from root if path is absolute
if (targetPath.startsWith('/')) {
currentPath = '/';
console.log(`Root directory (/): exists`);
}
for (let i = 0; i < segments.length; i++) {
currentPath = path.join(currentPath, segments[i]);
try {
const stats = fs.statSync(currentPath);
console.log(`✓ ${currentPath}: ${stats.isDirectory() ? 'directory' : 'file'}`);
if (stats.isDirectory()) {
try {
const contents = fs.readdirSync(currentPath);
console.log(` Contents: [${contents.join(', ')}]`);
} catch (err) {
console.log(` Cannot read directory: ${err.message}`);
}
}
} catch (err) {
console.log(`✗ ${currentPath}: ${err.code} - ${err.message}`);
break;
}
console.log('-'.repeat(30));
}
}
// Debug the specific path from your error
debugPath('/opt/nodejs/your/path');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment