Fix InvalidImageError: Invalid src prop on next/image, hostname "cdn.example.com" is not configured under images in your next.config.js in Next.js
This error occurs when the next/image component receives an external URL whose hostname is not listed in the images configuration in next.config.js. Next.js requires explicit allowlisting of external image domains for security and optimization. Fix it by adding the hostname to the images.remotePatterns array.
Reading the Stack Trace
Here's what each line means:
- at imageConfigDefault (/app/node_modules/next/dist/shared/lib/image-config.js:42:11): The image config is being checked against the allowed hostnames and the provided hostname is not in the list.
- at resolveImageData (/app/node_modules/next/dist/client/image.js:187:15): Next.js image resolver validates the src URL before processing the image for optimization.
- at Image (/app/node_modules/next/dist/client/image.js:238:26): The next/image component throws during render because the hostname validation failed.
Common Causes
1. External hostname not in remotePatterns
The next.config.js file does not include the CDN hostname in the images.remotePatterns array.
// next.config.js
module.exports = {
// No images configuration
};
2. Using deprecated domains instead of remotePatterns
The older images.domains array was used but is insufficient for newer Next.js versions that prefer remotePatterns.
// next.config.js
module.exports = {
images: {
domains: ['cdn.example.com'] // Deprecated in Next.js 14+
}
};
3. Typo in hostname configuration
The hostname in next.config.js does not exactly match the hostname in the image URL due to a typo.
// next.config.js
module.exports = {
images: {
remotePatterns: [
{ protocol: 'https', hostname: 'cdn.exmple.com' } // Typo
]
}
};
The Fix
Add the external image hostname to images.remotePatterns in next.config.js. Use pathname patterns to restrict which paths are allowed for additional security. This tells Next.js to allow and optimize images from that hostname.
// next.config.js
module.exports = {
reactStrictMode: true,
};
// next.config.js
module.exports = {
reactStrictMode: true,
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'cdn.example.com',
pathname: '/photos/**',
},
],
},
};
Testing the Fix
import { render, screen } from '@testing-library/react';
import Avatar from '@/components/Avatar';
describe('Avatar', () => {
it('renders an image with the correct src', () => {
render(<Avatar src="https://cdn.example.com/photos/avatar.jpg" alt="User" />);
const img = screen.getByAltText('User');
expect(img).toBeInTheDocument();
expect(img).toHaveAttribute('src');
});
it('renders alt text for accessibility', () => {
render(<Avatar src="https://cdn.example.com/photos/avatar.jpg" alt="Profile photo" />);
expect(screen.getByAltText('Profile photo')).toBeInTheDocument();
});
});
Run your tests:
npm test
Pushing Through CI/CD
git checkout -b fix/next-image-remote-pattern,git add next.config.js,git commit -m "fix: add cdn.example.com to next/image remotePatterns",git push origin fix/next-image-remote-pattern
Your CI config should look something like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: npm test -- --coverage
- run: npm run build
The Full Manual Process: 18 Steps
Here's every step you just went through to fix this one bug:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- Monitor production to confirm the error is resolved
Total time: 30-60 minutes. For one bug.
Or Let bugstack Fix It in Under 2 minutes
Every step above? bugstack does it automatically.
Step 1: Install the SDK
npm install bugstack-sdk
Step 2: Initialize
import { initBugStack } from 'bugstack-sdk'
initBugStack({ apiKey: process.env.BUGSTACK_API_KEY })
Step 3: There is no step 3.
bugstack handles everything from here:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- Deploys to production (or opens a PR for review)
Time from error to fix deployed: Under 2 minutes.
Human involvement: zero.
Try bugstack Free →No credit card. 5-minute setup. Cancel anytime.
Deploying the Fix (Manual Path)
- Update next.config.js with the correct remotePatterns entry.
- Run the Next.js build locally to confirm no image errors.
- Open a pull request with the config change.
- Wait for CI checks to pass on the PR.
- Merge to main and verify images load correctly in staging.
Frequently Asked Questions
BugStack verifies the remotePatterns configuration against your image URLs, runs your test suite, and confirms the build succeeds before marking the fix safe.
Every fix is delivered as a pull request with full CI validation. Your team reviews and approves before anything reaches production.
Use remotePatterns. The domains property is deprecated in Next.js 14 and beyond. remotePatterns provides more granular control with protocol and pathname restrictions.
You can use a wildcard hostname pattern, but this is not recommended for production. It disables security protections and could allow image optimization abuse.