簡體   English   中英

如何修復 Next.js Vercel 部署模塊未找到錯誤

[英]How to fix Next.js Vercel deployment module not found error

我的 next.js 應用程序可以在我的機器上運行,並且在部署在 Vercel 上時可以運行,但現在在 Vercel 上構建時它會失敗,並出現以下錯誤:

我嘗試刪除 node_modules 並運行npm install幾次,但沒有任何樂趣。

任何幫助將不勝感激。 謝謝!

運行“npm run build”20:43:24.926
tdwcks@1.0.0 構建 /vercel/5ccaedc9 20:43:24.926
下一個版本 20:43:24.967
內部/模塊/cjs/loader.js:983 20:43:24.967
拋出錯誤; 20:43:24.967
^ 20:43:24.967
錯誤:找不到模塊 '../build/output/log' 20:43:24.967
需要堆棧:20:43:24.967
- /vercel/5ccaedc9/node_modules/.bin/next 20:43:24.967
在 Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15) 20:43:24.967
在 Function.Module._load (internal/modules/cjs/loader.js:862:27) 20:43:24.967
在 Module.require (internal/modules/cjs/loader.js:1042:19) 20:43:24.967
在需要(內部/模塊/cjs/helpers.js:77:18)20:43:24.967
在 Object。 (/vercel/5ccaedc9/node_modules/.bin/next:2:46) 20:43:24.967
在 Module._compile (internal/modules/cjs/loader.js:1156:30) 20:43:24.967
在 Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10) 20:43:24.967
在 Module.load (internal/modules/cjs/loader.js:1000:32) 20:43:24.967
在 Function.Module._load (internal/modules/cjs/loader.js:899:14) 20:43:24.967
在 Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) { 20:43:24.967
代碼:'MODULE_NOT_FOUND',20:43:24.967
要求堆棧:['/vercel/5ccaedc9/node_modules/.bin/next'] 20:43:24.967
20:43:24.969
npm 錯誤:代碼 ELIFECYCLE 20:43.24.969
npm ERR: errno 1 20:43.24.970
npm 錯誤。 tdwcks@1.0:0 構建: next build 20:43:24.970
npm ERR:退出狀態1 20:43.24.970
npm 錯誤:20:43.24.970
npm 錯誤。 在 tdwcks@1.0.0 構建腳本失敗:20:43.24.970
npm 錯誤。 這可能不是 npm 的問題。 上面可能還有額外的日志記錄 output:20:43.24.974
npm ERR:此運行的完整日志可在以下位置找到:20:43.24.974
npm 錯誤。 /vercel/.npm/_logs/2020-06-17T19_43_24_971Z-debug:log 20:43.24.979
錯誤:命令“npm run build”以 1 20:43:25.342 退出
[dmesg] 如下:20:43:25.342
[962.449223] ecs-bridge: 端口 1(veth2a021300) 進入禁用 state 20:43:25.342
[962.453655] 設備 veth2a021300 進入混雜模式 20:43:25.342
[962.457686] ecs-bridge: 端口 1(veth2a021300) 進入阻塞 state 20:43:25.342
[962.462004] ecs-bridge: 端口 1(veth2a021300) 進入轉發 state 20:43:26.242
完成“package.json”

這是我的 Package.json

{
  "name": "tdwcks",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "next",
    "build": "next build",
    "start": "next start"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cjs": "0.0.11",
    "core-util-is": "^1.0.2",
    "framer-motion": "^1.11.0",
    "gray-matter": "^4.0.2",
    "next": "^9.4.4",
    "raw-loader": "^4.0.1",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-ga": "^3.0.0",
    "react-markdown": "^4.3.1",
    "react-player": "^2.2.0",
    "react-scripts": "^3.4.1"
  },
  "devDependencies": {
    "postcss-preset-env": "^6.7.0",
    "tailwindcss": "^1.4.6"
  }
}

如果您不小心將node_modules提交到項目的 Git 存儲庫,通常會發生此錯誤。

您可以嘗試執行以下操作嗎?

  1. 確保所有更改都已提交並且您有一個干凈的目錄。
  2. 運行rm -rf node_modules (或刪除 Windows 上的文件夾)。
  3. 運行git add -A然后git commit -m "Remove all module files"
  4. node_modules添加到您的.gitignore文件中(並保存)。
  5. 運行git add -A然后git commit -m "Update ignored files"
  6. 通過git status驗證您的目錄是否完全干凈。
  7. 然后,運行git push 此部署應在 Vercel 上運行。
  8. 最后,根據您的 package 管理器重新運行npm iyarn以使您的本地副本正常工作。

