Skip to content

Instantly share code, notes, and snippets.

@ishar19
Created July 20, 2024 18:01
Show Gist options
  • Select an option

  • Save ishar19/508709604e22290ca3e4def2b77adb39 to your computer and use it in GitHub Desktop.

Select an option

Save ishar19/508709604e22290ca3e4def2b77adb39 to your computer and use it in GitHub Desktop.
absolute to relative
const path = require('path');
const exec = require('child_process').exec;
function absoluteToRelative(absolutePath) {
// Normalize the paths to remove any unnecessary separators
// const normalizedAbsolutePath = path.normalize(absolutePath);
const currentPath = path.normalize(process.cwd());
const relativePath = path.relative(currentPath, absolutePath);
return relativePath;
}
// Example usage
const absolutePath = '/Users/ishar/Downloads/ffmpeg-exp/folder/index.js';
const relativePath = absoluteToRelative(absolutePath);
console.log(relativePath); // Output: src/components/Button.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment