Last active
July 3, 2026 12:01
-
-
Save xmedeko/5042e7e0b320461ca0bb86908239bd5b to your computer and use it in GitHub Desktop.
Electron.js fs.rmSync EPERM bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP --> | |
| <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'"> | |
| <title>Electron fs.rmSync EPERM bug</title> | |
| </head> | |
| <body> | |
| <p> | |
| Node.js <span id="node-version"></span> | |
| Electron <span id="electron-version"></span> | |
| <p> | |
| <p>Electron.js fs.rmSync EPERM bug<p> | |
| <p>See https://github.com/electron/electron/issues/52253</p> | |
| <p> | |
| <button id="create-file-btn">Create RO file</button> | |
| <br> | |
| <br> | |
| <button id="remove-file-sync-btn">Remove RO file sync</button> | |
| | |
| <button id="remove-file-btn">Remove RO file</button> | |
| | |
| <button id="remove-file-main-sync-btn">Remove RO file in main sync</button> | |
| <br> | |
| <br> | |
| <textarea id="out" rows="10" cols="80"></textarea> | |
| <p> | |
| <!-- You can also require other files to run in this process --> | |
| <script src="./renderer.js"></script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Modules to control application life and create native browser window | |
| const { app, BrowserWindow, ipcMain } = require('electron') | |
| const path = require('node:path') | |
| const fs = require('node:fs') | |
| function createWindow() { | |
| // Create the browser window. | |
| const mainWindow = new BrowserWindow({ | |
| width: 800, | |
| height: 600, | |
| webPreferences: { | |
| preload: path.join(__dirname, 'preload.js'), | |
| nodeIntegration: true, | |
| contextIsolation: false | |
| } | |
| }) | |
| // and load the index.html of the app. | |
| mainWindow.loadFile('index.html') | |
| // Open the DevTools. | |
| // mainWindow.webContents.openDevTools() | |
| } | |
| // This method will be called when Electron has finished | |
| // initialization and is ready to create browser windows. | |
| // Some APIs can only be used after this event occurs. | |
| app.whenReady().then(() => { | |
| createWindow() | |
| app.on('activate', function () { | |
| // On macOS it's common to re-create a window in the app when the | |
| // dock icon is clicked and there are no other windows open. | |
| if (BrowserWindow.getAllWindows().length === 0) createWindow() | |
| }) | |
| }) | |
| // Quit when all windows are closed, except on macOS. There, it's common | |
| // for applications and their menu bar to stay active until the user quits | |
| // explicitly with Cmd + Q. | |
| app.on('window-all-closed', function () { | |
| if (process.platform !== 'darwin') app.quit() | |
| }) | |
| ipcMain.handle('rm-sync', async (_event, filePath) => { | |
| fs.rmSync(filePath) | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "glossy-vest-settle-feue2", | |
| "productName": "glossy-vest-settle-feue2", | |
| "description": "My Electron application description", | |
| "keywords": [], | |
| "main": "./main.js", | |
| "version": "1.0.0", | |
| "author": "xmedeko", | |
| "scripts": { | |
| "start": "electron ." | |
| }, | |
| "dependencies": {}, | |
| "devDependencies": { | |
| "electron": "43.0.0" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { ipcRenderer } = require("electron"); | |
| const path = require('path') | |
| const fs = require('fs') | |
| const fsPromises = require('fs/promises') | |
| window.addEventListener('DOMContentLoaded', () => { | |
| fillVersions() | |
| const outEl = document.getElementById('out') | |
| const fullPath = path.resolve('testfile.txt') | |
| outEl.value = `File path: ${fullPath}` | |
| document.getElementById('create-file-btn').addEventListener('click', () => { | |
| try { | |
| fs.writeFileSync('testfile.txt', 'AAA', { mode: 0o444 }) // read-only for all | |
| out.value = `File created.\n${fullPath}` | |
| } catch (err) { | |
| console.error(err) | |
| out.value = `Create file error.\n${fullPath}\n${err}` | |
| } | |
| }) | |
| document.getElementById('remove-file-sync-btn').addEventListener('click', () => { | |
| try { | |
| fs.rmSync('testfile.txt') | |
| out.value = `File removed.\n${fullPath}` | |
| } catch (err) { | |
| console.error(err) | |
| out.value = `Remove file error.\n${fullPath}\n${err}` | |
| } | |
| }) | |
| document.getElementById('remove-file-btn').addEventListener('click', async () => { | |
| try { | |
| await fsPromises.rm('testfile.txt') | |
| out.value = `File removed.\n${fullPath}` | |
| } catch (err) { | |
| console.error(err) | |
| out.value = `Remove file error.\n${fullPath}\n${err}` | |
| } | |
| }) | |
| document.getElementById('remove-file-main-sync-btn').addEventListener('click', async () => { | |
| try { | |
| await ipcRenderer.invoke('rm-sync', fullPath) | |
| out.value = `File removed.\n${fullPath}` | |
| } catch (err) { | |
| console.error(err) | |
| out.value = `Remove file error.\n${fullPath}\n${err}` | |
| } | |
| }) | |
| }) | |
| function fillVersions() { | |
| const replaceText = (selector, text) => { | |
| const element = document.getElementById(selector) | |
| if (element) element.innerText = text | |
| } | |
| for (const type of ['node', 'electron']) { | |
| replaceText(`${type}-version`, process.versions[type]) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * This file is loaded via the <script> tag in the index.html file and will | |
| * be executed in the renderer process for that window. No Node.js APIs are | |
| * available in this process because `nodeIntegration` is turned off and | |
| * `contextIsolation` is turned on. Use the contextBridge API in `preload.js` | |
| * to expose Node.js functionality from the main process. | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment