Every spawn task throws ENOENT when electron app packaged for distribution


#1

Hello! I recently started developing an electron application and so far everything has been amazing. The one problem I’m having is when I package my application up for distribution all of the buttons I have that start spawn tasks now throw ENOENT.

It ONLY happens when I package for distribution… When using electron-prebuilt I have no problems. I was hoping someone could give me some information on what could possibly be causing this! I verified both that my PATH variable includes the executable, and that the folder I am executing the ChildProcess.spawn in exists.

If any more information helps please let me know. Thanks!


#2

So I discovered some information related to my problem. When in production, spawn does not use your regular PATH variable to locate executables. It will basically use a PATH that includes OS bin folders (such as /usr/bin), but for example it won’t include your nvm bin directory that holds all your npm modules. So if you are trying to spawn something like gulp, npm, or bower, code that looks like spawn('npm', ['command']) will not able to locate your executable.

For something like npm, or any npm package, you will want to set the path of your ChildProcess spawn. For simple executables that don’t have dependencies, you can just create a bin directory in your project itself, and do something like: spawn(path.resolve(__dirname + ‘/../bin/whatever’));.

Keep in mind, to get access to path.resolve you will want to require(‘path’)

Finally - I want to reiterate that npm modules have dependencies so you can’t just copy the executable from the nvm bin directory. You actually want to set the PATH variable for your ChildProcess to include the nvm bin directory.