I get this error in electron’s main.js while using nodejs fs.
fs.readFile('./index.html', 'utf8', ......
[Error: EACCES: permission denied, open './index.html'] {
errno: -13,
code: 'EACCES',
syscall: 'open',
path: './index.html'
}
Connections used:
Interface Plug Slot Notes
browser-support test:browser-support :browser-support -
hardware-observe test:hardware-observe :hardware-observe manual
home test:home :home -
network test:network :network -
network-bind test:network-bind :network-bind -
opengl test:opengl :opengl -
raw-usb test:raw-usb :raw-usb manual
wayland test:wayland :wayland manual
Snapd version:
snap 2.49.1+git814.gc4a4ea6
snapd 2.49.1+git814.gc4a4ea6
series 16
ubuntu 20.04
kernel 5.8.0-45-generic
Any idea why?
ogra
March 17, 2021, 2:48pm
2
hard to make any predictions here without knowing the structure of your snap, where you actually are at the moment when you try to open index.html in the same dir … and what the file permissions of index.html are etc etc … do you have a link to your source ?
Simple. I’m here:
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
}
This file has been truncated. show original
And index.html is
<!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'">
<meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
We are using Node.js <span id="node-version"></span>,
Chromium <span id="chrome-version"></span>,
and Electron <span id="electron-version"></span>.
<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
</body>
</html>
index.html is in same directory as main.js
i required
const fs = require('fs')
And before loadFile i added:
fs.readFile('./index.html', 'utf8', function (err, data) {...}
I snapcrafted it on Ubuntu 20.04, and installed it with sudo.
$ snapcraft
...
$ Snapped test_0.1.0_amd64.snap
$ sudo snap install --dangerous ./test_0.1.0_amd64.snap
Connections are as previous post.
When i try to run, it gives EACCES
$ sudo snap run test
Mir-kiosk is runned in another terminal…
In snap.yaml:
apps:
test:
daemon: simple
restart-condition: always
command: desktop-launch xwayland-kiosk-launch "$SNAP/main/electron-quick-start" "--no-sandbox"
plugs:
- browser-support
- network
- network-bind
- opengl
- wayland
- home
- raw-usb
- hardware-observe
parts:
test:
plugin: nil
source: .
after: [desktop-gtk3]
override-build: |
case $SNAPCRAFT_ARCH_TRIPLET in
"i386-linux-gnu") ARCH="ia32";;
"x86_64-linux-gnu") ARCH="x64";;
"arm-linux-gnueabihf") ARCH="armv7l";;
"aarch64-linux-gnu") ARCH="arm64";;
*) echo "ERROR: electron does not support the '$SNAPCRAFT_ARCH_TRIPLET' architecture" && exit 1;;
esac
rm -Rf ./node_modules &&
npm install -g npm@latest &&
npm install --unsafe-perm=true --build-from-source &&
./node_modules/.bin/electron-rebuild &&
npm install --unsafe-perm=true electron-packager &&
./node_modules/.bin/electron-packager . --overwrite --platform=linux --arch=$ARCH --output=release-build --prune=true
...........
Added 2 new lines:
const exec = require("child_process").execSync;
...
function createWindow() {
...
console.log(exec("pwd").toString())
console.log(exec("ls -la").toString())
mainWindow.loadFile('index.html')
...
}
And now i get:
>>>PWD response
/media/t0skfak3r/drive3/test
>>>ls -la response
ls: cannot open directory '.': Permission denied
(node:548955) UnhandledPromiseRejectionWarning: Error: Command failed: ls -la
ls: cannot open directory '.': Permission denied
at checkExecSyncError (child_process.js:625:11)
at execSync (child_process.js:661:15)
at electron/js2c/asar_bundle.js:5:12164
at createWindow (/snap/test/x11/main/resources/app/main.js:25:15)
at /snap/test/x11/main/resources/app/main.js:68:3
ogra
March 17, 2021, 3:27pm
5
well, your snap wont have access to that dir unless you add removable-media
to the app plugs and connect it with snap connect ...
Nice one. Never saw it coming.
1 Like
I tried to use better-sqlite, which creates a db file in ./test.db .
As i see the file is always created with only read permissions. I’m thinking that all files in snap are read only? Where should i create a file so that i can write to it?
ogra
March 17, 2021, 8:22pm
8
you have $SNAP_DATA whcih translates to /var/snap/<snapname>/current
, $SNAP_COMMON pointing to /var/snap/<snapname>/common
as well as $SNAP_USER_DATA and $SNAP_USER_COMMON pointing to ~/snap/<snapname>/current
and ~/snap/<snapname>/common
… all these are available through environment variables at runtime of each snap …
Yes, forgot to write here.
Solved it reading: https://askubuntu.com/questions/762354/where-can-ubuntu-snaps-write-data .
And in electron i get the path like this:
var path = process.env.SNAP_USER_DATA ? process.env.SNAP_USER_DATA + '/' : './';
Many thanks!
1 Like