You can pass an options object to control the global behavior and specific settings for individual columns.
Global Options
columns (or include): An array of strings specifying which keys to include as columns.showHeaders: Boolean. Whether to display the header row (defaults to true).maxLineWidth: The maximum width of the entire output line. Can be set to 'auto' to use the current terminal width (process.stdout.columns).paddingChr: The character used for padding (defaults to ' ').columnSplitter: The string used to separate columns (defaults to ' ').spacing: The string used to separate rows (defaults to '\n').config: An object containing per-column configuration overrides.
Per-Column Configuration
Inside the config object, you can specify settings for a specific column name:
maxWidth: Maximum width for this specific column.minWidth: Minimum width for this specific column.align: Alignment for the column content. Options: 'left' (default), 'center', or 'right'.truncate: Boolean. If true, long text will be truncated with the truncateMarker instead of wrapping.truncateMarker: The character used for truncation (defaults to '…').preserveNewLines: Boolean. If true, non-newline whitespace is merged but newlines are kept. If false, all whitespace is merged into single spaces.headingTransform: A function to transform the header name (e.g., key => key.toUpperCase()).dataTransform: A function (cell, column, index) => transformedCell to modify the data in each cell.
const columnify = require('columnify');
const data = [
{ name: 'Alice', age: 30 },
{ name: 'Bob', age: 25 }
];
const options = {
columns: ['name', 'age'],
showHeaders: true,
maxLineWidth: 'auto',
config: {
name: {
align: 'center',
maxWidth: 10,
truncate: true
},
age: {
align: 'right',
minWidth: 5
}
}
};
const output = columnify(data, options);
console.log(output);