Search code examples
javascriptnode.jsnext.jsdotenv

Read environment variables outside of next js


I am using next js as the basis for my application. My environment variables work as long as they are read within the scope of next js but I need to use them inside a script that does not use next js.

With dotenv we can do the following:

require('dotenv').config();

Which parses the environment config files and allows you to get the variables from process.env.

Because I am using next js I would prefer to use their environment variable implementation instead of adding a second way to store environment variables.

Is there an equivalent of require('dotenv').config(); for next js or another way to load the environment variables outside the scope of next js?


Solution

  • Ok I went into the git repo of next js and found a way to do it.

    You have to install @next/env and then run require('@next/env').loadEnvConfig('./');