From 40defd9f36b98fcee1a3020b48545ab11d79f482 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 25 Jul 2022 13:01:18 +0100 Subject: [PATCH] fix(nuxi): load `.env` file before starting dev server (#6119) --- packages/nuxi/src/commands/dev.ts | 6 ++++-- packages/nuxi/src/commands/preview.ts | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index 486aaab90e..57abb5793f 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -4,6 +4,7 @@ import { debounce } from 'perfect-debounce' import type { Nuxt } from '@nuxt/schema' import consola from 'consola' import { withTrailingSlash } from 'ufo' +import { setupDotenv } from 'c12' import { showBanner } from '../utils/banner' import { writeTypes } from '../utils/prepare' import { loadKit } from '../utils/kit' @@ -33,6 +34,9 @@ export default defineNuxtCommand({ return currentHandler ? currentHandler(req, res) : loadingHandler(req, res) } + const rootDir = resolve(args._[0] || '.') + await setupDotenv({ cwd: rootDir }) + const listener = await listen(serverHandler, { showURL: false, clipboard: args.clipboard, @@ -46,8 +50,6 @@ export default defineNuxtCommand({ } }) - const rootDir = resolve(args._[0] || '.') - const { loadNuxt, buildNuxt } = await loadKit(rootDir) let currentNuxt: Nuxt diff --git a/packages/nuxi/src/commands/preview.ts b/packages/nuxi/src/commands/preview.ts index c2628a6b51..0fb07832a0 100644 --- a/packages/nuxi/src/commands/preview.ts +++ b/packages/nuxi/src/commands/preview.ts @@ -1,7 +1,7 @@ import { existsSync, promises as fsp } from 'node:fs' import { dirname, relative } from 'node:path' import { execa } from 'execa' -import { loadDotenv } from 'c12' +import { setupDotenv } from 'c12' import { resolve } from 'pathe' import consola from 'consola' @@ -37,7 +37,7 @@ export default defineNuxtCommand({ if (existsSync(resolve(rootDir, '.env'))) { consola.info('Loading `.env`. This will not be loaded when running the server in production.') - process.env = await loadDotenv({ cwd: rootDir, fileName: '.env', env: process.env }) + await setupDotenv({ cwd: rootDir }) } consola.info('Starting preview command:', nitroJSON.commands.preview)