Fixing @libsql/linux-x64-musl Module Not Found in Next.js Standalone Docker Builds

If you see the error Cannot find module '@libsql/linux-x64-musl' when running your Next.js standalone build in a Docker container, you are likely using an Alpine-based image. Because Alpine is a minimal distribution, it does not include all the native binaries required by some Node.js packages.

Fortunately, the solution is straightforward. You just need to add the missing binary as an optional dependency and make sure it is included in your Docker image.


1. Add @libsql/linux-x64-musl as an optional dependency

First, install the @libsql/linux-x64-musl package as an optional dependency, so that the package install still succeeds even when it cannot be based on your cpu architecture and OS.

Use your preferred package manager:

npm

npm install --save-optional @libsql/linux-x64-musl

pnpm

pnpm add --save-optional @libsql/linux-x64-musl

yarn

yarn add --optional @libsql/linux-x64-musl

bun

bun add --optional @libsql/linux-x64-musl

2. Update your Dockerfile

From my tests it seems that the standalone build of Next.js drops any native binaries. Therefore you have to update your Dockerfile to manually copy the native binary into your final image.

Add this line after copying your standalone build:

# Other steps, such as base image setup, installing deps, running `next build`...
 
COPY --from=builder /src/public ./public
 
COPY --from=builder --chown=nextjs:nodejs /src/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /src/.next/static ./.next/static
 
COPY --from=builder --chown=nextjs:nodejs /src/node_modules/@libsql/linux-x64-musl ./node_modules/@libsql/linux-x64-musl
 
# ...

That's it! Your standalone build should now work on Alpine.