NextJS v15集成MDX后无法完成生产构建求助
Next.js 15 App Router 集成MDX后构建无产物问题
背景信息
正在开发基于NextJS v15 App Router的网站,已按照官方文档安装@next/mdx及相关依赖。
问题描述
执行bun run build命令后无报错退出,但未生成生产环境构建产物。启动生产服务器时提示找不到.next目录。
错误日志
构建时日志
$ bun run build $ next build ▲ Next.js 15.1.2 - Environments: .env.local Creating an optimized production build ... (node:15530) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead. (Use `node --trace-deprecation ...` to show where the warning was created) <w> [webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: Unable to snapshot resolve dependencies <w> [webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: Unable to snapshot resolve dependencies
启动生产服务器时日志
$ bun start $ next start ▲ Next.js 15.1.2 - Local: http://localhost:3000 - Network: http://10.255.255.254:3000 ✓ Starting... [Error: Could not find a production build in the '.next' directory. Try building your app with 'next build' before starting the production server.] error: script "start" exited with code 1
已尝试的解决方案
- 移除所有依赖、清理缓存后重新用
bun install安装 - 改用
npm执行安装和构建操作 - 将NextJS版本降级至
14.2.21
复现步骤
创建使用App Router的NextJS应用,按照官方文档安装MDX相关依赖即可复现。
补充信息
依赖列表
"dependencies": { "@hookform/resolvers": "^3.9.1", "@icons-pack/react-simple-icons": "^10.2.0", "@mdx-js/loader": "^3.1.0", "@mdx-js/react": "^3.1.0", "@next/mdx": "^15.1.2", "@radix-ui/react-accordion": "^1.2.2", "@radix-ui/react-dialog": "^1.1.4", "@radix-ui/react-icons": "^1.3.2", "@radix-ui/react-label": "^2.1.1", "@radix-ui/react-slot": "^1.1.1", "@types/mdx": "^2.0.13", "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", "embla-carousel-autoplay": "^8.5.1", "embla-carousel-react": "^8.5.1", "husky": "^9.1.7", "lint-staged": "^15.2.11", "lucide-react": "^0.469.0", "next": "^15.1.2", "next-themes": "^0.4.4", "react": "19.0.0", "react-countup": "^6.5.3", "react-dom": "19.0.0", "react-hook-form": "^7.54.2", "sharp": "^0.33.5", "sonner": "^1.7.1", "tailwind-merge": "^2.6.0", "tailwindcss-animate": "^1.0.7", "wemakedevs": ".", "zod": "^3.24.1" }
NextJS配置
// next.config.mjs import createMDX from "@next/mdx"; /** @type {import('next').NextConfig} */ const nextConfig = { // Configure `pageExtensions` to include markdown and MDX files pageExtensions: ["js", "jsx", "md", "mdx", "ts", "tsx"], // Optionally, add any other Next.js config below }; const withMDX = createMDX({ // Add markdown plugins here, as desired }); // Merge MDX config with Next.js config export default withMDX(nextConfig);
解决方案
1. 移除本地循环依赖
依赖列表中的"wemakedevs": "."会将当前项目作为依赖引入,触发循环依赖导致webpack缓存快照失败。直接删除该依赖项,重新执行构建。
2. 调整MDX配置适配Next.js 15
修改next.config.mjs,明确MDX配置项并使用defineConfig规范配置结构:
// next.config.mjs import createMDX from "@next/mdx"; import { defineConfig } from 'next/config'; const withMDX = createMDX({ options: { remarkPlugins: [], rehypePlugins: [], }, }); export default defineConfig(withMDX({ pageExtensions: ["js", "jsx", "md", "mdx", "ts", "tsx"], }));
3. 彻底清理缓存后重建
执行以下命令清空所有缓存并重新构建:
# 删除Next.js构建目录 rm -rf .next # 清理Bun缓存 bun cache clean # 重新安装依赖并构建 bun install && bun run build
4. 检查Node.js版本兼容性
Next.js 15要求Node.js版本为20.x或22.x,确认本地Node.js版本符合要求,避免版本不兼容导致的构建异常。
内容的提问来源于stack exchange,提问作者Adarsh Dubey
相关产品推荐
相关产品推荐

