128

I just updated npm to 5.4.0.
Now, Whenever I want install a npm package I get the following error:

D:\Sources\DownloadCms\Md.Download\Web.Angular>npm install mds.angular.datetimepicker@latest --save
npm ERR! path D:\Sources\DownloadCms\Md.Download\Web.Angular\node_modules\fsevents\node_modules\abbrev\package.json
npm ERR! code EPERM
npm ERR! errno -4048
npm ERR! syscall unlink
npm ERR! Error: EPERM: operation not permitted, unlink 'D:\Sources\DownloadCms\Md.Download\Web.Angular\node_modules\fsevents\node_modules\abbrev\package.json'
npm ERR!     at Error (native)
npm ERR!  { Error: EPERM: operation not permitted, unlink 'D:\Sources\DownloadCms\Md.Download\Web.Angular\node_modules\fsevents\node_modules\abbrev\package.jso
n'
npm ERR!     at Error (native)
npm ERR!   stack: 'Error: EPERM: operation not permitted, unlink \'D:\\Sources\\DownloadCms\\Md.Download\\Web.Angular\\node_modules\\fsevents\\node_modules\\ab
brev\\package.json\'\n    at Error (native)',
npm ERR!   errno: -4048,
npm ERR!   code: 'EPERM',
npm ERR!   syscall: 'unlink',
npm ERR!   path: 'D:\\Sources\\DownloadCms\\Md.Download\\Web.Angular\\node_modules\\fsevents\\node_modules\\abbrev\\package.json' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Mohammad\AppData\Roaming\npm-cache\_logs\2017-09-03T03_25_50_432Z-debug.log

I'm dead sure, run CMD as administrator.

Also I checked D:\Sources\DownloadCms\Md.Download\Web.Angular\node_modules\fsevents\node_modules\abbrev\package.json\, package.json is not exist in path !

Edit: Upgrade to v5.4.1, still get the same error, even cannot work around with --no-optional :-(

2
  • I'm having this issue on npm 8.5.0, nn-no-optional doesn't work
    – niico
    Commented Mar 26, 2022 at 19:34
  • All I needed to do, was to stop my dev server and retry the command Commented Nov 7, 2023 at 17:58

51 Answers 51

193

I was able to fix this by running the command prompt/bash as admin and closing VSCode! Seems like VSCode was locking some files. Potentially something else could be locking these files for you.

8
  • 4
    In my case, Process Explorer invariably tells me that the culprit locking the files npm is trying to delete is… another node.exe process spawned by npm running npm! Oh joy, this tool never ceases to surprise… (this is on Windows 10, Node 12.11.0, npm 6.11.3) Commented Oct 1, 2019 at 13:25
  • why the hell vscode is failing the installation of angular app? Commented Oct 10, 2019 at 5:44
  • 3
    +1 For closing any running processes that may be hanging onto the file - I had vue-cli-service serve running. Closing that solved it for me. Commented Mar 20, 2020 at 10:02
  • 3
    +1 - note for me I also had to close all the orphaned(?) instances of node.js that seem to build up over time when debugging in vscode on windows
    – mutex
    Commented May 28, 2020 at 23:26
  • VS Code locked a file in my case, which was fixed after restarting VS Code. The error was : An unhandled exception occurred: EPERM: operation not permitted, unlink fa-regular-400.1495f578452eb676f730.ttf
    – viking
    Commented Mar 15, 2021 at 16:27
103

It is an npm 5.4.0 issue https://github.com/npm/npm/issues/18287

Workarounds are

  • downgrade to 5.3
  • try running with --no-optional, i.e. npm install --no-optional
5
  • 11
    --no-optional completely nailed it for me (@5.4.1)!
    – sunny moon
    Commented Sep 8, 2017 at 16:09
  • 24
    if you have opened VSCode then close it and try running npm command it will be installed definitely, downgrading is not the solution. Commented Oct 10, 2019 at 5:46
  • 6
    Closing down the VS Code and ran npm i from outside worked for me. Commented Nov 16, 2019 at 22:48
  • Its fixed in modern versions.
    – Alex78191
    Commented Apr 3, 2021 at 14:46
  • I have npm version 6.0 and just restarting VScode worked for me. Commented Mar 27, 2022 at 5:19
23

Please close all IDE like visual studio code. run npm install command through node.js command prompt. Enjoy !

2
  • In some cases need to restart system. Commented Jul 31, 2019 at 5:53
  • Also check .NPMRC file Commented Nov 12, 2019 at 7:46
18

cache clean and npm update to latest with force work for me

npm cache clean --force

npm install -g npm@latest --force
0
17

If you downgrade to 5.3 and still get the same error in Windows like me.
After hours working with npm versions I found the following solution:

1. Download latest recommended version of nodejs, these days is node-v6.11.3-x64
2. Uninstall nodejs with it.
3. Go to C:\Users\{YourUsername}\AppData\Roaming folder and delete npm and npm-cache folders
4. Run installer of nodejs again and install it
5 Update npm to 5.3 with npm i -g [email protected] command line

Now you should use npm without any issues.

1
13

I tried this solution found at a How to fix Node.js blog

just use

npm cache clean

in windows if it refuses use

npm cache clean --force
1
  • 3
    Doesn't work for me. Windows in CI on VSTS (now Azure DevOps) Commented Sep 10, 2018 at 20:11
11

I fixed by downgrading npm from 5.4.0 to version 5.3

npm i -g [email protected]

I Hope this helps for you

11

I had the same problem on Windows.

The source of the problem is simple, it is access permission on folders and files.

In your project folder, you need

  1. After cloning the project, change the properties of the folder and change the permissions of the user (give full access to the current user).
  2. Remove the read-only option from the project folder. (Steps 1 and 2 take a long time because they are replicated to the entire tree below).
  3. Inside the project folder, reinstall the node (npm install reinstall -g)
  4. Disable Antivirus. (optional)
  5. Disable Firewall. (optional)
  6. Restart PC.
  7. Clear the npm cache (npm clear)
  8. Install the dependencies of your project (npm install)

After that, error "Error: EPERM: operation not permitted, unlink" will no longer be displayed.

Remember to reactivate the firewall and antivirus if necessary.

9

For those who are coming from Windows OS, you just need to stop the development server and then execute your npm install ... command.

It is strange how Windows doesn't allow installing dependencies while server is running, but MacOS does allow. If this doesn't work, then only go for the other options - clean cache, downgrade nodejs etc.

2
  • Been pulling my hair for hours. Turns out i had a GIt Bash running the dev server. Thanks
    – tomexsans
    Commented Feb 15, 2022 at 2:09
  • What's the development serveR?
    – AgentFire
    Commented Jan 17 at 9:19
8

I had the same issue and all I needed to do was login to npm

npm login

or alternatively

npm add user // consult the documentation for the params  

6

In my case, the problem was that, I did not install typescript. Although I did install Node and Angular. To check if you have installed typescript or not

Run this command: tsc -v

If not, then to install typescript

Run this command: npm install -g typescript

And, finally to install required dependencies

Run this command: npm install

in the root folder of the project.

---- Hope this helps someone ----

1
5

If all of the above failed to work for you, you might want to

  • restart your system
  • run command prompt as admin
  • run the npm command
0
4

I'm using VsCode and solved this issue by stopping the application server and them run npm install. There are files that were locked by the application server.

No need to close the IDE, just make sure there's no another process locking some files on your projects.

4

Mine was as a result of opening my project folder a 2 different terminals. I solved it by closing all running terminal (vs code was excluded) and executing the installation command again.

I hope this helps someone.

NB: deleting node_modules didn't solve it.

4

I fixed this by removing the dist folder

--- Remove dist or public folder

4

just close the working operations inside the project

3

I had this logs in Windows. I did the following

  • Close Visual Studio Code
  • Run Command Line as admin
3

Deleting package-lock.json fixed it for me.

3

For me it was Docker...

The moment that I closed the app container, I could do an npm install without any proble

P.S My node version is 14.15.5

3

In addition to closing VS CODE I also had to run this command

taskkill -F -IM node.exe

to stop the node process as my test server was still running which was causing the install error.

2

For me it worked in bash from git package try:

C:\Program Files\Git\bin\bash.exe

then:

npm install mds.angular.datetimepicker@latest
2

There seems to be many solutions out there that worked with downgrading npm versions. For me, the solution was

npm install -force

I tried the downgrading of npm versions, modifying my npm prefix config to match the npm directory, and clearing cache. None of these worked, but apparently they worked for others, so it may be worth a shot.

2

For me on Windows the problem was too long path length. I moved the project to a smaller length path and it worked.

2

After trying everything, including node/npm upgrade, cache cleaning and reverting code, nothing helped besides one simple thing: Turning OFF Windows 10's Real-time protection during the dev/build. Looks like latest updates made it super aggressive.

2

Check if you are running 'npm run dev' or 'npm run watch'. If you do, then exit it and re-run 'npm install "package"'.

I hope it works!!!

2

in my case i try to deploy my serverless app and showing this error,i've tried all the solution above and not works,unexpectedly the solution is as simple as delete the .build folder and try redeploy, voila everythings good!. try to delete the node_modules or build folder.

1
the same error comes to me when i update the npm version to the latest 5.4 downgrade to the version 5.3.0 is useful.the error comes from the npm 5.4,you can check it in the issuses in npm 5.4
npm install [email protected] -g
1

Fixed in NPM 5.6.0

Upgrade to NPM 5.6.0 solved problem for me.

1

I wanted to run npm install from within my external hard drive as this is where i saved my code workspace. Windows 10 OS.

But I was getting the same error as the original post.None of the previous answers worked for me, I tried all of them:

  1. uninstalling nodejs then re-installing
  2. uninstalling nodejs then downgrading/installing a lower version of nodejs.
  3. npm install -force
  4. deleting the folders from C:\Users{YourUsername}\AppData\Roaming ... npm and npm-cache then re-installing.
  5. npm cache clean --force
  6. npm cache clean
  7. npm install --g or npm install --global

What worked for me was this:

  1. copy the folder from C:\Program Files\nodejs to D:\Program Files\nodejs
  2. Then go to Control Panel\System and Security\System
  3. Advanced System Settings
  4. Environment Variables
  5. System Variables
  6. Double click Path
  7. Add a new path
  8. D:\Program Files\nodejs
  9. Click ok
  10. restart PC.
  11. try npm install from within D: Drive
1

npm cache verify solved my issue. I was doing: ng new my-app and I faced similar error

I have node version: 10.16.0
npm v 6.9.0

Not the answer you're looking for? Browse other questions tagged or ask your own question.