SvelteKit + Netlify Deployment: Error Creating build/_headers File

  • We need to know your netlify site name. Example: gifted-antelope-58b104.netlify.app

Deployment is failing, so no site name available yet

  • DNS issues? Tell us the custom domain, tell us the error message! We can’t help if we don’t know your domain.

No custom domain set up

  • Build problems? Link or paste the FULL build log & build settings screenshot

I’m encountering a build error when deploying my SvelteKit project using @sveltejs/adapter-netlify . The build fails with the following error:

3:25:51 PM: > Using @sveltejs/adapter-netlify
3:25:51 PM: error during build:
3:25:51 PM: Error: ENOENT: no such file or directory, open 'build/_headers'
3:25:51 PM:     at Object.openSync (node:fs:596:3)
3:25:51 PM:     at Object.writeFileSync (node:fs:2322:35)
3:25:51 PM:     at appendFileSync (node:fs:2384:6)
3:25:51 PM:     at adapt (file:///opt/build/repo/node_modules/@sveltejs/adapter-netlify/index.js:88:4)
3:25:51 PM:     at adapt (file:///opt/build/repo/node_modules/@sveltejs/kit/src/core/adapt/index.js:38:8)
3:25:51 PM:     at finalise (file:///opt/build/repo/node_modules/@sveltejs/kit/src/exports/vite/index.js:984:13)
3:25:51 PM:     at async Object.handler (file:///opt/build/repo/node_modules/@sveltejs/kit/src/exports/vite/index.js:1014:5)
3:25:51 PM:     at async PluginDriver.hookParallel (file:///opt/build/repo/node_modules/rollup/dist/es/shared/node-entry.js:20862:17)
3:25:51 PM:     at async Object.close (file:///opt/build/repo/node_modules/rollup/dist/es/shared/node-entry.js:21840:13)
3:25:51 PM:     at async build (file:///opt/build/repo/node_modules/vite/dist/node/chunks/dep-CHZK6zbr.js:65624:17)
3:25:51 PM: ​
3:25:51 PM: "build.command" failed                                        
3:25:51 PM: ────────────────────────────────────────────────────────────────
3:25:51 PM: ​
3:25:51 PM:   Error message
3:25:51 PM:   Command failed with exit code 1: npm run build (https://ntl.fyi/exit-code-1)
3:25:51 PM: ​
3:25:51 PM:   Error location
3:25:51 PM:   In build.command from netlify.toml:
3:25:51 PM:   npm run build
3:25:51 PM: ​
3:25:51 PM:   Resolved config
3:25:51 PM:   build:
3:25:51 PM:     command: npm run build
3:25:51 PM:     commandOrigin: config
3:25:51 PM:     environment:
3:25:51 PM:       - CONVEX_DEPLOY_KEY
3:25:51 PM:       - PUBLIC_CLERK_PUBLISHABLE_KEY
3:25:51 PM:       - PUBLIC_CONVEX_URL
3:25:51 PM:     publish: /opt/build/repo/build
3:25:51 PM:     publishOrigin: config
3:25:51 PM:   headers:
3:25:52 PM:     - for: /*
      values:
        Referrer-Policy: strict-origin-when-cross-origin
        Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
        X-Frame-Options: DENY
        X-XSS-Protection: 1; mode=block
3:25:52 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
3:25:52 PM: Failing build: Failed to build site
3:25:52 PM: Finished processing build request in 43.318s
3:25:52 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
  • Did you try Ask Netlify, our generative AI chatbot, before posting? It pulls info from Support Guides and recent solved forums posts.

Yes.

───────────────────────────────────────────

Things I’ve tried:

Manually included the _headers file within build\_headers with the following content:

/*
  Referrer-Policy: strict-origin-when-cross-origin
  Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
  X-Frame-Options: DENY
  X-XSS-Protection: 1; mode=block

However, the build still fails with the same error, and the build directory is deleted after the failed build.

───────────────────────────────────────────

  1. Manually included the _headers file within build\_headers AND changed the output directory of build process to dist instead of build, I made the following changes to these differnt files:

svelte.config.js

config.kit: {
  outDir: 'dist', // instead of build
  ...
}

tsconfig.json

{
   "extends": "./dist/tsconfig.json",
   ...
   ...
   "outDir": "./dist" // instead of build
}

doing so led to the deployment being successful. However, I realized that I forgot to change the publish property inside the netlify.toml file to dist, so, eventhough the build was successful, my build\ was being served by Netlify instead of the dist\, so that’s not a solution since my apps build is contained within dist and not build given the changes I made to the above files.

So, I changed netlify.toml to the following:

[build]
  command = "bun run build"
  publish = "dist"

but the moment I set `publish = “dist” instead of publish = “build”, I end up with the same error as before, but instead of the error being
Error: ENOENT: no such file or directory, open 'build/_headers' it ends up being Error: ENOENT: no such file or directory, open 'dist/_headers'

which makes sense, since, _headers is within build/ and not within dist/

───────────────────────────────────────────

  1. I noticed the documentation says we can either specificy the build\_headers file or we can put those settings inside of the netlify.toml file. So, I tried doing that, my new netlify.toml file looks like so:
[build]
  command = "npm run build"
  publish = "dist"
  
[[headers]]
  for = "/*"
  [headers.values]
    Referrer-Policy = "strict-origin-when-cross-origin"
    Strict-Transport-Security = "max-age=31536000; includeSubDomains; preload"
    X-Frame-Options = "DENY"
    X-XSS-Protection = "1; mode=block"

but that doesn’t work, it still gives me the erorr Error: ENOENT: no such file or directory, open ‘dist/_headers’```

it seems like the @sveltejs/adapter-netlify is hardcoded to look for the _headers file even if those settings are provided in the toml file, but that’s just a guess.

Would really like to solve issue, thank you in advance.

Somehow it’s working… I’m confused but i’ll take it! I’ll leave this open for a couple of days incase I end up in the same place. If anyone did anything behind the scenes, thank you.

The problem stemmed from my configurations. This can be closed, thanks.