'Error: EACCES: permission denied; cannot mkdir

I attempted to run a local hardhat project, but came across this problem:

➜  hardhat sudo npx hardhat accounts                          
An unexpected error occurred:

Error: EACCES: permission denied, mkdir '/root/.config/hardhat-nodejs'
    at Object.mkdirSync (node:fs:1336:3)
    at Object.mkdirsSync (/home/hxq/hardhat/node_modules/fs-extra/lib/mkdirs/mkdirs-sync.js:31:9)
    at getConfigDirSync (/home/hxq/hardhat/node_modules/hardhat/src/internal/util/global-dir.ts:21:6)
    at hasConsentedTelemetry (/home/hxq/hardhat/node_modules/hardhat/src/internal/util/global-dir.ts:103:21)
    at main (/home/hxq/hardhat/node_modules/hardhat/src/internal/cli/cli.ts:147:70) {
  errno: -13,
  syscall: 'mkdir',
  code: 'EACCES',
  path: '/root/.config/hardhat-nodejs'
}

I tried many solutions, including:

sudo npm install -g appium --unsafe-perm=true --allow-root

and

sudo chmod a+w /root/.config

but both didn't work for me. I'm a beginner in Nodejs, and want a soluion and, hopefully, its theoretical explaination



Solution 1:[1]

Since you are in Ubunutu, try to make yourself as a root user first i.e sudo -i. Then install your hardhat package and it's dependencies. Then logout from root and start working from your account. This worked for me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Praveen