TypeError:winston.createLogger不是构造函数

时间:2019-01-03 17:19:57

标签: winston

我要做什么:为Nodejs应用创建记录器

代码:

const winston = require('winston');
const fs = require('fs');
const moment = require('moment');

// const envLogger;
const logDir = 'logs/aws';

// Create the log directory if it does not exist
fs.access(logDir, (err) => {
  if (err) {
    fs.mkdir(logDir, ()=>console.log('Log file created'));
  }
});

const timestampFormat = () => {
  return moment().format('YYYY-MM-DD hh:mm:ss');
};

const logger = new winston.createLogger({
  transports: [
    new winston.transports.Console({
      timestamp: timestampFormat,
      colorize: true,
      prettyPrint: true,
      level: 'info',
    }),
  ],
});
console.log('Created logger');
module.exports = logger;

在Amazon EC2实例上运行此命令,会出现以下错误:

[2019-01-03T16:38:26.580Z][error][sql]: uncaughtException: winston.createLogger is not a constructor
TypeError: winston.createLogger is not a constructor
    at Object.<anonymous> (/###/common/helper/logger.js:19:16)
    at Module._compile (internal/modules/cjs/loader.js:722:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    at Module.require (internal/modules/cjs/loader.js:658:17)
    at Module._compile (internal/modules/cjs/loader.js:722:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    at Module.require (internal/modules/cjs/loader.js:658:17)
    at Module._compile (internal/modules/cjs/loader.js:722:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    at Module.require (internal/modules/cjs/loader.js:658:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Module._compile (internal/modules/cjs/loader.js:722:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
    at Module.load (internal/modules/cjs/loader.js:620:32) 

package.json上的Winston版本

"winston": "^3.0.0-rc1"

根据这些链接-Node js logging - winston.Logger is not a constructorTypeError: winston.Logger is not a constructor with winston and morgan

,这似乎是Winston.logger版本3上的一个众所周知的问题。

但是我在v3上遇到了同样的问题,这应该不会发生。该应用程序在我的本地计算机上运行良好,并且该问题仅在运行Amazon Linux AMI 2的EC2实例上发生。

本地Mac上的Winston版本:

 ├─┬ apidoc@0.17.6
 │ └── winston@2.3.1
 ├─┬ db-migrate@0.10.7
 │ └─┬ prompt@1.0.0
 │   └── winston@2.1.1
 └── winston@3.0.0-rc1

EC2实例上的Winston版本:

├─┬ apidoc@0.17.6
│ └── winston@2.3.1 
├─┬ db-migrate@0.10.7
│ └─┬ prompt@1.0.0
│   └── winston@2.1.1 
└── winston@3.1.0 

1 个答案:

答案 0 :(得分:0)

我终于通过从package.json文件中删除插入符号(^)来解决了这个问题

在package.json上编辑此行

"winston": "^3.0.0-rc1"

"winston": "3.0.0-rc1"

这将确保仅安装v3.0.0。