How to Solve Insufficient Permission Errors When Installing Global Packages with NPM
The insufficient permission error (EACCES: permission denied) when trying to install Node.js packages globally (with the npm install -g command) occurs because NPM's default directory (/usr/local/lib/node_modules/) is owned exclusively by the root user of your operating system's filesystem.
Quick Diagnostics
sudo chown -R $USER:$USER node_modulesnpm cache clean --forceThe insufficient permission error (EACCES: permission denied) when trying to install Node.js packages globally (with the npm install -g command) occurs because NPM's default directory (/usr/local/lib/node_modules/) is owned exclusively by the root user of your operating system's filesystem.
Step-by-Step Solution
-
1
Step 1: Configurar una ruta de instalación local para NPM
You should not use
sudoto install global packages, as this can allow the execution of malicious scripts with root privileges. Instead, configure a local directory in your personal folder:BASH# Crear la carpeta de instalación en tu directorio de usuario mkdir -p ~/.npm-global # Indicar a NPM que use esta ruta para los paquetes globales npm config set prefix '~/.npm-global' -
2
Step 2: Exportar la nueva ruta a tu entorno de terminal
To be able to run the installed binaries from anywhere, add the new directory to your
PATHvariable:BASH# Agregar la línea de exportación a tu perfil de shell (.bashrc o .zshrc) echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc # Recargar la configuración del shell actual en memoria source ~/.bashrc -
3
Step 3: Probar la instalación limpia sin privilegios de root
Install any global tool (for example, the Firebase CLI) to confirm that you no longer need to use
sudo:BASHnpm install -g firebase-tools
Prevention Advice
Recommended security practices:
- Avoid forcing the installation of development dependencies using the
sudo npm installcommand. By using the superuser account on Node.js repositories, you grant full control permissions to any post-installation script of external third-party packages, which exposes your configuration files and credentials to vulnerabilities of information theft or malware injection in your work environments.