Cara Mengatasi - Solusi Error: EACCES: permission denied, access '/usr/local/lib/node_modules'

Jadi ceritanya aku mau install package npm tapi kadang muncul pesan Error: EACCES: permission denied, access '/usr/local/lib/node_modules' .

Detail pesannya yang mucul di terminal seperti ini:

npm WARN deprecated [email protected]: request has been deprecated, see https://github.com/request/request/issues/3142

npm WARN deprecated [email protected]: this library is no longer supported

npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules

npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  [Error: EACCES: permission denied, access '/usr/local/lib/node_modules'] {
npm ERR!   stack: "Error: EACCES: permission denied, access '/usr/local/lib/node_modules'",
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules'
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.

Nah ternyata setelah aku cari tahu ternyata aku ga bisa install karena semua di handle sama root, jadi kalo misal gagal seperti itu aku harus menambahkan perintah sudo misal: sudo npm install [nama-package]

karena aku menghindari perintah sudo, maka aku ganti handle accessnya ke user.

cara untuk nampilin user siapa aja yang bisa nge handle atau mengakses directory /usr/local/lib/node_modules:

$  ls -la /usr/local/lib/node_modules

kalo di punyaku akan tampil seperti ini:

ls -la /usr/local/lib/node_modules

total 0

drwxr-xr-x    8 root             wheel   256 May 26 01:35 .
drwxrwxr-x  104 sanengineer      admin  3328 Aug 27 06:59 ..
drwxr-xr-x    8 root             wheel   256 May 26 01:34 expo-cli
drwxr-xr-x   11 root             wheel   352 Dec  4  2019 ghost-cli
drwxr-xr-x   22 root             wheel   704 Mar  6  2020 gscan
drwxr-xr-x   11 root             wheel   352 Dec  6  2019 gulp-cli
drwxr-xr-x   13 root             wheel   416 May 23 03:14 nodemon
drwxr-xr-x   23 root             wheel   736 Mar  6  2020 npm

Jadi dari hasil log terminal diatas, banyak permission directory yang hanya bisa di access oleh user root.

Oleh karena itu karena aku ingin mengganti permission directory .../node_module hanyak untuk nama User, jadi aku tulis perintah

$ sudo chown -R sanengineer /usr/local/lib/node_modules

sanengineer adalah nama user di pc aku.

kalo misal di check lagi pake command dibawah ini:

$ ls -la /usr/local/lib/node_modules

maka hasilnya seperti ini:

$ ls -la /usr/local/lib/node_modules

total 0

drwxr-xr-x    9 sanengineer  wheel   288 Sep 20 06:47 .
drwxrwxr-x  104 sanengineer  admin  3328 Aug 27 06:59 ..
drwxr-xr-x    8 sanengineer  wheel   256 May 26 01:34 expo-cli
drwxr-xr-x   11 sanengineer  wheel   352 Dec  4  2019 ghost-cli
drwxr-xr-x   22 sanengineer  wheel   704 Mar  6  2020 gscan
drwxr-xr-x   11 sanengineer  wheel   352 Dec  6  2019 gulp-cli
drwxr-xr-x   13 sanengineer  wheel   416 May 23 03:14 nodemon
drwxr-xr-x   23 sanengineer  wheel   736 Mar  6  2020 npm
drwxr-xr-x   22 sanengineer  wheel   704 Sep 20 06:47 serverless

jadi kalo misal aku mau nge install sesuatu di directory node_modules aku ga perlu pake perintah sudo asalkan aku udah login di pc aku make user sanengineer.