Unverified Commit 6972a09f authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add message when automatic dotenv loading is disabled (#12744)

parent e91c0fcc
# Env Loading Disabled
#### Why This Error Occurred
In your project you have `dotenv` listed as a `dependency` or a `devDependency` which opts-out of Next.js' automatic loading to prevent creating a conflict with any existing `dotenv` behavior in your project.
This is also disabled if a `package.json` isn't able to found in your project somehow.
#### Possible Ways to Fix It
Remove `dotenv` from your `devDependencies` or `dependencies` and allow Next.js to load your `dotenv` files for you
### Useful Links
- [dotenv](https://npmjs.com/package/dotenv)
- [dotenv-expand](https://npmjs.com/package/dotenv-expand)
- [Environment Variables](https://en.wikipedia.org/wiki/Environment_variable)
- [Next.js Environment Variables Docs](https://nextjs.org/docs/basic-features/environment-variables)
......@@ -2,6 +2,7 @@ import fs from 'fs'
import path from 'path'
import * as log from '../build/output/log'
import findUp from 'next/dist/compiled/find-up'
import { execOnce } from '../next-server/lib/utils'
import dotenvExpand from 'next/dist/compiled/dotenv-expand'
import dotenv, { DotenvConfigOutput } from 'next/dist/compiled/dotenv'
......@@ -19,6 +20,15 @@ const packageJsonHasDep = (packageJsonPath: string, dep: string): boolean => {
let combinedEnv: Env | undefined = undefined
const envLoadingDisabledWarning = execOnce((packageFile?: string) => {
log.warn(
(packageFile
? `dotenv loading was disabled due to the \`dotenv\` package being installed in: ${packageFile}`
: `dotenv loading was disabled due to no package.json file able to be found`) +
`\nSee more info here: https://err.sh/next.js/env-loading-disabled`
)
})
export function loadEnvConfig(dir: string, dev?: boolean): Env | false {
if (combinedEnv) return combinedEnv
......@@ -30,6 +40,7 @@ export function loadEnvConfig(dir: string, dev?: boolean): Env | false {
if (packageJson) {
// check main `package.json` first
if (packageJsonHasDep(packageJson, 'dotenv')) {
envLoadingDisabledWarning(path.relative(dir, packageJson))
return false
}
// check for a yarn.lock or lerna.json file in case it's a monorepo
......@@ -44,12 +55,14 @@ export function loadEnvConfig(dir: string, dev?: boolean): Env | false {
try {
if (packageJsonHasDep(monorepoPackageJson, 'dotenv')) {
envLoadingDisabledWarning(path.relative(dir, monorepoPackageJson))
return false
}
} catch (_) {}
}
} else {
// we should always have a package.json but disable in case we don't
envLoadingDisabledWarning()
return false
}
......
......@@ -25,11 +25,15 @@ const runTests = () => {
describe('dev mode', () => {
it('should start dev server without errors', async () => {
let stderr = ''
let stdout = ''
appPort = await findPort()
app = await launchApp(appDir, appPort, {
onStderr(msg) {
stderr += msg || ''
},
onStdout(msg) {
stdout += msg || ''
},
})
const html = await renderViaHTTP(appPort, '/')
......@@ -37,23 +41,36 @@ const runTests = () => {
expect(html).toContain('hi')
expect(stderr).not.toContain('Failed to load env')
expect(stdout).toContain(
'dotenv loading was disabled due to the `dotenv` package being installed in'
)
})
})
describe('production mode', () => {
it('should build app successfully', async () => {
const { stderr, code } = await nextBuild(appDir, [], { stderr: true })
const { stderr, stdout, code } = await nextBuild(appDir, [], {
stderr: true,
stdout: true,
})
expect(code).toBe(0)
expect((stderr || '').length).toBe(0)
expect(stdout).toContain(
'dotenv loading was disabled due to the `dotenv` package being installed in'
)
})
it('should start without error', async () => {
let stderr = ''
let stdout = ''
appPort = await findPort()
app = await nextStart(appDir, appPort, {
onStderr(msg) {
stderr += msg || ''
},
onStdout(msg) {
stdout += msg || ''
},
})
const html = await renderViaHTTP(appPort, '/')
......@@ -61,6 +78,9 @@ const runTests = () => {
expect(html).toContain('hi')
expect(stderr).not.toContain('Failed to load env')
expect(stdout).toContain(
'dotenv loading was disabled due to the `dotenv` package being installed in'
)
})
})
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment