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
});That's it! The plugin automatically:
- Reads path aliases from your
tsconfig.json - Detects Bun+Docker environments
- Applies the necessary webpack fixes
When troubleshooting, enable debug mode to see exactly what's happening:
// next.config.js
const { withBunDocker } = require('next-bun-docker');
module.exports = withBunDocker(
{
// your existing Next.js config
},
{
debug: true, // Shows detection reason, aliases loaded, and configuration applied
}
);Example debug output:
π§ next-bun-docker: Detected Bun environment
Reason: DOCKER_BUILD=true
Build type: server
π Loaded 3 aliases from tsconfig.json
π 9/12 alias paths exist
β
next-bun-docker: Configuration applied
β’ Symlinks: disabled
β’ Aliases: 12 configured
β’ Extensions: .ts, .tsx, .js, .jsx, .json
// next.config.js
const { withBunDocker } = require('next-bun-docker');
module.exports = withBunDocker(
{
// your existing Next.js config
},
{
debug: true,
aliases: {
'@custom': './src/custom', // Additional aliases (highest priority)
},
loadTsConfig: true, // Auto-load from tsconfig.json (default: true)
tsConfigPath: 'tsconfig.json' // Custom tsconfig path (default: 'tsconfig.json')
}
);// next.config.mjs
import { withBunDocker } from 'next-bun-docker';
export default withBunDocker({
// your existing Next.js config
});In your Dockerfile, set the environment variable for reliable detection:
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 (via env vars, process.argv, or process.versions)
- Reads path aliases from your
tsconfig.jsonautomatically - Disables webpack's symlink resolution (
symlinks: false) - Configures TypeScript path aliases for webpack
- Optimizes module resolution for containerized environments
| Option | Type | Default | Description |
|---|---|---|---|
aliases |
Record<string, string> |
{} |
Additional path aliases (highest priority, overrides tsconfig) |
debug |
boolean |
false |
Enable debug logging with detailed output |
detectBun |
() => boolean |
Auto-detect | Custom function to determine if in Bun environment |
loadTsConfig |
boolean |
true |
Auto-load path aliases from tsconfig.json |
tsConfigPath |
string |
'tsconfig.json' |
Path to tsconfig.json (relative to project root) |
When multiple alias sources exist, they are merged with this priority (highest wins):
- User-provided aliases (via
aliasesoption) - tsconfig.json paths (auto-loaded)
- Default aliases (fallback)
The plugin provides these fallback aliases when not found in tsconfig:
@β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 (in order):
DOCKER_BUILD=trueenvironment variableBUN_RUNTIME=trueenvironment variable- Process argv containing 'bun'
process.versions.bunpresence
-
Enable debug mode to see what's happening:
withBunDocker(config, { debug: true })
-
Check the debug output:
- Is Bun being detected? (look for "Detected Bun environment")
- Are your tsconfig aliases being loaded?
- Do the alias paths exist?
-
Ensure you're using the latest version of Bun
-
Set
DOCKER_BUILD=truein your Dockerfile
withBunDocker(config, {
detectBun: () => {
// Your custom detection logic
return process.env.MY_CUSTOM_ENV === 'true';
}
})// For monorepos or custom setups
withBunDocker(config, {
tsConfigPath: 'tsconfig.app.json'
})withBunDocker(config, {
loadTsConfig: false,
aliases: {
// Manually specify all your aliases
'@': './src',
}
})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
bun run test:watch # Run tests in watch mode
bun run typecheck # Type check without emitting
bun run lint # Check code with Biome
bun run lint:fix # Auto-fix lint issues
bun run check # Run all checks (types + lint + tests)
bun run dev # Build and start test watcherIssues 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
- Debug output (
debug: true)
MIT
Created by vespo92 for the ESPO Engineering platform and the wider Next.js community.