nextjs config environment

nextjs

In a Next.js app, you can use the next.config.js file to configure environment variables. Environment variables are variables that are specific to the environment in which the app is running (e.g., development, staging, production). They are useful for storing sensitive information, such as API keys, that you don’t want to hardcode into your app.

To configure environment variables in Next.js, you can use the dotenv package and the next-env package. Here’s an example of how you can set up these packages in your next.config.js file:

const withNextEnv = require('next-env')
const dotenvLoad = require('dotenv-load')

dotenvLoad()

module.exports = withNextEnv({
    env: {
        API_KEY: process.env.API_KEY,
    },
})

In this example, we are using the dotenvLoad function to load the environment variables from a .env file, and then we are using the withNextEnv function to configure the environment variables in Next.js. The env object in the withNextEnv function specifies the environment variables that we want to expose to our app.