Node formating colors missing after upgrading from version 8.8.1 to 9 or above
Bug Report
Using node 16.x, console.log({ hello : 1 })

Using serverless-offline@8 it worked the same way:

Upgrading to serverless-offline@8, serverless-offline@9, or serverless-offline@10, or serverless-offline@11

Sample Code
- file: serverless.yml
service: my-service
plugins:
- serverless-offline
provider:
runtime: nodejs16.x
stage: dev
functions:
hello:
events:
- http:
method: get
path: hello
handler: handler.hello
- file: handler.js
'use strict'
exports.hello = async function hello() {
console.log({ hello: 1 })
return { body: 'OK' statusCode: 200, }
}
Environment
-
serverlessversion: 3.24.1 -
serverless-offlineversion: 11.3.0 -
node.jsversion: v16.18.0 -
OS: Mac OS 13
Possible Solution
I believe serverless-offline is running in its own child_process and the colours are not appropriately passed (like there).
I'll be happy to do a PR is someone can point me here serverless-offline is instantiated.
thank you for filing the issue @Edweis
I believe serverless-offline is running in its own child_process and the colours are not appropriately passed (like there).
serverless-offline switched to using worker threads from v9 onwards. that said, I didn't even notice that console.log is using colors in the main process. that might be something node.js itself has to fix. if you could do some research on that topic that would be much appreciated.
on a similar note, what I noticed, when developers are using third party color modules (e.g. chalk), that those most of the time don't work out of the box, as the terminal color detection does not seem to work for worker threads. one can force using colors with an environment variable FORCE_COLOR='true'. something we should add to the docs.
example:
// this should work
console.log('\x1b[36m%s\x1b[0m', 'I am cyan');
// this probably does not, unless we set process.env.FORCE_COLOR
// (either by code, or setting the env in the serverless config,
// or in the local dev environment by using the flag `localEnvironment`)
import chalk from 'chalk';
console.log(chalk.blue('Hello world!'));
Thank you. I had a quick look and I found that colors should be passed into the worker at src/lambda/handler-runner/worker-thread-runner/WorkerThreadRunner.js. However the colors from the workers are not set. My guess is that util.format used in console.xxx() does not detect the TTY (since the worker is technically not a TTY), hence not applying colors.
I might investigate more when I'll have more time.