The key "NEXT_RUNTIME" under "env" in next.config.js is not allowed.

Have you ever received this error when upgrading next.js


The key "NEXT_RUNTIME" under "env" in next.config.js is not allowed.

The first place to check is the "env" section in next.config.js

In some next.js projects you will notice env is configured as

env: {
    ...getAllAllowedEnvironmentVariables(),
  }

and this method is defined as

const getAllAllowedEnvironmentVariables = () => {
  return Object.entries(process.env).reduce((vars, [name, value]) => (/^(?:__|NODE_)/.test(name) ? vars : { ...vars, [name]: value }), {})
}

This method is adding all environment variables to the next.js build. This is not a good idea. You should only add the environment variables that you need.

However the easy fix is to remove the "NEXT_RUNTIME" var from process.env

const getAllAllowedEnvironmentVariables = () => {
  let out = Object.entries(process.env).reduce((vars, [name, value]) => (/^(?:__|NODE_)/.test(name) ? vars : { ...vars, [name]: value }), {})
  delete out['NEXT_RUNTIME']
  return out
}

Now if you run next build you should not see the error.

Please note that this is not a good solution. You should only add the environment variables that you need.