0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Next.jsのserver actionsでlocalhostのIPv6名前解決ができなかった

Posted at

遭遇した問題

Next.jsのServerActionsは、SSRにおいてサーバー側で関数を実行することのできる機能です。

ServerActions経由でサーバー側の関数を叩こうとしたところ500エラーが返ってきてしまい、APIを叩くことができなくなってしまいました。

今まで動いていたのにも関わらす、急に動かなくなってしまったのでどういう原因か本当にわからずとても困りました。

一つ分かったのは、サーバー側のenvファイルで記述していたhttp://localhost:3000http://127.0.0.1:3000に変更したところ動くようになりました。

localhostはエイリアスとして、Ipv4とIpv6が登録されています。

規格 アドレス
Ipv4 127.0.0.1
Ipv6 ::0

どうやらServerActions以外でAPIを叩けているときは、Ipv6で叩かれており、ServerActionsではIpv6を使ってネットワーク解決ができていないようでした。

対処方法

GithubのIssueに上に関連する問題が見つかりました。

Node17/18では、どうやらサーバー側でAPIをホスティングすると、Ipv4でリスニングされてしまい、Ipv6ではリスニングされていないという状況が起こってしまっているらしいです。

この問題は、Nodeを17/18よりも上のversionにアップデートすることで解決することができます。

Nodeのversionを固定する

共同開発をしているプロジェクトで上の問題に遭遇しており、大分認知がむずかしいと感じました。
なので、Nodeのversionを固定したいと考え、そのやり方を調べてみました。

まず、package.jsonでランタイムのversionを指定します。

package.json
  "engines": {
    "node": "22.x.x"
  }

次に.npmrcで以下を設定します。

.npmrc
engine-strict=true

こうすることで起動しようとするとエラーが出るようになります。

Expected version: 22.x.x
Got: v18.20.4

This is happening because the package's manifest has an engines.node field specified.
To fix this issue, install the required Node version.
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?