---
title: "How to “Escape” Next.js to Access the Browser"
description: "Sometimes you need what’s called an “escape hatch” to access certain browser properties and methods in a Next.js application. Here’s how to make one!"
source: "https://www.netlify.com/blog/2020/12/04/escaping-next.js-to-access-the-browser/"
last_updated: "2026-08-05T06:04:40.000Z"
---
Welcome to [Blogvent](https://www.netlify.com/blog/tags/blogvent/), day 4!

Often when you are trying to add event listeners or DOM variables outside of a Next.js application, you’ll get a particularly unhelpful error:

```
ReferenceError: window is not defined
```

To get around this, you can use the React hook, `useEffect`! There’s a couple options depending on what you need.

If you just need to access the window, you can use `useEffect` by itself, in something like this:

```
import { useEffect } from 'react'
function Page() {  useEffect(() => {    // use/set the window variable in here  })  ...}
```

And if you need to get an object in the browser (a DOM node or something) outside of Next.js, and render something into it, you can combine `useEffect` with `useRef`!

```
import ReactDOM from 'react-dom';import { useRef, useEffect } from 'react'
function Page() {  let ref = useRef()
  useEffect(() => {    ReactDOM.render(<OtherThing/> ref.current);  }, [])
  return <div ref={ref}/>}
```

## I can’t think of a use case for this!

That’s okay! It’s something that comes up for very specific cases of “escaping” Next.js (or even Gatsby or vanilla React projects), for example using event listeners, using external JavaScript libraries, or adding certain animations. If you’d like to see an example in a real codebase, [check out this part of the Activity Graph](https://github.com/netlify/explorers/blob/main/src/components/UserActivityGraph.js#L25-L34) in Jamstack Explorers.

Speaking of which, if you’d like to learn more about Next.js, check out the course (with more to come) on [Jamstack Explorers](https://www.youtube.com/watch?v=0qXjtznmhDI&list=PLzlG0L9jlhENGgDUr09a7JdRgSTybmE1P)!

### Share

-   [X (fka Twitter)](https://twitter.com/intent/tweet?text=“Escaping” Next.js to access the browser&url=https://www.netlify.com/blog/2020/12/04/escaping-next.js-to-access-the-browser//)
-   [LinkedIn](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fwww.netlify.com%2Fblog%2F2020%2F12%2F04%2Fescaping-next.js-to-access-the-browser%2F%2F)
-   [Facebook](https://www.facebook.com/sharer.php?u=https://www.netlify.com/blog/2020/12/04/escaping-next.js-to-access-the-browser//)
-   [Bluesky](https://bsky.app/intent/compose?text=“Escaping” Next.js to access the browser+https://www.netlify.com/blog/2020/12/04/escaping-next.js-to-access-the-browser//)

* * *

### Tags

-   [Next.js](/blog/tags/next.js/)
-   [blogvent](/blog/tags/blogvent/)

## Keep reading

![](/images/blog-fallback-thumbnail.svg)

Guides & Tutorials December 2, 2020

[

### Next.js: Should I use SSR or SSG?

](/blog/2020/12/02/next.js-should-i-use-ssr-or-ssg/)

-   ![Profile picture of Cassidy Williams](/_astro/a62099fab0f946e063c4b84ff4b4d9c94f9aa7a5-400x400_ZdakPa.webp)
    
    Cassidy Williams
    

![](/_astro/3f45eb6eda4ea8814be310e3df4a7883a5bd9ba0-1200x675_ZcBDUS.webp)

Guides & Tutorials May 15, 2026

[

### How to build a real-time AI chatbot in minutes with Netlify Agent Runners (no backend)

](/blog/how-to-build-a-real-time-ai-chatbot-in-minutes-with-netlify-agent-runners-no-backend)

-   ![Profile picture of Nahrin Jalal](/_astro/f0e7c8f227a03fe58340c99ef5439d5a896c0733-272x272_Z23kDpD.webp)
    
    Nahrin Jalal
    

## Recent posts

Opinions & Insights July 28, 2026

[

### Open Weights and the 2nd World

](/blog/open-weights-and-the-2nd-world)

-   ![Profile picture of Mathias Biilmann](/_astro/f193d4707a6391d843d5aaa73338b5a5971ec5cc-960x960_1Yylqm.webp)
    
    Mathias Biilmann
    

Tools & Services July 28, 2026

[

### MCP goes stateless and extensible

](/blog/mcp-goes-stateless-and-extensible)

-   ![Profile picture of Sean Roberts](/_astro/bbf2243f8171dbddd80ab2103622106cef84d125-512x512_Z1d2LKE.webp)
    
    Sean Roberts
    

News & Announcements July 28, 2026

[

### New Netlify projects are now private by default

](/blog/new-netlify-projects-are-now-private-by-default)

-   ![Profile picture of Taylor Barnett-Torabi](/_astro/cf4624da8c1286738397b6fecb53bad504df140b-400x400_ZsvK0s.webp)
    
    Taylor Barnett-Torabi
    

![](/_astro/3f255b372fa958df35802666ee33b4609b2d71bd-1200x1586_1VtE2D.webp)

### How do the best dev and marketing teams work together?

[Access the report](https://www.netlify.com/reports/2024-leadership-trend-report/access/)