Skip to content

Loader is run twice if requireModule is called from CJS dependency #231

@thecopy

Description

@thecopy

Hello,

My loader file is executed twice and it seems it is caused by a CJS dependency which calls createRequire. Importing createRequire in my ESM script does not execute loader script twice.

Is this expected?

Repro:
loader.mjs

console.log('loaded')

index.mjs

import { pino } from 'pino';
const logger = pino({ transport: { target: 'pino-pretty' }});
logger.info("hello")

Execute:

$ npm i pino pino-pretty
$ node --import=./loader.mjs index.mjs
loaded
loaded
[10:59:13.501] INFO (23004): hello

If i remove { transport: { target: 'pino-pretty' }}:

node --import=./loader.mjs index.mjs
loaded
{"level":30,"time":1728896437188,"pid":23035,"hostname":"Eriks-MacBook-Pro.local","msg":"hello"}

Node v20.18.0
Pino createRequire call: https://github.com/pinojs/pino/blob/main/lib/transport.js#L150

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions