最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - writeFileSync fails when called inside of setInterval - Stack Overflow

programmeradmin1浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论