Languages
[Edit]
EN

Node.js - resolve path

0 points
Created by:
Vanessa-Drake
388

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.

References

Native Advertising
ūüöÄ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

‚̧ԳŹūüíĽ ūüôā

Join