oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager ā€“ all in one
https://bun.sh
Other
74.22k stars 2.77k forks source link

Bun able to build Next.js locally, but not in docker. #3904

Open ImBIOS opened 1 year ago

ImBIOS commented 1 year ago

What version of Bun is running?

0.7.1

What platform is your computer?

Darwin 23.0.0 arm64 arm

What steps can reproduce the bug?

āœ… Reproduce Next.js Local Build

  1. build usually, and success.

šŸ”“ Reproduce Next.js in Docker

Prerequisites:

# syntax = docker/dockerfile:1

# Adjust BUN_VERSION as desired
ARG BUN_VERSION=0.7.1
FROM oven/bun:${BUN_VERSION} as base

LABEL fly_launch_runtime="Next.js"

# Next.js app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV="production"

# Throw-away build stage to reduce the size of the final image
FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
    apt-get install -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link bun.lockb package.json ./
RUN bun install

# Copy application code
COPY --link . .

# Build application
RUN bun run build

# Remove development dependencies
RUN rm -rf node_modules && \
    bun install --ci

# Final stage for app image
FROM base

# Copy built application
COPY --from=build /app /app

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD [ "bun", "run", "start" ]
  1. run docker build .
  2. It'll error in line 31 (RUN bun run build)

What is the expected behavior?

It should successfully build locally or docker.

What do you see instead?

 => ERROR [build 5/6] RUN bun run build                                                                                                                                            0.4s
------
 > [build 5/6] RUN bun run build:
0.135 $ next build
0.354  9 | 
0.354 10 | class NotImplementedError extends Error {
0.354 11 |   code;
0.354 12 |   constructor(feature, issue) {
0.354 13 | 
0.354 14 |     super(feature + " is not yet implemented in Bun." + (issue ? " Track the status & thumbs up the issue: https://github.com/oven-sh/bun/issues/" + issue : ""));
0.354        ^
0.354 NotImplementedError: node:diagnostics_channel is not yet implemented in Bun. Track the status & thumbs up the issue: https://github.com/oven-sh/bun/issues/2688
0.354  code: "ERR_NOT_IMPLEMENTED"
0.354 
0.354       at new NotImplementedError (node:diagnostics_channel:14:4)
0.354       at throwNotImplemented (node:diagnostics_channel:2:44)
0.354       at channel (node:diagnostics_channel:21:2)
0.354       at 4822 (/app/node_modules/next/dist/compiled/undici/index.js:2:163873)
0.354       at __nccwpck_require__ (/app/node_modules/next/dist/compiled/undici/index.js:3:13952)
0.354       at 4475 (/app/node_modules/next/dist/compiled/undici/index.js:3:6547)
0.354       at __nccwpck_require__ (/app/node_modules/next/dist/compiled/undici/index.js:3:13952)
0.354       at 9002 (/app/node_modules/next/dist/compiled/undici/index.js:1:27356)
0.354       at __nccwpck_require__ (/app/node_modules/next/dist/compiled/undici/index.js:3:13952)
0.354       at /app/node_modules/next/dist/compiled/undici/index.js:3:14201
0.357 error: script "build" exited with code 1 (SIGHUP)
------
Dockerfile:31
--------------------
  29 |     
  30 |     # Build application
  31 | >>> RUN bun run build
  32 |     
  33 |     # Remove development dependencies
--------------------
ERROR: failed to solve: process "/bin/sh -c bun run build" did not complete successfully: exit code: 1

Additional information

The Dockerfile is generated by flyctl (fly.io's cli). As far as I know, only fly.io supports bun, so I must deploy there.

ImBIOS commented 1 year ago

It tells me to check https://github.com/oven-sh/bun/issues/2688

Electroid commented 1 year ago

Thanks for reporting this issue, Next.js is likely loading node:diagnostics_channel when NODE_ENV=production, so it's unlikely this is a Docker-specific issue.

As you mentioned, we need to implement #2688 for this issue to be fixed.