Pino-pretty as a transport target causes `--require` modules to be loaded multiple times
Closed this issue · 5 comments
Hiya, we're currently using pino-pretty as part of our logging in local development. Today we found that, if you import modules via the node --require
flag, pino-pretty causes them to load twice.
Node.js: v20
Pino: v8.16.2
Pino-pretty: v10.2.3
Reproducing
Given the following:
package.json
{
"dependencies": {
"pino": "^8.16.2",
"pino-pretty": "^10.2.3"
}
}
main.js
const pino = require('pino');
const logger = pino({
transport: {
target: 'pino-pretty'
}
});
required.js
console.log('required file loaded', new Date());
Running
Run the following:
node --require ./required.js ./main.js
You will see the console.log
statement from required.js
appear twice with different dates:
required file loaded 2023-11-21T15:50:18.651Z
required file loaded 2023-11-21T15:50:19.137Z
Workaround
This doesn't happen if you pass pino-pretty as a stream to the pino logger:
const pino = require('pino');
const pretty = require('pino-pretty');
const logger = pino({}, pretty());
Because of this I wasn't sure whether this was an issue in pino or pino-pretty. I tried digging into both codebases but was unable to work out how you might be loading the --require
multiple times.
Transports of this type are run in a worker thread. My guess is that the required
module gets propagated to the child thread and invoked again.
Moved it to pino, might even be one for thread-stream
I researched this problem and this is not a bug, but a feature of workers to enable setting up APMs, etc.
If you want to remove the --require
, you can do:
const pino = require('pino');
const logger = pino({
transport: {
target: 'pino-pretty',
worker: { execArgv: [] } // drops all Node specific CLI options
}
});
Amazing, yep this works! Thanks so much for looking into it 🙂
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.