Repository: next.js
Issue: Missing shared workspace dependencies when using standalone output with pnpm
Labels: bug,Output (export/standalone)

### Link to the code that reproduces this issue

https://github.com/saleor/apps

### To Reproduce

- `git clone https://github.com/saleor/apps`
- `pnpm i`
- pick an app, for example `cd ./apps/avatax`
- Add `output: "standalone"` to nextConfig inside `apps/avatax/next.config.js`
- `pnpm i && pnpm run build`
- `node .next/standalone/apps/avatax/server.js` works, so it built successfully
- copy `apps/avatax/.next` somewhere out of this workspace, like `cp -r apps/avatax/.next /home/user/Documents/test`
- inside isolated folder,  `node .next/standalone/apps/avatax/server.js` Errors, missing dependencies. I thought standalone meant it runs standalone?

### Current vs. Expected behavior

```
Error: Cannot find module 'next/dist/server/future/route-modules/pages/vendored/contexts/head-manager-context'
Require stack:
- /home/djkato/Documents/standalone/standalone/node_modules/.pnpm/next@14.1.0_@opentelemetry+api@1.7.0_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/client/script.js
...
code: 'MODULE_NOT_FOUND',
requireStack: [
'/home/djkato/Documents/standalone/standalone/node_modules/.pnpm/next@14.1.0_@opentelemetry+api@1.7.0_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/client/script.js',
```

My actual intention is to run it in a docker image. My dockerfile:
```dockerfile
FROM node:18-alpine AS base

FROM base AS builder
RUN apk add --no-cache libc6-compat
RUN apk update
# Set working directory
WORKDIR /app
RUN npm i -g turbo pnpm
COPY . .

ARG TARGET_APP
# Generate a partial monorepo with a pruned lockfile for a target workspace.
RUN turbo prune "app-$TARGET_APP" --docker 
# Assuming "TARGET_APP" is the name entered in the project's package.json: { name: "TARGET_APP" }

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer

ARG TARGET_APP
RUN apk add --no-cache libc6-compat
RUN apk update
WORKDIR /app

# First install the dependencies (as they change less often)
COPY .gitignore .gitignore
COPY --from=builder /app/out/json/ .
RUN npm i -g turbo pnpm
RUN pnpm i

# Build the project
COPY --from=builder /app/out/full/ .
RUN turbo run build --filter="app-$TARGET_APP"...


FROM base AS runner

ARG TARGET_APP
WORKDIR /app

# Don't run production as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs

COPY --from=installer /app/apps/$TARGET_APP/next.config.js .
COPY --from=installer /app/apps/$TARGET_APP/package.json .

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=installer --chown=nextjs:nodejs /app/apps/$TARGET_APP/.next/standalone ./
COPY --from=installer --chown=nextjs:nodejs /app/apps/$TARGET_APP/.next/static ./apps/$TARGET_APP/.next/static
COPY --from=installer --chown=nextjs:nodejs /app/apps/$TARGET_APP/public ./apps/$TARGET_APP/public
# Without this it won't run
COPY --from=installer --chown=nextjs:nodejs /app/node_modules ./node_modules
WORKDIR /app/apps/$TARGET_APP
CMD node server.js
```

### Provide environment information

```bash
Operating System:
  Platform: linux
  Arch: x64
  Version: #1 ZEN SMP PREEMPT_DYNAMIC Thu, 02 May 2024 17:48:53 +0000
Binaries:
  Node: 20.5.1
  npm: 10.5.1
  Yarn: N/A
  pnpm: 8.15.2
Relevant Packages:
  next: 14.1.0
  eslint-config-next: 13.4.8
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.6
Next.js Config:
  output: N/A


warn  - Latest canary version not detected, detected: "14.1.0", newest: "14.3.0-
canary.57".
        Please try the latest canary version (`npm install next@canary`) to conf
irm the issue still exists before creating a new issue.
        Read more - https://nextjs.org/docs/messages/opening-an-issue
```


### Which area(s) are affected? (Select all that apply)

Output (export/standalone)

### Which stage(s) are affected? (Select all that apply)

next build (local)

### Additional context

Initial research I found #48017, which was similar. I tried adding this to .npmrc:
```
node-linker=hoisted
```
But that failed to even build.

I saw some posts ( #48077 ) that suggested copying the missing resources, but It kept complaining about more and more, till I realized that the whole workspace root node_modules might be missing.
```dockerfile
COPY --from=installer --chown=nextjs:nodejs /app/node_modules/next/dist/server/future/route-modules ./node_modules/next/dist/server/future/route-modules
COPY --from=installer --chown=nextjs:nodejs /app/node_modules/next/dist/compiled/next-server ./node_modules/next/dist/compiled/next-server
COPY --from=installer --chown=nextjs:nodejs /app/node_modules/next/dist/compiled/next-server ./node_modules/next/dist/compiled/next-server
COPY --from=installer --chown=nextjs:nodejs /app/node_modules/react/jsx-runtime ./node_modules/react/jsx-runtime
```
All these, till it got to react. 

Doing this solved it, but now my final image has 1 gig in size...
```dockerfile
COPY --from=installer --chown=nextjs:nodejs /app/node_modules ./node_modules
```