Home > Net >  Cannot find namespace of installed library
Cannot find namespace of installed library

Time:10-07

I have a small ExpressJS that uses Pino logger. I want to have a class that can be initialized with a Pino logger.

This is the code

import express, { NextFunction, Request, Response } from 'express';
import pino from 'pino-http';
const app = express();
const port = 8080;
app.use(pino());
app.use(async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    const logger: P.Logger = req.log;
    const myClass = new MyClass(logger)
})

I get an error from Typescript Compiler that says "Cannot find namespace P". It seems that req.log is (property) IncomingMessage.log: P.Logger. What am I doing wrong?

package.json

  "dependencies": {
    "express": "^4.17.1",
    "express-async-handler": "^1.1.4",
    "pino-http": "^5.8.0",
    "rxjs": "^5.5.12",
    "ws": "^3.3.3"
  },
  "devDependencies": {
    "@types/express": "^4.17.13",
    "@types/jest": "^27.0.2",
    "@types/node": "^16.10.2",
    "@types/pino-http": "^5.8.0",
    "@types/ws": "^8.2.0",
    "@typescript-eslint/eslint-plugin": "^4.32.0",
    "@typescript-eslint/parser": "^4.32.0",
    "eslint": "^7.32.0",
    "jest": "^27.2.4",
    "nodemon": "^2.0.13",
    "ts-jest": "^27.0.5",
    "ts-node": "^10.2.1",
    "tsc-watch": "^4.5.0",
    "typescript": "^4.4.3"
  }

CodePudding user response:

Logger is not directly exported by pino-logger. If you are trying to specifically type it as Logger that would be from pino or @types/pino. After installing @types/pino:

import { Logger } from 'pino';

// ...

const logger: Logger = req.log;

You can see that @types/pino-http references Logger from pino.

  • Related