Home    /    Answers    /    Error
Solve permission denied error while installing npm package

In this answer, We will see the solution to a problem we are getting(permission denied) when we install an npm package globally without the correct permissions.

Exact error: Error: EACCES: permission denied, open '/Users/test/package-lock.json'

Logs:

npm ERR! code EACCES
npm ERR! syscall open
npm ERR! path /Users/test/package-lock.json
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, open '/Users/test/package-lock.json'
npm ERR!  [Error: EACCES: permission denied, open '/Users/test/package-lock.json'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'open',
npm ERR!   path: '/Users/test/package-lock.json'
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.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/test/.npm/_logs/2023-02-18T05_38_18_123Z-debug-0.log

Method 1: Use sudo command

You can use sudo command to install any package with administrator permissions. To install any package with npm run the given command:

sudo npm install -g <package-name>

After running this command it may ask you to enter the password of your computer and after entering the password your package will install.

Method 2: Change the ownership

On the other side, we can change the `ownership` of the directory where npm is installed on your computer. To do that we can use the given command:

sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

Method 3: Install packages locally

Try to install the npm package locally instead of globally. To install any package locally use the given command:

npm install <package-name>

Hope one of the solutions works for you ☑️.