在React/Next.js电商项目中安装Sanity.io依赖时遇报错问题
解决Sanity项目创建时
npm install --legacy-peer-deps报错(spawn cmd.exe ENOENT) 问题场景
我用React.js、Next.js、Sanity和Stripe构建电商应用,执行npm create sanity@latest创建Sanity项目时,在运行npm install --legacy-peer-deps步骤出现错误:
Error: Command failed with exit code 2 (ENOENT): npm install --legacy-peer-deps spawn cmd.exe ENOENT
完整操作过程:
PS C:\Users\KC\Desktop\ecommerce\ecommerce> npm create sanity@latest Need to install the following packages: create-sanity@3.12.0 Ok to proceed? (y) y You're setting up a new project! We'll make sure you have an account with Sanity.io. Then we'll install an open-source JS content editor that connects to the real-time hosted API on Sanity.io. Hang on. Press ctrl + C at any time to quit. Prefer web interfaces to terminals? You can also set up best practice Sanity projects with your favorite frontends on https://www.sanity.io/templates Looks like you already have a Sanity-account. Sweet! ✔ Fetching existing projects ? Select project to use Create new project ? Your project name: sanity_ecommerce Your content will be stored in a dataset that can be public or private, depending on whether you want to query your content with or without authentication. The default dataset configuration has a public dataset named "production". ? Use the default dataset configuration? Yes ✔ Creating dataset ? Would you like to add configuration files for a Sanity project in this Next.js folder? No ? Project output path: C:\Users\KC\Desktop\ecommerce\ecommerce\sanityecommerce ? Select project template Clean project with no predefined schemas ? Do you want to use TypeScript? No ✔ Bootstrapping files from template ✔ Resolving latest module versions ✔ Creating default project files ? Package manager to use for installing dependencies? npm Running 'npm install --legacy-peer-deps' Error: Command failed with exit code 2 (ENOENT): npm install --legacy-peer-deps spawn cmd.exe ENOENT
解决方案
这个错误是Windows系统下终端无法定位到cmd.exe导致的,以下是几种修复方法:
检查系统环境变量Path
右键「此电脑」→「属性」→「高级系统设置」→「环境变量」,在系统变量的Path中确认是否包含C:\Windows\System32,如果没有,添加该路径后重启终端重试。以管理员身份运行终端
关闭当前PowerShell窗口,右键选择「Windows PowerShell(管理员)」,重新执行npm create sanity@latest流程。手动进入项目目录执行安装
项目文件已经生成,进入指定路径:cd C:\Users\KC\Desktop\ecommerce\ecommerce\sanityecommerce然后手动执行安装命令:
npm install --legacy-peer-deps更新npm到稳定版本
执行以下命令更新全局npm:npm install -g npm@latest更新完成后重新尝试创建项目。
内容的提问来源于stack exchange,提问作者Muhammad Aamir
相关产品推荐
相关产品推荐

