Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
fix(nuxi): load .env file before starting dev server (#6119)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jul 25, 2022
1 parent cccafd6 commit 40defd9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions packages/nuxi/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand All @@ -46,8 +50,6 @@ export default defineNuxtCommand({
}
})

const rootDir = resolve(args._[0] || '.')

const { loadNuxt, buildNuxt } = await loadKit(rootDir)

let currentNuxt: Nuxt
Expand Down
4 changes: 2 additions & 2 deletions packages/nuxi/src/commands/preview.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 40defd9

Please sign in to comment.