Build Fail with exit code 2

I recently migrated to vite. After which builds are failing.
npm run build worked on local

I searched on google and netlify docs, but none of them worked. Please do help me here.

Site name: https://socialgram.netlify.app/

Deploy logs

2:13:53 AM: $ npm run build
2:13:53 AM: > socialgram@1.1.0 build /opt/build/repo
2:13:53 AM: > vite build
2:13:53 AM: (node:3377) ExperimentalWarning: The ESM module loader is experimental.
2:13:53 AM: file:///opt/build/repo/node_modules/vite/bin/vite.js:7
2:13:53 AM:     await import('source-map-support').then((r) => r.default.install())
2:13:53 AM:     ^^^^^
2:13:53 AM: SyntaxError: Unexpected reserved word
2:13:53 AM:     at Loader.moduleStrategy (internal/modules/esm/translators.js:81:18)
2:13:53 AM:     at async link (internal/modules/esm/module_job.js:37:21)
2:13:53 AM: npm ERR! code ELIFECYCLE
2:13:53 AM: npm ERR! errno 1
2:13:53 AM: npm ERR! socialgram@1.1.0 build: `vite build`
2:13:53 AM: npm ERR! Exit status 1
2:13:53 AM: npm ERR!
2:13:53 AM: npm ERR! Failed at the socialgram@1.1.0 build script.
2:13:53 AM: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2:13:53 AM: npm ERR! A complete log of this run can be found in:
2:13:53 AM: npm ERR!     /opt/buildhome/.npm/_logs/2022-12-25T20_43_53_674Z-debug.log
2:13:53 AM: ​
2:13:53 AM:   "build.command" failed                                        
2:13:53 AM: ────────────────────────────────────────────────────────────────
2:13:53 AM: ​
2:13:53 AM:   Error message
2:13:53 AM:   Command failed with exit code 1: npm run build (https://ntl.fyi/exit-code-1)
2:13:55 AM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
2:13:53 AM: ​
2:13:53 AM:   Error location
2:13:53 AM:   In Build command from Netlify app:
2:13:53 AM:   npm run build
2:13:53 AM: ​
2:13:53 AM:   Resolved config
2:13:53 AM:   build:
2:13:53 AM:     command: npm run build
2:13:53 AM:     commandOrigin: ui

Please follow our debugging builds guide here:

Once you’ve completed the work requested there, please provide an answer to the questions at the bottom of the post (you can paste them here, to keep them in this thread), and we’ll be able to help you as you debug further, once you’ve given us that info.

I had the same problem as you. Build works locally but netlify build fails.
Here’s how I solved it:

  • check the version of node and npm installed on your computer
  • set the environment variables in netlify: NODE_VERSION with value of the version installed on your computer. Do the same with NPM_VERSION
3 Likes

Hi @shelcia and @dhatGuy :wave:t6: welcome to the Netlify forums! I’m glad you’re here. @dhatGuy Thank you for providing that solution. Please give it a go and let us know if it helped resolve your issues. Happy building! :netliconfetti: :smiling_face_with_three_hearts:

How do i setup that ?

on your computer, run node -v to get the node version and npm -v for npm version.
then read here on how to create variables

like this ?

# Production context: all deploys from the Production branch
# set in your site’s Branches settings in the UI will inherit
# these settings. You can define environment variables
# here but we recommend using the Netlify UI for sensitive
# values to keep them out of your source repository.
[context.production]
  publish = "output/"
  command = "make publish"
  environment = { NODE_VERSION = "16.17.0" }

# Here is an example of how to define context-specific
# environment variables. Be mindful when using this
# option and avoid committing sensitive values to public
# source repositories.
[context.deploy-preview.environment]
  NOT_PRIVATE_ITEM = "not so secret"

# Branch Deploy context: all deploys that are not from
# a pull/merge request or from the Production branch
# will inherit these settings.
[context.branch-deploy.environment]
  NODE_ENV = "development"

# Dev context: environment variables set here 
# are available for local development environments 
# run using Netlify Dev. These values can be 
# overwritten on branches that have a more specific 
# branch context configured.
[context.dev.environment]
  NODE_ENV = "development"

# Specific branch context: all deploys from
# this specific branch will inherit these settings.
[context.staging.environment] # β€œstaging” is a branch name
  NODE_ENV = "development"
1 Like

this should work. If it didn’t, I will suggest you use Netlify UI

Hiya @dhatGuy :wave:t6: thanks for elaborating on the fix. I appreciate it. (:

Not working unfortunately, can you please elaborate more

I made an accounrt judst to sat THANK YOU, this worked for me. i set my environment variables as described using the netlify UI

1 Like

this worked for me!!!

that is awesome! welcome back and thanks for sharing this with the community. :partying_face: