Node __dirname、process.cwd()正在报告实际目录,而不是符号链接的目录

gk7wooem  于 2023-01-16  发布在  Node.js
关注(0)|答案(2)|浏览(403)

“简单”的问题--我试图获得节点脚本的目录路径,但是当我从符号链接目录运行时,我总是获得物理文件的路径,而不是符号链接结构的路径。

/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

[编辑]为了澄清我自己的理智:

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

qxsslcnc1#

您可以通过OSX上的process.env.PWD获得符号链接路径。
Windows看起来没有这个问题。

irtuqstp

irtuqstp2#

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

execSync('pwd').toString()

将pwd作为子进程运行将返回带有符号链接的当前目录。

相关问题