nodejs absolute path relative path conversion

Converting Absolute Paths to Relative Paths in Node.js

This article covers how to convert absolute paths of files or programs to relative paths in the Node.js programming language. Converting between absolute paths ←→ relative paths is a frequently used implementation in programming and coding. It's convenient to remember.

Shou Arisaka
1 min read
Nov 7, 2025

This article covers how to convert absolute paths of files or programs to relative paths in the Node.js programming language. Converting between absolute paths ←→ relative paths is a frequently used implementation in programming and coding. It’s convenient to remember.

Image

Apparently Node.js can’t load files with absolute path specifications by default. It’s the same with Ruby, but for someone who normally uses Bash, I find it tedious to have to convert.

javascript - Node require absolute path - Stack Overflow

To get the relative path of a file relative to the current script file, use the following:

var path = require('path');

path.relative( __dirname , "/mnt/c/path/to/file.js"

Usage example:

var noderc = require( path.relative( __dirname , process.env.NODE_RC_FILE ) );

Path | Node.js v12.6.0 Documentation

Share this article

Shou Arisaka Nov 7, 2025

🔗 Copy Links