Fixing the Error When Running npx for the First Time on Windows

Introduced with npm version 5.2 back in 2017, npx is a super useful command that lets you run a global package without needing to install it first with npm -g.

Today, I’m here to share a fix for a common but easy-to-solve issue. Often, when running the npx command for the first time after installing Node.js (which includes both npm and npx) on Windows, you might see an error message like this:

npm error code ENOENT
npm error syscall lstat
npm error path C:\Users\XXXXX\AppData\Roaming\npm
npm error errno -4058
npm error enoent ENOENT: no such file or directory, lstat 'C:\Users\XXXXX\AppData\Roaming\npm'
npm error enoent This is related to npm not being able to find a file.
npm error enoent
npm error A complete log of this run can be found in: C:\Users\XXXXX\AppData\Local\npm-cache_logs\2024-10-28T20_59_25_355Z-debug-0.log

The error is caused by the absence of the npm folder in the path indicated in line 5. This folder should have been created during the Node.js installation, but for some reason, it wasn’t. According to a closed issue in the npm/cli repository from December 2023, the bug has been fixed in recent versions of npm. However, I was still able to reproduce the issue with a relatively up-to-date version of the utility.

The fix, as I mentioned, is simple. Here’s a step-by-step guide.

  1. Open Windows Explorer, and in the address bar, type %APPDATA%. This will open the folder C:\Users\XXXXX\AppData\Roaming, where XXXXX is the name of the authenticated Windows user.
  2. In this folder, right-click, select New, and then Folder. Rename the newly created folder to npm.

And that’s it! The next time you run npx, it should work without any errors.

Happy coding!

Featured image credits: Freepik

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

Rolar para cima