描述:
我的 Next.js 项目在构建过程中遇到问题。本地构建和部署服务器期间的日志如下所示。
本地构建日志:
$ next build
▲ Next.js 14.1.0
Creating an optimized production build ...
✓ Compiled successfully
✓ Linting and checking validity of types
✓ Collecting page data
✓ Generating static pages (5/5)
✓ Collecting build traces
✓ Finalizing page optimization
Route (app) Size First Load JS
┌ ○ / 76.3 kB 160 kB
└ ○ /_not-found 882 B 85.1 kB
+ First Load JS shared by all 84.2 kB
├ chunks/69-28f79be715c06873.js 28.9 kB
├ chunks/fd9d1056-f6b668df0b3d2561.js 53.4 kB
└ other shared chunks (total) 1.96 kB
○ (Static) prerendered as static content
以上输出表明本地构建成功。
部署服务器错误:
Run (npm run build)
shell: /usr/bin/bash -e ***0***
> [email protected] build
> next build
sh: 1: next: not found
Error: Process completed with exit code 127.
在部署服务器上,我遇到了无法识别 next 的问题。构建失败,退出代码为 127
我尝试过的事情:
Package.json:
{
"name": "personal-portfolio",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@tabler/icons-react": "^2.47.0",
"@tsparticles/engine": "^3.2.1",
"@tsparticles/react": "^3.0.0",
"@tsparticles/slim": "^3.2.1",
"classnames": "^2.5.1",
"clsx": "^2.1.0",
"cross-env": "^7.0.3",
"framer-motion": "^11.0.5",
"next": "^14.1.0",
"react": "^18",
"react-dom": "^18",
"react-element-to-jsx-string": "^15.0.0",
"react-hot-toast": "^2.4.1",
"react-icons": "^5.0.1",
"react-player": "^2.14.1",
"sonner": "^1.4.0",
"tailwind-merge": "^2.2.1",
"tsparticles-engine": "^2.12.0"
},
"devDependencies": {
"@types/node": "20.11.18",
"@types/react": "18.2.55",
"autoprefixer": "^10.0.1",
"eslint": "^8",
"eslint-config-next": "14.1.0",
"postcss": "^8",
"tailwindcss": "^3.4.1",
"typescript": "^5.3.3"
}
}
任何有关解决此问题的见解或指导将不胜感激。谢谢!
您在部署服务器上收到“next:未找到”,因为命令 npm install 未在部署服务器上执行,并且依赖项未安装在那里。然而,构建命令在本地系统上成功,因为依赖项已经在那里可用。
要解决此问题,请替换 package.json 中的以下行
"build": "next build",
与
"build": "npm i && next build",
这将确保每当您部署新更改时,都会在部署服务器上执行 npm i(安装)命令。
我运行了
npm run build
,它编译了所有内容。然后您想要运行该项目,因此首先启动 npm start
。希望这有帮助。
步骤:
npm run build
npm start