Next.js Discord

Discord Forum

Datadog APM instrumentation setup - Node module import issue

Answered
Yellow-horned horntail posted this in #help-forum
Open in Discord
Yellow-horned horntailOP
I'm trying to setup instrumentation with Datadog APM.

I turned on the experimental instrumentationHook: true.

My instrumentation.js file:
export async function register() {
  await import('dd-trace/init');
}


Per the docs, importing that module in that file appears to be an ideal way to set it up:
https://docs.datadoghq.com/tracing/trace_collection/automatic_instrumentation/dd_libraries/nodejs/

However, when I start the server, I get a module error:
   â–² Next.js 14.0.4
   - Local:        http://localhost:4000
   - Environments: .env.development
   - Experiments (use at your own risk):
     · instrumentationHook

   Using tsconfig file: ./tsconfig.json
 â—‹ Compiling /instrumentation ...
 ⨯ ../../node_modules/.pnpm/@datadog+native-iast-rewriter@2.2.2/node_modules/@datadog/native-iast-rewriter/js/source-map/index.js:3:0
Module not found: Can't resolve 'fs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
../../node_modules/.pnpm/@datadog+native-iast-rewriter@2.2.2/node_modules/@datadog/native-iast-rewriter/main.js
../../node_modules/.pnpm/dd-trace@5.1.0/node_modules/dd-trace/packages/dd-trace/src/appsec/iast/taint-tracking/rewriter.js
../../node_modules/.pnpm/dd-trace@5.1.0/node_modules/dd-trace/packages/dd-trace/src/appsec/iast/taint-tracking/index.js
../../node_modules/.pnpm/dd-trace@5.1.0/node_modules/dd-trace/packages/dd-trace/src/appsec/iast/index.js
../../node_modules/.pnpm/dd-trace@5.1.0/node_modules/dd-trace/packages/dd-trace/src/proxy.js
../../node_modules/.pnpm/dd-trace@5.1.0/node_modules/dd-trace/packages/dd-trace/src/index.js
../../node_modules/.pnpm/dd-trace@5.1.0/node_modules/dd-trace/packages/dd-trace/index.js
../../node_modules/.pnpm/dd-trace@5.1.0/node_modules/dd-trace/index.js
../../node_modules/.pnpm/dd-trace@5.1.0/node_modules/dd-trace/init.js
./src/instrumentation.js


I tried pnpm i --save fs and I still get the same error.

Any guidance would be appreciated!
Answered by Yellow-horned horntail
I found my answer here. I was needing to scope to only run when process.env.NEXT_RUNTIME === 'nodejs'.
https://github.com/DataDog/dd-trace-js/issues/3457#issuecomment-1669499660
View full answer

1 Reply

Yellow-horned horntailOP
I found my answer here. I was needing to scope to only run when process.env.NEXT_RUNTIME === 'nodejs'.
https://github.com/DataDog/dd-trace-js/issues/3457#issuecomment-1669499660
Answer