Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the current way to import TypeScript transport file? #2147

Open
Serpentarius13 opened this issue Mar 13, 2025 · 0 comments
Open

What is the current way to import TypeScript transport file? #2147

Serpentarius13 opened this issue Mar 13, 2025 · 0 comments

Comments

@Serpentarius13
Copy link

Serpentarius13 commented Mar 13, 2025

As I understand, with v7+ transports using messageFormat method in options, we need to load it from another file as it cannot be serialized normally. I'm trying:

const logger = pino({
  transport: {
    target: "./tr",
  },
});

tr.ts:

import pinoPretty from "pino-pretty";

export default function pinoPrettyTransport(opts) {
  return pinoPretty({
    ...opts,
    colorize: true,
    ignore: "pid,hostname",
    timestampKey: "time",
    messageFormat(log, messageKey, levelLabel) {
      if (log.req)
        return `${log.req.method} ${log.req.url} - ${log.responseTime}ms`;
      return `${log.msg}`;
    },
  });
}

And the error is:

/home/luna/Learn/ts-node/node_modules/.pnpm/[email protected]/node_modules/pino/lib/transport.js:160
      throw new Error(`unable to determine transport target for "${origin}"`)
            ^

Error: unable to determine transport target for "./tr"
    at fixTarget (/home/luna/Learn/ts-node/node_modules/.pnpm/[email protected]/node_modules/pino/lib/transport.js:160:13)
    at transport (/home/luna/Learn/ts-node/node_modules/.pnpm/[email protected]/node_modules/pino/lib/transport.js:130:22)
    at normalizeArgs (/home/luna/Learn/ts-node/node_modules/.pnpm/[email protected]/node_modules/pino/lib/tools.js:316:16)
    at pino (/home/luna/Learn/ts-node/node_modules/.pnpm/[email protected]/node_modules/pino/pino.js:91:28)
    at file:///home/luna/Learn/ts-node/index.ts:2:16
    at ModuleJob.run (node:internal/modules/esm/module_job:273:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:600:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:98:5)

I've found the issue: #1243

But I can't seem to understand what is the way to do it? What am I doing wrong? The docs say compatibility is fine with ts, but it doesn't specify how to do it?

Im using ts-node/esm:

 "type": "module",
 "scripts": {
    "start": "node --loader ts-node/esm ./index.ts",
    "build": "tsc && pnpm sentry:sourcemaps",
{
  "compilerOptions": {
    "lib": ["es2024"],
    "module": "node16",
    "target": "es2024",
    "outDir": "./dist",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "moduleResolution": "node16",
    "sourceMap": true,
    "inlineSources": true,

    // Set `sourceRoot` to  "/" to strip the build path prefix
    // from generated source code references.
    // This improves issue grouping in Sentry.
    "sourceRoot": "/"
  },
  "include": ["index.ts", "tr.ts"]
}

Building works. It also works with cjs. Does it not work with esm?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant