question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dd-trace plugin fails to resolve module due to missing `main` entrypoint on `standalone` builds

See original GitHub issue

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

    Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 20.6.0: Tue Jun 21 20:50:28 PDT 2022; root:xnu-7195.141.32~1/RELEASE_X86_64
    Binaries:
      Node: 16.10.0
      npm: 7.24.0
      Yarn: 1.22.19
      pnpm: N/A
    Relevant packages:
      next: 12.3.1-canary.5
      eslint-config-next: N/A
      react: 17.0.2
      react-dom: 17.0.2

warn  - Latest canary version not detected, detected: "12.3.1-canary.5", newest: "12.3.1".
        Please try the latest canary version (`npm install next@canary`) to confirm the issue still exists before creating a new issue.
        Read more - https://nextjs.org/docs/messages/opening-an-issue

What browser are you using? (if relevant)

N/A

How are you deploying your application? (if relevant)

next start in Docker

Describe the Bug

dd-trace provides a next plugin: https://github.com/DataDog/dd-trace-js/blob/master/docs/API.md#available-plugins

Next is built with output: "standalone" and deployed in a Docker container being started with node server.js.

Startup succeeds, but attempting to load localhost results in a module error:

info  - Loaded env from /app/.env
Listening on port 8080
Error: Cannot find module '/app/node_modules/next/dist/server/next.js'. Please verify that the package.json has a valid"main" entry
    at tryPackage (node:internal/modules/cjs/loader:353:19)
    at Function.Module._findPath (node:internal/modules/cjs/loader:566:18)
    at Module.Hook.Module.require (/app/node_modules/dd-trace/packages/dd-trace/src/ritm.js:109:26)
    at require (node:internal/modules/cjs/helpers:102:18)
    at parseCookie (/app/node_modules/next/dist/server/api-utils/index.js:19:43)
    at NodeNextRequest.get [as cookies] (/app/node_modules/next/dist/server/api-utils/index.js:129:27)
    at NodeNextRequest.get originalRequest [as originalRequest] (/app/node_modules/next/dist/server/base-http/node.js:16:34)
    at NextNodeServer.attachRequestMeta (/app/node_modules/next/dist/server/next-server.js:1299:46)
    at NextNodeServer.handleRequest (/app/node_modules/next/dist/server/base-server.js:132:18)
    at /app/node_modules/next/dist/server/next-server.js:817:20 {
  code: 'MODULE_NOT_FOUND',
  path: '/app/node_modules/next/package.json',
  requestPath: 'next'
}

Reason being that nexts package.json main entry points to a file that is not copied over in standalone mode - but the package.json itself is so dd-trace can’t correctly import the package

Expected Behavior

Expected next page to be served correctly, instrumented with dd-trace

Link to reproduction

https://github.com/lukexor/nextjs-dd-trace-issue

To Reproduce

import tracer from "dd-trace";

tracer.init();
  • Update next.config.js to use output: "standalone"
  • Add a Dockerfile:
# https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile

FROM node:16.10.0-alpine
WORKDIR /app

COPY --chown=node:node next.config.js ./
COPY --chown=node:node public ./public/
COPY --chown=node:node .next/standalone/ ./
COPY --chown=node:node .next/static ./.next/static/

RUN apk update && \
  apk upgrade --no-cache && \
  rm -f /var/cache/apk/*.tar.gz

USER node

EXPOSE 8080
ENV PORT 8080

CMD [ "node", "server.js" ]

HEALTHCHECK --interval=15s --timeout=10s \
  CMD curl -f http://localhost:8080/ || exit 1
  • npm run build
  • docker build -f Dockerfile .
  • docker run -p 80:8080 <image>
  • Open http://localhost in your browser
  • See the following message:
Listening on port 8080
Error: Cannot find module '/app/node_modules/next/dist/server/next.js'. Please verify that the package.json has a valid "main" entry
    at tryPackage (node:internal/modules/cjs/loader:353:19)
    at Function.Module._findPath (node:internal/modules/cjs/loader:566:18)
    at Module.Hook.Module.require (/app/node_modules/dd-trace/packages/dd-trace/src/ritm.js:109:26)
    at require (node:internal/modules/cjs/helpers:102:18)
    at parseCookie (/app/node_modules/next/dist/server/api-utils/index.js:19:43)
    at NodeNextRequest.get [as cookies] (/app/node_modules/next/dist/server/api-utils/index.js:129:27)
    at NodeNextRequest.get originalRequest [as originalRequest] (/app/node_modules/next/dist/server/base-http/node.js:16:34)
    at NextNodeServer.attachRequestMeta (/app/node_modules/next/dist/server/next-server.js:1315:46)
    at NextNodeServer.handleRequest (/app/node_modules/next/dist/server/base-server.js:133:18)
    at /app/node_modules/next/dist/server/next-server.js:833:20 {
  code: 'MODULE_NOT_FOUND',
  path: '/app/node_modules/next/package.json',
  requestPath: 'next'
}

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:9
  • Comments:8

github_iconTop GitHub Comments

1reaction
diboshcommented, Nov 29, 2022

@fdc-viktor-luft you are right! I am sorry for trying to piggyback, was not finding any relevant issue that mentions about the issues with datadog in standalone mode. Hence wanted to put it here.

Thanks for the suggestion - let me try what you suggested. If it does not work, I will create a new issue instead.

1reaction
fdc-viktor-luftcommented, Nov 29, 2022

@dibosh Your issue is not related to this issue and should not have been raised here. If you’re loading the dd-trace code via node --require in standalone mode then you are not making use of the standalone output resolution of NextJS anymore. You have to take care yourself that the used modules is copied into the standalone output before trying to load it.

So your issue will not be solved by solving this issue. As workaround for your issue:

Try adding the following import to your top-level _document.tsx:

import 'dd-trace';

Just by adding this line, you will tell NextJS to include the dependency in the standalone output.

Read more comments on GitHub >

github_iconTop Results From Across the Web

v1.6.3 — ddtrace documentation - Read the Docs
Context serializable which fixes distributed tracing across processes. django: avoid SynchronousOnlyOperation when failing to retrieve user information. Add ...
Read more >
Changelog | skuba - GitHub Pages
ImportModuleError Error: Cannot find module 'dd-trace'. Alternatively, you can configure the Datadog Serverless plugin to bundle these dependencies via ...
Read more >
ChangeLog.openSUSE-Leap-15.3- ...
+ + Fixes: + - fix perf_buffer handling of offline/missing CPUs; ... edge-cases with overlay filesystems have been resolved in + the *partition*...
Read more >
conda-forge
A software package for algebraic, geometric and combinatorial problems... 7za, 9.20, LGPL, X, A file archiver with a high compression ratio. 7zip, 19.00 ......
Read more >
python 3.x - How to get rid of cryptography build error?
cryptography < 3.5: You can skip the rust installation and other related dependencies by adding the line below before apk add commands:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found