Electron: EACCES permission denied nodejs fs

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?

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:

And index.html is

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:

    daemon: simple
    restart-condition: always
    command: desktop-launch xwayland-kiosk-launch "$SNAP/main/electron-quick-start" "--no-sandbox"
    - browser-support
    - network
    - network-bind
    - opengl
    - wayland
    - home
    - raw-usb
    - hardware-observe

    plugin: nil
    source: .
    after: [desktop-gtk3]
    override-build: |
          "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;;
        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("ls -la").toString())

And now i get:

>>>PWD response
>>>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

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?

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