node.jsnpminstallationglobal

Whenever I try to install global npm packages, I get permission denied. How can I solve this. How can I install without using sudo first


when i try to install ex:

npm install -g n

I got a error:

npm error code EACCES
npm error syscall rename
npm error path /usr/local/lib/node_modules/n 
npm error dest /usr/local/lib/node_modules/.n-oBR4I52F 
npm error errno -13 
npm error Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/n' -> '/usr/local/lib/node_modules/.n-oBR4I52F' 
npm error     at async Object.rename (node:internal/fs/promises:782:10)
npm error     at async moveFile (/usr/lib64/node_modules/npm20/node_modules/@npmcli/fs/lib/move-file.js:30:5)
npm error     at async Promise.allSettled (index 0) 
npm error     at async [reifyPackages] (/usr/lib64/node_modules/npm20/node_modules/@npmcli/arborist/lib/arborist/reify.js:325:11) 
npm error     at async Arborist.reify (/usr/lib64/node_modules/npm20/node_modules/@npmcli/arborist/lib/arborist/reify.js:142:5) 
npm error     at async Install.exec (/usr/lib64/node_modules/npm20/lib/commands/install.js:150:5) 
npm error     at async module.exports (/usr/lib64/node_modules/npm20/lib/cli/entry.js:74:5) { 
npm error   errno: -13, 
npm error   code: 'EACCES', 
npm error   syscall: 'rename', 
npm error   path: '/usr/local/lib/node_modules/n', 
npm error   dest: '/usr/local/lib/node_modules/.n-oBR4I52F' 
npm error } 
npm error 

i try to install with sudo, but do not work. my company is blocking the installation.


Solution

  • Based on your log errors, i can see that is EACCES permissions errors

    As the npm package documentation suggest, try to manually npm default directory, you can follow the steps mentioned in this document:

    Note: Below links works in Mac and Linux based OS, not for Windows.

    So even if you do not get your issue resolved after changing directory manually, taking a look at these source should help you out:

    Hope these will help you fix installing the packages globally.