Skip to content

Instantly share code, notes, and snippets.

@codenickycode
Last active January 29, 2023 18:56
Show Gist options
  • Save codenickycode/8b416ecf0206319ac7a7322b3ac2a132 to your computer and use it in GitHub Desktop.
Save codenickycode/8b416ecf0206319ac7a7322b3ac2a132 to your computer and use it in GitHub Desktop.
[Node.js: Path Resolution] path module resolution #node #javascript #path #url #module #import
import path from 'path';
import { fileURLToPath } from 'url';
const currentModuleURL = import.meta.url;
const pathToFile = fileURLToPath(currentModuleURL);
console.log(pathToFile);
//=> /Users/nick/Repos/node-test/path.js
console.log(path.dirname(pathToFile));
//=> /Users/nick/Repos/node-test
console.log(path.dirname(currentModuleURL));
//=> file:///Users/nick/Repos/node-test
console.log(path.basename(currentModuleURL));
//=> path.js
console.log(path.extname(currentModuleURL));
//=> .js
console.log(path.parse(currentModuleURL));
/*=>
{
root: '',
dir: 'file:///Users/nick/Repos/node-test',
base: 'path.js',
ext: '.js',
name: 'path'
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment