NextJS项目静态导出构建失败问题求助
Next.js 静态导出构建失败问题解决
我有一个Next.js项目,需要导出静态代码用于发布,但执行npm run build时出现报错。尝试过安装swc、重置缓存、删除node_modules后重新安装等操作,问题仍未解决。以下是报错信息、package.json及next.config.js配置内容:
报错信息
npm run build > rus@0.1.0 build > next build warn - Invalid next.config.js options detected: warn - The value at .output must be a string but it was an object. warn - The value at .output must be one of: "standalone" or "export". warn - See more info here: https://nextjs.org/docs/messages/invalid-next-config warn - You have enabled experimental feature (appDir) in next.config.js. warn - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk. info - Thank you for testing `appDir` please leave your feedback at https://nextjs.link/app-feedback info - Using wasm build of next-swc warn - Attempted to load @next/swc-win32-x64-gnu, but it was not installed warn - Attempted to load @next/swc-win32-x64-msvc, but an error occurred: The specified module could not be found. \\\?\C:\Users\Ufhcdj\Desktop\react\rus\node_modules\@next\swc-win32-x64-msvc\next-swc.win32-x64-msvc.node Failed to compile. HookWebpackError: File C:\Users\Ufhcdj\Desktop\react\rus\.next\server\app\icons\icon\[[...__metadata_id__]]\route.js does not exist. at makeWebpackError (C:\Users\Ufhcdj\Desktop\react\rus\node_modules\next\dist\compiled\webpack\bundle5.js:28:308185) at C:\Users\Ufhcdj\Desktop\react\rus\node_modules\next\dist\compiled\webpack\bundle5.js:28:105236 at eval (eval at create (C:\Users\Ufhcdj\Desktop\react\rus\node_modules\next\dist\compiled\webpack\bundle5.js:13:28771), <anonymous>:57:1) at C:\Users\Ufhcdj\Desktop\react\rus\node_modules\next\dist\compiled\webpack\bundle5.js:28:68649 at C:\Users\Ufhcdj\Desktop\react\rus\node_modules\next\dist\build\webpack\plugins\next-trace-entrypoints-plugin.js:420:143 -- inner error -- Error: File C:\Users\Ufhcdj\Desktop\react\rus\.next\server\app\icons\icon\[[...__metadata_id__]]\route.js does not exist. at Job.emitDependency (C:\Users\Ufhcdj\Desktop\react\rus\node_modules\next\dist\compiled\@vercel\nft\index.js:1:39473) at async Promise.all (index 5) at async nodeFileTrace (C:\Users\Ufhcdj\Desktop\react\rus\node_modules\next\dist\compiled\@vercel\nft\index.js:1:35430) at async C:\Users\Ufhcdj\Desktop\react\rus\node_modules\next\dist\build\webpack\plugins\next-trace-entrypoints-plugin.js:141:28 at async Span.traceAsyncFn (C:\Users\Ufhcdj\Desktop\react\rus\node_modules\next\dist\trace\trace.js:97:20) at async TraceEntryPointsPlugin.createTraceAssets (C:\Users\Ufhcdj\Desktop\react\rus\node_modules\next\dist\build\webpack\plugins\next-trace-entrypoints-plugin.js:95:9) caused by plugins in Compilation.hooks.processAssets Error: File C:\Users\Ufhcdj\Desktop\react\rus\.next\server\app\icons\icon\[[...__metadata_id__]]\route.js does not exist. at Job.emitDependency (C:\Users\Ufhcdj\Desktop\react\rus\node_modules\next\dist\compiled\@vercel\nft\index.js:1:39473) at async Promise.all (index 5) at async nodeFileTrace (C:\Users\Ufhcdj\Desktop\react\rus\node_modules\next\dist\compiled\@vercel\nft\index.js:1:35430) at async C:\Users\Ufhcdj\Desktop\react\rus\node_modules\next\dist\build\webpack\plugins\next-trace-entrypoints-plugin.js:141:28 at async Span.traceAsyncFn (C:\Users\Ufhcdj\Desktop\react\rus\node_modules\next\dist\trace\trace.js:97:20) at async TraceEntryPointsPlugin.createTraceAssets (C:\Users\Ufhcdj\Desktop\react\rus\node_modules\next\dist\build\webpack\plugins\next-trace-entrypoints-plugin.js:95:9) > Build failed because of webpack errors info - Creating an optimized production build .
package.json 配置
{ "name": "rus", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev", "build": "next build", "export": "next export", "start": "next start", "lint": "next lint" }, "dependencies": { "eslint": "8.39.0", "eslint-config-next": "13.3.1", "next": "^13.3.1", "react": "^18.2.0", "react-dom": "^18.2.0" } }
next.config.js 配置
/** @type {import('next').NextConfig} */ const nextConfig = { experimental: { appDir: true, }, output: { dir: "out", }, }; module.exports = nextConfig;
解决步骤
1. 修正 next.config.js 的 output 配置
报错明确提示output必须是字符串类型,可选值为"standalone"或"export",当前写成对象格式是构建失败的核心原因。因为需要静态导出,直接将output设置为"export"即可(静态导出默认输出目录就是out,无需额外配置对象)。
修正后的next.config.js:
/** @type {import('next').NextConfig} */ const nextConfig = { experimental: { appDir: true, }, output: "export", // 启用静态导出模式 }; module.exports = nextConfig;
2. 清理缓存并重新构建
执行以下命令彻底清理旧构建文件和依赖,重新安装并构建:
# Windows系统 rmdir /s /q .next out node_modules npm install npm run build # macOS/Linux系统 rm -rf .next out node_modules npm install npm run build
3. 处理SWC加载警告(可选)
构建时出现的SWC加载警告,是因为缺少对应Windows平台的SWC编译包,可手动安装解决:
npm install @next/swc-win32-x64-msvc --save-dev
修正配置后重新构建,即可正常生成静态文件到out目录。
内容的提问来源于stack exchange,提问作者Boombaar
相关产品推荐
相关产品推荐

