简体   繁体   中英

Can I use @next/env package in next.config.js?

I am wanting to access the env config from next.config.js in order to surface some env vars set in .env.local and set some server runtime config based on them.

Is it reasonable in next.config.js to do...

const { loadEnvConfig } = require("@next/env");
const env = loadEnvConfig(".").combinedEnv;

It works, but I can't find any docs that explain how to do this, or if it's a supported package/api.

As documented here: https://nextjs.org/docs/basic-features/environment-variables#test-environment-variables

When you run loadEnvConfig it actually updates process.env to include config that next js uses, so this should work...

const { loadEnvConfig } = require("@next/env");
loadEnvConfig(".")
// process.env.SOMETHING <~~ includes value of SOMETHING set in .env.local

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM