@@ -6,45 +6,61 @@ FROM apify/actor-node:22 AS builder
66# override the default working directory set in the base image
77WORKDIR /home/myuser
88
9- # Copy just package.json and package-lock.json
9+ # Enable pnpm through Corepack. The base image runs as the non-root `myuser`, and the default
10+ # Corepack shim location (/usr/local/bin) is root-owned, so install the shims into a
11+ # user-writable directory and put it on PATH. The exact pnpm version comes from the
12+ # "packageManager" field in package.json.
13+ ENV PATH=/home/myuser/bin:$PATH
14+ ENV COREPACK_ENABLE_DOWNLOAD_PROMPT=0
15+ RUN mkdir -p /home/myuser/bin && corepack enable --install-directory /home/myuser/bin pnpm
16+
17+ # Copy just package.json, the lockfile, the workspace config and patches
1018# to speed up the build using Docker layer cache.
11- COPY --chown=myuser package*.json ./
19+ COPY --chown=myuser package.json pnpm-lock.yaml pnpm-workspace.yaml ./
20+ COPY --chown=myuser patches ./patches
1221
13- # Install all dependencies. Don't audit to speed up the installation.
14- RUN npm install --include=dev --audit=false
22+ # The base image ships a preinstalled node_modules. Remove it first so pnpm's isolated layout
23+ # doesn't get confused by a preexisting non-pnpm node_modules. `npm install` used to overwrite it
24+ # implicitly; pnpm does not.
25+ RUN rm -rf node_modules && pnpm install --frozen-lockfile
1526
1627# Next, copy the source files using the user set
1728# in the base image.
1829COPY --chown=myuser . ./
1930
20- # Install all dependencies and build the project.
21- # Don't audit to speed up the installation.
22- RUN npm run build
31+ # Build the project.
32+ RUN pnpm run build
2333
2434# Build Ghostery blockers for content filtering
25- RUN npm run build:playwright-blockers
35+ RUN pnpm run build:playwright-blockers
2636
2737# Create final image
2838FROM apify/actor-node-playwright-firefox:22-1.55.1
2939
30- # Copy just package.json and package-lock.json
40+ # Enable pnpm through Corepack (see the builder stage for why we use a custom install directory).
41+ ENV PATH=/home/myuser/bin:$PATH
42+ ENV COREPACK_ENABLE_DOWNLOAD_PROMPT=0
43+ RUN mkdir -p /home/myuser/bin && corepack enable --install-directory /home/myuser/bin pnpm
44+
45+ # Copy just package.json, the lockfile, the workspace config and patches
3146# to speed up the build using Docker layer cache.
32- COPY --chown=myuser package* .json ./
47+ COPY --chown=myuser package.json pnpm-lock.yaml pnpm-workspace.yaml ./
3348COPY --chown=myuser policies.json ./
3449COPY --chown=myuser patches ./patches
3550
36- # Install NPM packages, skip development dependencies to keep the image small. Avoid logging too
51+ # Install packages, skip development dependencies to keep the image small. Avoid logging too
3752# much and print the dependency tree for debugging. Optional dependencies are needed - `impit`
38- # ships its native bindings as one.
39- RUN npm --quiet set progress=false \
40- && npm install --omit=dev \
41- && echo "Installed NPM packages:" \
42- && (npm list --omit=dev --all || true) \
53+ # ships its native bindings as one. Remove the base image's preinstalled node_modules first
54+ # (see builder stage).
55+ RUN rm -rf node_modules \
56+ && pnpm install --prod --frozen-lockfile \
57+ && echo "Installed packages:" \
58+ && (pnpm list --prod --depth Infinity || true) \
4359 && echo "Node.js version:" \
4460 && node --version \
45- && echo "NPM version:" \
46- && npm --version \
47- && rm -r ~/.npm
61+ && echo "pnpm version:" \
62+ && pnpm --version \
63+ && pnpm store prune
4864
4965# Copy built JS files from builder image
5066COPY --from=builder --chown=myuser /home/myuser/dist ./dist
@@ -53,7 +69,7 @@ COPY --from=builder --chown=myuser /home/myuser/dist ./dist
5369COPY --from=builder --chown=myuser /home/myuser/blockers ./blockers
5470
5571# Next, copy the remaining files and directories with the source code.
56- # Since we do this after NPM install, quick build will be really fast
72+ # Since we do this after pnpm install, quick build will be really fast
5773# for most source file changes.
5874COPY --chown=myuser . ./
5975
@@ -69,4 +85,4 @@ ENV PLAYWRIGHT_FIREFOX_POLICIES_JSON="/home/myuser/policies.json"
6985ENV NODE_NO_WARNINGS=1
7086
7187# Run the image.
72- CMD ["npm " , "run" , "start:prod" , "--silent" ]
88+ CMD ["pnpm " , "run" , "start:prod" , "--silent" ]
0 commit comments