npm which function fails to get full path to an executable in macOS within an Electron app running in production mode but, works fine in development mode
wasimaftab opened this issue · comments
I am trying to get the path to Rscript
which runs R scripts from the command line using which function. I packaged the app using electron-packager (v15.2.0) in macOS Catalina (v10.15.7) and Ubuntu (v20.04). In Ubuntu, the app works perfectly before and after packaging i.e. in both development and production modes. However, in mac, it fails in the production mode. see the screenshots below.
I found Rscript can be executed from a Terminal (See screenshot below), so to me, it seems there are no issues related to executable not in path.
I'm wondering, why the npm function which
cannot detect Rscript
path in the production mode when it can discover it in the development mode? or is there a better way to get path that bypasses this problem?
The JS code to get the path is as follows:
'use strict'
const path = require("path");
const which = require("which");
const isDev = require('electron-is-dev');
if (isDev) {
console.log('Running in development');
} else {
console.log('Running in production');
}
which("Rscript", function (er, RscriptPath) {
console.log('RscriptPath = ' + RscriptPath);
})
Full source code to reproduce this issue can be obtained from this GitHub folder: https://github.com/wasimaftab/Utils/tree/master/Rscript_not_found
I tested using Electron v10.1.5 and v11.3.0
n/a
Where
- n/a
Who
- n/a
References
- n/a
@wasimaftab I have the same issue. After digging into this a bit, the problem is that on MacOS, Applications do not inherit the environment of the user (e.g., from ~/.zshrc). They inherit a very basic environment; for example, my Electron app is logging the value of the PATH environment variable value at startup:
{"message":"PATH environment variable: /usr/bin:/bin:/usr/sbin:/sbin","level":"notice"}
To change the environment in which your applications run, you need to configure launchd (e.g., see the config
subcommand in the launchctl
man page).
This issue looks to be solved by the previous comment. If that's not the case, please reopen or file a new issue.
@robertpatrick do you think there's a way for us to change it without the users noticing?
Or is there a way we can let user change it in GUI, not command line?
@addlistener In our app, we have a panel on our Settings page that only shows up when the application is run on the Mac. It allows the user to add directories to the path and add environment variables that we pass to all Electron-side actions that execute other scripts/applications via child processes. We realized the need for this early on and now it is just part of our framework.
@robertpatrick Thanks Robert. Clear GUI and great demonstration I'd love to learn from.
And I actually just spotted a package named fix-path. You could give it a try.