Vite React Simple Project Getting Build error script returned non-zero exit code: 2

During the building stage, I am getting a non-zero exit code 2 error. What could be causing this? How do I solve this issue?

I am deploying from a public gitHub repo I own and using the dist folder when setting up the deployment on Netlify.

Deploy Log under “Building” on Netlify:

12:49:26 PM: Netlify Build                                                 
12:49:26 PM: ────────────────────────────────────────────────────────────────
12:49:26 PM: ​
12:49:26 PM: ❯ Version
12:49:26 PM:   @netlify/build 29.15.3
12:49:26 PM: ​
12:49:26 PM: ❯ Flags
12:49:26 PM:   baseRelDir: true
12:49:26 PM:   buildId: 64acdf312ac5c0360222224e
12:49:26 PM:   deployId: 64acdf312ac5c03602222250
12:49:26 PM: ​
12:49:26 PM: ❯ Current directory
12:49:26 PM:   /opt/build/repo
12:49:26 PM: ​
12:49:26 PM: ❯ Config file
12:49:26 PM:   No config file was defined: using default values.
12:49:26 PM: ​
12:49:26 PM: ❯ Context
12:49:26 PM:   production
12:49:26 PM: ​
12:49:26 PM: Build command from Netlify app                                
12:49:26 PM: ────────────────────────────────────────────────────────────────
12:49:26 PM: ​
12:49:26 PM: $ npm run build
12:49:27 PM: > random_quote_lc@1.0.0 build
12:49:27 PM: > vite build
12:49:27 PM: vite v3.2.7 building for production...
12:49:27 PM: <script src=dist/bundle.js> in /index.html can't be bundled without type=module attribute
transforming...
12:49:29 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
12:49:28 PM: ✓ 34 modules transformed.
12:49:28 PM: rendering chunks...
12:49:28 PM: dist/index.html                  0.54 KiB
12:49:28 PM: dist/assets/index.28c2df18.css   227.35 KiB / gzip: 30.58 KiB
12:49:28 PM: dist/assets/index.644c0ae7.js    141.48 KiB / gzip: 45.78 KiB
12:49:28 PM: ​
12:49:28 PM: (build.command completed in 1.5s)
12:49:28 PM: ​
12:49:29 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
12:49:29 PM: Failing build: Failed to build site
12:49:30 PM: Finished processing build request in 14.636s

@collins It’s hard to know for certain, as you’ve only provided part of the log, and sometimes Netlify fails during Deployment, (but will show the fail as if it is against Building), usually when the folder you’re trying to deploy doesn’t exist.

It could also be because of this:

12:49:27 PM: <script src=dist/bundle.js> in /index.html can't be bundled without type=module attribute
transforming...

If you want that file to be bundled you need to have a type="module" attribute as per Vite’s Getting Started documentation.

The default behaviour on Netlify is that warnings are treated as errors and will cause your build to fail, so if that’s a warning it may be the cause.

You can disable that behaviour by setting CI to false, the easiest way to do that is to change your build command from npm run build to CI= npm run build

Hopefully it’s one of the usual “gotchas” above, but if you keep having trouble just provide a bit more detail.

1 Like