我必須編輯我的package.json才能使用node_modules/next目錄中的next二進制文件:

"scripts": {
  "start": "node_modules/next/dist/bin/next start -p $PORT"
}

不是最優雅的修復,但它有效。

我有這個完全相同的問題。 我認為這可能是 Vercel 部署基礎架構的內部問題。 注意它失敗的那一行:

Error: Cannot find module '../build/output/log' 20:43:24.967
Require stack: 20:43:24.967
- /vercel/5ccaedc9/node_modules/.bin/next 20:43:24.967

我的問題從昨天開始,非常出乎意料——即通過一個非常簡單的提交。 就我而言,以前成功的部署也失敗了。 同樣,刪除項目並重新開始也無濟於事。 我正在與 Vercel 支持人員進行溝通,但他們尚未承認問題已解決或提供任何類型的解決方案。

這個答案對我有用:https://stackoverflow.com/a/55541435/3051080

TL;博士; 更新git cache

git rm -r --cached .
git add --all .
git commit -a -m "Versioning untracked files"
git push origin master

我遇到過同樣的問題。 在我的 github 桌面中,我注意到在編輯器中大寫的文件不在 github 桌面中。 修復了拼寫以匹配 github 上顯示的內容,項目構建成功。

好像我遇到了同樣的錯誤。

奇怪的是,我整個周末都在 Vercel 上進行構建,沒有任何問題,並且在我將 Tailwind CSS 添加到我的項目后才開始失敗。

使用 Tailwind CSS 添加的第一個構建成功,但未加載樣式。

您仍然可以在https://rolfmadsen.now.sh/看到結果。

帶有“vercel dev”的本地構建仍然可以完美運行。

請參閱https://github.com/rolfmadsen/search上的存儲庫

構建日志中的錯誤:

22:28:35.104  
Running "npm run build"
22:28:35.287  
> search@0.1.0 build /vercel/6ddf29b8
22:28:35.287  
> next build
22:28:35.328  
internal/modules/cjs/loader.js:983
22:28:35.329  
  throw err;
22:28:35.329  
  ^
22:28:35.329  
Error: Cannot find module '../build/output/log'
22:28:35.329  
Require stack:
22:28:35.329  
- /vercel/6ddf29b8/node_modules/.bin/next
22:28:35.329  
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15)
22:28:35.329  
    at Function.Module._load (internal/modules/cjs/loader.js:862:27)
22:28:35.329  
    at Module.require (internal/modules/cjs/loader.js:1042:19)
22:28:35.329  
    at require (internal/modules/cjs/helpers.js:77:18)
22:28:35.329  
    at Object.<anonymous> (/vercel/6ddf29b8/node_modules/.bin/next:2:46)
22:28:35.329  
    at Module._compile (internal/modules/cjs/loader.js:1156:30)
22:28:35.329  
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
22:28:35.329  
    at Module.load (internal/modules/cjs/loader.js:1000:32)
22:28:35.329  
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
22:28:35.329  
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
22:28:35.329  
  code: 'MODULE_NOT_FOUND',
22:28:35.329  
  requireStack: [ '/vercel/6ddf29b8/node_modules/.bin/next' ]
22:28:35.329  
}
22:28:35.331  
npm ERR! code ELIFECYCLE
22:28:35.331  
npm ERR! errno 1
22:28:35.332  
npm ERR! search@0.1.0 build: `next build`
22:28:35.332  
npm ERR! Exit status 1
22:28:35.332  
npm ERR! 
22:28:35.332  
npm ERR! Failed at the search@0.1.0 build script.
22:28:35.332  
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
22:28:35.336  
npm ERR! A complete log of this run can be found in:
22:28:35.336  
npm ERR!     /vercel/.npm/_logs/2020-06-21T20_28_35_332Z-debug.log
22:28:35.342  
Error: Command "npm run build" exited with 1

在我的情況下,它看起來與yarn和下一個依賴關系有關,即在node_modules/next/dist/bin/next內部有關於某事的沖突信息。

