Span Trace showing instruments in commonjs but not in ECMA script

Hi all,

Mongo version 4.4.25
mongo node package version 4.4.1
Trace Sample showing api request with instrument like http,mongo & redis with commonjs, BUT NOT WITH ECMAScript module support.

I read this doc- ECMAScript module support | APM Node.js Agent Reference [4.x] | Elastic

But no luck. I added below line in package.json also

"start": "node app.js --experimental-loader=elastic-apm-node/loader.mjs server.mjs",

----------app.js -----------

// Import ES modules
import express from 'express';
import './apm.js'; // Assuming apm.js exports itself automatically
import cors from 'cors';
import bodyParser from 'body-parser';
import { MongoClient } from 'mongodb';
const app = express();
import { Redis } from "ioredis"
const redis = new Redis('redis://localhost:6379'); // Connect to Redis

// Redis Event Listeners
redis.on('connect', () => {
console.log('Connected to Redis');
});

redis.on('error', (err) => {
console.error('Redis connection error:', err);
});

const mongoUri = 'mongodb://localhost:27017';
const client = new MongoClient(mongoUri);
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));

client.on('serverClosed', () => {
console.log('MongoDB server closed');
});

client.on('error', (err) => {
console.error('MongoDB error', err);
});

client.on('timeout', () => {
console.error('MongoDB connection timed out');
});

app.get('/users', async (req, res) => {
try {
await client.connect();

  const cachedUsers = await redis.get('users');
  if (cachedUsers) {
    console.log('Returning users from Redis cache');
    return res.json(JSON.parse(cachedUsers));
  }
  
const db = client.db('mydb');
const users = await db.collection('users').find({}).toArray();

console.log('MongoDB query success');
res.json(users);

} catch (err) {
console.error('MongoDB query failed', err);
res.status(500).send('Error retrieving users');
}
});
app.listen(3100, () => {
console.log('Server is running on port 3100');
});

-----------apm.js------------
import apm from 'elastic-apm-node'
apm.start({
serviceName: 'Test app',
secretToken: '',
serverUrl: 'http://elastic:password@127.0.0.1:8200',
environment: 'local',
logLevel: 'debug',
active: true,
instrument: true,
captureBody: 'all',
captureSpanStackTraces: true,
exitSpanMinDuration: '0ms',
spanFramesMinDuration: '-1ms', // Capture all spans
transactionMaxSpans: 1000 // Increase span limit if needed
})
export const APM = apm


My elasticsearch, kibana is working fine.
Below attached image is of commonjs.
I am expecting same output for ECMA script.

i checked the logs.. for commonjs i can see mongodb logs, but the same is not present for ECAM script.
Let me know, if i am missing anything.
Thanks in advance.

From Elastic Agent to APM

Added nodejs

Solved this issue.. just use require instead of import for redis, mongo, etc.
no need to specify anything in package.json file

thank you.