npm/npx commands are failing.(errno -4058)

This article can be read in about 4 minutes.
PR

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.

PR

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).

PR

Result

In my environment, simply closing Visual Studio Code did not resolve the issue; the problem was only resolved after subsequently installing npx.

PR

Reference

【Next.js】プロジェクトを作成しようとした際に発生したエラーの解消法 - Qiita
背景新しいPCに買い替えたのでNext.jsの勉強をしようと思い、プロジェクトを作成しようとしたが、エラー発生。環境Node.js:20.10.0エラー内容npx create-next…
npm installがENOENTとか変な失敗をするとき、VS Codeを閉じると良いかも。 | Ginpen.com

comment

Copied title and URL