1

This is really a head scratcher...

I'm running Node 22.9.0 on a raspberry PI (Linux raspberrypi 6.1.21-v8+)

My script, which is run as root, can write a new file in the current directory and update that file just fine.

total 332
drwxr-xr-x  11 root root   4096 Feb  8 06:37 .
drwxr-xr-x   4 root root   4096 Dec 20 07:24 ..
.....
-rw-r--r--   1 root root   1861 Feb  8 06:37 database.json (THE FILE)

I have a event loop in the script that runs inside of setInterval. Inside that event loop, I receive the following error when trying to write to the file...

Error! Error: EACCES: permission denied, open 'database.json'

It seems that inside the loop, I lose access to the directory...

setInterval(() => {
  console.log(process.env.USER) // root
  fs.access("./database.json", fs.constants.W_OK, (err) => {
    if (err) {
      // am seeing this log!!
      console.error('Directory is not writable'); 
    }
  });
}, 2000);

Any ideas why I can write outside of setInterval, but not inside of setInterval?

3
  • You are attempting to access a file in the current directory of the callback process. Commented Feb 8 at 13:17
  • Yep that is correct! Is that… bad? Commented Feb 8 at 15:02
  • ./database.json is a relative path, what if you put an absolute path ie /root/database.json Commented Feb 16 at 23:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.