Error Command "build" not found

I’m creating a simple app using React, the files are organized in two folders inside the root project. One is called Client and the other Server. My frontend is displaying fine but the database data from mongoDB connected in my node.js server is not showing. Please help :slight_smile:

What I’ve tried:

  • I’ve installed the netlify CLI in the root folder.
  • Added the netlify functions inside the server folder and set the functions path to …/server/netlify/functions
  • Since the build folder is located inside the client folder, I’m deploying the application from this location from terminal.
  • I’ve added the CI environment variable with the false value

Netlify site url: https://simple-note-okd.netlify.app
GitHub repo: GitHub - ok-diego/simple-note: This is an app for organizing book highlights

Build settings:
Runtime
Not set

Base directory
Not set

Build command
CI= yarn run build

Publish directory
build

Deploy log visibility
Logs are public

Build status
Active

Error message:

Build log:

4:15:30 PM: build-image version: 5b0ab77c5e46895703390c45e86a907d4b78547b (focal)
4:15:30 PM: buildbot version: 5b0ab77c5e46895703390c45e86a907d4b78547b
4:15:30 PM: Building without cache
4:15:30 PM: Starting to prepare the repo for build
4:15:30 PM: No cached dependencies found. Cloning fresh repo
4:15:30 PM: git clone --filter=blob:none https://github.com/ok-diego/simple-note
4:15:30 PM: Preparing Git Reference refs/heads/main
4:15:31 PM: Parsing package.json dependencies
4:15:32 PM: Different functions path detected, going to use the one specified in the Netlify configuration file: 'server/netlify/functions' versus './server/netlify/functions' in the Netlify UI
4:15:32 PM: Starting build script
4:15:32 PM: Installing dependencies
4:15:32 PM: Python version set to 2.7
4:15:32 PM: v16.19.0 is already installed.
4:15:33 PM: Now using node v16.19.0 (npm v8.19.3)
4:15:33 PM: Enabling Node.js Corepack
4:15:33 PM: Started restoring cached build plugins
4:15:33 PM: Finished restoring cached build plugins
4:15:33 PM: Attempting Ruby version 2.7.2, read from environment
4:15:33 PM: Using Ruby version 2.7.2
4:15:34 PM: Using PHP version 8.0
4:15:34 PM: Started restoring cached corepack dependencies
4:15:34 PM: Finished restoring cached corepack dependencies
4:15:34 PM: No npm workspaces detected
4:15:34 PM: Started restoring cached node modules
4:15:34 PM: Finished restoring cached node modules
4:15:34 PM: Installing npm packages using npm version 8.19.3
4:15:34 PM: up to date, audited 1 package in 90ms
4:15:34 PM: found 0 vulnerabilities
4:15:34 PM: npm packages installed
4:15:34 PM: Started restoring cached go cache
4:15:34 PM: Finished restoring cached go cache
4:15:34 PM: go version go1.19.6 linux/amd64
4:15:35 PM: Detected 0 framework(s)
4:15:35 PM: Installing missing commands
4:15:35 PM: Verify run directory
4:15:35 PM: Section completed: initializing
4:15:36 PM: ​
4:15:36 PM:   Netlify Build                                                 
4:15:36 PM: ────────────────────────────────────────────────────────────────
4:15:36 PM: ​
4:15:36 PM: ❯ Version
4:15:36 PM:   @netlify/build 29.5.7
4:15:36 PM: ​
4:15:36 PM: ❯ Flags
4:15:36 PM:   baseRelDir: true
4:15:36 PM:   buildId: 63ee9cef0cefd7169be6c411
4:15:36 PM:   deployId: 63ee9cef0cefd7169be6c413
4:15:36 PM: ​
4:15:36 PM: ❯ Current directory
4:15:36 PM:   /opt/build/repo
4:15:36 PM: ​
4:15:36 PM: ❯ Config file
4:15:36 PM:   No config file was defined: using default values.
4:15:36 PM: ​
4:15:36 PM: ❯ Context
4:15:36 PM:   production
4:15:36 PM: ​
4:15:36 PM:   1. Build command from Netlify app                             
4:15:36 PM: ────────────────────────────────────────────────────────────────
4:15:36 PM: ​
4:15:36 PM: $ CI= yarn run build
4:15:36 PM: yarn run v1.22.19
4:15:36 PM: error Command "build" not found.
4:15:36 PM: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
4:15:36 PM: ​
4:15:36 PM:   "build.command" failed                                        
4:15:36 PM: ────────────────────────────────────────────────────────────────
4:15:36 PM: ​
4:15:36 PM:   Error message
4:15:36 PM:   Command failed with exit code 1: CI= yarn run build (https://ntl.fyi/exit-code-1)
4:15:36 PM: ​
4:15:36 PM:   Error location
4:15:37 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
4:15:36 PM:   In Build command from Netlify app:
4:15:36 PM:   CI= yarn run build
4:15:36 PM: ​
4:15:36 PM:   Resolved config
4:15:36 PM:   build:
4:15:36 PM:     command: CI= yarn run build
4:15:36 PM:     commandOrigin: ui
4:15:36 PM:     environment:
4:15:36 PM:       - CI
4:15:36 PM:       - MONGO_COLLECTION
4:15:36 PM:       - MONGO_DATABASE
4:15:36 PM:       - MONGO_URI
4:15:36 PM:     publish: /opt/build/repo/build
4:15:36 PM:     publishOrigin: ui
4:15:36 PM:   functionsDirectory: /opt/build/repo/server/netlify/functions
4:15:37 PM: Caching artifacts
4:15:37 PM: Started saving node modules
4:15:37 PM: Finished saving node modules
4:15:37 PM: Started saving build plugins
4:15:37 PM: Finished saving build plugins
4:15:37 PM: Started saving corepack cache
4:15:37 PM: Finished saving corepack cache
4:15:37 PM: Started saving pip cache
4:15:37 PM: Finished saving pip cache
4:15:37 PM: Started saving emacs cask dependencies
4:15:37 PM: Finished saving emacs cask dependencies
4:15:37 PM: Started saving maven dependencies
4:15:37 PM: Finished saving maven dependencies
4:15:37 PM: Started saving boot dependencies
4:15:37 PM: Finished saving boot dependencies
4:15:37 PM: Started saving rust rustup cache
4:15:37 PM: Finished saving rust rustup cache
4:15:37 PM: Started saving go dependencies
4:15:37 PM: Finished saving go dependencies
4:15:37 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
4:15:37 PM: Failing build: Failed to build site
4:15:37 PM: Finished processing build request in 7.507s

After reading another post I noticed I needed to add β€˜client’ as the base directory in my build settings. The previous command β€˜build’ error is fixed but I’m now getting a dependency installation error. This seems to be because the β€˜mongodb’ dependency is required is the top-level package.json, but it’s instead installed inside the server package.json. Since this is where all my server dependencies are installed, would it be possible to set it to this location?

Here are the new details:

Build settings:
Runtime
Not set

Base directory
client

Build command
CI= yarn run build

Publish directory
client/build

Deploy log visibility
Logs are public

Build status
Active

Error message:

Build log:

12:11:39 PM: build-image version: 5b0ab77c5e46895703390c45e86a907d4b78547b (focal)
12:11:39 PM: buildbot version: 5b0ab77c5e46895703390c45e86a907d4b78547b
12:11:39 PM: Building without cache
12:11:39 PM: Starting to prepare the repo for build
12:11:39 PM: No cached dependencies found. Cloning fresh repo
12:11:39 PM: git clone --filter=blob:none https://github.com/ok-diego/simple-note
12:11:40 PM: Preparing Git Reference refs/heads/main
12:11:40 PM: Parsing package.json dependencies
12:11:41 PM: Different publish path detected, going to use the one specified in the Netlify configuration file: 'client/build' versus 'build' in the Netlify UI
12:11:41 PM: Different functions path detected, going to use the one specified in the Netlify configuration file: 'server/netlify/functions' versus '../server/netlify/functions' in the Netlify UI
12:11:42 PM: Starting build script
12:11:42 PM: Installing dependencies
12:11:42 PM: Python version set to 2.7
12:11:42 PM: Downloading and installing node v16.19.1...
12:11:42 PM: Downloading https://nodejs.org/dist/v16.19.1/node-v16.19.1-linux-x64.tar.xz...
12:11:43 PM: Computing checksum with sha256sum
12:11:43 PM: Checksums matched!
12:11:45 PM: Now using node v16.19.1 (npm v8.19.3)
12:11:45 PM: Enabling Node.js Corepack
12:11:45 PM: Started restoring cached build plugins
12:11:45 PM: Finished restoring cached build plugins
12:11:45 PM: Attempting Ruby version 2.7.2, read from environment
12:11:46 PM: Using Ruby version 2.7.2
12:11:46 PM: Using PHP version 8.0
12:11:46 PM: Started restoring cached corepack dependencies
12:11:46 PM: Finished restoring cached corepack dependencies
12:11:46 PM: Started restoring cached yarn cache
12:11:46 PM: Finished restoring cached yarn cache
12:11:46 PM: No yarn workspaces detected
12:11:46 PM: Started restoring cached node modules
12:11:46 PM: Finished restoring cached node modules
12:11:47 PM: Installing npm packages using Yarn version 1.22.19
12:11:47 PM: yarn install v1.22.19
12:11:47 PM: warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
12:11:47 PM: [1/4] Resolving packages...
12:11:47 PM: [2/4] Fetching packages...
12:11:58 PM: [3/4] Linking dependencies...
12:11:58 PM: warning " > @testing-library/user-event@13.5.0" has unmet peer dependency "@testing-library/dom@>=7.21.4".
12:11:58 PM: warning " > dotenv-webpack@8.0.1" has unmet peer dependency "webpack@^4 || ^5".
12:11:58 PM: warning "react-scripts > eslint-config-react-app > eslint-plugin-flowtype@8.0.3" has unmet peer dependency "@babel/plugin-syntax-flow@^7.14.5".
12:11:58 PM: warning "react-scripts > eslint-config-react-app > eslint-plugin-flowtype@8.0.3" has unmet peer dependency "@babel/plugin-transform-react-jsx@^7.14.9".
12:11:58 PM: warning "react-scripts > react-dev-utils > fork-ts-checker-webpack-plugin@6.5.2" has unmet peer dependency "typescript@>= 2.7".
12:11:58 PM: warning "react-scripts > eslint-config-react-app > @typescript-eslint/eslint-plugin > tsutils@3.21.0" has unmet peer dependency "typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta".
12:11:58 PM: warning " > styled-components@5.3.6" has unmet peer dependency "react-is@>= 16.8.0".
12:12:04 PM: [4/4] Building fresh packages...
12:12:05 PM: Done in 17.94s.
12:12:05 PM: npm packages installed using Yarn
12:12:05 PM: Started restoring cached go cache
12:12:05 PM: Finished restoring cached go cache
12:12:05 PM: go version go1.19.6 linux/amd64
12:12:05 PM: Detected 1 framework(s)
12:12:05 PM: "create-react-app" at version "5.0.1"
12:12:05 PM: Installing missing commands
12:12:05 PM: Verify run directory
12:12:05 PM: Section completed: initializing
12:12:07 PM: ​
12:12:07 PM:   Netlify Build                                                 
12:12:07 PM: ────────────────────────────────────────────────────────────────
12:12:07 PM: ​
12:12:07 PM: ❯ Version
12:12:07 PM:   @netlify/build 29.5.7
12:12:07 PM: ​
12:12:07 PM: ❯ Flags
12:12:07 PM:   baseRelDir: true
12:12:07 PM:   buildId: 63efb5498b79630768e05ef3
12:12:07 PM:   deployId: 63efb5498b79630768e05ef5
12:12:07 PM: ​
12:12:07 PM: ❯ Current directory
12:12:07 PM:   /opt/build/repo/client
12:12:07 PM: ​
12:12:07 PM: ❯ Config file
12:12:07 PM:   No config file was defined: using default values.
12:12:07 PM: ​
12:12:07 PM: ❯ Context
12:12:07 PM:   production
12:12:07 PM: ​
12:12:07 PM:   1. Build command from Netlify app                             
12:12:07 PM: ────────────────────────────────────────────────────────────────
12:12:07 PM: ​
12:12:07 PM: $ CI= yarn run build
12:12:07 PM: yarn run v1.22.19
12:12:07 PM: $ react-scripts build
12:12:08 PM: Creating an optimized production build...
12:12:14 PM: Compiled with warnings.
12:12:14 PM: 
12:12:14 PM: [eslint]
12:12:14 PM: src/components/App.js
12:12:14 PM:   Line 2:8:  'styled' is defined but never used  no-unused-vars
12:12:14 PM:   Line 4:8:  'Header' is defined but never used  no-unused-vars
12:12:14 PM:   Line 6:8:  'Footer' is defined but never used  no-unused-vars
12:12:14 PM: src/components/Chapters/Chapters.js
12:12:14 PM:   Line 26:6:  React Hook useEffect has a missing dependency: 'setSelectedBook'. Either include it or remove the dependency array  react-hooks/exhaustive-deps
12:12:14 PM:   Line 66:7:  'Main' is assigned a value but never used                                                                           no-unused-vars
12:12:14 PM: src/components/Header/Header.js
12:12:14 PM:   Line 41:7:  'Theme' is assigned a value but never used  no-unused-vars
12:12:14 PM: src/components/Home/Home.js
12:12:14 PM:   Line 5:8:  'Chapters' is defined but never used  no-unused-vars
12:12:14 PM: src/components/Quotes/Quotes.js
12:12:14 PM:   Line 2:29:   'useEffect' is defined but never used                                              no-unused-vars
12:12:14 PM:   Line 23:36:  Array.prototype.map() expects a value to be returned at the end of arrow function  array-callback-return
12:12:14 PM:   Line 26:26:  Expected '===' and instead saw '=='                                                eqeqeq
12:12:14 PM:   Line 29:49:  Array.prototype.map() expects a value to be returned at the end of arrow function  array-callback-return
12:12:14 PM:   Line 31:41:  Expected '===' and instead saw '=='                                                eqeqeq
12:12:14 PM:   Line 56:7:   'Main' is assigned a value but never used                                          no-unused-vars
12:12:14 PM:   Line 76:7:   'NavLinkMenu' is assigned a value but never used                                   no-unused-vars
12:12:14 PM: src/components/SimpleContext.js
12:12:14 PM:   Line 2:10:  'useParams' is defined but never used  no-unused-vars
12:12:14 PM: src/components/Titles/Titles.js
12:12:14 PM:   Line 2:17:  'useEffect' is defined but never used                 no-unused-vars
12:12:14 PM:   Line 8:25:  'booksJourney' is assigned a value but never used     no-unused-vars
12:12:14 PM:   Line 8:39:  'setBooksJourney' is assigned a value but never used  no-unused-vars
12:12:14 PM: src/components/ToggleSwitch/ToggleSwitch.js
12:12:14 PM:   Line 4:9:  'switchTheme' is assigned a value but never used  no-unused-vars
12:12:14 PM: Search for the keywords to learn more about each warning.
12:12:14 PM: To ignore, add // eslint-disable-next-line to the line before.
12:12:14 PM: File sizes after gzip:
12:12:14 PM:   69.31 kB  build/static/js/main.2f7bbd23.js
12:12:14 PM: The project was built assuming it is hosted at /.
12:12:14 PM: You can control this with the homepage field in your package.json.
12:12:14 PM: The build folder is ready to be deployed.
12:12:14 PM: You may serve it with a static server:
12:12:14 PM:   yarn global add serve
12:12:14 PM:   serve -s build
12:12:14 PM: Find out more about deployment here:
12:12:14 PM:   https://cra.link/deployment
12:12:14 PM: Done in 7.45s.
12:12:14 PM: ​
12:12:14 PM: (build.command completed in 7.6s)
12:12:14 PM: ​
12:12:14 PM:   2. Functions bundling                                         
12:12:14 PM: ────────────────────────────────────────────────────────────────
12:12:14 PM: ​
12:12:14 PM: Packaging Functions from /opt/build/repo/server/netlify/functions directory:
12:12:14 PM:  - get_books/get_books.js
12:12:14 PM: ​
12:12:15 PM: ​
12:12:15 PM:   Dependencies installation error                               
12:12:15 PM: ────────────────────────────────────────────────────────────────
12:12:15 PM: ​
12:12:15 PM:   Error message
12:12:15 PM:   A Netlify Function failed to require one of its dependencies.
12:12:15 PM:   Please make sure it is present in the site's top-level "package.json".
​
12:12:15 PM:   In file "/opt/build/repo/server/netlify/functions/get_books/get_books.js"
12:12:15 PM:   Cannot find module 'mongodb'
12:12:15 PM:   Require stack:
12:12:15 PM:   - /opt/buildhome/node-deps/node_modules/@netlify/zip-it-and-ship-it/dist/runtimes/node/bundlers/zisi/resolve.js
12:12:15 PM: ​
12:12:15 PM:   Resolved config
12:12:15 PM:   build:
12:12:15 PM:     base: /opt/build/repo/client
12:12:15 PM:     command: CI= yarn run build
12:12:15 PM:     commandOrigin: ui
12:12:15 PM:     environment:
12:12:15 PM:       - CI
12:12:15 PM:       - MONGO_COLLECTION
12:12:15 PM:       - MONGO_DATABASE
12:12:15 PM:       - MONGO_URI
12:12:15 PM:     publish: /opt/build/repo/client/build
12:12:15 PM:     publishOrigin: ui
12:12:15 PM:   functionsDirectory: /opt/build/repo/server/netlify/functions
12:12:15 PM: Caching artifacts
12:12:15 PM: Started saving node modules
12:12:15 PM: Finished saving node modules
12:12:15 PM: Started saving build plugins
12:12:15 PM: Finished saving build plugins
12:12:15 PM: Started saving corepack cache
12:12:15 PM: Finished saving corepack cache
12:12:15 PM: Started saving yarn cache
12:12:19 PM: Finished saving yarn cache
12:12:19 PM: Started saving pip cache
12:12:19 PM: Finished saving pip cache
12:12:19 PM: Started saving emacs cask dependencies
12:12:19 PM: Finished saving emacs cask dependencies
12:12:19 PM: Started saving maven dependencies
12:12:19 PM: Finished saving maven dependencies
12:12:19 PM: Started saving boot dependencies
12:12:19 PM: Finished saving boot dependencies
12:12:19 PM: Started saving rust rustup cache
12:12:19 PM: Finished saving rust rustup cache
12:12:19 PM: Started saving go dependencies
12:12:19 PM: Finished saving go dependencies
12:12:20 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
12:12:20 PM: Failing build: Failed to build site
12:12:20 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
12:12:20 PM: Finished processing build request in 41.422s

Hiya, sorry you are having trouble with your build.

This Support Guide contains a ton of useful debugging tips that can likely help you solve your problem :slight_smile: Additionally, this Support Guide houses all of our resources for debugging build and deploy issues.

We also recommend trying to search the forums with the build error you encountered - it’s likely your question was already asked by someone else!

Hi Sam, thanks for your quick reply and suggestions :slight_smile:

I’ve been reading the resources and other users post about a similar issue. I’ve made progress with the build setup and the website works well in dev mode now, but it seems like I’m still missing something when deploying to production.

What I’ve tried:

  • Created the netlify toml file in the projects root.
  • Added the workspaces in the root’s package.json, with the path to the server and client folder’s package.json.

My folder structure is as follows:

β€” root
	β€” .netlify
	β€” netlify.toml
	β€” package.json

	β€” Client
		β€” build
		β€” package.json

	β€” Server 
		β€” netlify/functions/handlers/handlers.js
		β€” index.js
		β€” package.json

I installed the netlify functions inside the server folder because the index.js file is calling the handlers functions for the api endpoints, and because the Mongodb dependency is installed on this folder’s package.json.

I also added the workspaces in the root’s package.json with the path to the server and client package.json.

The function keeps failing to require the mongodb dependency it needs in order to connect to the mongoDB database.

Please advise a solution, I really love netlify!

toml file:
[build]
base = β€œclient/”
publish = β€œbuild/”

[functions]
directory = β€œβ€¦/server/netlify/functions/get_books”

Error message:

build log:

7:02:39 PM: build-image version: 5b0ab77c5e46895703390c45e86a907d4b78547b (focal)
7:02:39 PM: buildbot version: 5b0ab77c5e46895703390c45e86a907d4b78547b
7:02:39 PM: Fetching cached dependencies
7:02:39 PM: Starting to download cache of 182.5MB
7:02:41 PM: Finished downloading cache in 1.58s
7:02:41 PM: Starting to extract cache
7:02:44 PM: Finished extracting cache in 2.92s
7:02:44 PM: Finished fetching cache in 4.542s
7:02:44 PM: Starting to prepare the repo for build
7:02:44 PM: Preparing Git Reference refs/heads/main
7:02:44 PM: Parsing package.json dependencies
7:02:45 PM: Different publish path detected, going to use the one specified in the Netlify configuration file: 'client/build' versus 'build' in the Netlify UI
7:02:45 PM: Different functions path detected, going to use the one specified in the Netlify configuration file: 'server/netlify/functions/get_books' versus 'netlify/functions/get_books' in the Netlify UI
7:02:46 PM: Starting build script
7:02:46 PM: Installing dependencies
7:02:46 PM: Python version set to 2.7
7:02:46 PM: Started restoring cached Node.js version
7:02:47 PM: Finished restoring cached Node.js version
7:02:47 PM: No .nvmrc file found
7:02:47 PM: v19.6.1 is already installed.
7:02:48 PM: Now using node v19.6.1 (npm v9.4.0)
7:02:48 PM: nvm_ensure_default_set: a version is required
7:02:48 PM: Enabling Node.js Corepack
7:02:48 PM: Started restoring cached build plugins
7:02:48 PM: Finished restoring cached build plugins
7:02:48 PM: Attempting Ruby version 2.7.2, read from environment
7:02:49 PM: Using Ruby version 2.7.2
7:02:49 PM: Using PHP version 8.0
7:02:49 PM: Started restoring cached corepack dependencies
7:02:49 PM: Finished restoring cached corepack dependencies
7:02:49 PM: Started restoring cached yarn cache
7:02:51 PM: Finished restoring cached yarn cache
7:02:51 PM: yarn workspaces detected
7:02:51 PM: Started restoring workspace client node modules
7:02:51 PM: Finished restoring workspace client node modules
7:02:52 PM: Installing npm packages using Yarn version 1.22.19
7:02:52 PM: yarn install v1.22.19
7:02:52 PM: warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
7:02:52 PM: [1/4] Resolving packages...
7:02:52 PM: success Already up-to-date.
7:02:52 PM: Done in 0.31s.
7:02:52 PM: npm packages installed using Yarn
7:02:52 PM: Started restoring cached go cache
7:02:52 PM: Finished restoring cached go cache
7:02:52 PM: go version go1.19.6 linux/amd64
7:02:52 PM: Detected 1 framework(s)
7:02:53 PM: "create-react-app" at version "5.0.1"
7:02:53 PM: Installing missing commands
7:02:53 PM: Verify run directory
7:02:53 PM: Section completed: initializing
7:02:54 PM: ​
7:02:54 PM:   Netlify Build                                                 
7:02:54 PM: ────────────────────────────────────────────────────────────────
7:02:54 PM: ​
7:02:54 PM: ❯ Version
7:02:54 PM:   @netlify/build 29.5.7
7:02:54 PM: ​
7:02:54 PM: ❯ Flags
7:02:54 PM:   baseRelDir: true
7:02:54 PM:   buildId: 63f2b86301a506000898b87f
7:02:54 PM:   deployId: 63f2b86301a506000898b881
7:02:54 PM: ​
7:02:54 PM: ❯ Current directory
7:02:54 PM:   /opt/build/repo/client
7:02:54 PM: ​
7:02:54 PM: ❯ Config file
7:02:54 PM:   /opt/build/repo/netlify.toml
7:02:54 PM: ​
7:02:54 PM: ❯ Context
7:02:54 PM:   production
7:02:54 PM: ​
7:02:54 PM:   1. Build command from Netlify app                             
7:02:54 PM: ────────────────────────────────────────────────────────────────
7:02:54 PM: ​
7:02:54 PM: $ CI= yarn run build
7:02:54 PM: yarn run v1.22.19
7:02:54 PM: $ react-scripts build
7:02:55 PM: Creating an optimized production build...
7:02:58 PM: Compiled with warnings.
7:02:58 PM: 
7:02:58 PM: [eslint]
7:02:58 PM: src/components/App.js
7:02:58 PM:   Line 2:8:  'styled' is defined but never used  no-unused-vars
7:02:58 PM:   Line 4:8:  'Header' is defined but never used  no-unused-vars
7:02:58 PM:   Line 6:8:  'Footer' is defined but never used  no-unused-vars
7:02:58 PM: src/components/Chapters/Chapters.js
7:02:58 PM:   Line 26:6:  React Hook useEffect has a missing dependency: 'setSelectedBook'. Either include it or remove the dependency array  react-hooks/exhaustive-deps
7:02:58 PM:   Line 66:7:  'Main' is assigned a value but never used                                                                           no-unused-vars
7:02:58 PM: src/components/Header/Header.js
7:02:58 PM:   Line 41:7:  'Theme' is assigned a value but never used  no-unused-vars
7:02:58 PM: src/components/Home/Home.js
7:02:58 PM:   Line 5:8:  'Chapters' is defined but never used  no-unused-vars
7:02:58 PM: src/components/Quotes/Quotes.js
7:02:58 PM:   Line 2:29:   'useEffect' is defined but never used                                              no-unused-vars
7:02:58 PM:   Line 23:36:  Array.prototype.map() expects a value to be returned at the end of arrow function  array-callback-return
7:02:58 PM:   Line 26:26:  Expected '===' and instead saw '=='                                                eqeqeq
7:02:58 PM:   Line 29:49:  Array.prototype.map() expects a value to be returned at the end of arrow function  array-callback-return
7:02:58 PM:   Line 31:41:  Expected '===' and instead saw '=='                                                eqeqeq
7:02:58 PM:   Line 56:7:   'Main' is assigned a value but never used                                          no-unused-vars
7:02:58 PM:   Line 76:7:   'NavLinkMenu' is assigned a value but never used                                   no-unused-vars
7:02:58 PM: src/components/SimpleContext.js
7:02:58 PM:   Line 2:10:  'useParams' is defined but never used  no-unused-vars
7:02:58 PM: src/components/Titles/Titles.js
7:02:58 PM:   Line 2:17:  'useEffect' is defined but never used                 no-unused-vars
7:02:58 PM:   Line 8:25:  'booksJourney' is assigned a value but never used     no-unused-vars
7:02:58 PM:   Line 8:39:  'setBooksJourney' is assigned a value but never used  no-unused-vars
7:02:58 PM: src/components/ToggleSwitch/ToggleSwitch.js
7:02:58 PM:   Line 4:9:  'switchTheme' is assigned a value but never used  no-unused-vars
7:02:58 PM: Search for the keywords to learn more about each warning.
7:02:58 PM: To ignore, add // eslint-disable-next-line to the line before.
7:02:58 PM: File sizes after gzip:
7:02:58 PM:   69.31 kB  build/static/js/main.2f7bbd23.js
7:02:58 PM: The project was built assuming it is hosted at /.
7:02:58 PM: You can control this with the homepage field in your package.json.
7:02:58 PM: The build folder is ready to be deployed.
7:02:58 PM: You may serve it with a static server:
7:02:58 PM:   yarn global add serve
7:02:58 PM:   serve -s build
7:02:58 PM: Find out more about deployment here:
7:02:58 PM:   https://cra.link/deployment
7:02:58 PM: Done in 4.29s.
7:02:58 PM: ​
7:02:58 PM: (build.command completed in 4.4s)
7:02:58 PM: ​
7:02:58 PM:   2. Functions bundling                                         
7:02:58 PM: ────────────────────────────────────────────────────────────────
7:02:58 PM: ​
7:02:58 PM: Packaging Functions from /opt/build/repo/server/netlify/functions/get_books directory:
7:02:58 PM:  - get_books.js
7:02:58 PM: ​
7:02:58 PM: ​
7:02:58 PM:   Dependencies installation error                               
7:02:58 PM: ────────────────────────────────────────────────────────────────
7:02:58 PM: ​
7:02:58 PM:   Error message
7:02:58 PM:   A Netlify Function failed to require one of its dependencies.
7:02:58 PM:   Please make sure it is present in the site's top-level "package.json".​
7:02:58 PM:   In file "/opt/build/repo/server/netlify/functions/get_books/get_books.js"
7:02:58 PM:   Cannot find module 'mongodb'
7:02:58 PM:   Require stack:
7:02:58 PM:   - /opt/buildhome/node-deps/node_modules/@netlify/zip-it-and-ship-it/dist/runtimes/node/bundlers/zisi/resolve.js
7:02:58 PM: ​
7:02:58 PM:   Resolved config
7:02:58 PM:   build:
7:03:01 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
7:02:58 PM:     base: /opt/build/repo/client
7:02:58 PM:     command: CI= yarn run build
7:02:58 PM:     commandOrigin: ui
7:02:58 PM:     environment:
7:02:58 PM:       - CI
7:02:58 PM:       - MONGO_COLLECTION
7:02:58 PM:       - MONGO_DATABASE
7:02:58 PM:       - MONGO_URI
7:02:58 PM:       - NODE_ENV
7:02:58 PM:       - NODE_VERSION
7:02:58 PM:     publish: /opt/build/repo/client/build
7:02:58 PM:     publishOrigin: config
7:02:58 PM:   functionsDirectory: /opt/build/repo/server/netlify/functions/get_books
7:02:58 PM:   redirects:
7:02:59 PM:     - from: /*      status: 200      to: /index.htmlCaching artifacts
7:02:59 PM: Started saving workspace client node modules
7:02:59 PM: Finished saving workspace client node modules
7:02:59 PM: Started saving build plugins
7:02:59 PM: Finished saving build plugins
7:02:59 PM: Started saving corepack cache
7:02:59 PM: Finished saving corepack cache
7:02:59 PM: Started saving yarn cache
7:03:01 PM: Finished saving yarn cache
7:03:01 PM: Started saving pip cache
7:03:01 PM: Finished saving pip cache
7:03:01 PM: Started saving emacs cask dependencies
7:03:01 PM: Finished saving emacs cask dependencies
7:03:01 PM: Started saving maven dependencies
7:03:01 PM: Finished saving maven dependencies
7:03:01 PM: Started saving boot dependencies
7:03:01 PM: Finished saving boot dependencies
7:03:01 PM: Started saving rust rustup cache
7:03:01 PM: Finished saving rust rustup cache
7:03:01 PM: Started saving go dependencies
7:03:01 PM: Finished saving go dependencies
7:03:01 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
7:03:01 PM: Failing build: Failed to build site
7:03:01 PM: Finished processing build request in 22.095s

Here are a few more details about my issue as suggested.

My project works locally, but not with netlify dev or when deploying the website in production.

I have two async functions in my functions file, called handlers.js file, I’m exporting the functions with module.exports:

And I have another file called index.js file where I setup my api endpoints, and calling the functions by importing them from handlers.js.

On the frontend side I’m using useEffect to call the api endpoints from index.js file:

The documentation explains we can set up our functions by having one functions file and using fetch directly from the frontend to call them. So, is my setup above valid? Or do we need to use the approach explained in the docs only?

I also replaced the location of my netlify functions folder from the server folder to root to see if that would fix the issue, but I keep getting the same problem.

Here are other screenshots:

Site working locally:

When deployed:

Dev log:

I hope this helps clarify things a bit more.

I’ve responded to this in the helpdesk.

1 Like