1

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

黄宣祺
  • 11
  • 2

2 Answers2

0

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.

0

It looks like you're running sudo npx hardhat accounts;. npx attempts to become the user who owns the current directory (see Getting EACCESS when running NPM 8 as root and https://github.com/npm/cli/issues/3773).

A possible fix is to run sudo chown root:root .; before running your npx command. Someone else suggests using pnpm instead of npm (and pnpx instead of npx).

lmat - Reinstate Monica
  • 7,289
  • 6
  • 48
  • 62