rsnext/docs/api-reference/next.config.js/environment-variables.md
Luis Alvarez D d1fdd2bbf8 Add descriptions to documentation pages (#9901)
* Added descriptions

* Added descriptions to API Reference

* Added descriptions to API Routes

* Added descriptions to basic features

* Added descriptions to the routing docs

* Update exportPathMap.md

Co-authored-by: Joe Haddad <timer150@gmail.com>
2020-01-03 13:16:51 -05:00

1.2 KiB

description
Learn to add and access environment variables in your Next.js application at build time.

Environment Variables

Examples

To add environment variables to the JavaScript bundle, open next.config.js and add the env config:

module.exports = {
  env: {
    customKey: 'my-value',
  },
}

Now you can access process.env.customKey in your code. For example:

function Page() {
  return <h1>The value of customKey is: {process.env.customKey}</h1>
}

export default Page

Next.js will replace process.env.customKey with 'my-value' at build time. Trying to destructure process.env variables won't work due to the nature of webpack DefinePlugin.

For example, the following line:

return <h1>The value of customKey is: {process.env.customKey}</h1>

Will end up being:

return <h1>The value of customKey is: {'my-value'}</h1>