Electron: feature request: API to get the app source code folder path

Created on 16 May 2015  ·  3Comments  ·  Source: electron/electron

When I do e.g. cd myapp/ and electron app.js, the output of process.cwd() is /Users/maxogden/myapp.

However, when I package + run my app using electron-packager, meaning my app is now located in inside the Contents/Resources/app folder in a modified copy of the official Electron.app from the releases page, process.cwd() is /.

I'm not sure why it is that Electron is deciding to run my app with process.cwd set to /, but thats how it works, and if you run process.cwd() in the console in Atom.app you will also get /.

My problem is that I have no way to get the path to the app source files, the only way would to capture the value of __dirname in the main executable script of the app, but this is not a good solution for cases like my module menubar, where the API is currently this:

var menubar = require('menubar')
var mb = menubar()
mb.on('ready', function ready () {
  console.log('app is ready')
})

But this doesn't work when packaged in an app container, because menubar looks for the users index.html page to load using process.cwd() here, but because process.cwd() is / instead of /Users/maxogden/myapp/Example.app/Contents/Resources/app it doesn't find index.html and the app doesn't load.

Node apps in general don't have this problem because process.cwd() is always reliable, the issue is caused by process.cwd() getting set to / (it might be a Mac OS X thing, who knows).

To fix it I can require users who use menubar to pass in __dirname, but this is not ideal or user friendly.

I propose one of these solutions to fix this:

  • change the process.cwd() to the actual app folder
  • add a new option to app.getPath that returns the app folder path

Most helpful comment

Check out process.resourcesPath, it's basically what you're describing for the 2nd option

All 3 comments

Check out process.resourcesPath, it's basically what you're describing for the 2nd option

@paulcbetts ahhhhhh thanks, I didn't even realize https://github.com/atom/electron/blob/cd0aa4a956cb7a13cbe0e12029e6156c3e892924/docs/api/process.md#process-object existed (I didn't expect electron to monkeypatch process)

Had that issue in atom where path.resolve('something') would resolve to /something. It broke tslint for me. This helped:

open init.coffee (Atom > Preferences > Init Script...) and add:

process.chdir(atom.project.getPaths()[0])
Was this page helpful?
0 / 5 - 0 ratings

Related issues

feross picture feross  ·  3Comments

tengyifei picture tengyifei  ·  3Comments

dangan-ronpa picture dangan-ronpa  ·  3Comments

jviotti picture jviotti  ·  3Comments

sindresorhus picture sindresorhus  ·  3Comments