How to fix npm error: Please try running this command as root/Administrator

When running npm commands, you might encounter an error saying you need to run the command again as root.

The error usually happens when you don’t have permission to access or modify folders located inside your Node project.

An example of the error is shown below:

npm ERR! Error: EACCES: permission denied, open 'npm-debug.log.3300950547'
npm ERR!     at Error (native)
npm ERR!  {[Error: EACCES: permission denied, open 'npm-debug.log.3300950547']
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'open',
npm ERR!   path: 'npm-debug.log.3300950547' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/nsebhastian/Desktop/DEV/learn-node/npm-debug.log

When you see this error, the first thing to do is try running the command as administrator.

For macOS or Linux, you can add the sudo command before your npm command as shown below:

sudo npm install
# or
sudo npm start

For Windows users, you need to run the Command Prompt or PowerShell as Administrator.

You can find the option in the Start window as shown below:

Or you can also right-click on the application icon and select Run as Administrator from the context menu:

Run the npm command in the newly opened terminal and see if you can run it successfully.

Please note that if running as root/Administrator is successful, then there are two things that can cause this issue:

  • You may have created your project using root/Administrator privileges.
  • You create the project under a different user. The current user has no privilege to read, write or execute the project.

This means that each time you want to run npm commands like npm install, then you need to run it as the root/Administrator user.

To let the npm commands run without admin roles, you can change the permission using chmod command:

sudo chmod -R 777 project-folder/

For Windows, you need to run the CACLS command while opening the Command Prompt or PowerShell as Administrator.

The example is as follows:

cacls C:\project-folder /e /p Everyone:f

First, specify the project folder after the cacls command. /e will preserve old permissions, while /p will add new permissions.

Everyone is the username and :f allows full control (Read, Write, Execute)

This way, you should be able to run npm install without having root/Administrator privileges.

For Windows: remove the read-only attribute from your project

If you’re using a Windows computer, then you also need to make sure that the read-only attribute is removed from your project.

Right-click on your project folder and select Properties from the context menu.

Make sure that the read-only attribute is unchecked as shown below:

Once done, click OK and run your npm commands again. You may also do the same with your node_modules/ folder.

If you still see the error, then you may need to verify or clean the npm cache.

Cleaning npm cache

If you recently upgraded your npm to version 5 and above, then you need to verify your cache to run the npm install command.

You can verify your npm cache with the command below:

npm cache verify

Once the command has finished running, run the npm install command again.

If you still see the error, then clear the npm cache:

npm cache clean --force

Once the cache has been cleared, try running your npm commands again.

Conclusion

The npm error Please try running this command again as root/Administrator is usually caused by a lack of permissions to write or execute the files inside your project.

This error can be resolved by making sure that you have the right permissions to access the project files and folders, usually using chmod or cacls command.

You can also use the user interface to change the permissions. Right-click on the project folder and select Get info for Mac or Properties for Windows.

From there, you can change the permissions and give access to the current user.

I hope this tutorial has helped you. Good luck solving the error! 👍

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.