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 ?
5 Answers
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()
.

- 2,830
- 19
- 37
-
12At newer versions of electron, one should use `require('electron').remote.app` – Nikita Ivanov May 13 '18 at 20:15
-
8For even newer versions of electron, one should use `require('electron').app` – Fergal Dec 29 '18 at 05:36
-
1@Fergal using `5.0.6` I get a null value using `require('electron').app` while `require('electron').remote.app` returns the correct reference – Felipe Jul 16 '19 at 19:48
-
1In 2020 it is truncated further as require('electron').app.getAppPath(); – Ram Kumar Apr 27 '20 at 13:50
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')
.

- 14,463
- 2
- 52
- 45
-
8
-
@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
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"))

- 864
- 7
- 16
-
2For newer electron versions use `require("path").dirname(require('electron').app.getPath("exe"))` – yoel halb Aug 16 '21 at 23:43
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;

- 2,377
- 7
- 20
- 39

- 21
- 1