[Next.js] Internal error during "Functions bundling" on Build

Site name: friendly-mcnulty-040c07

Build log: Netlify App


It works perfectly on my localhost, but I’m new to netlify and i don’t know if I’m missing something.

Hi @glantucan

Your build log is private (because your repository is) so cannot see the error. Is it the same as posted in https://answers.netlify.com/t/nextjs-deployment-issue/43628?

Hi, coelmay. I tried to follow up on that thread because it seems a similar issue. But somehow my post didn’t get published, maybe because I just had signed in into the forum and I didn’t have yet that permission.

Anyway, here it is the log

12:00:18 AM: Build ready to start
12:00:20 AM: build-image version: fa439ad1ab9393b2c0d449d8d7c033927683f4b0
12:00:20 AM: build-image tag: v4.3.0
12:00:20 AM: buildbot version: 1ed4041128c7c024b4c4b72e2ad30a0740c570a7
12:00:21 AM: Building without cache
12:00:21 AM: Starting to prepare the repo for build
12:00:21 AM: No cached dependencies found. Cloning fresh repo
12:00:21 AM: git clone git@gitlab.com:rockalingua/static-basic-page
12:00:23 AM: Preparing Git Reference refs/heads/main
12:00:24 AM: Parsing package.json dependencies
12:00:25 AM: Starting build script
12:00:25 AM: Installing dependencies
12:00:25 AM: Python version set to 2.7
12:00:25 AM: Downloading and installing node v16.8.0...
12:00:26 AM: Downloading https://nodejs.org/dist/v16.8.0/node-v16.8.0-linux-x64.tar.xz...
12:00:26 AM: Computing checksum with sha256sum
12:00:26 AM: Checksums matched!
12:00:30 AM: Now using node v16.8.0 (npm v7.21.0)
12:00:30 AM: Started restoring cached build plugins
12:00:30 AM: Finished restoring cached build plugins
12:00:30 AM: Attempting ruby version 2.7.2, read from environment
12:00:32 AM: Using ruby version 2.7.2
12:00:32 AM: Using PHP version 8.0
12:00:32 AM: Started restoring cached yarn cache
12:00:32 AM: Finished restoring cached yarn cache
12:00:32 AM: Installing yarn at version 1.22.10
12:00:32 AM: Installing Yarn!
12:00:32 AM: > Downloading tarball...
12:00:32 AM: [1/2]: https://yarnpkg.com/downloads/1.22.10/yarn-v1.22.10.tar.gz --> /tmp/yarn.tar.gz.VOUJrFzIpB
12:00:32 AM:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
12:00:32 AM:                                  Dload  Upload   Total   Spent    Left  Speed
12:00:32 AM:   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
12:00:32 AM: 100    81  100    81    0     0    595      0 --:--:-- --:--:-- --:--:--   595
12:00:32 AM:   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
12:00:32 AM: 100    95  100    95    0     0    264      0 --:--:-- --:--:-- --:--:-- 95000
12:00:32 AM: 100   626  100   626    0     0   1369      0 --:--:-- --:--:-- --:--:--  1369
12:00:32 AM: 100 1215k  100 1215k    0     0  2132k      0 --:--:-- --:--:-- --:--:-- 2132k
12:00:32 AM: [2/2]: https://yarnpkg.com/downloads/1.22.10/yarn-v1.22.10.tar.gz.asc --> /tmp/yarn.tar.gz.VOUJrFzIpB.asc
12:00:32 AM:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
12:00:32 AM:                                  Dload  Upload   Total   Spent    Left  Speed
12:00:33 AM:   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
12:00:33 AM: 100    85  100    85    0     0   1250      0 --:--:-- --:--:-- --:--:--  1250
12:00:33 AM: 100    99  100    99    0     0   1020      0 --:--:-- --:--:-- --:--:--  1020
12:00:33 AM: 100   630  100   630    0     0   5040      0 --:--:-- --:--:-- --:--:--  5040
12:00:33 AM: 100   832  100   832    0     0   6072      0 --:--:-- --:--:-- --:--:--  6072
12:00:33 AM: > Verifying integrity...
12:00:33 AM: gpg: Signature made Fri 02 Oct 2020 11:17:27 AM UTC
12:00:33 AM: gpg:                using RSA key 6D98490C6F1ACDDD448E45954F77679369475BAA
12:00:33 AM: gpg: Good signature from "Yarn Packaging <yarn@dan.cx>" [unknown]
12:00:33 AM: gpg: WARNING: This key is not certified with a trusted signature!
12:00:33 AM: gpg:          There is no indication that the signature belongs to the owner.
12:00:33 AM: Primary key fingerprint: 72EC F46A 56B4 AD39 C907  BBB7 1646 B01B 86E5 0310
12:00:33 AM:      Subkey fingerprint: 6D98 490C 6F1A CDDD 448E  4595 4F77 6793 6947 5BAA
12:00:33 AM: > GPG signature looks good
12:00:33 AM: > Extracting to ~/.yarn...
12:00:33 AM: > Adding to $PATH...
12:00:33 AM: > Successfully installed Yarn 1.22.10! Please open another terminal where the `yarn` command will now be available.
12:00:34 AM: No yarn workspaces detected
12:00:34 AM: Started restoring cached node modules
12:00:34 AM: Finished restoring cached node modules
12:00:34 AM: Installing NPM modules using Yarn version 1.22.10
12:00:35 AM: yarn install v1.22.10
12:00:35 AM: [1/4] Resolving packages...
12:00:35 AM: [2/4] Fetching packages...
12:00:50 AM: info @next/swc-darwin-arm64@11.1.2: The platform "linux" is incompatible with this module.
12:00:50 AM: info "@next/swc-darwin-arm64@11.1.2" is an optional dependency and failed compatibility check. Excluding it from installation.
12:00:50 AM: info @next/swc-darwin-arm64@11.1.2: The CPU architecture "x64" is incompatible with this module.
12:00:50 AM: info @next/swc-darwin-x64@11.1.2: The platform "linux" is incompatible with this module.
12:00:50 AM: info "@next/swc-darwin-x64@11.1.2" is an optional dependency and failed compatibility check. Excluding it from installation.
12:00:50 AM: info @next/swc-win32-x64-msvc@11.1.2: The platform "linux" is incompatible with this module.
12:00:50 AM: info "@next/swc-win32-x64-msvc@11.1.2" is an optional dependency and failed compatibility check. Excluding it from installation.
12:00:50 AM: info fsevents@2.3.2: The platform "linux" is incompatible with this module.
12:00:50 AM: info "fsevents@2.3.2" is an optional dependency and failed compatibility check. Excluding it from installation.
12:00:50 AM: [3/4] Linking dependencies...
12:00:50 AM: warning "next > styled-jsx > @babel/plugin-syntax-jsx@7.14.5" has unmet peer dependency "@babel/core@^7.0.0-0".
12:00:50 AM: warning "eslint-config-next > @typescript-eslint/parser > @typescript-eslint/typescript-estree > 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:00:55 AM: [4/4] Building fresh packages...
12:00:55 AM: Done in 20.52s.
12:00:55 AM: NPM modules installed using Yarn
12:00:55 AM: Started restoring cached go cache
12:00:55 AM: Finished restoring cached go cache
12:00:55 AM: go version go1.16.5 linux/amd64
12:00:55 AM: go version go1.16.5 linux/amd64
12:00:55 AM: Installing missing commands
12:00:55 AM: Verify run directory
12:00:57 AM: ​
12:00:57 AM: ────────────────────────────────────────────────────────────────
12:00:57 AM:   Netlify Build                                                 
12:00:57 AM: ────────────────────────────────────────────────────────────────
12:00:57 AM: ​
12:00:57 AM: ❯ Version
12:00:57 AM:   @netlify/build 18.7.3
12:00:57 AM: ​
12:00:57 AM: ❯ Flags
12:00:57 AM:   baseRelDir: true
12:00:57 AM:   buildId: 6133fa8221e0ddc0b2a0e4b1
12:00:57 AM:   deployId: 6133fa8221e0ddc0b2a0e4b3
12:00:57 AM: ​
12:00:57 AM: ❯ Current directory
12:00:57 AM:   /opt/build/repo
12:00:57 AM: ​
12:00:57 AM: ❯ Config file
12:00:57 AM:   /opt/build/repo/netlify.toml
12:00:57 AM: ​
12:00:57 AM: ❯ Context
12:00:57 AM:   production
12:00:57 AM: ​
12:00:57 AM: ❯ Installing plugins
12:00:57 AM:    - @netlify/plugin-nextjs@3.9.0
12:01:08 AM: ​
12:01:08 AM: ❯ Loading plugins
12:01:08 AM:    - @netlify/plugin-nextjs@3.9.0 from Netlify app
12:01:09 AM: ​
12:01:09 AM: ────────────────────────────────────────────────────────────────
12:01:09 AM:   1. onPreBuild command from @netlify/plugin-nextjs             
12:01:09 AM: ────────────────────────────────────────────────────────────────
12:01:09 AM: ​
12:01:09 AM: Using Next.js 11.1.2
12:01:09 AM: Warning: support for Next.js >=11.1.0 is experimental
12:01:09 AM: info  - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5
12:01:09 AM: No Next.js cache to restore.
12:01:09 AM: ​
12:01:09 AM: (@netlify/plugin-nextjs onPreBuild completed in 752ms)
12:01:09 AM: ​
12:01:09 AM: ────────────────────────────────────────────────────────────────
12:01:09 AM:   2. build.command from netlify.toml                            
12:01:09 AM: ────────────────────────────────────────────────────────────────
12:01:09 AM: ​
12:01:09 AM: $ npm run build
12:01:10 AM: > static-basic-page@0.1.0 build
12:01:10 AM: > next build
12:01:11 AM: info  - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5
12:01:11 AM: warn  - No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
12:01:11 AM: info  - Checking validity of types...
12:01:15 AM: ./pages/videos/index.js
12:01:15 AM: 15:19  Warning: Do not use <img>. Use Image from 'next/image' instead. See https://nextjs.org/docs/messages/no-img-element.  @next/next/no-img-element
12:01:15 AM: 15:19  Warning: img elements must have an alt prop, either with meaningful text, or an empty string for decorative images.  jsx-a11y/alt-text
12:01:15 AM: ./components/Footer.js
12:01:15 AM: 7:19  Warning: Do not use <img>. Use Image from 'next/image' instead. See https://nextjs.org/docs/messages/no-img-element.  @next/next/no-img-element
12:01:15 AM: info  - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/basic-features/eslint#disabling-rules
12:01:15 AM: info  - Creating an optimized production build...
12:01:33 AM: (node:1697) [DEP_WEBPACK_CHUNK_HAS_ENTRY_MODULE] DeprecationWarning: Chunk.hasEntryModule: Use new ChunkGraph API
12:01:33 AM: (Use `node --trace-deprecation ...` to show where the warning was created)
12:01:34 AM: warn  - Compiled with warnings
12:01:34 AM: ./node_modules/knex/lib/migrations/util/import-file.js
12:01:34 AM: Critical dependency: the request of a dependency is an expression
12:01:34 AM: ./node_modules/knex/lib/migrations/util/import-file.js
12:01:34 AM: Critical dependency: the request of a dependency is an expression
12:01:34 AM: ./node_modules/next/dist/server/load-components.js
12:01:34 AM: Critical dependency: the request of a dependency is an expression
12:01:34 AM: ./node_modules/next/dist/server/load-components.js
12:01:34 AM: Critical dependency: the request of a dependency is an expression
12:01:34 AM: ./node_modules/next/dist/server/load-components.js
12:01:34 AM: Critical dependency: the request of a dependency is an expression
12:01:34 AM: ./node_modules/next/dist/server/require.js
12:01:34 AM: Critical dependency: the request of a dependency is an expression
12:01:34 AM: ./node_modules/next/dist/server/require.js
12:01:34 AM: Critical dependency: the request of a dependency is an expression
12:01:34 AM: ./node_modules/next/dist/server/require.js
12:01:34 AM: Critical dependency: the request of a dependency is an expression
12:01:34 AM: info  - Collecting page data...
12:01:36 AM: info  - Generating static pages (0/76)
12:01:38 AM: info  - Generating static pages (19/76)
12:01:39 AM: info  - Generating static pages (38/76)
12:01:39 AM: info  - Generating static pages (57/76)
12:01:41 AM: info  - Generating static pages (76/76)
12:01:41 AM: info  - Finalizing page optimization...
12:01:41 AM: Page                                Size     First Load JS
12:01:41 AM: ┌ ○ / (505 ms)                      3.88 kB        70.5 kB
12:01:41 AM: ├   └ css/61278f179f48db7df8f1.css  765 B
12:01:41 AM: ├   /_app                           0 B            66.7 kB
12:01:41 AM: ├ ○ /404                            194 B          66.9 kB
12:01:41 AM: ├ ● /videos (3735 ms)               1.75 kB        68.4 kB
12:01:41 AM: └ ● /videos/[videoId] (12423 ms)    739 B          67.4 kB
12:01:41 AM:     ├ /videos/359 (1635 ms)
12:01:41 AM:     ├ /videos/79 (1568 ms)
12:01:41 AM:     ├ /videos/210 (1434 ms)
12:01:41 AM:     ├ /videos/209 (1413 ms)
12:01:41 AM:     ├ /videos/266 (1209 ms)
12:01:41 AM:     ├ /videos/265 (1055 ms)
12:01:41 AM:     ├ /videos/197
12:01:41 AM:     └ [+65 more paths]
12:01:41 AM: + First Load JS shared by all       66.7 kB
12:01:41 AM:   ├ chunks/framework.895f06.js      42 kB
12:01:41 AM:   ├ chunks/main.62b8ca.js           23.3 kB
12:01:41 AM:   ├ chunks/pages/_app.a40023.js     555 B
12:01:41 AM:   ├ chunks/webpack.fb7614.js        770 B
12:01:41 AM:   └ css/120f2e2270820d49a21f.css    209 B
12:01:41 AM: λ  (Lambda)  server-side renders at runtime (uses getInitialProps or getServerSideProps)
12:01:41 AM: ○  (Static)  automatically rendered as static HTML (uses no initial props)
12:01:41 AM: ●  (SSG)     automatically generated as static HTML + JSON (uses getStaticProps)
12:01:41 AM:    (ISR)     incremental static regeneration (uses revalidate in getStaticProps)
12:01:41 AM: ​
12:01:41 AM: (build.command completed in 31.8s)
12:01:41 AM: ​
12:01:41 AM: ────────────────────────────────────────────────────────────────
12:01:41 AM:   3. onBuild command from @netlify/plugin-nextjs                
12:01:41 AM: ────────────────────────────────────────────────────────────────
12:01:41 AM: ​
12:01:41 AM: Detected Next.js site. Copying files...
12:01:41 AM: ** Running Next on Netlify package **
12:01:41 AM: 🚀 Next on Netlify 🚀
12:01:41 AM: 🌍️ Copying public folder to /opt/build/repo/out
12:01:41 AM: 💼 Copying static NextJS assets to /opt/build/repo/out
12:01:41 AM: 💫 Setting up API endpoints as Netlify Functions in /opt/build/repo/.netlify/functions-internal
12:01:41 AM: 💫 Setting up pages with getInitialProps as Netlify Functions in /opt/build/repo/.netlify/functions-internal
12:01:41 AM: 💫 Setting up pages with getServerSideProps as Netlify Functions in /opt/build/repo/.netlify/functions-internal
12:01:41 AM: 🔥 Copying pre-rendered pages with getStaticProps and JSON data to /opt/build/repo/out
12:01:41 AM: 💫 Setting up pages with getStaticProps and fallback: true as Netlify Functions in /opt/build/repo/.netlify/functions-internal
12:01:41 AM: 💫 Setting up pages with getStaticProps and revalidation interval as Netlify Functions in /opt/build/repo/.netlify/functions-internal
12:01:41 AM: 🔥 Copying pre-rendered pages without props to /opt/build/repo/out
12:01:41 AM: Building 150 pages
12:01:41 AM: 🔀 Setting up redirects
12:01:41 AM: 🔀 Setting up headers
12:01:41 AM: ✅ Success! All done!
12:01:42 AM: ​
12:01:42 AM: (@netlify/plugin-nextjs onBuild completed in 296ms)
12:01:42 AM: ​
12:01:42 AM: ────────────────────────────────────────────────────────────────
12:01:42 AM:   4. Functions bundling                                         
12:01:42 AM: ────────────────────────────────────────────────────────────────
12:01:42 AM: ​
12:01:42 AM: Packaging Functions from .netlify/functions-internal directory:
12:01:42 AM:  - next_image/next_image.js
12:01:42 AM:  - next_videos/next_videos.js
12:01:42 AM:  - next_videos_videoId/next_videos_videoId.js
12:01:42 AM: ​
12:01:43 AM: ​
12:01:43 AM: ────────────────────────────────────────────────────────────────
12:01:43 AM:   Internal error during "Functions bundling"                    
12:01:43 AM: ────────────────────────────────────────────────────────────────
12:01:43 AM: ​
12:01:43 AM:   Error message
12:01:43 AM:   Error: readdirp: root argument is required. Usage: readdirp(root, options)
12:01:43 AM: ​
12:01:43 AM:   Error location
12:01:43 AM:   During Functions bundling
12:01:43 AM:       at readdirp (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/readdirp/index.js:262:11)
12:01:43 AM:       at /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/readdirp/index.js:276:5
12:01:43 AM:       at new Promise (<anonymous>)
12:01:43 AM:       at Function.readdirpPromise [as promise] (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/readdirp/index.js:274:10)
12:01:43 AM:       at hasFunctionRootFile (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins_core/functions/error.js:119:32)
12:01:43 AM:       at lacksNodeModules (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins_core/functions/error.js:111:12)
12:01:43 AM:       at getModuleNotFoundMessage (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins_core/functions/error.js:50:43)
12:01:43 AM:       at getModuleNotFoundErrorObject (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins_core/functions/error.js:41:25)
12:01:43 AM:       at getModuleNotFoundErrorFromZISI (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins_core/functions/error.js:91:10)
12:01:43 AM:       at getModuleNotFoundError (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins_core/functions/error.js:27:31)
12:01:43 AM: ​
12:01:43 AM:   Resolved config
12:01:43 AM:   build:
12:01:43 AM:     command: npm run build
12:01:43 AM:     commandOrigin: config
12:01:43 AM:     environment:
12:01:43 AM:       - CMS_DB_DB
12:01:43 AM:       - CMS_DB_HOST
12:01:43 AM:       - CMS_DB_PASS
12:01:43 AM:       - CMS_DB_PORT
12:01:43 AM:       - CMS_DB_USER
12:01:43 AM:     publish: /opt/build/repo/out
12:01:43 AM:     publishOrigin: config
12:01:43 AM:   headers:
12:01:50 AM:     - for: /_next/static/chunks/*      values:        cache-control: public, max-age=31536000, immutable  plugins:    - inputs: {}      origin: ui      package: '@netlify/plugin-nextjs'  redirects:    - conditions:        Cookie: __prerender_bypass,__next_preview_data      force: true      from: /_next/data/PrXMXj3rnOOkLS_ruEsUY/videos.json      status: 200      to: /.netlify/functions/next_videos    - conditions:        Cookie: __prerender_bypass,__next_preview_data      force: true      from: /_next/data/PrXMXj3rnOOkLS_ruEsUY/videos/10248.json      status: 200      to: /.netlify/functions/next_videos_videoId    - conditions:        Cookie: __prerender_bypass,__next_preview_data      force: true      from: /_next/data/PrXMXj3rnOOkLS_ruEsUY/videos/127.json      status: 200      to: /.netlify/functions/next_videos_videoId    
[...]                                                                                                 
                                                               
 - conditions:        Cookie: __prerender_bypass,__next_preview_data      force: true      from: /videos/9619      status: 200      to: /.netlify/functions/next_videos_videoId    - force: true      from: /_next/image*      query:        q: ':quality'        url: ':url'        w: ':width'      status: 301      to: /nextimg/:url/:width/:quality    - from: /nextimg/*      status: 200      to: /.netlify/functions/next_imageCaching artifacts
12:01:50 AM: Started saving node modules
12:01:50 AM: Finished saving node modules
12:01:50 AM: Started saving build plugins
12:01:50 AM: Finished saving build plugins
12:01:50 AM: Started saving yarn cache
12:01:53 AM: Finished saving yarn cache
12:01:53 AM: Started saving pip cache
12:01:53 AM: Finished saving pip cache
12:01:53 AM: Started saving emacs cask dependencies
12:01:53 AM: Finished saving emacs cask dependencies
12:01:53 AM: Started saving maven dependencies
12:01:53 AM: Finished saving maven dependencies
12:01:53 AM: Started saving boot dependencies
12:01:53 AM: Finished saving boot dependencies
12:01:53 AM: Started saving rust rustup cache
12:01:53 AM: Finished saving rust rustup cache
12:01:53 AM: Started saving go dependencies
12:01:53 AM: Finished saving go dependencies
12:01:56 AM: Creating deploy upload records
12:01:56 AM: Build failed due to an internal system error: Build script returned non-zero exit code: 4
12:01:57 AM: Failing build: Failed to build site
12:01:57 AM: Failed during stage 'building site': Build script returned non-zero exit code: 4
12:01:57 AM: Finished processing build request in 1m36.569706265s

I cutted down a lot lines similar to this:

- conditions:
        Cookie: __prerender_bypass,__next_preview_data
      force: true
      from: /videos/68158
      status: 200
      to: /.netlify/functions/next_videos_videoId

because I was exceeding the post limit.

Thanks for posting your log @glantucan. Indeed you are receiving the same error as @utdev.

Error: readdirp: root argument is required. Usage: readdirp(root, options)

This is also the same error received by @robhicks (see: Node version not being set using .nvmrc in root of monorepo or as NODE_VERSION envar) though using SvelteKit not Next.js. @hrishikesh has posted some potential solutions you might like to try (though not sure it is entirely solved yet.)

The common element through all three is

readdirp (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/readdirp/index.js:262:11)

Hopefully a Support Engineer will have more information on this issue soon. Hang tight.

Thanks @coelmay. I’m shooting blind here, but how is it the node version on that log trace seems to be 12.16.3?
Shouldn’t be 16.8.0? as per

8:29:07 AM: Now using node v16.8.0 (npm v7.21.0)

I don’t know the full ins-and-outs, but it appears the function bundling is done with a different node.js version. Whether by design, necessity, or otherwise I cannot say.

Funny, I tried to use esbuild for the functions as @hrishikesh suggested on the other thread
netlify.toml

[build]
  command = "npm run build"
  publish = "out"
[functions]
  node_bundler = "esbuild"

And now I get some extra error messages:

9:12:32 AM: ────────────────────────────────────────────────────────────────
9:12:32 AM:   4. Functions bundling                                         
9:12:32 AM: ────────────────────────────────────────────────────────────────
9:12:32 AM: ​
9:12:33 AM: Packaging Functions from .netlify/functions-internal directory:
9:12:33 AM:  - next_image/next_image.js
9:12:33 AM:  - next_videos/next_videos.js
9:12:33 AM:  - next_videos_videoId/next_videos_videoId.js
9:12:33 AM: ​
9:12:33 AM:  > .netlify/functions-internal/next_videos/nextPage/pages/videos.js:253:25: error: Could not resolve "mysql2" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
9:12:33 AM:     253 │ module.exports = require("mysql2");
9:12:33 AM:         ╵                          ~~~~~~~~
9:12:33 AM:  > .netlify/functions-internal/next_videos_videoId/nextPage/pages/videos/[videoId].js:373:25: error: Could not resolve "mysql2" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
9:12:33 AM:     373 │ module.exports = require("mysql2");
9:12:33 AM:         ╵                          ~~~~~~~~
9:12:33 AM:  > .netlify/functions-internal/next_videos/nextPage/pages/videos.js:277:25: error: Could not resolve "oracledb" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
9:12:33 AM:     277 │ module.exports = require("oracledb");
9:12:33 AM:         ╵                          ~~~~~~~~~~
9:12:33 AM:  > .netlify/functions-internal/next_videos_videoId/nextPage/pages/videos/[videoId].js:394:25: error: Could not resolve "oracledb" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
9:12:33 AM:     394 │ module.exports = require("oracledb");
9:12:33 AM:         ╵                          ~~~~~~~~~~
9:12:33 AM:  > .netlify/functions-internal/next_videos/nextPage/pages/videos.js:301:25: error: Could not resolve "pg" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
9:12:33 AM:     301 │ module.exports = require("pg");
9:12:33 AM:         ╵                          ~~~~
9:12:33 AM:  > .netlify/functions-internal/next_videos_videoId/nextPage/pages/videos/[videoId].js:415:25: error: Could not resolve "pg" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
9:12:33 AM:     415 │ module.exports = require("pg");
9:12:33 AM:         ╵                          ~~~~
9:12:33 AM:  > .netlify/functions-internal/next_videos/nextPage/pages/videos.js:309:25: error: Could not resolve "pg-query-stream" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
9:12:33 AM:     309 │ module.exports = require("pg-query-stream");
9:12:33 AM:         ╵                          ~~~~~~~~~~~~~~~~~
9:12:33 AM:  > .netlify/functions-internal/next_videos_videoId/nextPage/pages/videos/[videoId].js:422:25: error: Could not resolve "pg-query-stream" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
9:12:33 AM:     422 │ module.exports = require("pg-query-stream");
9:12:33 AM:         ╵                          ~~~~~~~~~~~~~~~~~
9:12:33 AM:  > .netlify/functions-internal/next_videos/nextPage/pages/videos.js:325:25: error: Could not resolve "sqlite3" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
9:12:33 AM:     325 │ module.exports = require("sqlite3");
9:12:33 AM:         ╵                          ~~~~~~~~~
9:12:33 AM:  > .netlify/functions-internal/next_videos_videoId/nextPage/pages/videos/[videoId].js:436:25: error: Could not resolve "sqlite3" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
9:12:33 AM:     436 │ module.exports = require("sqlite3");
9:12:33 AM:         ╵                          ~~~~~~~~~
9:12:33 AM:  > .netlify/functions-internal/next_videos/nextPage/pages/videos.js:349:25: error: Could not resolve "tedious" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
9:12:33 AM:     349 │ module.exports = require("tedious");
9:12:33 AM:         ╵                          ~~~~~~~~~
9:12:33 AM:  > .netlify/functions-internal/next_videos_videoId/nextPage/pages/videos/[videoId].js:457:25: error: Could not resolve "tedious" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
9:12:33 AM:     457 │ module.exports = require("tedious");
9:12:33 AM:         ╵                          ~~~~~~~~~
9:12:34 AM: ​
9:12:34 AM: ────────────────────────────────────────────────────────────────
9:12:34 AM:   Internal error during "Functions bundling"                    
9:12:34 AM: ────────────────────────────────────────────────────────────────
9:12:34 AM: ​
9:12:34 AM:   Error message
9:12:34 AM:   Error: readdirp: root argument is required. Usage: readdirp(root, options)
9:12:34 AM: ​
9:12:34 AM:   Error location
9:12:34 AM:   During Functions bundling
9:12:34 AM:       at readdirp (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/readdirp/index.js:262:11)
9:12:34 AM:       at /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/readdirp/index.js:276:5
9:12:34 AM:       at new Promise (<anonymous>)
9:12:34 AM:       at Function.readdirpPromise [as promise] (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/readdirp/index.js:274:10)
9:12:34 AM:       at hasFunctionRootFile (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins_core/functions/error.js:119:32)
9:12:34 AM:       at lacksNodeModules (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins_core/functions/error.js:111:12)
9:12:34 AM:       at getModuleNotFoundMessage (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins_core/functions/error.js:50:43)
9:12:34 AM:       at getModuleNotFoundErrorObject (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins_core/functions/error.js:41:25)
9:12:34 AM:       at getModuleNotFoundErrorFromEsbuild (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins_core/functions/error.js:81:10)
9:12:34 AM:       at getModuleNotFoundError (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins_core/functions/error.js:33:34)

Which makes no sense because I already had that issue on my local environment and it seemed to solve when I did what it suggests on my next.config.js:

module.exports = {
  reactStrictMode: true,
  target: "serverless", // Apparently this is required by Netlify
  webpack5: true, 
  webpack: (config, { webpack, isServer}) => {
    config.externals = config.externals.concat([
'mssql', 'mysql2', 'oracle', 'oracledb', 'postgres', 'redshift', 'sqlite3', 'pg', 'pg-query-stream', 'tedious']);
    return config;
  },
}

Ops! I managed to make it work!! :smile:

I don’t remember where did I get this idea from, but added this extra bit ({ knex: 'commonjs knex' } ) to the externals :

config.externals = config.externals.concat(['mssql', 'mysql2', 'oracle', 'oracledb', 'postgres', 'redshift', 'sqlite3', 'pg', 'pg-query-stream', 'tedious', 
      { knex: 'commonjs knex' } ]);

And suddenly the build no longer fails.

It says this though:

9:51:08 AM: ❯ The following Node.js modules use dynamic expressions to include files:
9:51:08 AM:    - sharp
9:51:08 AM:    - knex
9:51:08 AM:    - @ampproject/toolbox-optimizer
9:51:08 AM: ​
9:51:08 AM:   Because files included with dynamic expressions aren't bundled with your serverless functions by default,
  this may result in an error when invoking a function. To resolve this error, you can mark these Node.js
9:51:08 AM:   modules as external in the [functions] section of your `netlify.toml` configuration file:
9:51:08 AM: ​
9:51:08 AM:   [functions]
9:51:08 AM:     external_node_modules = ["sharp", "knex", "@ampproject/toolbox-optimizer"]

I don’t know about ‘sharp’ and ‘@ampproject/toolbox-optimizer’, I didn’t include them explicitly, I guess they are dependencies of some other module.

I don’t know what dynamic expressions exactly mean. I don’t think I’m using them. I just use require() to import knex in a simple module get a database connection:

var knex = require('knex');
var knexConf = {
  client: 'mysql',
  connection: {
    host : process.env.CMS_DB_HOST,
    port: process.env.CMS_DB_PORT,
    user : process.env.CMS_DB_USER,
    password : process.env.CMS_DB_PASS,
    database : process.env.CMS_DB_DB
  }
};
var drupalDB;

export default function getDrupalDB () {
  if (drupalDB) {
    return drupalDB;
  }
  drupalDB = knex(knexConf);
  return drupalDB;
}

and then have other simple modules to perform the queries (needed for my getStaticProps() and getStaticPaths() functions) which have the following structure:

import getDrupalDB from './drupalDb';
var videosCache;
export default async function getAllVideos() {
  if (videosCache) {
    return videosCache;
  } else {
    var drupalDB = getDrupalDB();
    var videosQuery = drupalDB.from('node') ... ; // complex query expression here
    videosCache = await videosQuery;
    return videosCache;
  }
}

Am I doing something wrong or is it the knex module?

Do you maybe recommend some other module to connect to a mariaDb database? I like the knex syntax and features, but I don’t like that it’s giving me so many troubles. It hurts my trust on it seriously. Maybe because I don’t really understand what the problem is.

This is part of some tests we are doing before migrating our rockalingua.com site. We have a lot of customers and we really need this to work flawlessly.

While I’m not sure about the library,

this would mean that you’re trying to load a file from the file-system (not a module) dynamically. It might be in an if/else block or something else. You should try to require the files regardless or whether they would be used or not. You can try something like:

Sorry for my late response, this week has been really busy.

I’m not sure I understand. Those modules may be doing what you say internally, but they are not mine. I imported ‘knex’ explicitly to access and query the database. The other two must be imported by other third party modules I used because I don’t have any explicit import or require pointing to them.

What happens when you add that line to your netlify.toml?