What I am trying to do: Create a logger for a Nodejs app
Code:
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;
Running this on Amazon EC2 instance, gets the following error:
[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)
Winston version on package.json
"winston": "^3.0.0-rc1"
This seems to be a well known problem for Winston.logger on version 3 as per these links - Node js logging - winston.Logger is not a constructor and TypeError: winston.Logger is not a constructor with winston and morgan
However I am getting the same problem on v3 which should not be happening. The application works fine on my local machine and the problem only occurs on the EC2 instance running Amazon Linux AMI 2.
Winston version on local Mac:
├─┬ 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
Winston version on EC2 instance:
├─┬ 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
I finally solved this by removing the caret(^) sign from the package.json file
edit this line on package.json from
"winston": "^3.0.0-rc1"
to
"winston": "3.0.0-rc1"
This will make sure that only v3.0.0 is installed.