{"id":11254,"library":"loupe","title":"Loupe Object Inspection Utility","description":"Loupe is a robust, cross-platform utility for converting JavaScript objects into string representations, designed to work consistently in both Node.js and browser environments. It provides functionality similar to Node.js' built-in `util.inspect()`, making it a valuable tool for debugging and generating human-readable output of complex data structures. The current stable major version is `4.x`, with `v4.0.0` introducing significant breaking changes regarding custom inspection methods. The package maintains a moderate release cadence, with minor and patch updates preceding major version increments, reflecting ongoing development and refinement. As a core component within the Chai.js assertion library ecosystem, Loupe's key differentiator is its unified inspection behavior across diverse JavaScript runtimes, addressing the common challenge of inconsistent object serialization.","status":"active","version":"3.2.1","language":"javascript","source_language":"en","source_url":"https://github.com/chaijs/loupe","tags":["javascript","typescript"],"install":[{"cmd":"npm install loupe","lang":"bash","label":"npm"},{"cmd":"yarn add loupe","lang":"bash","label":"yarn"},{"cmd":"pnpm add loupe","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary `inspect` function is a named export. Default imports are not recommended for direct inspection.","wrong":"import loupe from 'loupe';\n// then `loupe.inspect` is correct, but direct destructuring is preferred","symbol":"inspect","correct":"import { inspect } from 'loupe';"},{"note":"CommonJS environments should use destructuring for the `inspect` function as it's a named export.","wrong":"const inspect = require('loupe');\n// Then `inspect.inspect` would be needed, or a TypeError if called directly","symbol":"inspect","correct":"const { inspect } = require('loupe');"},{"note":"Since v4.0.0, custom inspect methods must use `Symbol('nodejs.util.inspect.custom')` or `Symbol('chai/inspect')` rather than a plain `.inspect` property for compatibility and best practices.","wrong":"obj.inspect = function() { /* ... */ };","symbol":"Custom inspection","correct":"obj[Symbol('nodejs.util.inspect.custom')] = function() { /* ... */ };"}],"quickstart":{"code":"import { inspect } from 'loupe';\n\n// Basic types\nconsole.log(inspect({ foo: 'bar', baz: 123 }));\nconsole.log(inspect([1, 'two', { three: 3 }]));\nconsole.log(inspect(new Date('2023-01-15T10:00:00Z')));\nconsole.log(inspect(/pattern/gi));\n\n// Functions\nfunction greet(name) { return `Hello, ${name}`; }\nconsole.log(inspect(greet));\n\n// Complex objects with custom inspection (v4.x compatible)\nconst customInspectable = {\n  value: 'secret',\n  [Symbol('nodejs.util.inspect.custom')]: function(depth, opts) {\n    return `CustomObject { value: '${this.value.slice(0, 3)}...' }`;\n  }\n};\nconsole.log(inspect(customInspectable));\n\n// Errors and Promises\nconst error = new Error('Something went wrong');\nconsole.log(inspect(error));\nconsole.log(inspect(Promise.resolve(42)));\n","lang":"typescript","description":"Demonstrates how to import and use the `inspect` function with various data types, including objects, arrays, dates, regexes, functions, and a custom-inspectable object (v4.x style)."},"warnings":[{"fix":"Migrate custom inspect functions to use `Symbol('nodejs.util.inspect.custom')` or `Symbol('chai/inspect')` instead. This aligns with Node.js' `util.inspect` behavior.","message":"Version 4.0.0 removed support for custom inspection methods defined via a plain `.inspect` property on objects.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Loupe's `inspect` function accepts options for controlling depth and truncation. Consider using `inspect(obj, { depth: 2, maxLineLength: 80 })` to limit output size.","message":"When inspecting very large or deeply nested objects, the output can become excessively long and impact performance or readability.","severity":"gotcha","affected_versions":">=3.0.0"},{"fix":"No direct fix needed, but be aware of changes in build output if you were relying on these declaration maps for debugging or tooling purposes in older versions. For v4.0.0+, declaration maps are no longer shipped.","message":"The `loupe` package outputs declaration maps ('.d.ts.map' files) prior to v4.0.0, which were then removed in v4.0.0.","severity":"gotcha","affected_versions":"<4.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use named import `import { inspect } from 'loupe';` for ESM or destructuring `const { inspect } = require('loupe');` for CommonJS.","cause":"Attempting to call `require('loupe')` or `import loupe from 'loupe'` directly as a function, without destructuring the `inspect` named export.","error":"TypeError: inspect is not a function"},{"fix":"For `loupe` v4.0.0 and above, custom inspect methods must be defined using the `Symbol('nodejs.util.inspect.custom')` or `Symbol('chai/inspect')` symbol keys. Example: `obj[Symbol('nodejs.util.inspect.custom')] = function() { /* ... */ };`","cause":"Using a plain `.inspect` property for custom inspection logic on an object, which was deprecated and removed in v4.0.0.","error":"Custom inspection logic not being invoked for my object."}],"ecosystem":"npm"}