【问题标题】:What causes an EPERM error when traversing my home directory?遍历我的主目录时导致 EPERM 错误的原因是什么?
【发布时间】:2019-05-18 23:11:35
【问题描述】:

我用谷歌搜索过

EPERM:不允许操作

我收到了很多关于 npm 问题和这个错误的点击。

这不是我的情况(不是重复的),因为我没有运行 npm,我正在运行我自己的 Node 代码。

我收到此错误:

错误

{ 
  Error: 'EPERM: operation not permitted',
  errno: -1,
  code: 'EPERM',
  syscall: 'scandir',
  path: '../../../Library/Application Support/CallHistoryDB/' 
};

在我的主目录上运行以下代码时。

我同时使用了它

节点获取信息

sudo 节点获取信息

但我得到同样的错误。

如果我在本地存储库上运行此代码,它可以正常工作,但是当我尝试遍历我的整个主目录时,我得到了错误。

执行代码:

// Libraries
const fs = require('fs');
const h = require('./helper');

// Start recursing here
const start = '../../../';

// API
getThings(start).then(() => {
  console.log(data);
}).catch(h.error)

// Accesses the file system and returns an array of files / folders
async function getThings (folder) { 
  const things = await fs.promises.readdir(folder);
  for(let i = 0; i < things.length; i++) {
    await getStats(things[i], folder);
  }
}

// Gets statistics for each file/folder
async function getStats (thing, folder) {
  const path = folder + thing;
  const stats = await fs.promises.stat(path);
  await checkForFolder(stats, thing, path); 
}

// if the file/folder is a folder, recurse and do it again
async function checkForFolder(stats, thing, path){
  // logThing(stats, thing, path);
  if (stats.isDirectory() ) {
    await getThings(path + '/');
  }
}

研究

SO - EPERM vs EACCES

【问题讨论】:

  • 您是否尝试重建它是一个一般性错误。真正的错误不是这个

标签: javascript node.js macos recursion runtime-error


【解决方案1】:
'../../../Library/Application Support/CallHistoryDB/' 

此路径受 macOS 安全设置保护,因为它可能包含敏感的电话历史数据。

如果您特别需要在 Node 应用程序中访问它,则需要在系统偏好设置中为您用于运行此脚本的应用程序(例如 Terminal.app、iTerm 或 IDE)启用全磁盘访问权限, 如下所示。请注意,这将使您在终端中运行的所有应用程序访问您的敏感个人文件;小心行事。

但是,如果您不需要专门访问此路径(并且您可能不应该),更好的解决方案可能是在每次调用 fs.promises.stat(path) 时单独捕获错误。执行此操作的最简单方法是将调用 await fs.promises.stat(path) 包装在 try … catch 块中,然后打印或忽略错误。

【讨论】:

  • 这个答案帮助我努力修复我得到的“scandir”。在我的情况下,我还必须确保在“文件和文件夹”部分中为我的“iTerm.app”启用了“网络卷”访问权限,该部分也可以在答案中的“隐私”选项卡中找到。
猜你喜欢
  • 2011-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多