Skip to content

Conversation

rachnac-emeritus
Copy link
Contributor

@rachnac-emeritus rachnac-emeritus commented Oct 13, 2025

What?

> Build error occurred
TypeError: Expected a non-empty string
    at Array.map (<anonymous>)
error Command failed with exit code 1.

Why?

I couldn’t figure out until a very long time where was the Source of the error. Hence, it would be good to know the origin of the error more precisely in the app.

How? Next build failed.

  1. Run: yarn build

Sample snippet of my next.config.tsScreenshot 2025-10-13 at 6 17 01 PM
The env variable process.env.CMS_HOSTNAME was missing for the environment in use, it fell back to '' (since hostname has string) type and failed the build.

IMO: It would be an overhead to conditionally check all env variables (if they exist) before adding them to config. I feel this would be a nice to have.

> Build error occurred
TypeError: Expected a non-empty string
    at Array.map (*<anonymous>*)
error Command failed with exit code 1.

When my build failed with the above error; I couldn’t figure out until a very long time where was the Source of the error. Hence, it would be good to know the origin of the error more precisely.
@ijjk
Copy link
Member

ijjk commented Oct 13, 2025

Allow CI Workflow Run

  • approve CI run for commit: 5e95e7d

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

Copy link
Contributor

@vercel vercel bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Comments:

packages/next/src/build/index.ts (line 676):
The validation for empty hostname added to matchRemotePattern should also be added to writeImagesManifest before calling makeRe(p.hostname).source. Without this, users can still encounter the cryptic picomatch error "Expected a non-empty string" during build if their config has empty hostnames.

View Details
📝 Patch Details
diff --git a/packages/next/src/server/config.ts b/packages/next/src/server/config.ts
index 53d7cebe49..f27ebc92bb 100644
--- a/packages/next/src/server/config.ts
+++ b/packages/next/src/server/config.ts
@@ -505,6 +505,11 @@ function assignDefaultsAndValidate(
               `Specified images.remotePatterns must have protocol "http" or "https" received "${proto}".`
             )
           }
+          if (hostname === undefined || hostname === '') {
+            throw new Error(
+              `Pattern should define hostname but found\n${JSON.stringify({ protocol, hostname, port, pathname, search })}`
+            )
+          }
           return {
             protocol: proto as 'http' | 'https' | undefined,
             hostname,

Analysis

Empty hostname in remotePatterns causes cryptic picomatch error at build time

What fails: writeImagesManifest() in packages/next/src/build/index.ts calls makeRe(p.hostname).source without validating that p.hostname is non-empty, causing a cryptic "Expected a non-empty string" error from picomatch.

How to reproduce:

// next.config.js with empty hostname (e.g., from unset env variable)
module.exports = {
  images: {
    remotePatterns: [
      { protocol: 'https', hostname: process.env.CDN_HOSTNAME || '', pathname: '/**' }
    ]
  }
  // No assetPrefix starting with 'http' - so matchRemotePattern validation isn't triggered
}

Then run next build - this fails with cryptic error: Error: Expected a non-empty string

Root cause: Config validation in packages/next/src/server/config.ts (lines 500-516) validates protocol but not hostname. The validation for empty hostname exists only in matchRemotePattern(), which is only called when assetPrefix starts with 'http'. Users without this config won't get validation until build time when makeRe('') is called.

Expected: Should fail during config validation with clear error message: Pattern should define hostname but found\n{...config...} (matching the error from matchRemotePattern())

@rachnac-emeritus rachnac-emeritus marked this pull request as ready for review October 13, 2025 12:57
Copy link
Contributor

vercel bot commented Oct 13, 2025

Notifying the following users due to files changed in this PR based on this repo's notify modifiers:

@timneutkens, @ijjk, @shuding, @huozhi:

packages/next/src/server/config.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants