solana-labs / solana-web3.js

Solana JavaScript SDK
https://solana-labs.github.io/solana-web3.js
MIT License
2.09k stars 840 forks source link

Cannot find package 'ws' imported from '@solana/rpc-subscriptions-transport-websocket/...' #3100

Closed lithdew closed 1 week ago

lithdew commented 4 weeks ago

Overview

I tried to build and run a Vite project using @solana/web3.js@rc in Docker. I tried the docker images gcr.io/distroless/nodejs22-debian12 and node:22-slim and ran into the same error.

The error:

$ docker run -e DATABASE_URL=$DATABASE_URL -p 3000:3000 web
node:internal/modules/esm/resolve:839
  throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), null);
        ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'ws' imported from /app/server/node_modules/@solana/rpc-subscriptions-transport-websocket/dist/index.node.mjs
    at packageResolve (node:internal/modules/esm/resolve:839:9)
    at moduleResolve (node:internal/modules/esm/resolve:908:18)
    at defaultResolve (node:internal/modules/esm/resolve:1039:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:554:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:523:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:246:38)
    at ModuleJob._link (node:internal/modules/esm/module_job:126:49) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Node.js v22.6.0
error: script "docker:start" exited with code 1

Steps to reproduce

The Dockerfile:

# syntax = docker/dockerfile:1

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

# Install packages needed to build node modules
RUN --mount=type=cache,id=cache,target=/var/cache/apt \
    --mount=type=cache,id=lib,target=/var/lib/apt \
    apt-get update -qq && \
    apt-get install --no-install-recommends -y unzip

LABEL fly_launch_runtime="Bun"

WORKDIR /app

ENV NODE_ENV="production"

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

# Install node modules
COPY --link bun.lockb package.json ./
RUN --mount=type=cache,id=bun,target=/root/.bun \
    bun install --frozen-lockfile

# Copy application code
COPY --link . .

# Build application
RUN bun run build

# Final stage for app image
FROM gcr.io/distroless/nodejs22-debian12 AS run

LABEL fly_launch_runtime="Node.js"

WORKDIR /app

ENV NODE_ENV="production"

COPY --from=build /app/.output ./

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./server/index.mjs"]

Let me know if you need a full reproduction.

Description of bug

'ws' should be resolved inside of @solana/rpc-subscriptions-transport-websocket in a NodeJS environment.

mcintyre94 commented 4 weeks ago

I'm wondering if this could be a bun issue wrt resolving the dependencies. I haven't used bun before and I'm not aware of anyone having used it with web3js yet.

If you could provide a complete repro that'd be really helpful, since I don't think any of us have anything similar yet.

Can you also clarify whether you've tested this with bun outside of Docker? Or are you only using bun in the Dockerfile? It'd be good to narrow this down to bun or the Docker image as a starting point.

steveluscher commented 1 week ago

Do you have peer = false configured in your bunfig?

https://bun.sh/docs/runtime/bunfig#install-peer

ws is a peer dependency; if you have auto-install-peers disabled, this would explain why it's missing.

Feel free to reopen this if that's not the case.

github-actions[bot] commented 11 hours ago

Because there has been no activity on this issue for 7 days since it was closed, it has been automatically locked. Please open a new issue if it requires a follow up.