elastic / elastic/apm-agent-nodejs

next js output standalone - next:13.2.1, elastic-apm-node: 3.46.0

Open
#3,342 7 comments 0 reactions 0 assignees View on GitHub
agent-nodejs community
Dominant language
JavaScript
Stars
594
Forks
244
Avg merge
1d 8h
Merged PRs (30d)
16

Description

**Describe the bug**

**To Reproduce**
I have a monorepo with several applications. Package manager - pnpm v8.4.0.

my Dockerfile:
```
FROM node:16-alpine AS builder

ARG BUILD_ENV=prod
ARG PROJECT_NAME

WORKDIR /app

RUN apk add --no-cache build-base g++ cairo-dev jpeg-dev pango-dev giflib-dev
RUN npm install -g pnpm@8.4.0

COPY . .

RUN pnpm config set store-dir /app/.pnpm-store
RUN --mount=type=secret,id=npmrc,dst=/root/.npmrc \
--mount=type=tmpfs,target=/app/node_modules/ \
pnpm install && pnpm build:${BUILD_ENV} --filter=${PROJECT_NAME}

# Production image, copy the necessary files and run next
FROM node:16-alpine AS runner
WORKDIR /app

# args needed to build the docker image
ARG PATH_TO_PROJECT
ARG PROJECT_NAME

# service env variables
ENV PATH_TO_PROJECT $PATH_TO_PROJECT
ENV PROJECT_NAME $PROJECT_NAME
ENV RUN_ENV prod
ENV PORT 3000

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --chown=nextjs:nodejs --from=builder /app/${PATH_TO_PROJECT}/.next/standalone .
COPY --chown=nextjs:nodejs --from=builder /app/${PATH_TO_PROJECT}/public ./${PATH_TO_PROJECT}/public
COPY --chown=nextjs:nodejs --from=builder /app/${PATH_TO_PROJECT}/.next/static ./${PATH_TO_PROJECT}/.next/static

USER nextjs

EXPOSE 3000

CMD node NODE_OPTIONS=--require=elastic-apm-node/start-next.js ./${PATH_TO_PROJECT}/server.js
```

I have a elastic-apm-node.js
In normal mode, everything worked - the Dockerfile was a little different. When I turned on output: standalone - apm stopped working.
[You told me](https://github.com/elastic/apm-agent-nodejs/issues/1611#issuecomment-1541023864) to import the api route package to include in the bundle. The package really dragged on and locally it seemed to even start. But when I build in ci - module not found at startup.

I solved this by removing the options for node `NODE_OPTIONS=--require=elastic-apm-node/start-next.js` from the Dockerfile and running apm agent manually in api routes. The agent worked, but now it says that the routes are unknown

**Expected behavior**

Working apm agent with `output: standalone`

**Environment (please complete the following information)**

- OS: macOS 13.3.1 22E261 arm64
- Node.js version: 18.16.0
- "elastic-apm-node": "3.45.0",
- "next": "13.2.1",

**How are you starting the agent? (please tick one of the boxes)**

- [x] Calling `agent.start()` directly (e.g. `require('elastic-apm-node').start(...)`) - `work, but routes are unknown`
- [ ] Requiring `elastic-apm-node/start` from within the source code
- [x] Starting node with `-r elastic-apm-node/start` - `module not found`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.