pinojs / pino-pretty

🌲Basic prettifier for Pino log lines
MIT License
1.22k stars 147 forks source link

TypeScript: "no call signatures" when using ESM #521

Closed joshkel closed 1 month ago

joshkel commented 1 month ago

When configuring a project for ESM ("type": "module" in package.json, and tsconfig.json using "moduleResolution": "node16", "module": "node16"), if I try to import PinoPretty as import PinoPretty from 'pino-pretty' and call it, I get TypeScript error:

src/node.ts:23:48 - error TS2349: This expression is not callable.
  Type 'typeof import("node_modules/pino-pretty/index")' has no call signatures.

23       PinoPretty({ colorize: true })

Found 1 error in src/node.ts:23

If I instead try a named import (import { PinoPretty } from 'pino-pretty'), then TypeScript compiles, but Node.js throws an error at runtime:

import { PinoPretty } from 'pino-pretty';
         ^^^^^^^^^^
SyntaxError: Named export 'PinoPretty' not found. The requested module 'pino-pretty' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'pino-pretty';
const { PinoPretty } = pkg;

I noticed that Are the Types Wrong is reporting a "Missing export =" error for pino-pretty. I believe that this relates to the error I'm seeing.