5 April 2024 Leave a comment Tech-Help
If you’re encountering an EACCES: permission denied
error when trying to globally install a package with npm on macOS, this guide will walk you through a solution that has helped many users overcome this hurdle.
Understanding the Problem
When you attempt to install a package globally on macOS using npm, you might run into permission issues, especially if you’re using a command like sudo npm install -g
. This error occurs due to npm trying to access or modify directories that your user may not have the necessary permissions for.
Recommended Solution
To solve this problem, you can use a flag that tells npm to run with elevated permissions, as well as to allow it to execute as root. Here’s the command that you can use:
sudo npm install -g appium --unsafe-perm=true --allow-root
This command bypasses the permission checks and allows npm to perform the global installation without encountering the EACCES
error.
Alternative Solution
While the above command can provide a quick fix, it’s generally a good practice to avoid using sudo with npm to prevent future permission issues. Here are the steps to set up npm to operate globally without elevated permissions:
- Install Node Version Manager (nvm): This tool allows you to install and manage multiple versions of Node.js without the need for sudo.
- Install Node.js: Use nvm to install the latest Long Term Support (LTS) version of Node.js.
- Set Default Node.js Version: Make the newly installed version your default environment.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
After installation, you can verify nvm with command -v nvm
.
nvm install --lts
nvm alias default lts/*
By following these steps, you should be able to install npm packages globally without encountering permission issues.
Final Notes
Permission errors can be a nuisance, but with the right approach, they are easily resolved. If you frequently need to install packages globally, consider using nvm to manage your Node.js versions and avoid permission-related complications altogether.
For those who prefer not to use nvm, changing the ownership of npm’s directories to the current user is another option, though this method is not recommended due to security implications and potential conflicts with other system applications.
Remember, it’s essential to understand the commands and their implications before running them on your system, especially when they involve elevated permissions.