Appearance
Environment Variables
Since the release of Next.js 9.4 we now have a more intuitive and ergonomic experience for adding environment variables. Give it a try!
Examples
To add environment variables to the JavaScript bundle, open next.config.js
and add the env
config:
javascript
module.exports = {
env: {
customKey: 'my-value',
},
}
Now you can access p
in your code. For example:
jsx
function Page() {
return <h1>The value of customKey is: {process.env.customKey}</h1>
}
export default Page
Next.js will replace p
with 'my-value'
at build time. Trying to destructure p
variables won't work due to the nature of webpack DefinePlugin.
For example, the following line:
jsx
return <h1>The value of customKey is: {process.env.customKey}</h1>
Will end up being:
jsx
return <h1>The value of customKey is: {'my-value'}</h1>