2

I just created a new project using the "Basic Node.js Express 4 Application" template, and the project appears to already have errors:

enter image description here

Is this a bug in Visual Studio? How do I fix the errors?

Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235

1 Answers1

2

Just Wait

When you create a new project using this template, Visual Studio runs npm install in the background to download packages and type information from the npm registry. Until that type information is downloaded, you may temporarily see errors in the editor.

Running npm install might take as long as 2 minutes depending on your network and disk conditions. If you don't yet see the npm command completed message, then npm is still running and you need to go grab a quick coffee. Once the install finishes, Visual Studio will automatically updated the Intellisense errors.

Waiting Didn't Work?

If you've waited a little and the errors are still there, the first step to diagnosing the problem is to check the Output window to see how the npm install is going. From the top menu, select View -> Output, then in the Output Window, select Npm in the combo box labeled Show output from: Output window showing npm status

What you do next depends on what you see in the output window.

Try Again

Due to a bug in the interaction between npm's staging behavior, graceful-fs, and the Win32 MoveFileEx API, npm install may randomly fail. You'll see an error trace in the Output Window that looks like this:

npm ERR! path C:\Users\ryanca\source\repos\ExpressApp41\ExpressApp41\node_modules\@types\debug\package.json.2541088048
npm ERR! code EPERM
npm ERR! errno -4048
npm ERR! syscall rename
npm ERR! Error: EPERM: operation not permitted, rename 'C:\Users\ryanca\source\repos\ExpressApp41\ExpressApp41\node_modules\@types\debug\package.json.2541088048' -> 'C:\Users\ryanca\source\repos\ExpressApp41\ExpressApp41\node_modules\@types\debug\package.json'
npm ERR!  { Error: EPERM: operation not permitted, rename 'C:\Users\ryanca\source\repos\ExpressApp41\ExpressApp41\node_modules\@types\debug\package.json.2541088048' -> 'C:\Users\ryanca\source\repos\ExpressApp41\ExpressApp41\node_modules\@types\debug\package.json'
npm ERR!   cause: 
npm ERR!    { Error: EPERM: operation not permitted, rename 'C:\Users\ryanca\source\repos\ExpressApp41\ExpressApp41\node_modules\@types\debug\package.json.2541088048' -> 'C:\Users\ryanca\source\repos\ExpressApp41\ExpressApp41\node_modules\@types\debug\package.json'
npm ERR!      errno: -4048,
npm ERR!      code: 'EPERM',
npm ERR!      syscall: 'rename',
npm ERR!      path: 'C:\\Users\\ryanca\\source\\repos\\ExpressApp41\\ExpressApp41\\node_modules\\@types\\debug\\package.json.2541088048',
npm ERR!      dest: 'C:\\Users\\ryanca\\source\\repos\\ExpressApp41\\ExpressApp41\\node_modules\\@types\\debug\\package.json' },
npm ERR!   stack: 'Error: EPERM: operation not permitted, rename \'C:\\Users\\ryanca\\source\\repos\\ExpressApp41\\ExpressApp41\\node_modules\\@types\\debug\\package.json.2541088048\' -> \'C:\\Users\\ryanca\\source\\repos\\ExpressApp41\\ExpressApp41\\node_modules\\@types\\debug\\package.json\'',
npm ERR!   errno: -4048,
npm ERR!   code: 'EPERM',
npm ERR!   syscall: 'rename',
npm ERR!   path: 'C:\\Users\\ryanca\\source\\repos\\ExpressApp41\\ExpressApp41\\node_modules\\@types\\debug\\package.json.2541088048',
npm ERR!   dest: 'C:\\Users\\ryanca\\source\\repos\\ExpressApp41\\ExpressApp41\\node_modules\\@types\\debug\\package.json',
npm ERR!   parent: 'express-app41' }
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\ryanca\AppData\Roaming\npm-cache\_logs\2018-03-28T16_48_13_111Z-debug.log
====npm command completed with exit code -4048====

DO NOT follow npm's suggestion to re-run the command as an Administrator; this will only make things worse. Run npm install from a regular commandline, or equivalently, select Install Missing npm Packages from the npm node in the Solution Explorer: Image of the context menu

Note that this will run npm install, which may fail again (but probably won't).

Close the Solution

A small portion of users have seen npm install fail repeatedly. In the very unlikely event this fails again, you can try closing the solution, running npm install from the commandline, and re-opening the solution, but in general this should not be necessary. If npm install fails even with Visual Studio not running, then you likely have another problem, such as a corrupted npm cache, disk error, or some other npm bug not covered by this answer.

Consider Downgrading or Upgrading npm

The bug causing the EPERM errors was introduced in npm version 5, so if this is somehow causing serious problems for you, you can consider downgrading to any 4.x.x version. At time of writing the bug has not been fixed, but hopefully in the future a new version of npm with the graceful-fs bug fix will be released and you can simply upgrade.

Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235