`npm run build` 在 Next.js 14 中不起作用

问题描述 投票:0回答:2

Next.js 构建问题:部署服务器上“next:未找到”

描述:

我的 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

我尝试过的事情:

  1. 删除了node_modules,清理了npm缓存并重新安装-> npm i
  2. 删除下一个并重新安装 -> npm i next
  3. 添加了 cross-env -> npm i cross-env
  4. 添加了依赖项 -> "dev": "cross-env NODE_OPTIONS='--inspect' next dev"

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"
  }
}

任何有关解决此问题的见解或指导将不胜感激。谢谢!

reactjs next.js next.js14
2个回答
2
投票

您在部署服务器上收到“next:未找到”,因为命令 npm install 未在部署服务器上执行,并且依赖项未安装在那里。然而,构建命令在本地系统上成功,因为依赖项已经在那里可用。

要解决此问题,请替换 package.json 中的以下行

"build": "next build",

"build": "npm i && next build",

这将确保每当您部署新更改时,都会在部署服务器上执行 npm i(安装)命令。


0
投票

我运行了

npm run build
,它编译了所有内容。然后您想要运行该项目,因此首先启动
npm start
。希望这有帮助。

步骤:

  1. npm run build
  2. npm start
© www.soinside.com 2019 - 2024. All rights reserved.