unable to determine transport target for "pino-pretty"

Viewed 1446

I am trying to use pino library but I am getting error

My code

I created a logger.js file and imported pino from node_module and added transport of pino-pretty.

logger.js

   import pino from "pino";
    const logger = pino({
      transport: {
        target: "pino-pretty",
        options: {
          colorize: true,
        },
      },
    });
    export default logger;

I created a database file and imported pino from logger file and used info function to display my error.

database.js

import mongoose from "mongoose";
import logger from "./logger";
const DB_CONNECTION_STRING =
  process.env.DB_CONNECTION_STRING ||
  "mongodb://localhost:27017/*******";
try {
    await mongoose.connect(DB_CONNECTION_STRING);
    logger.info("Connect to database");
  } catch (e) {
    logger.error(e, "Failed to connect to database. Goodbye");
    process.exit(1);
  }

enter image description here

3 Answers

If you don't have it installed you have to install pino-pretty

npm i pino-pretty

pino-pretty could be missing from your package.json, have you tried adding it as a dependency?

The best way seems to be to use it by piping output to the CLI tool.

So don't import it in your project, and add it to your npm scripts:

  "scripts": {
    (...)
    "dev": "nodemon --watch src/* --exec 'node src/index.js | pino-pretty'",
    "start": "node src/index.js"
  }
Related