-
Notifications
You must be signed in to change notification settings - Fork 26.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial support for new env handling (#10525)
* Add initial support for new env config file * Fix serverless processEnv call when no env is provided * Add missing await for test method * Update env config to .env.json and add dotenv loading * ncc dotenv package * Update type * Update with new discussed behavior removing .env.json * Update hot-reloader createEntrypoints * Make sure .env is loaded before next.config.js * Add tests for all separate .env files * Remove comments * Add override tests * Add test for overriding env vars based on local environment * Add support for .env.test * Apply suggestions from code review Co-Authored-By: Joe Haddad <joe.haddad@zeit.co> * Use chalk for env loaded message * Remove constant as it’s not needed * Update test * Update errsh, taskr, and CNA template ignores * Make sure to only consider undefined missing * Remove old .env ignore * Update to not populate process.env with loaded env * Add experimental flag and add loading of global env values Co-authored-by: Tim Neutkens <timneutkens@me.com> Co-authored-by: Joe Haddad <joe.haddad@zeit.co>
- Loading branch information
1 parent
a391d32
commit d8155b2
Showing
42 changed files
with
1,103 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Missing Env Value | ||
|
||
#### Why This Error Occurred | ||
|
||
One of your pages' config requested an env value that wasn't populated. | ||
|
||
```js | ||
// pages/index.js | ||
export const config = { | ||
// this value isn't provided in `.env` | ||
env: ['MISSING_KEY'], | ||
} | ||
``` | ||
|
||
``` | ||
// .env (notice no `MISSING_KEY` provided here) | ||
NOTION_KEY='...' | ||
``` | ||
|
||
#### Possible Ways to Fix It | ||
|
||
Either remove the requested env value from the page's config, populate it in your `.env` file, or manually populate it in your environment before running `next dev` or `next build`. | ||
|
||
### 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import chalk from 'chalk' | ||
import dotenvExpand from 'next/dist/compiled/dotenv-expand' | ||
import dotenv, { DotenvConfigOutput } from 'next/dist/compiled/dotenv' | ||
import findUp from 'find-up' | ||
|
||
export type Env = { [key: string]: string } | ||
|
||
export function loadEnvConfig(dir: string, dev?: boolean): Env | false { | ||
const packageJson = findUp.sync('package.json', { cwd: dir }) | ||
|
||
// only do new env loading if dotenv isn't installed since we | ||
// can't check for an experimental flag in next.config.js | ||
// since we want to load the env before loading next.config.js | ||
if (packageJson) { | ||
const { dependencies, devDependencies } = require(packageJson) | ||
const allPackages = Object.keys({ | ||
...dependencies, | ||
...devDependencies, | ||
}) | ||
|
||
if (allPackages.some(pkg => pkg === 'dotenv')) { | ||
return false | ||
} | ||
} else { | ||
// we should always have a package.json but disable in case we don't | ||
return false | ||
} | ||
|
||
const isTest = process.env.NODE_ENV === 'test' | ||
const mode = isTest ? 'test' : dev ? 'development' : 'production' | ||
const dotenvFiles = [ | ||
`.env.${mode}.local`, | ||
`.env.${mode}`, | ||
// Don't include `.env.local` for `test` environment | ||
// since normally you expect tests to produce the same | ||
// results for everyone | ||
mode !== 'test' && `.env.local`, | ||
'.env', | ||
].filter(Boolean) as string[] | ||
|
||
const combinedEnv: Env = { | ||
...(process.env as any), | ||
} | ||
|
||
for (const envFile of dotenvFiles) { | ||
// only load .env if the user provided has an env config file | ||
const dotEnvPath = path.join(dir, envFile) | ||
|
||
try { | ||
const contents = fs.readFileSync(dotEnvPath, 'utf8') | ||
let result: DotenvConfigOutput = {} | ||
result.parsed = dotenv.parse(contents) | ||
|
||
result = dotenvExpand(result) | ||
|
||
if (result.parsed) { | ||
console.log(`> ${chalk.cyan.bold('Info:')} Loaded env from ${envFile}`) | ||
} | ||
|
||
Object.assign(combinedEnv, result.parsed) | ||
} catch (err) { | ||
if (err.code !== 'ENOENT') { | ||
console.log( | ||
`> ${chalk.cyan.bold('Error: ')} Failed to load env from ${envFile}`, | ||
err | ||
) | ||
} | ||
} | ||
} | ||
|
||
// load global env values prefixed with `NEXT_APP_` to process.env | ||
for (const key of Object.keys(combinedEnv)) { | ||
if ( | ||
key.startsWith('NEXT_APP_') && | ||
typeof process.env[key] === 'undefined' | ||
) { | ||
process.env[key] = combinedEnv[key] | ||
} | ||
} | ||
|
||
return combinedEnv | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.