如何获取 node.js 系统 CLI 应用程序位置?

How to get node.js system CLI app location?

我有 node.js CLI 应用程序,它需要从我的应用程序代码库内部执行 rsync。这是结构:

.
├── app.js
└── rsync
    └── mac
        └── rsync

该应用程序是全局安装的,因此可以从任何文件夹中运行。我需要找出如何执行位于我的应用程序代码库内的 rsync。为此,我需要找出应用程序的绝对路径。

您可以使用which命令查看应用程序的绝对路径。

> which rsync
/usr/bin/rsync

__dirname - returns 应用程序根目录的路径;当前执行脚本所在的目录名称。

更多信息:https://nodejs.org/docs/latest/api/globals.html#globals_dirname