The purpose
Executing an npm/npx command failed with an errno -4058 error.
Example:
npx create-next-app@latest
The following error appeared when I ran the above command.
npm error code ENOENT
npm error syscall lstat
npm error path C:\Users\XXXXXXXXX\AppData\Roaming\npm
npm error errno -4058
npm error enoent ENOENT: no such file or directory, lstat 'C:\Users\XXXXXXXXX\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\XXXXXXXXX\AppData\Local\npm-cache\_logs\2024-09-22T06_50_41_859Z-debug-0.log
XXXXXXXXX is user name.
solution
Here are two solutions.
While neither is guaranteed to work on its own, you should try both if the first one doesn’t solve the problem.
Shutting down the application running the Node.js server.
It seems that a Node.js server running in an application (like one started with npm run dev
) might be interfering.
This could include applications such as Command Prompt, PowerShell, or Visual Studio Code.
Please close these applications first, and then try running the failed npm/npx command again.
install npx
Install npx by running the following command in your command line (or similar).
npm i -g npx
retry the failed npm/npx command.
Even if npx
works in other projects, it won’t be available outside the project where it’s installed unless you’ve installed it globally with the -g
option.
Therefore, even if npx
worked before, it’s worth trying again (in the current project).
Result
In my environment, simply closing Visual Studio Code did not resolve the issue; the problem was only resolved after subsequently installing npx.
Reference

comment