Search code examples
node.jstypescriptloggingbunyan

Bunyan Logger typescript configuration Errror named logger cannot be named


I am trying to configure my Bunyan Logger for NodeJS project. This configuration was working fine, until I started to convert my project to Typescript.

Here is the configuration function for configuring my loggers in the file MyLogger.ts:

import { createWriteStream } from 'fs'
import { stdSerializers, createLogger } from 'bunyan'
const pkg = require('../../package.json')

export default function (logLevel: string = 'debug', logFolder: string) {
  const logOptions = {
    serializers: stdSerializers,
    name: `${pkg.name}-${pkg.version}`,
    streams: [
      { level: 'error', stream: process.stderr },
      { level: logLevel, stream: process.stdout }
    ]
  }

  if (logFolder) {
    logOptions.streams.push(
      { level: logLevel, stream: createWriteStream(`${logFolder}/${pkg.name}.log`) })
  }

  const logger = createLogger ({
    name: logOptions.name,
    streams: logOptions.streams
  })

  return logger
}

now when I compile the typescript, I get the following error:

lib/config/MyLogger.ts(5,1): error TS4058: Return type of exported function has or is using name 'Logger' from external module "project-folder/node_modules/@types/bunyan/index" but cannot be named.

I am using node version: 6.9.2 Bunyan version: 1.8.5 @types/bunyan": "0.0.35

Any help understanding the error or how to solve this.. would be great. Thanks.


Solution

  • based on the typescript issue here: TS4023

    I got the compilation running by adding an import as:

    import * as Logger from '@types/bunyan'
    

    and then addding a return type to my exported function as Logger.