Build error when deploying React + Vite

Hello,

I am deploying my site to Netlify by way of Github and I am receiving and error when I try to deploy the site. The react based app runs perfectly smoothly locally. I have come across many similar problems as mine but all the solutions have failed to work. I have deployed similar apps succesfully but these did not contain ‘react-router-dom’.

Please see logs:

3:46:54 PM: 1. Build command from Netlify app
3:46:54 PM: ────────────────────────────────────────────────────────────────
3:46:54 PM: ​
3:46:54 PM: $ npm run build
3:46:54 PM: > port@0.0.0 build
3:46:54 PM: > vite build
3:46:54 PM: vite v3.2.5 building for production…
3:46:54 PM: transforming…
3:46:54 PM: ✓ 3 modules transformed.
3:46:54 PM: [vite]: Rollup failed to resolve import “react-router-dom” from “src/main.jsx”.
3:46:54 PM: This is most likely unintended because it can break your application at runtime.
3:46:54 PM: If you do want to externalize this module explicitly add it to
3:46:54 PM: build.rollupOptions.external
3:46:54 PM: error during build:
3:46:54 PM: Error: [vite]: Rollup failed to resolve import “react-router-dom” from “src/main.jsx”.
3:46:54 PM: This is most likely unintended because it can break your application at runtime.
3:46:54 PM: If you do want to externalize this module explicitly add it to
3:46:54 PM: build.rollupOptions.external
3:46:54 PM: at onRollupWarning (file:///opt/build/repo/node_modules/vite/dist/node/chunks/dep-5605cfa4.js:45832:19)
3:46:54 PM: at onwarn (file:///opt/build/repo/node_modules/vite/dist/node/chunks/dep-5605cfa4.js:45603:13)
3:46:54 PM: at Object.onwarn (file:///opt/build/repo/node_modules/rollup/dist/es/shared/rollup.js:23263:13)
3:46:54 PM: at ModuleLoader.handleResolveId (file:///opt/build/repo/node_modules/rollup/dist/es/shared/rollup.js:22158:26)
3:46:54 PM: at file:///opt/build/repo/node_modules/rollup/dist/es/shared/rollup.js:22119:26
3:46:54 PM: at processTicksAndRejections (node:internal/process/task_queues:96:5)
3:46:54 PM: ​
3:46:54 PM: “build.command” failed
3:46:54 PM: ────────────────────────────────────────────────────────────────
3:46:54 PM: ​
3:46:54 PM: Error message
3:46:54 PM: Command failed with exit code 1: npm run build (Search results for '"non-zero exit code: 1"' - Netlify Support Forums)
3:46:54 PM: ​
3:46:54 PM: Error location
3:46:54 PM: In Build command from Netlify app:
3:46:56 PM: Failed during stage ‘building site’: Build script returned non-zero exit code: 2 (Search results for '"non-zero exit code: 2"' - Netlify Support Forums)
3:46:54 PM: npm run build
3:46:54 PM: ​
3:46:54 PM: Resolved config
3:46:54 PM: build:
3:46:54 PM: command: npm run build
3:46:54 PM: commandOrigin: ui
3:46:54 PM: publish: /opt/build/repo/dist
3:46:54 PM: publishOrigin: ui
3:46:55 PM: Caching artifacts
3:46:55 PM: Started saving node modules
3:46:55 PM: Finished saving node modules
3:46:55 PM: Started saving build plugins
3:46:55 PM: Finished saving build plugins
3:46:55 PM: Started saving pip cache
3:46:55 PM: Finished saving pip cache
3:46:55 PM: Started saving emacs cask dependencies
3:46:55 PM: Finished saving emacs cask dependencies
3:46:55 PM: Started saving maven dependencies
3:46:55 PM: Finished saving maven dependencies
3:46:55 PM: Started saving boot dependencies
3:46:55 PM: Finished saving boot dependencies
3:46:55 PM: Started saving rust rustup cache
3:46:55 PM: Finished saving rust rustup cache
3:46:55 PM: Started saving go dependencies
3:46:55 PM: Finished saving go dependencies
3:46:56 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
3:46:56 PM: Failing build: Failed to build site
3:46:56 PM: Finished processing build request in 16.94907013s

Thank you for your time reading this.

Hiya @samspuds :wave:t6: Welcome to the Netlify Support forums! Sorry to hear about your build issues. Before moving forward can you take a look at this resource 7? It is a compilation of all of our build and deploy resources. This will be a good starting point.

If those resources don’t get you on the right track, can you share a bit more information?

Lastly, please share these additional details so we can look into your experience:

  • the name of your build instance: “example-jones.netlify.com 1
  • your project repo
  • what you have already tried
  • any error messages you have received in your terminal window or in the developer console

Thanks so much!

Thank you for your response.

Here is the full log:

3:46:39 PM: build-image version: d55944c047ef0aee2ac01971edde72cb2a50acfe (focal)
3:46:39 PM: build-image tag: v4.16.1
3:46:39 PM: buildbot version: d48008661c052442028ef45a37b42c87e988550c
3:46:39 PM: Fetching cached dependencies
3:46:39 PM: Failed to fetch cache, continuing with build
3:46:39 PM: Starting to prepare the repo for build
3:46:39 PM: No cached dependencies found. Cloning fresh repo
3:46:39 PM: git clone --filter=blob:none GitHub - samspuds/my-portfolio-site
3:46:44 PM: Preparing Git Reference refs/heads/main
3:46:44 PM: Parsing package.json dependencies
3:46:46 PM: Starting build script
3:46:46 PM: Installing dependencies
3:46:46 PM: Python version set to 2.7
3:46:46 PM: v16.19.0 is already installed.
3:46:47 PM: Now using node v16.19.0 (npm v8.19.3)
3:46:47 PM: Enabling node corepack
3:46:47 PM: Started restoring cached build plugins
3:46:47 PM: Finished restoring cached build plugins
3:46:47 PM: Attempting ruby version 2.7.2, read from environment
3:46:48 PM: Using ruby version 2.7.2
3:46:48 PM: Using PHP version 8.0
3:46:48 PM: No npm workspaces detected
3:46:48 PM: Started restoring cached node modules
3:46:48 PM: Finished restoring cached node modules
3:46:48 PM: Installing NPM modules using NPM version 8.19.3
3:46:49 PM: npm WARN deprecated sourcemap-codec@1.4.8: Please use @jridgewell/sourcemap-codec instead
3:46:51 PM: added 162 packages, and audited 163 packages in 2s
3:46:51 PM: 17 packages are looking for funding
3:46:51 PM: run npm fund for details
3:46:51 PM: found 0 vulnerabilities
3:46:51 PM: NPM modules installed
3:46:51 PM: Creating package sha
3:46:51 PM: Started restoring cached go cache
3:46:51 PM: Finished restoring cached go cache
3:46:51 PM: go version go1.19.4 linux/amd64
3:46:52 PM: Detected 1 framework(s)
3:46:52 PM: “vite” at version “3.2.5”
3:46:52 PM: Installing missing commands
3:46:52 PM: Verify run directory
3:46:52 PM: Section completed: initializing
3:46:54 PM: ​
3:46:54 PM: Netlify Build
3:46:54 PM: ────────────────────────────────────────────────────────────────
3:46:54 PM: ​
3:46:54 PM: ❯ Version
3:46:54 PM: @netlify/build 29.2.1
3:46:54 PM: ​
3:46:54 PM: ❯ Flags
3:46:54 PM: baseRelDir: true
3:46:54 PM: buildId: 63a32a5d4684a528bee63094
3:46:54 PM: deployId: 63a32a5d4684a528bee63096
3:46:54 PM: ​
3:46:54 PM: ❯ Current directory
3:46:54 PM: /opt/build/repo
3:46:54 PM: ​
3:46:54 PM: ❯ Config file
3:46:54 PM: No config file was defined: using default values.
3:46:54 PM: ​
3:46:54 PM: ❯ Context
3:46:54 PM: production
3:46:54 PM: ​
3:46:54 PM: 1. Build command from Netlify app
3:46:54 PM: ────────────────────────────────────────────────────────────────
3:46:54 PM: ​
3:46:54 PM: $ npm run build
3:46:54 PM: > port@0.0.0 build
3:46:54 PM: > vite build
3:46:54 PM: vite v3.2.5 building for production…
3:46:54 PM: transforming…
3:46:54 PM: ✓ 3 modules transformed.
3:46:54 PM: [vite]: Rollup failed to resolve import “react-router-dom” from “src/main.jsx”.
3:46:54 PM: This is most likely unintended because it can break your application at runtime.
3:46:54 PM: If you do want to externalize this module explicitly add it to
3:46:54 PM: build.rollupOptions.external
3:46:54 PM: error during build:
3:46:54 PM: Error: [vite]: Rollup failed to resolve import “react-router-dom” from “src/main.jsx”.
3:46:54 PM: This is most likely unintended because it can break your application at runtime.
3:46:54 PM: If you do want to externalize this module explicitly add it to
3:46:54 PM: build.rollupOptions.external
3:46:54 PM: at onRollupWarning (file:///opt/build/repo/node_modules/vite/dist/node/chunks/dep-5605cfa4.js:45832:19)
3:46:54 PM: at onwarn (file:///opt/build/repo/node_modules/vite/dist/node/chunks/dep-5605cfa4.js:45603:13)
3:46:54 PM: at Object.onwarn (file:///opt/build/repo/node_modules/rollup/dist/es/shared/rollup.js:23263:13)
3:46:54 PM: at ModuleLoader.handleResolveId (file:///opt/build/repo/node_modules/rollup/dist/es/shared/rollup.js:22158:26)
3:46:54 PM: at file:///opt/build/repo/node_modules/rollup/dist/es/shared/rollup.js:22119:26
3:46:54 PM: at processTicksAndRejections (node:internal/process/task_queues:96:5)
3:46:54 PM: ​
3:46:54 PM: “build.command” failed
3:46:54 PM: ────────────────────────────────────────────────────────────────
3:46:54 PM: ​
3:46:54 PM: Error message
3:46:54 PM: Command failed with exit code 1: npm run build (Search results for '"non-zero exit code: 1"' - Netlify Support Forums)
3:46:54 PM: ​
3:46:54 PM: Error location
3:46:54 PM: In Build command from Netlify app:
3:46:56 PM: Failed during stage ‘building site’: Build script returned non-zero exit code: 2 (Search results for '"non-zero exit code: 2"' - Netlify Support Forums)
3:46:54 PM: npm run build
3:46:54 PM: ​
3:46:54 PM: Resolved config
3:46:54 PM: build:
3:46:54 PM: command: npm run build
3:46:54 PM: commandOrigin: ui
3:46:54 PM: publish: /opt/build/repo/dist
3:46:54 PM: publishOrigin: ui
3:46:55 PM: Caching artifacts
3:46:55 PM: Started saving node modules
3:46:55 PM: Finished saving node modules
3:46:55 PM: Started saving build plugins
3:46:55 PM: Finished saving build plugins
3:46:55 PM: Started saving pip cache
3:46:55 PM: Finished saving pip cache
3:46:55 PM: Started saving emacs cask dependencies
3:46:55 PM: Finished saving emacs cask dependencies
3:46:55 PM: Started saving maven dependencies
3:46:55 PM: Finished saving maven dependencies
3:46:55 PM: Started saving boot dependencies
3:46:55 PM: Finished saving boot dependencies
3:46:55 PM: Started saving rust rustup cache
3:46:55 PM: Finished saving rust rustup cache
3:46:55 PM: Started saving go dependencies
3:46:55 PM: Finished saving go dependencies
3:46:56 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
3:46:56 PM: Failing build: Failed to build site
3:46:56 PM: Finished processing build request in 16.94907013s

ubiquitous-starlight-110a36

I have updated all packages and I have tried changing the build command to ‘CI= npm run build’ to no avail.

Hey @samspuds,

Could you try to remove the node_modules folder from your repo? As a general advice, this should never be committed to your repo.

Thanks for the tip, unfortunately I’m still getting the same error with react-router-dom :frowning:

Issue has been resolved:

"Basically I made a mistake and installed react-router using npm install react-router.

I uninstalled it and installed it back using npm install react-router-dom@6."

1 Like

Worked for me as well, thanks a lot!

1 Like