永遠不明白為什么在使用next並將我們的代碼構建到生產環境之后,我們仍然必須依賴(相對)沉重的模塊next

構建構建的整個概念被認為是獨立於構建工具的。

我嘗試了上述所有問題,但沒有任何效果。

當我更改 next.js 的版本時,問題得到了解決。 萬一有人在尋找解決方案但沒有任何效果......

如何創建一個.gitignore文件,並將.next文件夾添加到其中?

我如何解決 Vercel 上缺少的模塊錯誤。

  1. 明確安裝 package,使其出現在package.json
  2. 然后將假定的缺失模塊導入應用程序並使用它。

例如(只是一個場景) //讓我們假設lodash被認為是缺少的模塊,

1 確保它存在於您的 package.json

"dependencies": {
    // some dependencies ...
    "lodash": "^4.17.20",
    // some other dependencies ...
},

2 導入並在您的應用程序中使用它(通常,我只是 console.log 在非生產環境中導入。)

import LODASH from 'lodash'

if (process.env.NODE_ENV !== 'production') console.log(LODASH)

我以小寫字母創建文件夾,然后將其重命名為大寫,更新了所有導入,但是,由於某種原因,Github 在我推送更改時沒有更新文件夾名稱。 我需要用不同的名稱重命名。 有效。

對我來說,當我在我的 package.json 和 node_modules 下找不到它時,特定的 package 是一個問題。 即使它以某種方式在本地構建中工作。

我在 vercel 中添加了一個 NODE_ENV="production" 環境變量,它為我解決了所有問題。 一旦我刪除它,事情就恢復了。

如果程序通過執行node_modules/next/dist/bin/next可以正常運行,你應該懷疑文件的符號鏈接壞了。
就我而言,它發生在 AWS 部署期間,並且發生在壓縮文件以進行部署的過程中。
因此,我能夠通過在壓縮期間添加符號鏈接選項來解決問題,如下所示。

zip -r --symlinks xxxx

如果像我一樣部署在AWS等服務器上,下載實際分發的程序,檢查node_modules/.bin/next文件。 如果符號鏈接斷開,您需要在部署過程中查找並修復斷開鏈接的原因。

cf) https://github.com/vercel/next.js/discussions/14897#

  1. 刪除 package-lock.json (rm package-lock.json)
  2. 刪除 node_modules (rm -R node_modules)
  3. 切換 Node 版本,如果您通過 NVM 安裝了 Node,這很容易(nvm install 17,nvm use 17)
  4. 使用新版本的節點(npm install)再次安裝依賴項

我在運行節點 16.15.0 LTS 的服務器上、在我的本地計算機節點 v16.12.0 和另一台運行節點 v12.22.10 的服務器上遇到了這個問題,但它沒有給出錯誤。

查看了我的依賴項並決定切換到 Node 17。

devDependencies": {
    "@types/node": "17.0.23",
    "@types/react": "17.0.43",

按照上述步驟並使用 Node 17 代碼成功運行后,不再出現錯誤。

可能無法解決 OP 的情況,因為我在嘗試導入本地模塊/文件時遇到此錯誤。

在那種情況下,我沒有意識到我正在導入的文件是用大寫字母編寫的,並且僅更改文件名的大小寫並沒有添加到我的 commit中。

要修復,我必須運行git mv -f Oldname newname然后提交該更改。 出現此問題的原因是不區分大小寫的文件系統允許本地構建通過(因為路徑已解析),但 Vercel 構建步驟(區分大小寫)未通過。

如果如@Joe Haddad 的回答中所述,node_modules 通過 git 傳輸到服務器。 處理這個問題的不同策略對我來說效果更好。

在服務器上,刪除所有節點和下一個構建文件轉移到服務器,重新安裝並重新構建

rm -R node_modules && rm -R .next && rm package-lock.json && npm install && npm run build

編輯或創建.gitignore 並添加 node_modules 和.next 然后執行以下操作。

清除 git 存儲庫中的緩存文件,重新添加文件尊重.gitignore,提交並將更改推送回本地計算機。

git rm -r --cached ./ && git add ./ && git commit -a -m "Removing ignored files" && git push

在本地計算機上與新忽略的文件同步。 不影響本地工作環境,現在可以推拉成功。

git pull
npm install

這種方法更快,並且省去了在本地機器上處理 git 內容並將其推回服務器的麻煩。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM