---
title: "How do you allow external image domains in Next.js?"
description: "Add remotePatterns to next.config.js so next/image can optimize images from external hosts, and learn why the older domains option is deprecated."
url: "https://bugsee.com/answers/nextjs/allow-external-image-domains/"
---

# How do you allow external image domains in Next.js?

List the allowed hosts in `images.remotePatterns` in `next.config.js`. The older `images.domains` option still works but is deprecated.

```js
// next.config.js
module.exports = {
  images: {
    remotePatterns: [
      {
        protocol: "https",
        hostname: "images.example.com",
        pathname: "/uploads/**",
      },
    ],
  },
};
```

Restart the dev server after changing the config. Patterns can match a path prefix, a port and wildcard subdomains, which lets you allow only what you need instead of a whole domain.

If the project uses a static export (`output: "export"`), the built-in image optimizer is not available, so set `images.unoptimized` to true.

## Related questions

- [How do you fix "window is not defined" in Next.js?](https://bugsee.com/answers/nextjs/window-is-not-defined/)
