11

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

XCA
  • 127
  • 1
  • 1
  • 10

3 Answers3

24

Update: this problem and the answer below only applies to NPM 7 and 8 (more info).

This quickfix should remove the issue by installing an NPM version >= 9.

npm install -g npm@latest

Original answer (applies to NPM 7 and 8 shipped with Node 15-19)

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 version 6 or >= 9 of NPM

As mentioned in the linked answer:

This behavior is only for NPM versions 7 and 8. NPM versions < 7, which are the ones shipped with Node 12-14, work perfectly fine when running as root.

So some quick fixes would be

  • npm install -g npm@latest # Use the latest version of NPM
  • npm install -g nvm && nvm use 14 # Note: End of Life on May 2023
  • npm install -g nvm && nvm use 18 # Uses NPM 9, too new for some deps?
oligofren
  • 20,744
  • 16
  • 93
  • 180
  • i already try to change the owner, nothing work :/, maybe i do something wrong, but i dont figure out – XCA Feb 02 '22 at 10:00
  • To change the owner to root, you do `chown -R root /path/of/your/project`. – oligofren Feb 02 '22 at 10:11
  • 3
    damm, im doing chown command wrong, the error is no more thanks ! – XCA Feb 02 '22 at 10:13
  • sadly node 14 is end-of-life next month so, just staying on 14 isnt a very good solution any more... – steev Mar 30 '23 at 21:06
  • @steev Good point. I will update the "try Node 14" bit to reflect that. I already did that in the longer answer I linked to (https://stackoverflow.com/a/70298239/200987). The old behavior is back in NPM 9, so that is the quickest fix :) – oligofren Mar 31 '23 at 09:05
1

Alternative approach: use a non-root user.

NPM is trying really hard (see other answer for details) to not execute anything as root. So what about trying just that - not being root? You would do something like this

  1. Add a local user (AMI guide), typically adduser john.doe --disabled-password
  2. Login as the new user sudo -s -u john.doe
  3. Clone your repo: git clone https://github.com/foo/bar; cd bar
  4. Run NPM command npm i;npm run build:prod
oligofren
  • 20,744
  • 16
  • 93
  • 180
  • no any other way to do it with root ? – XCA Feb 02 '22 at 10:10
  • I posted a way to do this as root in my linked answer. I will repost it below – oligofren Feb 02 '22 at 10:11
  • Why do you have to be root, btw? I have been using Linux for 20 years and never need to be root for anything but occational admin work, if doing it right. – oligofren Feb 02 '22 at 10:12
  • it's not my server, my "boss" say "do it in root" so ... x) – XCA Feb 02 '22 at 10:16
  • haha, if the boss says so, and he is the kind of boss that does not listen to reasonable suggestions, then I totally understand. Thankfully never had to experience that. Good luck! – oligofren Feb 02 '22 at 10:19
-3

glob error [Error: EACCES: permission denied, scandir '/root/.npm/_logs'] {

This means it is trying to read something on 'root' directory, something which always needs root access.

Try to run the command like this:

sudo npm run build:prod

Extra: Usually you should NOT run commands with sudo. If you come from a Windows background think of it as 'Run as administrator', only with even more privileges, so if you are running a malicious script it could have access to almost everything on your system. There are ofc some JS libraries which need root/sudo access to do their things tho

zedian
  • 397
  • 5
  • 16
  • hi, thanks for reply, im on Linux 4.19.0-16-cloud-amd64 and im loged as root running with sudo dont change anything :/ – XCA Feb 02 '22 at 09:42
  • That won't worK in newer NPM versions – oligofren Feb 02 '22 at 09:44
  • Can you try to use Node 16? Also `sudo chown -R username:username /root/.npm/_logs` might work if you don't have permissions set correctly – zedian Feb 02 '22 at 09:50
  • im on node v17.4.0, i will try node 16, chown command dont solve the error – XCA Feb 02 '22 at 09:51
  • Also found this if this is your case https://stackoverflow.com/questions/48910876/error-eacces-permission-denied-access-usr-local-lib-node-modules/59575266#59575266 – zedian Feb 02 '22 at 09:53
  • same error with node 16 edit: installing node with nvm dont work too – XCA Feb 02 '22 at 09:54
  • @sediballiu See my answer for why none of these approaches will ever work. It works on older Node versions that ship with NPM <= v6. – oligofren Feb 02 '22 at 09:58