使用npx create-next-app创建的Next.js应用初始化后无法正常启动
Next.js 全新项目启动报错排查步骤
我仅执行以下命令创建Next.js应用,未添加任何自定义代码,昨天还能正常运行,但今天每次创建新应用都会触发错误(见错误截图):
npx create-next-app example-app cd example-app npm run dev
请问有哪些解决步骤?
相关文件信息
package.json(截图)
pages/index.tsx 代码
import Head from 'next/head' import Image from 'next/image' import { Inter } from '@next/font/google' import styles from '@/styles/Home.module.css' const inter = Inter({ subsets: ['latin'] }) export default function Home() { return ( <> <Head> <title>Create Next App</title> <meta name="description" content="Generated by create next app" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <link rel="icon" href="/favicon.ico" /> </Head> <main className={styles.main}> <div className={styles.description}> <p> Get started by editing <code className={styles.code}>pages/index.tsx</code> </p> <div> <a href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app" target="_blank" rel="noopener noreferrer" > By{' '} <Image src="/vercel.svg" alt="Vercel Logo" className={styles.vercelLogo} width={100} height={24} priority /> </a> </div> </div> <div className={styles.center}> <Image className={styles.logo} src="/next.svg" alt="Next.js Logo" width={180} height={37} priority /> <div className={styles.thirteen}> <Image src="/thirteen.svg" alt="13" width={40} height={31} priority /> </div> </div> <div className={styles.grid}> <a href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app" className={styles.card} target="_blank" rel="noopener noreferrer" > <h2 className={inter.className}> Docs <span>-></span> </h2> <p className={inter.className}> Find in-depth information about Next.js features and API. </p> </a> <a href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app" className={styles.card} target="_blank" rel="noopener noreferrer" > <h2 className={inter.className}> Learn <span>-></span> </h2> <p className={inter.className}> Learn about Next.js in an interactive course with quizzes! </p> </a> <a href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app" className={styles.card} target="_blank" rel="noopener noreferrer" > <h2 className={inter.className}> Templates <span>-></span> </h2> <p className={inter.className}> Discover and deploy boilerplate example Next.js projects. </p> </a> <a href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app" className={styles.card} target="_blank" rel="noopener noreferrer" > <h2 className={inter.className}> Deploy <span>-></span> </h2> <p className={inter.className}> Instantly deploy your Next.js site to a shareable URL with Vercel. </p> </a> </div> </main> </> ) }
pages/_app.tsx 代码
import '@/styles/globals.css' import type { AppProps } from 'next/app' export default function App({ Component, pageProps }: AppProps) { return <Component {...pageProps} /> }
pages/_document.tsx 代码
import { Html, Head, Main, NextScript } from 'next/document' export default function Document() { return ( <Html lang="en"> <Head /> <body> <Main /> <NextScript /> </body> </Html> ) }
排查解决步骤
- 清理npm缓存:缓存损坏可能导致依赖安装异常,执行:
npm cache clean --force - 更新Node.js版本:Node.js版本不兼容可能引发问题,建议升级到LTS版本(18或20均可),之后重新创建项目。
- 清除npx全局缓存:全局脚手架缓存可能存在问题,执行:
npx clear-npx-cache - 指定稳定版本创建项目:如果最新版
create-next-app存在bug,尝试指定已知稳定版本:npx create-next-app@13.4.0 example-app - 检查网络环境:网络不稳定会导致依赖下载不完整,切换稳定网络后重新执行创建流程。
- 彻底删除残留项目文件:确保之前的项目目录完全删除,无残留文件后再重新创建。
- 更新npm版本:npm版本过旧也可能出现兼容性问题,执行:
npm install -g npm@latest
内容的提问来源于stack exchange,提问作者Julius Goddard
相关产品推荐
相关产品推荐

