简体   繁体   English

如何检测 macOS 上的 Node 应用程序启动文件夹?

[英]How can I detect Node app startup folder on macOS?

I am developing a cross-platform NodeJS server/app that is distributed as a prebuilt binary, built with pkg ( https://www.npmjs.com/package/pkg ) for Windows, Mac and Linux.我正在为 Windows、Mac 和 ZEDC9F0A5A5D57797BF6Z3E 开发一个跨平台 NodeJS 服务器/应用程序,它作为预构建的二进制文件分发,使用pkg ( https://www.npmjs.com/package/pkg ) 构建。

At start-up, the app should generate a default config.json to the application directory.在启动时,应用程序应该生成一个默认的config.json到应用程序目录。 I'm detecting the current, start-up folder of the app with:我正在检测应用程序的当前启动文件夹:

var startUpPath = process.cwd();

and this works on Windows, and Linux, but on macOS this fails and the file is generated to the user's home directory instead.这适用于 Windows 和 Linux,但在 macOS 上这会失败,而是将文件生成到用户的主目录。

Further notes on macOS :关于macOS的进一步说明:

  • this works when running the source code with node myApp.js使用node myApp.js运行源代码时有效
  • this works when running the binary from command line with ./myApp使用./myApp从命令行运行二进制文件时有效
  • this fails when myApp is started by double-clicking in the Finder当通过在 Finder 中双击启动myApp时,此操作失败

The root cause seems to be that macOS processes always start in the home directory , not in current directory ...?根本原因似乎是macOS进程总是在主目录中启动,而不是在当前目录中......?

I'm sure it's trivial but haven't figured it out.我敢肯定这是微不足道的,但还没有弄清楚。

Any pointers here?这里有任何指示吗?

Found a way eventually - in case somebody else is searching for a solution.最终找到了一种方法——以防其他人正在寻找解决方案。

// Detect startup folder at pkg runtime
if ( process.pkg ) {
    startUpPath = path.resolve(process.execPath + '/..');
} else {
    startUpPath = process.cwd();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM