Critical path resolution patterns for Electron apps - avoid process.cwd() and __dirname pitfalls
View on GitHubplugins/aai-stack-electron/skills/electron-path-resolution/SKILL.md
February 1, 2026
Select agents to install to:
npx add-skill https://github.com/the-answerai/alphaagent-team/blob/main/plugins/aai-stack-electron/skills/electron-path-resolution/SKILL.md -a claude-code --skill electron-path-resolutionInstallation paths:
.claude/skills/electron-path-resolution/# Electron Path Resolution Skill
Path resolution is the #1 cause of Electron app failures. This skill covers all the pitfalls and their solutions.
## The Core Problem
When an Electron app is launched from Finder/Explorer (NOT terminal):
- `process.cwd()` returns `/` on macOS, `C:\Windows\System32` on Windows
- `__dirname` in bundled code is resolved at bundle time, not runtime
- Node.js module resolution fails for bundled apps
## Environment Setup Pattern
In your main process, set environment variables that ALL code can use:
```typescript
// electron/main.ts
import { app } from 'electron';
import path from 'path';
async function initializeEnvironment() {
// User data directory (database, uploads, cache)
const userDataDir = app.getPath('userData');
// App resources directory (bundled assets, node_modules)
const appRoot = app.getAppPath();
// Set environment variables for all processes
process.env.ELECTRON_USER_DATA = userDataDir;
process.env.ELECTRON_APP_ROOT = appRoot;
process.env.ELECTRON_DB_PATH = path.join(userDataDir, 'app.db');
// Ensure directories exist
const fs = require('fs');
const dirs = ['uploads', 'generated', 'cache', 'logs'];
for (const dir of dirs) {
const dirPath = path.join(userDataDir, dir);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}
}
```
## Safe Path Helper Functions
Create utility functions that ALL code uses:
```typescript
// src/shared/electron-paths.ts
import path from 'path';
import os from 'os';
/**
* Get the user data directory (for database, uploads, etc.)
* Works in both Electron and development
*/
export function getUserDataDir(): string {
if (process.env.ELECTRON_USER_DATA) {
return process.env.ELECTRON_USER_DATA;
}
if (process.env.ELECTRON_DB_PATH) {
return path.dirname(process.env.ELECTRON_DB_PATH);
}
// Development fallback
return path.resolve(process.cwd(), 'data');
}
/**
* Get the app root directory (for bundled