Error: spawn cmd.exe ENOENT when installing Sanity in existing NextJS project

2 replies
Last updated: Sep 7, 2023
Hello all, I tried following the instructions to install sanity into an existing NextJS product and I was getting this error after :
npm create sanity@latest -- --project ... --dataset production --provider google

Error: Command failed with exit code 2 (ENOENT): npm install --legacy-peer-deps --save @sanity/vision@3 sanity@3 @sanity/image-url@1 styled-components@5.2
spawn cmd.exe ENOENT
I tried
npm install --legacy-peer-deps
and restarting VSCode in Windows, but still gives the same error. Someone here had the same error, but there are no replies to that question.
AI Update

The error you're encountering (spawn cmd.exe ENOENT) is a Windows-specific issue related to how Node.js tries to spawn child processes, not actually a Sanity-specific problem. The ENOENT error means "no such file or directory" - essentially, npm can't find or execute cmd.exe properly.

Here are several solutions to try:

1. Check your ComSpec environment variable

The most common cause is a missing or incorrect ComSpec environment variable. Open a Command Prompt or PowerShell as Administrator and run:

echo %ComSpec%

It should return C:\Windows\System32\cmd.exe. If it doesn't exist or is incorrect, set it:

setx ComSpec "C:\Windows\System32\cmd.exe"

Then restart your terminal and VSCode completely.

2. Use an alternative installation method

Instead of using npm create sanity@latest, try installing the Sanity CLI globally first, then running init:

npm install -g @sanity/cli
cd your-nextjs-project
sanity init

This approach gives you more control and often avoids the spawning issues with npm create. The sanity init command will walk you through an interactive setup process for adding Sanity to your existing Next.js project.

3. Run from Command Prompt instead of VSCode terminal

Sometimes VSCode's integrated terminal has issues with environment variables. Try running the same command from a native Windows Command Prompt or PowerShell (not inside VSCode).

4. Manual installation

If the automated setup continues to fail, you can manually install Sanity into your Next.js project:

npm install sanity @sanity/vision styled-components
npm install --save-dev @sanity/types

Then create your Sanity Studio files manually following the Next.js integration documentation.

5. Check your PATH

Ensure C:\Windows\System32 is in your system PATH. Sometimes security software or system configurations can interfere with this.

6. Try a different package manager

If you have pnpm or yarn installed, try using those instead:

pnpm create sanity@latest
# or
yarn create sanity

The spawn cmd.exe ENOENT error is frustrating but usually fixable with the ComSpec fix. After trying these solutions, make sure to completely close and reopen your terminal/VSCode to ensure environment variables are refreshed. The issue is almost always environment-related on Windows rather than a problem with Sanity itself.

Show original thread
2 replies
This is a Windows error having to do with permissions on your machine, not with Sanity.
Thank you, I just switched to another OS to install and add it to the project and then pulled the changes to the Windows machine and it worked.

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?