"Bundling of edge function failed" Next.js 14 App Router

I can’t deploy my Next.js app to netlify, I always get this edge bundling error.

12:42:25 PM:   error: Uncaught (in promise) Error: Error: Could not find file: file:///opt/build/repo/.netlify/edge-functions/___netlify-edge-handler-src-middleware/server/edge-runtime/vendor/deno.land/std@0.175.0/encoding/base64.ts
12:42:25 PM:         const ret = new Error(getStringFromWasm0(arg0, arg1));
12:42:25 PM:                     ^
12:42:25 PM:       at __wbg_new_d258248ed531ff54 (file:///opt/buildhome/node-deps/node_modules/@netlify/edge-bundler/deno/vendor/deno.land/x/eszip@v0.55.2/eszip_wasm.generated.js:443:19)

Log:

12:41:25 PM: dependencies:
12:41:25 PM: + @auth/core 0.29.0
12:41:25 PM: + @auth/prisma-adapter 1.5.2
12:41:25 PM: + @hookform/resolvers 3.3.4
12:41:25 PM: + @neondatabase/serverless 0.9.4
12:41:25 PM: + @prisma/adapter-neon 5.18.0
12:41:25 PM: + @prisma/client 5.18.0
12:41:25 PM: + @radix-ui/react-aspect-ratio 1.0.3
12:41:25 PM: + @radix-ui/react-avatar 1.0.4
12:41:25 PM: + @radix-ui/react-dropdown-menu 2.0.6
12:41:25 PM: + @radix-ui/react-hover-card 1.0.7
12:41:25 PM: + @radix-ui/react-label 2.0.2
12:41:25 PM: + @radix-ui/react-select 2.0.0
12:41:25 PM: + @radix-ui/react-separator 1.0.3
12:41:25 PM: + @radix-ui/react-slot 1.0.2
12:41:25 PM: + @radix-ui/react-tooltip 1.0.7
12:41:25 PM: + @uploadthing/react 6.4.4
12:41:25 PM: + bcryptjs 2.4.3
12:41:25 PM: + class-variance-authority 0.7.0
12:41:25 PM: + clsx 2.1.0
12:41:25 PM: + lucide-react 0.364.0
12:41:25 PM: + next 14.2.4
12:41:25 PM: + next-auth 5.0.0-beta.16
12:41:25 PM: + next-themes 0.3.0
12:41:25 PM: + react 18.2.0
12:41:25 PM: + react-dom 18.2.0
12:41:25 PM: + react-hook-form 7.51.2
12:41:25 PM: + react-icons 5.2.1
12:41:25 PM: + react-medium-image-zoom 5.2.7
12:41:25 PM: + react-responsive 10.0.0
12:41:25 PM: + react-string-replace 1.1.1
12:41:25 PM: + tailwind-merge 2.2.2
12:41:25 PM: + tailwindcss-animate 1.0.7
12:41:25 PM: + tailwindcss-animated 1.0.1
12:41:25 PM: + ts-sse 0.0.6
12:41:25 PM: + uploadthing 6.9.0
12:41:25 PM: + zod 3.22.4
12:41:25 PM: + zod-form-data 2.0.2
12:41:25 PM: devDependencies:
12:41:25 PM: + @types/bcryptjs 2.4.6
12:41:25 PM: + @types/node 20.11.30
12:41:25 PM: + @types/react 18.2.68
12:41:25 PM: + @types/react-dom 18.2.22
12:41:25 PM: + @typescript-eslint/eslint-plugin 7.3.1
12:41:25 PM: + @typescript-eslint/parser 7.3.1
12:41:25 PM: + autoprefixer 10.4.19
12:41:25 PM: + eslint 8.57.0
12:41:25 PM: + eslint-config-next 14.1.4
12:41:25 PM: + eslint-config-prettier 9.1.0
12:41:25 PM: + eslint-plugin-prettier 5.1.3
12:41:25 PM: + eslint-plugin-react-hooks 4.6.0
12:41:25 PM: + eslint-plugin-react-refresh 0.4.6
12:41:25 PM: + eslint-plugin-simple-import-sort 12.0.0
12:41:25 PM: + only-allow 1.2.1
12:41:25 PM: + postcss 8.4.38
12:41:25 PM: + prettier 3.2.5
12:41:25 PM: + prettier-plugin-tailwindcss 0.5.12
12:41:25 PM: + prisma 5.18.0
12:41:25 PM: + tailwindcss 3.4.1
12:41:25 PM: + typescript 5.4.3
12:41:25 PM: > yap-app@1.0.0 postinstall /opt/build/repo
12:41:25 PM: > prisma generate
12:41:25 PM: Prisma schema loaded from prisma/schema.prisma
12:41:26 PM: ✔ Generated Prisma Client (v5.18.0) to ./node_modules/.pnpm/@prisma+client@5.18.0_prisma@5.18.0/node_modules/@prisma/client in 106ms
12:41:26 PM: Start by importing your Prisma Client (See: http://pris.ly/d/importing-client)
12:41:26 PM: Tip: Interested in query caching in just a few lines of code? Try Accelerate today! https://pris.ly/tip-3-accelerate
12:41:26 PM: Done in 7.9s
12:41:26 PM: npm packages installed using pnpm
12:41:27 PM: Successfully installed dependencies
12:41:27 PM: Starting build script
12:41:28 PM: Detected 1 framework(s)
12:41:28 PM: "next" at version "14.2.4"
12:41:28 PM: Section completed: initializing
12:41:29 PM: ​
12:41:29 PM: Netlify Build                                                 
12:41:29 PM: ────────────────────────────────────────────────────────────────
12:41:29 PM: ​
12:41:29 PM: ❯ Version
12:41:29 PM:   @netlify/build 29.53.0
12:41:29 PM: ​
12:41:29 PM: ❯ Flags
12:41:29 PM:   accountId: 652ebe78fd970b338edf56d2
12:41:29 PM:   baseRelDir: true
12:41:29 PM:   buildId: 66c0d2962e79779ac7e44e13
12:41:29 PM:   deployId: 66c0d2962e79779ac7e44e15
12:41:29 PM: ​
12:41:29 PM: ❯ Current directory
12:41:29 PM:   /opt/build/repo
12:41:29 PM: ​
12:41:29 PM: ❯ Config file
12:41:29 PM:   /opt/build/repo/netlify.toml
12:41:29 PM: ​
12:41:29 PM: ❯ Context
12:41:29 PM:   production
12:41:30 PM: ​
12:41:30 PM: ❯ Using Next.js Runtime - v5.6.0
12:41:31 PM: No Next.js cache to restore
12:41:31 PM: ​
12:41:31 PM: build.command from netlify.toml                               
12:41:31 PM: ────────────────────────────────────────────────────────────────
12:41:31 PM: ​
12:41:31 PM: $ pnpm run build
12:41:32 PM: > yap-app@1.0.0 build /opt/build/repo
12:41:32 PM: > next build
12:41:32 PM: ⚠ No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
12:41:32 PM:   ▲ Next.js 14.2.4
12:41:32 PM:    Creating an optimized production build ...
12:41:50 PM:  ⚠ Compiled with warnings
12:41:50 PM: ./node_modules/.pnpm/@prisma+debug@5.18.0/node_modules/@prisma/debug/dist/index.js
12:41:50 PM: A Node.js API is used (process.stdout at line: 68) which is not supported in the Edge Runtime.
12:41:50 PM: Learn more: https://nextjs.org/docs/api-reference/edge-runtime
12:41:50 PM: Import trace for requested module:
12:41:50 PM: ./node_modules/.pnpm/@prisma+debug@5.18.0/node_modules/@prisma/debug/dist/index.js
12:41:50 PM: ./node_modules/.pnpm/@prisma+driver-adapter-utils@5.18.0/node_modules/@prisma/driver-adapter-utils/dist/index.mjs
12:41:50 PM: ./node_modules/.pnpm/@prisma+adapter-neon@5.18.0_@neondatabase+serverless@0.9.4/node_modules/@prisma/adapter-neon/dist/index.mjs
12:41:50 PM: ./src/lib/database/db.ts
12:41:50 PM: ./src/app/api/auth/[...nextauth]/auth.ts
12:41:50 PM: ./node_modules/.pnpm/@prisma+debug@5.18.0/node_modules/@prisma/debug/dist/index.js
12:41:50 PM: A Node.js API is used (process.stdout at line: 68) which is not supported in the Edge Runtime.
12:41:50 PM: Learn more: https://nextjs.org/docs/api-reference/edge-runtime
12:41:50 PM: Import trace for requested module:
12:41:50 PM: ./node_modules/.pnpm/@prisma+debug@5.18.0/node_modules/@prisma/debug/dist/index.js
12:41:50 PM: ./node_modules/.pnpm/@prisma+driver-adapter-utils@5.18.0/node_modules/@prisma/driver-adapter-utils/dist/index.mjs
12:41:50 PM: ./node_modules/.pnpm/@prisma+adapter-neon@5.18.0_@neondatabase+serverless@0.9.4/node_modules/@prisma/adapter-neon/dist/index.mjs
12:41:50 PM: ./src/lib/database/db.ts
12:41:50 PM: ./src/app/api/auth/[...nextauth]/auth.ts
12:41:50 PM: ./node_modules/.pnpm/bcryptjs@2.4.3/node_modules/bcryptjs/dist/bcrypt.js
12:41:50 PM: A Node.js API is used (process.nextTick at line: 351) which is not supported in the Edge Runtime.
12:41:50 PM: Learn more: https://nextjs.org/docs/api-reference/edge-runtime
12:41:50 PM: Import trace for requested module:
12:41:50 PM: ./node_modules/.pnpm/bcryptjs@2.4.3/node_modules/bcryptjs/dist/bcrypt.js
12:41:50 PM: ./src/app/api/auth/[...nextauth]/auth.ts
12:41:50 PM: ./node_modules/.pnpm/bcryptjs@2.4.3/node_modules/bcryptjs/dist/bcrypt.js
12:41:50 PM: A Node.js API is used (setImmediate at line: 352) which is not supported in the Edge Runtime.
12:41:50 PM: Learn more: https://nextjs.org/docs/api-reference/edge-runtime
12:41:50 PM: Import trace for requested module:
12:41:50 PM: ./node_modules/.pnpm/bcryptjs@2.4.3/node_modules/bcryptjs/dist/bcrypt.js
12:41:50 PM: ./src/app/api/auth/[...nextauth]/auth.ts
12:41:50 PM: ./node_modules/.pnpm/bcryptjs@2.4.3/node_modules/bcryptjs/dist/bcrypt.js
12:41:50 PM: A Node.js API is used (setImmediate at line: 352) which is not supported in the Edge Runtime.
12:41:50 PM: Learn more: https://nextjs.org/docs/api-reference/edge-runtime
12:41:50 PM: Import trace for requested module:
12:41:50 PM: ./node_modules/.pnpm/bcryptjs@2.4.3/node_modules/bcryptjs/dist/bcrypt.js
12:41:50 PM: ./src/app/api/auth/[...nextauth]/auth.ts
12:41:50 PM: ./node_modules/.pnpm/bcryptjs@2.4.3/node_modules/bcryptjs/dist/bcrypt.js
12:41:50 PM: A Node.js API is used (process.nextTick at line: 352) which is not supported in the Edge Runtime.
12:41:50 PM: Learn more: https://nextjs.org/docs/api-reference/edge-runtime
12:41:50 PM: Import trace for requested module:
12:41:50 PM: ./node_modules/.pnpm/bcryptjs@2.4.3/node_modules/bcryptjs/dist/bcrypt.js
12:41:50 PM: ./src/app/api/auth/[...nextauth]/auth.ts
12:41:59 PM:  ✓ Compiled successfully
12:41:59 PM:    Linting and checking validity of types ...
12:42:05 PM:    Collecting page data ...
12:42:09 PM:    Generating static pages (0/15) ...
12:42:10 PM:    Generating static pages (3/15)
12:42:10 PM:    Generating static pages (7/15)
12:42:10 PM:    Generating static pages (11/15)
12:42:10 PM:  ✓ Generating static pages (15/15)
12:42:11 PM:    Finalizing page optimization ...
12:42:11 PM:    Collecting build traces ...
12:42:26 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
12:42:20 PM: Route (app)                               Size     First Load JS
12:42:20 PM: ┌ ○ /_not-found                           281 B          87.4 kB
12:42:20 PM: ├ ƒ /api/auth/[...nextauth]               0 B                0 B
12:42:20 PM: ├ ƒ /api/notifications                    0 B                0 B
12:42:20 PM: ├ ○ /authentication-error                 2.92 kB         108 kB
12:42:20 PM: ├ ƒ /following                            799 B           129 kB
12:42:20 PM: ├ ƒ /home                                 784 B           129 kB
12:42:20 PM: ├ ○ /icon.svg                             0 B                0 B
12:42:20 PM: ├ ○ /login                                4.58 kB         137 kB
12:42:20 PM: ├ ƒ /notifications                        2.54 kB         108 kB
12:42:20 PM: ├ ○ /onboarding                           1.57 kB         137 kB
12:42:20 PM: ├ ƒ /post                                 918 B           126 kB
12:42:20 PM: ├ ○ /register                             4.6 kB          137 kB
12:42:20 PM: ├ ƒ /search                               799 B           129 kB
12:42:20 PM: ├ ƒ /settings                             11.2 kB         170 kB
12:42:20 PM: ├ ƒ /user/[username]                      784 B           129 kB
12:42:20 PM: ├ ƒ /user/[username]/echoes               799 B           129 kB
12:42:20 PM: ├ ƒ /user/[username]/followers            1.07 kB         106 kB
12:42:20 PM: ├ ƒ /user/[username]/following            1.07 kB         106 kB
12:42:20 PM: ├ ƒ /user/[username]/likes                799 B           129 kB
12:42:20 PM: ├ ƒ /user/[username]/media                799 B           129 kB
12:42:20 PM: ├ ƒ /user/[username]/post/[postId]        146 B          87.3 kB
12:42:20 PM: ├ ƒ /user/[username]/post/[postId]/reply  919 B           126 kB
12:42:20 PM: ├ ƒ /user/[username]/yaps                 799 B           129 kB
12:42:20 PM: └ ƒ /users                                1.07 kB         106 kB
12:42:20 PM: + First Load JS shared by all             87.2 kB
12:42:20 PM:   ├ chunks/399-e62d9802a8441546.js        31.5 kB
12:42:20 PM:   ├ chunks/3d4fc10a-601299ff15bccd8c.js   53.6 kB
12:42:20 PM:   └ other shared chunks (total)           2.01 kB
12:42:20 PM: ƒ Middleware                              174 kB
12:42:20 PM: ○  (Static)   prerendered as static content
12:42:20 PM: ƒ  (Dynamic)  server-rendered on demand
12:42:20 PM: ​
12:42:20 PM: (build.command completed in 48.3s)
12:42:20 PM: Next.js cache saved
12:42:21 PM: ​
12:42:21 PM: Functions bundling                                            
12:42:21 PM: ────────────────────────────────────────────────────────────────
12:42:21 PM: ​
12:42:21 PM: Packaging Functions from .netlify/functions-internal directory:
12:42:21 PM:  - ___netlify-server-handler/___netlify-server-handler.mjs
12:42:21 PM: ​
12:42:23 PM: ​
12:42:23 PM: (Functions bundling completed in 1.8s)
12:42:23 PM: ​
12:42:23 PM: Edge Functions bundling                                       
12:42:23 PM: ────────────────────────────────────────────────────────────────
12:42:23 PM: ​
12:42:23 PM: Packaging Edge Functions from .netlify/edge-functions directory:
12:42:23 PM:  - ___netlify-edge-handler-src-middleware
12:42:25 PM: ​
12:42:25 PM: Bundling of edge function failed                              
12:42:25 PM: ────────────────────────────────────────────────────────────────
12:42:25 PM: ​
12:42:25 PM:   Error message
12:42:25 PM:   Command failed with exit code 1: deno run --allow-all --no-config --import-map=/opt/buildhome/node-deps/node_modules/@netlify/edge-bundler/deno/vendor/import_map.json --quiet /opt/buildhome/node-deps/node_modules/@netlify/edge-bundler/deno/bundle.ts {"basePath":"/opt/build/repo","destPath":"/tmp/edge-66c0d2962e79779ac7e44e15/0322b185-d23e-4bc8-95ee-6e3daf1bdbd7.eszip","externals":[],"functions":[{"name":"___netlify-edge-handler-src-middleware","path":"/opt/build/repo/.netlify/edge-functions/___netlify-edge-handler-src-middleware/___netlify-edge-handler-src-middleware.js"}],"importMapData":"{\"imports\":{\"zlib\":\"node:zlib\",\"worker_threads\":\"node:worker_threads\",\"wasi\":\"node:wasi\",\"vm\":\"node:vm\",\"v8\":\"node:v8\",\"util/types\":\"node:util/types\",\"util\":\"node:util\",\"url\":\"node:url\",\"tty\":\"node:tty\",\"trace_events\":\"node:trace_events\",\"tls\":\"node:tls\",\"timers/promises\":\"node:timers/promises\",\"timers\":\"node:timers\",\"sys\":\"node:sys\",\"string_decoder\":\"node:string_decoder\",\"stream/web\":\"node:stream/web\",\"stream/promises\":\"node:stream/promises\",\"stream/consumers\":\"node:stream/consumers\",\"stream\":\"node:stream\",\"repl\":\"node:repl\",\"readline/promises\":\"node:readline/promises\",\"readline\":\"node:readline\",\"querystring\":\"node:querystring\",\"punycode\":\"node:punycode\",\"process\":\"node:process\",\"perf_hooks\":\"node:perf_hooks\",\"path/win32\":\"node:path/win32\",\"path/posix\":\"node:path/posix\",\"path\":\"node:path\",\"os\":\"node:os\",\"net\":\"node:net\",\"module\":\"node:module\",\"inspector\":\"node:inspector\",\"https\":\"node:https\",\"http2\":\"node:http2\",\"http\":\"node:http\",\"fs/promises\":\"node:fs/promises\",\"fs\":\"node:fs\",\"events\":\"node:events\",\"domain\":\"node:domain\",\"dns/promises\":\"node:dns/promises\",\"dns\":\"node:dns\",\"diagnostics_channel\":\"node:diagnostics_channel\",\"dgram\":\"node:dgram\",\"crypto\":\"node:crypto\",\"constants\":\"node:constants\",\"console\":\"node:console\",\"cluster\":\"node:cluster\",\"child_process\":\"node:child_process\",\"buffer\":\"node:buffer\",\"async_hooks\":\"node:async_hooks\",\"assert/strict\":\"node:assert/strict\",\"assert\":\"node:assert\",\"_tls_wrap\":\"node:_tls_wrap\",\"_tls_common\":\"node:_tls_common\",\"_stream_writable\":\"node:_stream_writable\",\"_stream_wrap\":\"node:_stream_wrap\",\"_stream_transform\":\"node:_stream_transform\",\"_stream_readable\":\"node:_stream_readable\",\"_stream_passthrough\":\"node:_stream_passthrough\",\"_stream_duplex\":\"node:_stream_duplex\",\"_http_server\":\"node:_http_server\",\"_http_outgoing\":\"node:_http_outgoing\",\"_http_incoming\":\"node:_http_incoming\",\"_http_common\":\"node:_http_common\",\"_http_client\":\"node:_http_client\",\"_http_agent\":\"node:_http_agent\",\"@netlify/edge-functions\":\"https://edge.netlify.com/v1/index.ts\",\"netlify:edge\":\"https://edge.netlify.com/v1/index.ts?v=legacy\"},\"scopes\":{}}","vendorDirectory":"/tmp/tmp-5803-IDUru3lL6RDr"} (https://ntl.fyi/exit-code-1)
12:42:25 PM:   error: Uncaught (in promise) Error: Error: Could not find file: file:///opt/build/repo/.netlify/edge-functions/___netlify-edge-handler-src-middleware/server/edge-runtime/vendor/deno.land/std@0.175.0/encoding/base64.ts
12:42:25 PM:         const ret = new Error(getStringFromWasm0(arg0, arg1));
12:42:25 PM:                     ^
12:42:25 PM:       at __wbg_new_d258248ed531ff54 (file:///opt/buildhome/node-deps/node_modules/@netlify/edge-bundler/deno/vendor/deno.land/x/eszip@v0.55.2/eszip_wasm.generated.js:443:19)
12:42:25 PM:       at <anonymous> (file:///opt/buildhome/node-deps/node_modules/@netlify/edge-bundler/deno/vendor/deno.land/x/eszip@v0.55.2/eszip_wasm_bg.wasm:1:41320)
12:42:25 PM:       at <anonymous> (file:///opt/buildhome/node-deps/node_modules/@netlify/edge-bundler/deno/vendor/deno.land/x/eszip@v0.55.2/eszip_wasm_bg.wasm:1:1967317)
12:42:25 PM:       at <anonymous> (file:///opt/buildhome/node-deps/node_modules/@netlify/edge-bundler/deno/vendor/deno.land/x/eszip@v0.55.2/eszip_wasm_bg.wasm:1:2501533)
12:42:25 PM:       at __wbg_adapter_40 (file:///opt/buildhome/node-deps/node_modules/@netlify/edge-bundler/deno/vendor/deno.land/x/eszip@v0.55.2/eszip_wasm.generated.js:240:8)
12:42:25 PM:       at real (file:///opt/buildhome/node-deps/node_modules/@netlify/edge-bundler/deno/vendor/deno.land/x/eszip@v0.55.2/eszip_wasm.generated.js:225:14)
12:42:25 PM:       at eventLoopTick (ext:core/01_core.js:182:7)
12:42:25 PM: ​
12:42:25 PM:   Error location
12:42:25 PM:   While bundling edge function
12:42:25 PM: ​
12:42:25 PM:   Resolved config
12:42:25 PM:   build:
12:42:25 PM:     command: pnpm run build
12:42:25 PM:     commandOrigin: config
12:42:25 PM:     environment:
12:42:25 PM:       - AUTH_SECRET
12:42:25 PM:       - AUTH_TRUST_HOST
12:42:25 PM:       - AUTOFOLLOW
12:42:25 PM:       - DATABASE_URL
12:42:25 PM:       - GITHUB_CLIENT_ID
12:42:25 PM:       - GITHUB_SECRET
12:42:25 PM:       - NEXTAUTH_URL
12:42:25 PM:       - NEXT_DISABLE_NETLIFY_EDGE
12:42:25 PM:       - NEXT_PUBLIC_APP_URL
12:42:25 PM:       - NODE_VERSION
12:42:25 PM:       - PNPM_FLAGS
12:42:25 PM:       - UPLOADTHING_APP_ID
12:42:25 PM:       - UPLOADTHING_SECRET
12:42:25 PM:     publish: /opt/build/repo/.next
12:42:25 PM:     publishOrigin: config
12:42:25 PM:   plugins:
12:42:25 PM:     - inputs: {}
12:42:25 PM:       origin: config
12:42:25 PM:       package: '@netlify/plugin-nextjs'
12:42:25 PM:   redirects:
12:42:25 PM:     - from: /_next/image
12:42:25 PM:       query:
12:42:25 PM:         q: ':quality'
12:42:25 PM:         url: ':url'
12:42:25 PM:         w: ':width'
12:42:25 PM:       status: 200
12:42:25 PM:       to: /.netlify/images?url=:url&w=:width&q=:quality
12:42:26 PM:     - from: /_ipx/*
      query:
        q: ':quality'
        url: ':url'
        w: ':width'
      status: 200
      to: /.netlify/images?url=:url&w=:width&q=:quality
  redirectsOrigin: inline
12:42:26 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
12:42:26 PM: Failing build: Failed to build site
12:42:26 PM: Finished processing build request in 1m18.913s

Have you set:

public-hoist-pattern[]=*

in your .npmrc?

No, but adding it didn’t change anything. Same error.

Can you share a reproduction repo?

Thank you! I was able to reproduce this. I’ll ask the devs to check further.

The issue should be resolved by moving your middleware.ts file to the root instead. However, when we tried it, we noticed there’s no middleware being generated by Next.js at all.

While we’d be working on fixing the original use-case of having the middleware in src along with WASM, till then if you could let us know why the middleware is not working at the root, that could help you be unblocked.

It’s already in root. As it says in the next docs, “at the same level as app or pages folder, or inside src if applicable.” I opted to use the src convention, so middleware.ts is in src as it should be.

Ah you’re right, sorry about that. I’ve passed it back to the devs.

This should be fixed with Next.js Runtime v5.7.1.