diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 07e0f658e28353..012f358e2ac90a 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -508,7 +508,8 @@ function stylizeWithColor(str, styleType) { const style = inspect.styles[styleType]; if (style !== undefined) { const color = inspect.colors[style]; - return `\u001b[${color[0]}m${str}\u001b[${color[1]}m`; + if (color !== undefined) + return `\u001b[${color[0]}m${str}\u001b[${color[1]}m`; } return str; } diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index a16acc64de3456..78184d85f6e9f3 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -2245,6 +2245,12 @@ assert.strictEqual( assert.deepStrictEqual(inspect.colors[bgColor], [40 + i, 49]); assert.deepStrictEqual(inspect.colors[`${bgColor}Bright`], [100 + i, 49]); }); + + // Unknown colors are handled gracefully: + const stringStyle = inspect.styles.string; + inspect.styles.string = 'UNKNOWN'; + assert.strictEqual(inspect('foobar', { colors: true }), "'foobar'"); + inspect.styles.string = stringStyle; } assert.strictEqual(