How to solve Error: EACCES: permission denied in pm2?

How to solve Error: EACCES: permission denied in pm2?

我不知道为什么会遇到这个错误,当我 运行 pm2 status:

kaitoSwift@kaito-MacBook-Pro school-sms2 % pm2 status
node:internal/fs/utils:344
    throw err;
    ^

Error: EACCES: permission denied, open '/Users/kaitoSwift/.pm2/pm2.log'
    at Object.openSync (node:fs:585:3)
    at module.exports.Client.launchDaemon (/usr/local/lib/node_modules/pm2/lib/Client.js:228:12)
    at /usr/local/lib/node_modules/pm2/lib/Client.js:104:10
    at /usr/local/lib/node_modules/pm2/lib/Client.js:321:14
    at processTicksAndRejections (node:internal/process/task_queues:78:11)
    at runNextTicks (node:internal/process/task_queues:65:3)
    at listOnTimeout (node:internal/timers:528:9)
    at processTimers (node:internal/timers:502:7) {
  errno: -13,
  syscall: 'open',
  code: 'EACCES',
  path: '/Users/kaitoSwift/.pm2/pm2.log'
}

当我尝试使用 npm install pm2 -g 下载 pm2 时:

kevintrinidad@kaito-MacBook school-sms2 % npm install pm2 -g 
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules/pm2
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/pm2'
npm ERR!  [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/pm2'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/usr/local/lib/node_modules/pm2'
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/kaitoSwift/.npm/_logs/2022-04-07T11_15_28_141Z-debug-0.log

您需要 运行 在 sudo 中执行此操作,并确认您在同一端口上没有 运行 的其他进程。

要安装 PM2,请使用:

sudo npm install -g pm2

对于pm2 status的错误,同样尝试:

sudo pm2 status

错误告诉你 /Users/kaitoSwift/.pm2/pm2.log 不存在。