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?

[Nuxt.js] Runtime Configの値が、nitro 起動時に設定した環境変数の値で書き換わらない

Last updated at Posted at 2025-01-15

前提

Nuxt.js (Nuxt3) には、Runtime Configという機能があり、
実行時に環境変数から読み込んだ値で、設定を上書きする事ができる。

よく他の記事などで設定している際に、下記のように設定されている場合がある。

export default defineNuxtConfig({
  runtimeConfig: {
    exampleSecret: 'example',
    public: {
      baseUrl: process.env.BASE_URL || '(デフォルト値)',
    }
  }
})

この場合、baseUrl は、ビルド時に設定されている、環境変数「BASE_URL」の内容が適用される。

そのため、nitroサーバ起動時に環境変数「BASE_URL」を指定してもその値は適用されない。

それだと、ビルドは先に済ませておき、デプロイ先で起動時の環境変数によって設定を変更したい場合に対処できない。

結論

nitroサーバを起動した際に、 baseUrl の値を上書きしたい場合は

NUXT_PUBLIC_BASE_URL="(上書きしたい値)"

のように指定する必要がある。

つまり、

「NUXT_」 + 「PUBLIC_」 + 「(RuntimeConfig変数名のスネークケース)」

で環境変数に指定してやる必要がある。

Privateの場合は「PUBLIC_」が不要になるので上記の例における、
exampleSecret の値をサーバ起動時に上書きしたい場合は

NUXT_EXAMPLE_SECRET="(上書きしたい値)"

のように指定することになる。

あとがき

公式ドキュメントや動画にも記載があるが見逃しやすかったため記事化

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?