winstonjs / winston

A logger for just about everything.
http://github.com/winstonjs/winston
MIT License
22.91k stars 1.81k forks source link

Uncaught TypeError: winston__WEBPACK_IMPORTED_MODULE_0__.format.errors is not a function #1724

Open bruceauyeung opened 5 years ago

bruceauyeung commented 5 years ago

Please tell us about your environment:

I am using winston in an Electron application, based on VueJS framework, typescript class style

my code:

import { createLogger, format, transports } from 'winston';

const logger = createLogger({
    level: 'info',
    format: format.combine(
        format.timestamp({
            format: 'YYYY-MM-DD HH-mm-ss'
        }),
        format.errors({ stack: true }),
        format.splat(),
        format.json(),
    ),
    defaultMeta: { service: 'my-service-name' },
    transports: [
        new transports.File({ filename: 'test-file-log-err.log', level: 'error' }),
        new transports.File({ filename: 'test-file-log-all.log' })
    ]
});
  1. in vscode, when i type in format.err, vscode will suggest me errors, auto-completion looks good.
  2. typescript compilation has no error.
  3. but when i running this app, there is an error thrown out:
    logger.ts:9 Uncaught TypeError: winston__WEBPACK_IMPORTED_MODULE_0__.format.errors is not a function
    at Module../src/logger.ts (logger.ts:9)
    at __webpack_require__ (bootstrap:790)
    at fn (bootstrap:150)
    at Module../node_modules/cache-loader/dist/cjs.js?!./node_modules/babel-loader/lib/index.js!./node_modules/ts-loader/index.js?!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/components/HelloWorld.vue?vue&type=script&lang=ts& (app.js:1030)
    at __webpack_require__ (bootstrap:790)
    at fn (bootstrap:150)
    at Module../src/components/HelloWorld.vue?vue&type=script&lang=ts& (HelloWorld.vue?5a2d:1)
    at __webpack_require__ (bootstrap:790)
    at fn (bootstrap:150)
    at Module../src/components/HelloWorld.vue (HelloWorld.vue?f177:1)

    What do you expect to happen instead?

    there shouldn't be an error related to errors function

rhyek commented 4 years ago

I think this is related to webpack and your project's configuration. I tested this in a newly created create react app project and I get the same issue. In the same project, if I run node -e "const { format } = require('winston'); console.log(format.errors);" I can see that the function does indeed exist. When running the react app, I get the error in the browser's console.

rhyek commented 4 years ago

Related: https://github.com/winstonjs/logform/issues/97 So not related to webpack. It's just the errors formatter is not being exposed to the browser by logform.

bruceauyeung commented 4 years ago

@rhyek today i tried to reproduce this bug, and like you said, format.errors function is not available in electron renderer process even when nodeIntegration is true