OpenTelemetry JS "Cannot find module '@opentelemetry/...'" in CI
Node could not resolve an @opentelemetry/* package at require/import time. The instrumentation code imports it, but the package is not in the installed node_modules - usually a devDependency-only install or a peer that was never added.
What this error means
A test or app boot fails with "Error: Cannot find module '@opentelemetry/api'" or "@opentelemetry/auto-instrumentations-node", even though it runs on a developer machine where it was installed ad hoc.
Error: Cannot find module '@opentelemetry/api'
Require stack:
- /home/runner/work/app/app/src/tracing.js
at Module._resolveFilename (node:internal/modules/cjs/loader:...)Common causes
The OTel package is not in package.json
@opentelemetry/api is a peer dependency of many instrumentation packages. If it was only present transitively before, a clean CI install may not hoist it, so the direct import fails.
Instrumentation packages installed only locally
The tracing setup was added and tested with an untracked local install; CI runs npm ci from the lockfile, which does not include it.
How to fix it
Add the OTel packages as real dependencies
- List every
@opentelemetry/*package the code imports directly. - Install them so they are recorded in package.json and the lockfile.
- Commit the updated lockfile so
npm ciinstalls them in CI.
npm install @opentelemetry/api @opentelemetry/sdk-node \
@opentelemetry/auto-instrumentations-nodeMatch the API version required by instrumentations
The @opentelemetry/api version must satisfy the peer range of your SDK. Pin a compatible version if the install warns about an unmet peer.
npm ls @opentelemetry/apiHow to prevent it
- Declare @opentelemetry/api explicitly; do not rely on transitive resolution.
- Run npm ci locally to reproduce the exact CI install.
- Commit the lockfile whenever you add instrumentation packages.