Can't use couchbase 4.3.1 npm module with Next.js in a Nx managed monorepo

I receive the following error when I try to connect to Couchbase from within the Next.s 14.3.2 application managed by Nx on a Intel Macbook Pro, Node 20.13.1, Couchbase Node 4.3.1

> nx run ScanSafeguard:build

> next build

  ā–² Next.js 14.2.3
  - Environments: .env.local

   Creating an optimized production build ...
 āœ“ Compiled successfully
   Skipping linting
   Checking validity of types ...
   Collecting page data ...
Error: Could not find native build for platform=darwin, arch=x64, runtime=napi, nodeVersion=20.13.1, sslType=openssl3, webpack=true loaded from /Users/bwarner/nxtest/ssg/apps/ScanSafeguard/.next/server/app/api.
    at C (/Users/bwarner/nxtest/ssg/apps/ScanSafeguard/.next/server/app/api/hello/route.js:1:205635)
    at Object.loadPrebuild (/Users/bwarner/nxtest/ssg/apps/ScanSafeguard/.next/server/app/api/hello/route.js:1:209150)
    at 2971 (/Users/bwarner/nxtest/ssg/apps/ScanSafeguard/.next/server/app/api/hello/route.js:1:22276)
    at t (/Users/bwarner/nxtest/ssg/apps/ScanSafeguard/.next/server/webpack-runtime.js:1:128)
    at 5079 (/Users/bwarner/nxtest/ssg/apps/ScanSafeguard/.next/server/app/api/hello/route.js:1:92338)

This does not have when use (Quickstart tutorial) [Developer Portal | Couchbase]

I suspect this because the Monorepo structure is causing Couchbase issues resolving the location of the binary.

Here is the Next.js middleware I tried

import couchbase from 'couchbase';

export async function GET(request: Request) {
    return new Response('COUCHBASE_CONNECTION_STRING is not set!', {
      status: 500,
  if (!process.env.COUCHBASE_USERNAME) {
    return new Response('COUCHBASE_USERNAME is not set!', { status: 500 });
  if (!process.env.COUCHBASE_PASSWORD) {
    return new Response('COUCHBASE_PASSWORD is not set!', { status: 500 });
  if (!process.env.COUCHBASE_BUCKET) {
    return new Response('COUCHBASE_BUCKET is not set!', { status: 500 });
  const cluster = await couchbase.connect(
      username: process.env.COUCHBASE_USERNAME,
      password: process.env.COUCHBASE_PASSWORD,

  const bucket = cluster.bucket(process.env.COUCHBASE_BUCKET);

  return new Response(`Hello, from API!, ${}`);

After quite a bit of digging, I found the issue. Since the Couchbase package is a native module, it must be marked as external in Webpack.

I found a similar problem with Bcrypt Fails in Next.js app/ directory incl. with API Route Handlers #979

Here is what I ended up putting in my next.config.cjs

const nextConfig = {
  webpack: (config) => {
    config.externals = [...config.externals, 'couchbase'];
    return config;

module.exports = nextConfig;

Also, Couchbase needs to be loaded with require, not import.

1 Like