railwayapp / nixpacks

App source + Nix packages + Docker = Image
https://nixpacks.com
MIT License
2.33k stars 222 forks source link

"Error: Cannot find module './pnpm.cjs'" when building Nx monorepo with NIXPACKS_NX_APP_NAME set #1034

Open Nick-Lucas opened 6 months ago

Nick-Lucas commented 6 months ago

Is there an existing issue for this?

Describe the bug

When building an app within an Nx monorepo, with pnpm, pnpx build command fails with the below error

nixpacks build . --name myimagename -e NIXPACKS_NX_APP_NAME=api

╔═══════════════ Nixpacks v1.20.0 ══════════════╗
║ setup      │ nodejs_18, pnpm-8_x              ║
║───────────────────────────────────────────────║
║ install    │ pnpm i --frozen-lockfile         ║
║───────────────────────────────────────────────║
║ build      │ pnpx nx run api:build:production ║
║───────────────────────────────────────────────║
║ start      │ node dist/apps/api/main.js       ║
╚═══════════════════════════════════════════════╝

[+] Building 45.0s (12/14)                                                                                                            docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                                                                  0.0s
 => => transferring dockerfile: 1.14kB                                                                                                                0.0s
 => [internal] load .dockerignore                                                                                                                     0.0s
 => => transferring context: 2B                                                                                                                       0.0s
 => [internal] load metadata for ghcr.io/railwayapp/nixpacks:ubuntu-1702339400                                                                        0.8s
 => [stage-0  1/10] FROM ghcr.io/railwayapp/nixpacks:ubuntu-1702339400@sha256:1a9c1eed040aacf8f898be048210ef2d3366b1228373c4e6818362bb75611b32        0.0s
 => [internal] load build context                                                                                                                    11.1s
 => => transferring context: 1.02GB                                                                                                                  10.7s
 => CACHED [stage-0  2/10] WORKDIR /app/                                                                                                              0.0s
 => CACHED [stage-0  3/10] COPY .nixpacks/nixpkgs-bf744fe90419885eefced41b3e5ae442d732712d.nix .nixpacks/nixpkgs-bf744fe90419885eefced41b3e5ae442d73  0.0s
 => CACHED [stage-0  4/10] RUN nix-env -if .nixpacks/nixpkgs-bf744fe90419885eefced41b3e5ae442d732712d.nix && nix-collect-garbage -d                   0.0s
 => [stage-0  5/10] COPY . /app/.                                                                                                                    10.7s
 => [stage-0  6/10] RUN --mount=type=cache,id=PrhjbjBYoZA-/root/local/share/pnpm/store/v3,target=/root/.local/share/pnpm/store/v3 pnpm i --frozen-l  13.3s
 => [stage-0  7/10] COPY . /app/.                                                                                                                     8.7s 
 => ERROR [stage-0  8/10] RUN --mount=type=cache,id=PrhjbjBYoZA-node_modules/cache,target=/app/node_modules/.cache pnpx nx run api:build:production   0.1s 
------                                                                                                                                                     
 > [stage-0  8/10] RUN --mount=type=cache,id=PrhjbjBYoZA-node_modules/cache,target=/app/node_modules/.cache pnpx nx run api:build:production:              
0.127 node:internal/modules/cjs/loader:1080                                                                                                                
0.127   throw err;                                                                                                                                         
0.127   ^                                                                                                                                                  
0.127                                                                                                                                                      
0.127 Error: Cannot find module './pnpm.cjs'
0.127 Require stack:
0.127 - /nix/store/7nxrwg60is6i75nh3a6f4mk6yzs5203h-pnpm/bin/pnpx
0.127     at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
0.127     at Module._load (node:internal/modules/cjs/loader:922:27)
0.127     at Module.require (node:internal/modules/cjs/loader:1143:19)
0.127     at require (node:internal/modules/cjs/helpers:119:18)
0.127     at Object.<anonymous> (/nix/store/7nxrwg60is6i75nh3a6f4mk6yzs5203h-pnpm/bin/pnpx:5:1)
0.127     at Module._compile (node:internal/modules/cjs/loader:1256:14)
0.127     at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
0.127     at Module.load (node:internal/modules/cjs/loader:1119:32)
0.127     at Module._load (node:internal/modules/cjs/loader:960:12)
0.127     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:86:12) {
0.127   code: 'MODULE_NOT_FOUND',
0.127   requireStack: [ '/nix/store/7nxrwg60is6i75nh3a6f4mk6yzs5203h-pnpm/bin/pnpx' ]
0.127 }
0.127 
0.127 Node.js v18.18.2
------
Dockerfile:24
--------------------
  22 |     # build phase
  23 |     COPY . /app/.
  24 | >>> RUN --mount=type=cache,id=PrhjbjBYoZA-node_modules/cache,target=/app/node_modules/.cache pnpx nx run api:build:production
  25 |     
  26 |     
--------------------
ERROR: failed to solve: process "/bin/bash -ol pipefail -c pnpx nx run api:build:production" did not complete successfully: exit code: 1
Error: Docker build failed

To reproduce

  1. Create Nx monorepo with an app in, using pnpm
  2. Build the app with nixpacks build . --name myimagename -e NIXPACKS_NX_APP_NAME=appname
  3. Build fails with pnpm.cjs error

Expected behavior

Build works

Environment

macOS locally, and Railway.app build env

github-actions[bot] commented 6 months ago

Hello, @Nick-Lucas! Thanks for your submission.

Our team will respond soon. If you need more immediate help, try our Forum or our Discord. Thanks!

Nick-Lucas commented 6 months ago

Workaround: override the build command with --build-cmd="pnpm nx run api:build:production --skip-nx-cache"

pnpx is broken in nixpacks, and also so is the nx cache if you don't include --skip-nx-cache too, the cache may fail the build due to some integrity issue