How to Solve Error Message EACCES: permission denied in NodeJS Application

Posted on

Introduction

This article will show how to solve an error message. The error message exist as in the title of this article. Actually, the error happen on a certain occasion. The error is in the following line :

Error: EACCES: permission denied, mkdir '/usr/lib/node_modules/nodemon'

Actually, the full error message appear as follow upon executing a command for installing a module with the name of ‘nodemon’ :

[admin@10 nodejs]$ npm i nodemon -g
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/lib/node_modules/nodemon
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/lib/node_modules/nodemon'
npm ERR!  [Error: EACCES: permission denied, mkdir '/usr/lib/node_modules/nodemon'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/usr/lib/node_modules/nodemon'
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!     /home/admin/.npm/_logs/2021-03-04T23_01_20_638Z-debug.log
[admin@10 nodejs]$ 

It appears that installing a module by passing an argument of -g end up failing. Why passing an argument of -g with the command execution ?. It is because there is a need to be able to execute the module across all users and all paths. There is a reason why the command execution is failing. That is because the current user running the command above does not have any privileges at all. The current user above running the command is ‘admin’.

 

Solution for solving the problem

It appears that installing a module by passing an argument of -g end up failing. That is because the current user running the command above does not have any privileges at all. The current user above running the command is ‘admin’. So, the solution to solve the problem is by switching to a super admin user such as ‘root’ as follows :

[admin@10 nodejs]$
sudo su -
[sudo] password for admin:
Last login: Thu Mar  4 10:29:37 EST 2021 on pts/1
[root@10 ~]#

Soon after successfully switching to a super user account, execute the command for installing the module globally as follows :

[root@10 ~]# npm i nodemon -g
added 119 packages, and audited 120 packages in 6s
11 packages are looking for funding
  run `npm fund` for details
found 0 vulnerabilities
[root@10 ~]#

In order to prove that the module with the name ‘nodemon’ is available as a global module, just execute the following command :

[root@10 ~]# npm list -g
/usr/lib
├── [email protected]
└── [email protected]
[root@10 ~]#

Finally, just try to execute the module with the name ‘nodemon’ as follows :

[root@10 ~]# nodemon
  Usage: nodemon [nodemon options] [script.js] [args]
  See "nodemon --help" for more.
[root@10 ~]#

One thought on “How to Solve Error Message EACCES: permission denied in NodeJS Application

Leave a Reply