Winston: ES2015 import fails with Rollup, no docs

Created on 6 Feb 2016  Â·  11Comments  Â·  Source: winstonjs/winston

I'm using Winston in an ES2015 project. I import Winston with import log from 'winston' and log with log.info(), log.debug(), etc. When I transpile and run with Babel it works just fine.

However if I use Rollup to resolve all the relative import paths and then the babel Rollup plugin to transpile (with the es2015-rollup plugin for Babel), the import of Winston fails. In particular, it imports the following object:

{ transports: 
   { console: 
      EventEmitter {
        domain: null,
        _events: {},
        _eventsCount: 0,
        _maxListeners: undefined,
        silent: false,
        raw: false,
        name: 'console',
        formatter: undefined,
        level: undefined,
        handleExceptions: false,
        exceptionsLevel: 'error',
        humanReadableUnhandledException: false,
        json: false,
        colorize: false,
        prettyPrint: false,
        timestamp: false,
        showLevel: true,
        label: null,
        logstash: false,
        depth: null,
        align: false,
        stderrLevels: [Object],
        eol: '\n',
        _onError: [Function: bound ] } },
  exceptionHandlers: {} }

This can be fixed by using import * as log from 'winston', but this is not at all obvious. I'm not sure what Rollup is doing in detail, but it looks like a great piece of ES2015 tooling, so Winston should work with it cleanly.

I would recommend adding a section to the Winston README on how to use it with ES2015, and perhaps add a caveat with respect to Rollup. Currently there is no guidance, and nothing on Google either.

All 11 comments

Also, I just noticed that when import Winston with import * as log from 'winston', you can no longer use log.level = '<level>', since imports can't be assigned to. As a workaround, you _can_ still use require('winston').level = '<level>' and it will work.

Thanks! I just lost 30 minutes trying to figure out why it's not logging.

Thanks for the tip. Changing my code to use CJS like const winston = require('winston') resolved the issue for me (all other imports in the file are ES6-style)

I got a bit further but got stuck with the circular dependencies (winston top level pulls in everything and lots of things it pulls in then reference winston back)

This issue is still not fixed in [email protected]

This issue has been solved in 3.0.0

I’m still facing the issue with winston. I use commonjs rollup pluging as well. Rollup is able to bundle the code properly but when I run it fails loudly saying Cannot read property prototype of undefined

@kamelshchandnani Is that true if you use the ‘master’ branch of Winston? If so, can you please provide more info (full error messages and stack trace, relevant configs, etc.)? We usually need to be able to reproduce problems in order to fix them.

@DABH
Yes I'm using v3.1.0. I'll share more details.

Here's my rollup config

export default {
  input: `src/logger.js`,
  external: Object.keys(packageJson.peerDependencies),
  plugins: [
    babel({
      exclude: 'node_modules/**',
      runtimeHelpers: true,
      externalHelpers: true,
    }),
    nodeBuiltIns(),
    commonjs(),
    resolve({ browser: true }),
    json(),
  ],
  output: [{
    file: `logger.js`,
    format: 'esm',
  }, {
    file: `cjs/logger.js`,
    format: 'cjs',
  }],
}

And this the logger.js file

import winston from 'winston'

const logger = winston.createLogger({
  transports: [
    new winston.transports.Console(),
  ],
  exceptionHandlers: [
    new winston.transports.Console(),
  ],
  exitOnError: false,
});

export default logger;

now when I import logger.js in my projects

import logger from 'utils/logger'

// lambda handler
export const handler = (event, context) => {
  logger.info('handler', { event, context });
};

It throws out following error:

[ 'TypeError: Cannot read property \'prototype\' of undefined',
  'at eval (webpack:///../utils/logger.js?:1336:40)',

@DABH Any thoughts on this?

Definitely still broken in 3.1.0.

If you use Node's builtin 'stream' module instead of 'readable-stream' from NPM, you resolve the undefined / circular dependency thing, and if you get rid of the dynamic import crap in logform, then it works for me.

To put that in the hackiest possible terms…

find node_modules/{winston,winston-transport,logform} -type f -exec sed -i -e "s/require(.readable-stream.)/require('stream')/g" -e "s/require(.readable-stream\/writable.)/require('stream').Writable/g" -e "s/exposeFormat('\(\w*\)', '\(.*\)'/format.\1 = require('.\/\2.js'/g" -e "s/exposeFormat('\(\w*\)'/format.\1 = require('.\/\1.js'/g" {} +
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Nepoxx picture Nepoxx  Â·  4Comments

anks333 picture anks333  Â·  3Comments

ghost picture ghost  Â·  4Comments

greenhat616 picture greenhat616  Â·  3Comments

amiram picture amiram  Â·  4Comments