NPM / Error: EACCES: permission denied, scandir

Issue

when i type "npm run build:prod"

build:prod
    npm run build -- --configuration production --aot --optimization=false

i get this error :

> fancy-name@0.0.0 build:prod
> npm run build -- --configuration production --aot --optimization=false

glob error [Error: EACCES: permission denied, scandir '/root/.npm/_logs'] {
 errno: -13,
 code: 'EACCES',
 syscall: 'scandir',
 path: '/root/.npm/_logs'
}
npm WARN logfile Error: EACCES: permission denied, scandir '/root/.npm/_logs'
npm WARN logfile  error cleaning log files [Error: EACCES: permission denied, scandir '/root/.npm/_logs'] {
npm WARN logfile   errno: -13,
npm WARN logfile   code: 'EACCES',
npm WARN logfile   syscall: 'scandir',
npm WARN logfile   path: '/root/.npm/_logs'
npm WARN logfile }
⸨⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⸩ ⠙ : WARN logfile Error: EACCES: permission denied, scandir '/root/.npm/_logs'
> fancy-name@0.0.0 build
> ng build "--configuration" "production" "--aot" "--optimization=false"

Node.js version v17.4.0 detected.file Error: EACCES: permission denied, scandir '/root/.npm/_logs'
Odd numbered Node.js versions will not enter LTS status and should not be used for production. For more information, please see https://nodejs.org/en/about/releases/.
This version of CLI is only compatible with Angular versions ^13.0.0-next || >=13.0.0 <14.0.0,ogs'
but Angular version 12.1.3 was found instead.

Please visit the link below to find instructions on how to update Angular.
https://update.angular.io/

i already try to delete node_module & package.json and run NPM install again, but it didnt work
(Linux 4.19.0-16-cloud-amd64)

EDIT : solved with chown -R root /path/of/your/project

Solution

This is understandably a confusing error; you run npm as root, and yet it says you do not have access to the files? There is a good reason 🙂 These questions are essentially about the same thing:

My quite long answer goes into details on what is happening, but the essence is this:

(NPM) … uses this bit of code to determine who to run as:

const { uid, gid } = isRoot ? inferOwner.sync(cwd) : {}

and as the docs of infer-owner module says:
Infer the owner of a path based on the owner of its nearest existing parent

So if you are running NPM as root you need to change the owner of the current directory to root as well:

chown -R root /path/of/your/project

But all this trouble should point you to the real solution: do not run NPM as root. You have no idea what kind of scripts might run as a postinstall trigger or similar. Be safe; only use the root account when necessary using sudo.

Addendum: try Node 14

As mentioned in the linked answer:

This behavior is only for NPM versions >= 7. NPM versions < 7, which are the ones shipped with Node 12-14 work perfectly fine when running as root. So the quickest fix might simply be to use Node 14.

  • npm install -g nvm
  • nvm use 14

Answered By – oligofren

Answer Checked By – Jay B. (AngularFixing Admin)

Leave a Reply

Your email address will not be published.