6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

サブディレクトリ動かすNuxt.jsでもルートディレクトリの.envを読み込む

Last updated at Posted at 2019-11-22

背景

Nuxt.jsをサブディレクトリで動かして、ルートディレクトリの.envを読もうとすると

WARN  No .env file found 

んなものないって警告でる。

対策

dotenvのpathを設定する。

nuxt.config.js
require("dotenv").config();

export default {
  srcDir: "hogehoge",

  // ~~略~~
  buildModules: ["@nuxtjs/dotenv"],
  dotenv: {
    path: process.cwd()
  },

Herokuとかでも環境変数を読みたい場合

envを設定する。

nuxt.config.js
require("dotenv").config();

export default {
  srcDir: "hogehoge",

  // ~~略~~
  env: {
    APP_ID: process.env.APP_ID,
    APP_KEY: process.env.APP_KEY,
  },

Herokuで環境変数を設定するのをお忘れなく。

参考

nuxt-community/dotenv-module
API: env プロパティ

6
4
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
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?