user578895
user578895

Reputation:

Node __dirname, process.cwd() are reporting actual directory instead of symlinked one

"Simple" question -- I'm trying to get the path to a directory of a node script, but when I run from a symlinked directory, I keep getting the path to the physical file instead of the path to the symlinked structure. How do I get the symlinked path?

/path/to/symlink --> /path/to/real

/path/to/symlink> node echo.js

# echo.js
console.log( __dirname );     // /path/to/real
console.log( process.cwd() ); // /path/to/real

[edit] Just for clarification of my own sanity:

$ mkdir test
$ cd test
test$ mkdir a
test$ ln -s a b
test$ cd b
b$ node
> process.cwd()
'/test/a'

Upvotes: 4

Views: 2308

Answers (2)

SubHero
SubHero

Reputation: 61

const { execSync } = require('child_process');

execSync('pwd').toString()

Running pwd as a child process returns the current directory with symlinks.

Upvotes: 3

Roman Dvornov
Roman Dvornov

Reputation: 131

You can get symlink path through process.env.PWD on OSX.

Windows looks as has no this issue.

Upvotes: 6

Related Questions