`chalk` library does not print colored text after bundle using webpack

712 Views Asked by At

I use chalk to write a simple script, and bundle the code using webpack. The code print colored text before bundle, but print text without color after bundle using webpack.

The test code is:

const chalk = require('chalk')

function main() {
  console.log(chalk.blue('chalk'))
}

main()

The webpack.config.js:

const path = require('path')

module.exports = {
  mode: 'production',
  entry: path.resolve(__dirname, 'src/main.js'),
  output: {
    filename: 'bundle.js',
  },
 
  resolve: {
    extensions: ['.js'],
  },
}

What happened ?

2

There are 2 best solutions below

0
On

For me was in the webpack configuration, I was compiling for web target instead of node target =]

0
On

It could be a problem with your individual computer or terminal. I had a similar situation, where a simple use of chalk wasn't working. Once I stopped using Solarized themes in my terminal preferences, chalk worked as expected.

(In my searching for solutions, there were a few other solutions out there for weird behavior, solved by such things as uninstalling iTerm2.)