29

I am trying to create a desktop application with electron, angular2, typescript and neDB.In order to be able create a 'file' database with neDB I want the path to my project.How can I get this with typescript ?

geo
  • 2,283
  • 5
  • 28
  • 46

5 Answers5

51

Use app.getAppPath()

Typescript is a superset of javascript so you could do it in the same way you would do it with javascript, though you may want to declare typings, or use other typescript features when you do so.

Example:

const remote = require('remote'), 
      app = remote.require('app');

var basepath = app.getAppPath();

Update - these days you should use:

const app = require('electron').remote.app

To get the app handle for app.getAppPath().

Dan
  • 2,830
  • 19
  • 37
24

Writing data to the application installation directory is generally a bad idea since the user running the app may not have permission to write files to that directory. What you should probably do instead is create the database file at the location returned by app.getPath('userData').

Vadim Macagon
  • 14,463
  • 2
  • 52
  • 45
  • 8
    Unless if it's an app made to be portable – Pedro Serpa Aug 19 '19 at 01:49
  • @Vadim Macagon, I have `app = require('electron')` defined in *main.js* and then in another file attempting to use app.getPath('userData'). It comes out as undefined and I think it's because there can be only one instance of **app**. Is there a workaround? – Vass Apr 15 '21 at 21:48
  • 1
    @Vass Using `const app = require('electron').app;` across multiple files should resolve your problem. – midnight-coding Mar 24 '23 at 23:40
13

If you're running a packaged app and you want to get the path to the app executable (NOT the main Node process index script path, which could be inside an ASAR), app.getAppPath() is incorrect. You want app.getPath("exe"), and to get the path it's:

require("path").dirname(require('electron').remote.app.getPath("exe"))
keymap
  • 864
  • 7
  • 16
4

Here is what worked for me:

require('electron').remote.app.getAppPath()
2

If that can help somebody, I was downloaded the npm packages electron-root-path and it work perfectly in my case.

// Import ES6 way
import { rootPath } from 'electron-root-path';
 
// Import ES5 way
const rootPath = require('electron-root-path').rootPath;
Stefano Sansone
  • 2,377
  • 7
  • 20
  • 39
Florian
  • 21
  • 1