diff --git a/packages/next/build/polyfills/process.js b/packages/next/build/polyfills/process.js
index 054c87b91088681..26a02f5b34f7f84 100644
--- a/packages/next/build/polyfills/process.js
+++ b/packages/next/build/polyfills/process.js
@@ -1 +1,4 @@
-module.exports = global.process || require('../../compiled/process')
+module.exports =
+ global.process?.env && typeof global.process?.env === 'object'
+ ? global.process
+ : require('../../compiled/process')
diff --git a/test/integration/polyfills/pages/process.js b/test/integration/polyfills/pages/process.js
new file mode 100644
index 000000000000000..c293e0f5df0dabd
--- /dev/null
+++ b/test/integration/polyfills/pages/process.js
@@ -0,0 +1,7 @@
+export default function Page() {
+ return (
+
+ Hello, {process.env.NEXT_PUBLIC_VISITOR ?? 'stranger'}
+
+ )
+}
diff --git a/test/integration/polyfills/test/index.test.js b/test/integration/polyfills/test/index.test.js
index b37d3417a004a9b..48734881d92b82e 100644
--- a/test/integration/polyfills/test/index.test.js
+++ b/test/integration/polyfills/test/index.test.js
@@ -36,9 +36,18 @@ describe('Polyfills', () => {
await browser.close()
})
+ it('should allow using process.env when there is an element with `id` of `process`', async () => {
+ const browser = await webdriver(appPort, '/process')
+ const text = await browser.elementByCss('#process').text()
+
+ expect(text).toBe('Hello, stranger')
+
+ await browser.close()
+ })
+
it('should contain generated page count in output', async () => {
- expect(output).toContain('Generating static pages (0/4)')
- expect(output).toContain('Generating static pages (4/4)')
+ expect(output).toContain('Generating static pages (0/5)')
+ expect(output).toContain('Generating static pages (5/5)')
// we should only have 1 segment and the initial message logged out
expect(output.match(/Generating static pages/g).length).toBe(5)
})