Pino: Pino is not a function - TypeScript

Created on 10 Nov 2018  路  6Comments  路  Source: pinojs/pino

Hi!, i'm doing a migration of a project to TypeScript and it uses Pino.js but it says Pino is not a function.
I would like to know if I can get an answer here or if you can suggest me anything else to solve this due to I'm trying to follow a convention style by using some rules in my eslint.

This is an example to reproduce it:

import * as Pino from 'pino';

console.log('------->', Pino({ name: 'MyApp' }));

Stack trace:

TypeError: Pino is not a function
    at Object.Pino (/Users/xx/development/www-typescript/src/demo.ts:5:25)
    at Module._compile (module.js:652:30)
    at Module._compile (/Users/xx/development/www-typescript/node_modules/pirates/lib/index.js:83:24)
    at Module._extensions..js (module.js:663:10)
    at Object.newLoader [as .ts] (/Users/xx/development/www-typescript/node_modules/pirates/lib/index.js:88:7)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at Object.<anonymous> (/Users/xx/development/www-typescript/node_modules/@babel/node/lib/_babel-node.js:224:23)

A workaround that it actually works is using the require statement:

const Pino = require('pino');

console.log('------->', Pino({ name: 'MyApp' }));

Output:

-------> Pino {
  levels:
   { labels:
      { '10': 'trace',
        '20': 'debug',
        '30': 'info',
        '40': 'warn',
        '50': 'error',
        '60': 'fatal' },
     values: { trace: 10, debug: 20, info: 30, warn: 40, error: 50, fatal: 60 } },
  trace: [Function: noop],
  debug: [Function: noop],
  info: [Function: LOG],
  warn: [Function: LOG],
  error: [Function: LOG],
  fatal: [Function: LOG],
  [Symbol(pino.useLevelLabels)]: false,
  [Symbol(pino.changeLevelName)]: 'level',
  [Symbol(pino.useOnlyCustomLevels)]: false,
  [Symbol(pino.stream)]:
   SonicBoom {
     _buf: '',
     fd: 1,
     _writing: false,
     _writingBuf: '',
     _ending: false,
     _reopening: false,
     file: null,
     destroyed: false,
     minLength: 0,
     release: [Function],
     _events: { error: [Function: filterBrokenPipe] },
     _eventsCount: 1 },
  [Symbol(pino.time)]: [Function: epochTime],
  [Symbol(pino.stringify)]: [Function: stringify],
  [Symbol(pino.stringifiers)]: {},
  [Symbol(pino.end)]: ',"v":1}\n',
  [Symbol(pino.formatOpts)]: { stringify: [Function: stringify] },
  [Symbol(pino.messageKeyString)]: ',"msg":',
  [Symbol(pino.serializers)]: { err: [Function: errSerializer] },
  [Symbol(pino.chindings)]: ',"pid":59824,"hostname":"xxxx.local","name":"MyApp"',
  [Symbol(pino.levelVal)]: 30 }

Versions in package.json:

{
  ...
  "@types/pino": "^5.8.1",
  "pino": "^5.0.1",
  "typescript": "^3.1.6"
  ...
}

Node & NPM versions:

node --version 
v8.11.1
npm --version
6.4.1

Thanks!.

question

Most helpful comment

does

import Pino from `pino`

work?

All 6 comments

does

import Pino from `pino`

work?

This may be useful: https://www.typescriptlang.org/docs/handbook/module-resolution.html

You can usually work with any node package with import pkg = require('pkg').

Alternatively, setting "moduleResolution": "Node" in your tsconfig.json will probably make import * as Pino from 'pino'; work.

@davidmarkclements No, it doesn't, but the following way works in the same way as the require statement:

import { default as Pino } from 'pino';

@jstewmon yes, I have the "moduleResolution":"node" setting in my tsconfig.json file. And no,

import * as Pino from 'pino';

did not work, as I mentioned to @davidmarkclements it is solved with that syntax.

Thanks guys!.

Seems like typescript may have changed slightly again - this time to align more with es6 module behaviour. I expect the pino types need to be updated

Closing as you have a workaround and typescript definitions are the responsibility of the pino types author

The following works for me

import * as Pino from 'pino';

@thangchung I noticed that turning the compilerOption.esModuleInterop option in tsconfig.json on and off had an effect on the result of that syntax.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JamesKyburz picture JamesKyburz  路  7Comments

davidmarkclements picture davidmarkclements  路  7Comments

davidmarkclements picture davidmarkclements  路  6Comments

dunklesToast picture dunklesToast  路  3Comments

cekvenich picture cekvenich  路  6Comments