Function dependencies not found even after explicitly installing in build command

Netlify site name: bantu-workspace-home.netlify.app

When deploying a site with functions, Build complains that the dependencies for functions have not been installed yet, even though the dependencies for the functions have been explicitly installed by the build command.

Build settings (from netlify.toml)
[build]
  publish = "dist"
  command = "npm run build && cd functions && npm i"
  functions = "functions"

[build.environment]
  NPM_VERSION = "7.5.4"

Note that the build command has a section to explicitly install function dependencies (&& cd functions && npm i).

Build log from a fresh build (ie. triggered by "Clear cache and deploy site")
5:23:33 PM: Build ready to start
5:23:35 PM: build-image version: d84c79427e8f83c1ba17bcdd7b3fe38059376b68
5:23:35 PM: build-image tag: v3.6.1
5:23:35 PM: buildbot version: df42d44cc6a0381cf657a671baf049c03981bafe
5:23:35 PM: Building without cache
5:23:35 PM: Starting to prepare the repo for build
5:23:36 PM: No cached dependencies found. Cloning fresh repo
5:23:36 PM: git clone git@[redacted]
5:23:37 PM: Preparing Git Reference merge-requests/[redacted]/head
5:23:39 PM: Different functions path detected, going to use the one specified in the Netlify configuration file: 'functions' versus '' in the Netlify UI
5:23:39 PM: Starting build script
5:23:39 PM: Installing dependencies
5:23:39 PM: Python version set to 2.7
5:23:41 PM: v12.18.0 is already installed.
5:23:42 PM: Now using node v12.18.0 (npm v6.14.4)
5:23:42 PM: Started restoring cached build plugins
5:23:42 PM: Finished restoring cached build plugins
5:23:42 PM: Attempting ruby version 2.7.1, read from environment
5:23:43 PM: Using ruby version 2.7.1
5:23:44 PM: Using PHP version 5.6
5:23:44 PM: Started restoring cached node modules
5:23:44 PM: Finished restoring cached node modules
5:23:44 PM: Found npm version (6.14.4) that doesn't match expected (7.5.4)
Installing npm at version 7.5.4
5:23:55 PM: /opt/buildhome/.nvm/versions/node/v12.18.0/bin/npm -> /opt/buildhome/.nvm/versions/node/v12.18.0/lib/node_modules/npm/bin/npm-cli.js
5:23:55 PM: /opt/buildhome/.nvm/versions/node/v12.18.0/bin/npx -> /opt/buildhome/.nvm/versions/node/v12.18.0/lib/node_modules/npm/bin/npx-cli.js
5:23:55 PM: + npm@7.5.4
5:23:55 PM: added 58 packages from 25 contributors, removed 240 packages and updated 195 packages in 10.219s
5:23:55 PM: NPM installed successfully
5:23:56 PM: Installing NPM modules using NPM version 7.5.4
5:24:03 PM: npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
5:24:04 PM: npm WARN deprecated har-validator@5.1.5: this library is no longer supported
5:24:04 PM: npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
5:24:06 PM: npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
5:24:06 PM: npm WARN deprecated fsevents@1.2.13: fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2.
5:24:07 PM: npm WARN deprecated chokidar@2.1.8: Chokidar 2 will break on node v14+. Upgrade to chokidar 3 with 15x less dependencies.
5:24:07 PM: npm WARN deprecated fsevents@1.2.13: fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2.
5:24:07 PM: npm WARN deprecated chokidar@2.1.8: Chokidar 2 will break on node v14+. Upgrade to chokidar 3 with 15x less dependencies.
5:24:10 PM: npm WARN deprecated eslint-loader@2.2.1: This loader has been deprecated. Please use eslint-webpack-plugin
5:24:12 PM: npm WARN deprecated babel-eslint@10.1.0: babel-eslint is now @babel/eslint-parser. This package will no longer receive updates.
5:24:12 PM: npm WARN deprecated @hapi/topo@3.1.6: This version has been deprecated and is no longer supported or maintained
5:24:12 PM: npm WARN deprecated @hapi/bourne@1.3.2: This version has been deprecated and is no longer supported or maintained
5:24:13 PM: npm WARN deprecated @hapi/address@2.1.4: Moved to 'npm install @sideway/address'
5:24:13 PM: npm WARN deprecated @hapi/hoek@8.5.1: This version has been deprecated and is no longer supported or maintained
5:24:13 PM: npm WARN deprecated @hapi/joi@15.1.1: Switch to 'npm install joi'
5:24:28 PM: > workspace-home@0.1.0 postinstall
5:24:28 PM: > cd functions && npm i
5:24:32 PM: added 35 packages, and audited 37 packages in 4s
5:24:32 PM: 2 packages are looking for funding
5:24:32 PM:   run `npm fund` for details
5:24:32 PM: found 0 vulnerabilities
5:24:32 PM: added 1502 packages, and audited 1503 packages in 36s
5:24:32 PM: 72 packages are looking for funding
5:24:32 PM:   run `npm fund` for details
5:24:32 PM: found 0 vulnerabilities
5:24:32 PM: NPM modules installed
5:24:33 PM: Started restoring cached go cache
5:24:33 PM: Finished restoring cached go cache
5:24:33 PM: go version go1.14.4 linux/amd64
5:24:33 PM: go version go1.14.4 linux/amd64
5:24:33 PM: Installing missing commands
5:24:33 PM: Verify run directory
5:24:35 PM: ​
5:24:35 PM: ────────────────────────────────────────────────────────────────
5:24:35 PM:   Netlify Build                                                 
5:24:35 PM: ────────────────────────────────────────────────────────────────
5:24:35 PM: ​
5:24:35 PM: ❯ Version
5:24:35 PM:   @netlify/build 9.1.3
5:24:35 PM: ​
5:24:35 PM: ❯ Flags
5:24:35 PM:   deployId: 60337815258d66c630220a01
5:24:35 PM:   mode: buildbot
5:24:35 PM: ​
5:24:35 PM: ❯ Current directory
5:24:35 PM:   /opt/build/repo
5:24:35 PM: ​
5:24:35 PM: ❯ Config file
5:24:35 PM:   /opt/build/repo/netlify.toml
5:24:35 PM: ​
5:24:35 PM: ❯ Context
5:24:35 PM:   deploy-preview
5:24:35 PM: ​
5:24:35 PM: ────────────────────────────────────────────────────────────────
5:24:35 PM:   1. build.command from netlify.toml                            
5:24:35 PM: ────────────────────────────────────────────────────────────────
5:24:35 PM: ​
5:24:35 PM: $ npm run build && cd functions && npm i
5:24:36 PM: > workspace-home@0.1.0 build
5:24:36 PM: > vue-cli-service build --modern
5:24:38 PM: -  Building legacy bundle for production...
[redacted]
5:25:32 PM: -  Building modern bundle for production...
[redacted]
5:26:26 PM:  DONE  Build complete. The dist directory is ready to be deployed.
5:26:26 PM:  INFO  Check out deployment instructions at https://cli.vuejs.org/guide/deployment.html
5:26:28 PM: up to date, audited 37 packages in 865ms
5:26:28 PM: 2 packages are looking for funding
5:26:28 PM:   run `npm fund` for details
5:26:28 PM: found 0 vulnerabilities
5:26:28 PM: ​
5:26:28 PM: (build.command completed in 1m 52.2s)
5:26:28 PM: ​
5:26:28 PM: ────────────────────────────────────────────────────────────────
5:26:28 PM:   2. Functions bundling                                         
5:26:28 PM: ────────────────────────────────────────────────────────────────
5:26:28 PM: ​
5:26:28 PM: Packaging Functions from functions directory:
5:26:28 PM:  - download-resource.js
5:26:28 PM:  - get-resource-info.js
5:26:28 PM:  - list-resources.js
5:26:28 PM: ​
5:26:28 PM: ────────────────────────────────────────────────────────────────
5:26:28 PM:   Dependencies installation error                               
5:26:28 PM: ────────────────────────────────────────────────────────────────
5:26:28 PM: ​
5:26:28 PM:   Error message
5:26:28 PM:   A Netlify Function is using "node-fetch" but that dependency has not been installed yet.
5:26:28 PM: ​
5:26:28 PM:   By default, dependencies inside a Netlify Function's "package.json" are not automatically installed.  There are several ways to fix this problem:
5:26:28 PM:     - Removing your Function's "package.json" and adding the dependencies to the project's top-level "package.json" instead. This is the fastest and safest solution.
5:26:28 PM:     - Running "npm install" or "yarn" inside your Netlify Function in your build command.
5:26:28 PM:     - Adding the following plugin to your "netlify.toml":
5:26:28 PM: ​
5:26:28 PM:   [[plugins]]
5:26:28 PM:   package = "@netlify/plugin-functions-install-core"
5:26:28 PM: ​​
5:26:28 PM:   In file "/opt/build/repo/functions/download-resource.js"
5:26:28 PM:   Cannot find module 'node-fetch'
5:26:28 PM:   Require stack:
5:26:28 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/@netlify/zip-it-and-ship-it/src/node_dependencies/resolve.js
5:26:28 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/@netlify/zip-it-and-ship-it/src/node_dependencies/index.js
5:26:28 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/@netlify/zip-it-and-ship-it/src/main.js
5:26:28 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins_core/functions/index.js
5:26:28 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/commands/get.js
5:26:28 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/core/main.js
5:26:28 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/core/bin.js
5:26:28 PM: ​
5:26:28 PM:   Resolved config
5:26:28 PM:   build:
5:26:28 PM:     command: npm run build && cd functions && npm i
5:26:28 PM:     commandOrigin: config
5:26:28 PM:     environment:
5:26:28 PM:       - REVIEW_ID
5:26:28 PM:       - NPM_VERSION
5:26:28 PM:     functions: /opt/build/repo/functions
5:26:28 PM:     publish: /opt/build/repo/dist
5:26:28 PM:   redirects:
5:26:28 PM:     - force: false
5:26:28 PM:       from: /demo
5:26:28 PM:       status: 200
5:26:28 PM:       to: /schedule-demo
5:26:28 PM:     - force: false
5:26:39 PM:       from: /*      status: 404      to: /not-foundCaching artifacts
5:26:39 PM: Started saving node modules
5:26:39 PM: Finished saving node modules
5:26:39 PM: Started saving build plugins
5:26:39 PM: Finished saving build plugins
5:26:39 PM: Started saving pip cache
5:26:39 PM: Finished saving pip cache
5:26:39 PM: Started saving emacs cask dependencies
5:26:39 PM: Finished saving emacs cask dependencies
5:26:39 PM: Started saving maven dependencies
5:26:39 PM: Finished saving maven dependencies
5:26:39 PM: Started saving boot dependencies
5:26:39 PM: Finished saving boot dependencies
5:26:39 PM: Started saving rust rustup cache
5:26:39 PM: Finished saving rust rustup cache
5:26:39 PM: Started saving go dependencies
5:26:39 PM: Finished saving go dependencies
5:26:42 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
5:26:42 PM: Failing build: Failed to build site
5:26:42 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2
5:26:42 PM: Finished processing build request in 3m7.275834703s

I can see from the build log that the function dependencies did install.

  1. Note the postinstall, and the output of two install commands (added 35 packages from functions and added 1502 packages from the main repo).
5:24:28 PM: > workspace-home@0.1.0 postinstall
5:24:28 PM: > cd functions && npm i
5:24:32 PM: added 35 packages, and audited 37 packages in 4s
5:24:32 PM: 2 packages are looking for funding
5:24:32 PM:   run `npm fund` for details
5:24:32 PM: found 0 vulnerabilities
5:24:32 PM: added 1502 packages, and audited 1503 packages in 36s
5:24:32 PM: 72 packages are looking for funding
5:24:32 PM:   run `npm fund` for details
5:24:32 PM: found 0 vulnerabilities
5:24:32 PM: NPM modules installed
  1. And for good measure, an npm install after npm run build completes - this would be part of the build command (npm run build && cd functions && npm i).
5:26:26 PM:  DONE  Build complete. The dist directory is ready to be deployed.
5:26:26 PM:  INFO  Check out deployment instructions at https://cli.vuejs.org/guide/deployment.html
5:26:28 PM: up to date, audited 37 packages in 865ms
5:26:28 PM: 2 packages are looking for funding
5:26:28 PM:   run `npm fund` for details
5:26:28 PM: found 0 vulnerabilities
5:26:28 PM: ​
5:26:28 PM: (build.command completed in 1m 52.2s)

However, Build fails to find the dependencies, as if it was never installed.

5:26:28 PM: ────────────────────────────────────────────────────────────────
5:26:28 PM:   2. Functions bundling                                         
5:26:28 PM: ────────────────────────────────────────────────────────────────
5:26:28 PM: ​
5:26:28 PM: Packaging Functions from functions directory:
5:26:28 PM:  - download-resource.js
5:26:28 PM:  - get-resource-info.js
5:26:28 PM:  - list-resources.js
5:26:28 PM: ​
5:26:28 PM: ────────────────────────────────────────────────────────────────
5:26:28 PM:   Dependencies installation error                               
5:26:28 PM: ────────────────────────────────────────────────────────────────
5:26:28 PM: ​
5:26:28 PM:   Error message
5:26:28 PM:   A Netlify Function is using "node-fetch" but that dependency has not been installed yet.
5:26:28 PM: ​
5:26:28 PM:   By default, dependencies inside a Netlify Function's "package.json" are not automatically installed.  There are several ways to fix this problem:
5:26:28 PM:     - Removing your Function's "package.json" and adding the dependencies to the project's top-level "package.json" instead. This is the fastest and safest solution.
5:26:28 PM:     - Running "npm install" or "yarn" inside your Netlify Function in your build command.
5:26:28 PM:     - Adding the following plugin to your "netlify.toml":
5:26:28 PM: ​
5:26:28 PM:   [[plugins]]
5:26:28 PM:   package = "@netlify/plugin-functions-install-core"
5:26:28 PM: ​​
5:26:28 PM:   In file "/opt/build/repo/functions/download-resource.js"
5:26:28 PM:   Cannot find module 'node-fetch'
5:26:28 PM:   Require stack:
5:26:28 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/@netlify/zip-it-and-ship-it/src/node_dependencies/resolve.js
5:26:28 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/@netlify/zip-it-and-ship-it/src/node_dependencies/index.js
5:26:28 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/@netlify/zip-it-and-ship-it/src/main.js
5:26:28 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins_core/functions/index.js
5:26:28 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/commands/get.js
5:26:28 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/core/main.js
5:26:28 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/core/bin.js

Does anyone have an idea of what is happening? Thanks in advance! :pray:

Hiya @bantu_team and sorry to be slow to get back to you!

The error messages say what we mean: we could not find your function dependencies at bundle time. Without knowing your site to see its configuration, I can’t tell - are your functions OUTSIDE of your site build? In that case you’d need to install their dependencies separately.

It is also possible that you have everything configured well, but don’t have ‘node-fetch’ as a dependency. You would think “but I don’t use it?” however I suspect that one of your dependencies may itself have node-fetch as a dependency - and it may be require'd inside a conditional which is not followed (at least that has been the cause of a prior similar sounding problem).

If that doesn’t guide you to a solution, here are next steps:

  1. check out this repo that shows 3 different ways to handle functions; let me know which one you use: GitHub - netlify/function-deploy-test and how you have things configured.
  2. let me know what site you are working on, so I can check out its configuration.
  3. let me know if you have an explicit node-fetch requirement as a Dependency in your package.json in the functions directory; if not, what happens when you add it (as Dependency, NOT devDependency)?