EN
Node.js - resolve path
0
points
In this article, we would like to show you how toĀ resolve pathĀ inĀ Node.js.
1. ImportĀ path
Ā module using:
const path = require('path');
2. UseĀ path.resolve()
Ā method that resolves the specified paths into an absolute path.
Practical example:
const path = require('path');
// current directory
console.log('CURRENT DIRECTORY: ' + __dirname);
const path1 = path.resolve('public/images', 'logo.png');
console.log('PATH 1: ' + path1);
const path2 = path.resolve('/public/images', 'logo.png');
console.log('PATH 2: ' + path2);
const path3 = path.resolve('public', 'images', 'logo.png');
console.log('PATH 3: ' + path3);
Output:
CURRENT DIRECTORY: C:\projects\app
PATH 1: C:\projects\app\public\images\logo.png
PATH 2: C:\public\images\logo.png
PATH 3: C:\projects\app\public\images\logo.png
Note:
IfĀ noĀ pathĀ segmentsĀ areĀ specifiedĀ asĀ parameters,Ā theĀ absoluteĀ pathĀ ofĀ theĀ currentĀ directoryĀ isĀ used.