Skip to content

Commit

Permalink
fix process.env not being available in standalone mode (#54203)
Browse files Browse the repository at this point in the history
### What?
When running Next in standalone mode, `process.env` is not made
available to the render workers, making it impossible to access
environment variables that aren't provided in `.env` files.

### Why?
`initialEnv` is undefined in `createWorkers` when the server is started
in standalone mode.

### How?
This initializes the workers with `process.env` in case `initialEnv` is
unavailable, similar to the behavior of `loadEnvConfig()`

Closes NEXT-1508
Fixes #53367
  • Loading branch information
ztanner committed Aug 18, 2023
1 parent 819a163 commit 803bbe5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
8 changes: 6 additions & 2 deletions packages/next/src/server/lib/router-server.ts
Expand Up @@ -198,21 +198,25 @@ export async function initialize(opts: {
},
} as any)

const { initialEnv } = require('@next/env') as typeof import('@next/env')

if (!!config.experimental.appDir) {
renderWorkers.app = await createWorker(
ipcPort,
ipcValidationKey,
opts.isNodeDebugging,
'app',
config
config,
initialEnv
)
}
renderWorkers.pages = await createWorker(
ipcPort,
ipcValidationKey,
opts.isNodeDebugging,
'pages',
config
config,
initialEnv
)

// pre-initialize workers
Expand Down
5 changes: 3 additions & 2 deletions packages/next/src/server/lib/server-ipc/index.ts
Expand Up @@ -8,6 +8,7 @@ import isError from '../../../lib/is-error'
import { genRenderExecArgv } from '../worker-utils'
import { deserializeErr } from './request-utils'
import { RenderWorker } from '../router-server'
import type { Env } from '@next/env'

// we can't use process.send as jest-worker relies on
// it already and can cause unexpected message errors
Expand Down Expand Up @@ -87,9 +88,9 @@ export const createWorker = async (
ipcValidationKey: string,
isNodeDebugging: boolean | 'brk' | undefined,
type: 'pages' | 'app',
nextConfig: NextConfigComplete
nextConfig: NextConfigComplete,
initialEnv: NodeJS.ProcessEnv | Env = process.env
): Promise<RenderWorker> => {
const { initialEnv } = require('@next/env') as typeof import('@next/env')
const useServerActions = !!nextConfig.experimental.serverActions
const { Worker } =
require('next/dist/compiled/jest-worker') as typeof import('next/dist/compiled/jest-worker')
Expand Down
Expand Up @@ -3,5 +3,6 @@ export default function handler(_, res) {
env: process.env.FOO,
envLocal: process.env.LOCAL_SECRET,
envProd: process.env.PROD_SECRET,
envFromHost: process.env.ENV_FROM_HOST,
})
}
Expand Up @@ -122,6 +122,7 @@ describe('should set-up next', () => {
/ready started server on/,
{
...process.env,
ENV_FROM_HOST: 'FOOBAR',
PORT: appPort,
},
undefined,
Expand Down Expand Up @@ -1266,14 +1267,15 @@ describe('should set-up next', () => {
}
})

it('should copy and read .env file', async () => {
it('should read .env files and process.env', async () => {
const res = await fetchViaHTTP(appPort, '/api/env')

const envVariables = await res.json()

expect(envVariables.env).not.toBeUndefined()
expect(envVariables.envProd).not.toBeUndefined()
expect(envVariables.envLocal).toBeUndefined()
expect(envVariables.envFromHost).toBe('FOOBAR')
})

it('should run middleware correctly (without minimalMode, with wasm)', async () => {
Expand Down

0 comments on commit 803bbe5

Please sign in to comment.