Fix Next.js build issues when using Bun in Docker containers. This plugin resolves webpack configuration problems that occur when Bun spawns Node.js for webpack compilation in containerized environments.
When building Next.js applications with Bun inside Docker containers, you may encounter:
Module not found: Can't resolve '@/components/...'
errors- TypeScript path alias resolution failures
- Symlink-related issues in Docker's filesystem layer
- Build failures despite working locally
This plugin automatically detects Bun+Docker environments and applies the necessary webpack fixes to ensure successful builds.
npm install next-bun-docker
# or
yarn add next-bun-docker
# or
pnpm add next-bun-docker
# or
bun add next-bun-docker
// next.config.js
const { withBunDocker } = require('next-bun-docker');
module.exports = withBunDocker({
// your existing Next.js config
});
// next.config.js
const { withBunDocker } = require('next-bun-docker');
module.exports = withBunDocker(
{
// your existing Next.js config
},
{
// plugin options
debug: true, // Enable debug logging
aliases: {
// Add custom path aliases
'@custom': './src/custom',
},
}
);
// next.config.mjs
import { withBunDocker } from 'next-bun-docker';
export default withBunDocker({
// your existing Next.js config
});
In your Dockerfile, ensure you set the environment variable:
FROM oven/bun:latest
WORKDIR /app
COPY . .
RUN bun install
# This helps the plugin detect the Docker environment
ENV DOCKER_BUILD=true
RUN bun run build
CMD ["bun", "run", "start"]
The plugin:
- Detects when running in a Bun+Docker environment
- Disables webpack's symlink resolution (
symlinks: false
) - Explicitly configures TypeScript path aliases
- Optimizes module resolution for containerized environments
Option | Type | Default | Description |
---|---|---|---|
aliases |
object |
{} |
Additional path aliases to add to webpack config |
debug |
boolean |
false |
Enable debug logging to see what changes are applied |
detectBun |
function |
Auto-detect | Custom function to determine if in Bun environment |
The plugin automatically configures these common Next.js path aliases:
@
→src/
@/components
→src/components/
@/lib
→src/lib/
@/utils
→src/utils/
@/styles
→src/styles/
@/types
→src/types/
@/hooks
→src/hooks/
@/app
→src/app/
@/pages
→src/pages/
The plugin detects Bun+Docker environments by checking:
DOCKER_BUILD=true
environment variableBUN_RUNTIME=true
environment variable- Process argv containing 'bun'
process.versions.bun
presence
- Ensure you're using the latest version of Bun
- Set
DOCKER_BUILD=true
in your Dockerfile - Enable debug mode to see what's happening:
withBunDocker(config, { debug: true })
withBunDocker(config, {
detectBun: () => {
// Your custom detection logic
return process.env.MY_CUSTOM_ENV === 'true';
}
})
Next.js's webpack configuration makes assumptions about module resolution that break when:
- Bun spawns Node.js for webpack compilation
- Docker's filesystem layer affects symlink resolution
- TypeScript path aliases need explicit resolution in hybrid environments
This plugin bridges that gap until official support lands in Next.js core.
This package is built with Bun (because we believe in it!):
bun install # Install dependencies
bun run build:all # Build with Bun + generate types
bun run test # Run tests (when added)
Issues and PRs welcome! If you encounter build issues with Bun+Docker+Next.js, please open an issue with:
- Your Next.js version
- Your Bun version
- Your Dockerfile
- The error message
MIT
Created by vespo92 for the ESPO Engineering platform and the wider Next.js community.