Deploying fails with build but gives 404 error without build

Please assist, when I deploy without any build it deploys successfully however when I try click the link to access the website it gives me the 404 error. When I add the build which is “npm run build” it fails to deploy and gives the below error 9:22:01 PM: “build.command” failed
9:22:01 PM: ────────────────────────────────────────────────────────────────
9:22:01 PM: ​
9:22:01 PM: Error message
9:22:01 PM: Command failed with exit code 254: npm run build (Search results for '"non-zero exit code: 254"' - Netlify Support Forums)
9:22:01 PM: ​
9:22:01 PM: Error location
9:22:01 PM: In Build command from Netlify app:
9:22:01 PM: npm run build
9:22:01 PM: ​

@Dibu You’ve not provided enough of the build log to know for certain, but it looks like exit code 254 occurs when it cannot find a package.json.

Check if your build log does mention that being the issue.

No matter how you run your build, you can use the Deploy File Browser to see what you’ve submitted to Netlify. If there is no index.html then you’ll see that 404 error.

If you’re still stuck provide your full build log (not just the error code section).

Good day, below is the full log it does mention the package.json was not found however it is there on my code. When I deploy without adding any build it goes through successfully but the link gives a 404 error.
10:44:55 PM: Netlify Build
10:44:55 PM: ────────────────────────────────────────────────────────────────
10:44:55 PM: ​
10:44:55 PM: ❯ Version
10:44:55 PM: @netlify/build 29.36.0
10:44:55 PM: ​
10:44:55 PM: ❯ Flags
10:44:55 PM: baseRelDir: true
10:44:55 PM: buildId: 65ee1baad69705b0e1669cb1
10:44:55 PM: deployId: 65ee1baad69705b0e1669cb3
10:44:55 PM: ​
10:44:55 PM: ❯ Current directory
10:44:55 PM: /opt/build/repo
10:44:55 PM: ​
10:44:55 PM: ❯ Config file
10:44:55 PM: No config file was defined: using default values.
10:44:55 PM: ​
10:44:55 PM: ❯ Context
10:44:55 PM: production
10:44:55 PM: ​
10:44:55 PM: Build command from Netlify app
10:44:55 PM: ────────────────────────────────────────────────────────────────
10:44:55 PM: ​
10:44:55 PM: $ npm run build
10:44:55 PM: npm ERR! code ENOENT
10:44:55 PM: npm ERR! syscall open
10:44:55 PM: npm ERR! path /opt/build/repo/package.json
10:44:55 PM: npm ERR! errno -2
10:44:55 PM: npm ERR! enoent Could not read package.json: Error: ENOENT: no such file or directory, open “/opt/build/repo/package.json”
10:44:55 PM: npm ERR! enoent This is related to npm not being able to find a file.
10:44:55 PM: npm ERR! enoent
10:44:55 PM: npm ERR! A complete log of this run can be found in: /opt/buildhome/.npm/_logs/2024-03-10T20_44_55_664Z-debug-0.log
10:44:55 PM: ​
10:44:55 PM: “build.command” failed
10:44:55 PM: ────────────────────────────────────────────────────────────────
10:44:55 PM: ​
10:44:55 PM: Error message
10:44:55 PM: Command failed with exit code 254: npm run build (Search results for '"non-zero exit code: 254"' - Netlify Support Forums)
10:44:55 PM: ​
10:44:55 PM: Error location
10:44:55 PM: In Build command from Netlify app:
10:44:55 PM: npm run build
10:44:55 PM: ​
10:44:55 PM: Resolved config
10:44:55 PM: build:
10:44:55 PM: command: npm run build
10:44:55 PM: commandOrigin: ui
10:44:55 PM: publish: /opt/build/repo
10:44:55 PM: publishOrigin: default
10:44:56 PM: Failed during stage “building site”: Build script returned non-zero exit code: 2
10:44:56 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
10:44:56 PM: Failing build: Failed to build site
10:44:56 PM: Finished processing build request in 19.14s

This is saying there is no package.json in the root of the repository or base path set in the build settings.

Possibly the files for your project are in a sub-directory?

1 Like

The package.json is there and it does not seem to be under a cub-category. If it is how do I move it out because I have tried every solution that I came across and I just cannot get the deploy to work without getting any errors.

I can’t tell you, and neither could @nathanmartin, exactly what they issue unless you provide

  1. The build settings you are using
  2. A link to the public git repository you are deploying from