render deployでの uncaughtException Error. 解決方法 next.js ts

render で最新のrepositryをデプロイ

エラー内容
```
...

Aug 12 03:43:32 AM  ==> Using Node version 14.17.0 (default)
Aug 12 03:43:32 AM  ==> Docs on specifying a Node version: https://render.com/docs/node-version
Aug 12 03:43:32 AM  ==> Running build command 'npm install; npm run build'...
Aug 12 03:43:57 AM  added 241 packages from 324 contributors and audited 250 packages in 24.911s
Aug 12 03:43:57 AM  
Aug 12 03:43:57 AM  46 packages are looking for funding
Aug 12 03:43:57 AM    run `npm fund` for details
Aug 12 03:43:57 AM  
Aug 12 03:43:57 AM  found 0 vulnerabilities
Aug 12 03:43:57 AM  
Aug 12 03:43:58 AM  
Aug 12 03:43:58 AM  > quest-board@0.1.0 build /opt/render/project/src
Aug 12 03:43:58 AM  > next build
Aug 12 03:43:58 AM  
Aug 12 03:43:58 AM  uncaughtException /opt/render/project/src/node_modules/next/dist/compiled/undici/index.js:1
Aug 12 03:43:58 AM  (()=>{var __webpack_modules__={1900:(A,e,t)=>{"use strict";const{parseContentType:s}=t(4318);function getInstance(A){const e=A.headers;const t=s(e["content-type"]);if(!t)throw new Error("Malformed content type");for(const s of o){const o=s.detect(t);if(!o)continue;const r={limits:A.limits,h

...

DA){if(!e[U]||e.writableNeedDrain||o[F]>1){s(!A.paused,"cannot be paused while waiting for headers");r.destroy(e,new B)}}else if(t===kA){if(!A.paused){r.destroy(e,new l)}}else if(t===wA){s(o[F]===0&&o[P]);r.destroy(e,new h("socket idle timeout"))}}function onSocketReadable(){const{[N]:A}=this;A.readMore()}function onSocketError(A){const{[N]:e}=this;s(A.code!=="ERR_TLS_CERT_ALTNAME_INVALID");if(A.code==="ECONNRESET"&&e.statusCode&&!e.shouldKeepAlive){e.onMessageComplete();return}this[x]=A;onError(this[S],A)}function onError(A,e){if(A[F]===0&&e.code!=="UND_ERR_INFO"&&e.code!=="UND_ERR_SOCKET"){s(A[K]===A[v]);const t=A[Y].splice(A[v]);for(let s=0;s<t.length;s++){const o=t[s];errorRequest(A,o,e)}s(A[M]===0)}}function onSocketEnd(){const{[N]:A}=this;if(A.statusCode&&!A.shouldKeepAlive){A.onMessageComplete();return}r.destroy(this,new c("other side closed",r.getSocketInfo(this)))}function onSocketClose(){const{[S]:A}=this;if(!this[x]&&this[N].statusCode&&!this[N].shouldKeepAlive){this[N].onMessageComplete()}this[N].destroy();this[N]=null;const e=this[x]||new c("closed",r.getSocketInfo(this));A[O]=null;if(A.destroyed){s(A[L]===0);const t=A[Y].splice(A[v]);for(let s=0;s<t.length;s++){const o=t[s];errorRequest(A,o,e)}}else if(A[F]>0&&e.code!=="UND_ERR_INFO"){const t=A[Y][A[v]];A[Y][A[v]++]=null;errorRequest(A,t,e)}A[K]=A[v];s(A[F]===0);A.emit("disconnect",A[D],[A],e);resume(A)}async function connect(A){s(!A[q]);s(!A[O]);let{host:e,hostname:t,protocol:i,port:n}=A[D];if(t[0]==="["){const A=t.indexOf("]");s(A!==-1);const e=t.substr(1,A-1);s(o.isIP(e));t=e}A[q]=true;if(IA.beforeConnect.hasSubscribers){IA.beforeConnect.publish({connectParams:{host:e,hostname:t,protocol:i,port:n,servername:A[w],localAddress:A[gA]},connector:A[AA]})}try{const o=await new Promise(((s,o)=>{A[AA]({host:e,hostname:t,protocol:i,port:n,servername:A[w],localAddress:A[gA]},((A,e)=>{if(A){o(A)}else{s(e)}}))}));if(A.destroyed){r.destroy(o.on("error",(()=>{})),new f);return}if(!hA){hA=await lA;lA=null}A[q]=false;s(o);o[G]=false;o[U]=false;o[k]=false;o[b]=false;o[x]=null;o[N]=new Parser(A,o,hA);o[S]=A;o[sA]=0;o[tA]=A[tA];o.on("error",onSocketError).on("readable",onSocketReadable).on("end",onSocketEnd).on("close",onSocketClose);A[O]=o;if(IA.connected.hasSubscribers){IA.connected.publish({connectParams:{host:e,hostname:t,protocol:i,port:n,servername:A[w],localAddress:A[gA]},connector:A[AA],socket:o})}A.emit("connect",A[D],[A])}catch(o){if(A.destroyed){return}A[q]=false;if(IA.connectError.hasSubscribers){IA.connectError.publish({connectParams:{host:e,hostname:t,protocol:i,port:n,servername:A[w],localAddress:A[gA]},connector:A[AA],error:o})}if(o.code==="ERR_TLS_CERT_ALTNAME_INVALID"){s(A[F]===0);while(A[L]>0&&A[Y][A[K]].servername===A[w]){const e=A[Y][A[K]++];errorRequest(A,e,o)}}else{onError(A,o)}A.emit("connectionError",A[D],[A],o)}resume(A)}function emitDrain(A){A[T]=0;A.emit("drain",A[D],[A])}function resume(A,e){if(A[m]===2){return}A[m]=2;_resume(A,e);A[m]=0;if(A[v]>256)npm ERR! code ELIFECYCLE
Aug 12 03:43:58 AM  npm ERR! errno 1
Aug 12 03:43:58 AM  npm ERR! quest-board@0.1.0 build: `next build`
Aug 12 03:43:58 AM  npm ERR! Exit status 1
Aug 12 03:43:58 AM  npm ERR!
Aug 12 03:43:58 AM  npm ERR! Failed at the quest-board@0.1.0 build script.
Aug 12 03:43:58 AM  npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Aug 12 03:43:58 AM  
Aug 12 03:43:58 AM  npm ERR! A complete log of this run can be found in:
Aug 12 03:43:58 AM  npm ERR!     /opt/render/.cache/_logs/2023-08-11T18_43_58_441Z-debug.log
Aug 12 03:43:58 AM  ==> Build failed 😞
```

エラーメッセージからは、next buildコマンドの実行中にuncaughtExceptionが発生していることがわかります。

## 解決方法
### 1. node versionの指定

- プロジェクトのルートディレクトリに.node-versionファイルを作成 (next.config.jsと同じディレクトリ)

このファイルには、使用したいNode.jsのバージョン番号のみを記述します。例えば、バージョン18.12.0を使用したい場合、ファイルの内容は次のようになります：

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3328398/a0100bdc-3cd0-0a16-9806-4c7a8c757418.png)
.node-versionファイルは next.config.jsと同じディレクトリ

ローカル開発環境で使用しているNode.jsのバージョンを確認するには、コマンドラインで以下のコマンドを実行します

```terminal
node -v
```

### 2. もう一度デプロイ
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3328398/3fb3839e-e7a0-ec0b-d275-03cc4f2852e1.png)

この方法で解決しました。




### 参考サイト

https://paulintrognon.fr/blog/typescript-prettier-eslint-next-js