Languages
[Edit]
EN

Node.js - resolve path

0 points
Created by:
Vanessa-Drake
718

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

Alternative titles

  1. Node.js - safe way to add path to path (resolve path)
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
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