How to configure Node.js to log to a file instead of the console?

Winston is a popular Node.js logging library that can be configured to log to a file.

First, install it via npm:

npm install winston

Then, configure Winston to log messages to a file:

const winston = require('winston');

const logger = winston.createLogger({
  transports: [
    new winston.transports.File({ filename: 'app.log' })
  ]
});

logger.info('This is a log message');

Winston provides advanced features such as log levels, file rotation, and more.