Log nested objects in Node
•
The default “recurse depth” of console.dir
is 2. This is rarely what I want.
Consider the following contrived example.
const person = {
fullName: {
given: 'John',
family: 'Doe',
},
visited: [
{
place: 'Paris',
country: {
label: 'France',
code: 'FR',
},
},
{
place: 'Nha Trang',
country: {
label: 'Viet Nam',
code: 'VN',
},
},
],
}
By default, console.dir
stops short of expanding the country
object.
console.dir(person)
// Output
{
fullName: { given: 'John', family: 'Doe' },
visited: [
{ place: 'Paris', country: [Object] },
{ place: 'Nha Trang', country: [Object] }
]
}
Fix this by specifying the depth
option.
console.dir(person, { depth: Infinity })
// Output
{
fullName: { given: 'John', family: 'Doe' },
visited: [
{ place: 'Paris', country: { label: 'France', code: 'FR' } },
{ place: 'Nha Trang', country: { label: 'Viet Nam', code: 'VN' } }
]
}
Sign up for my newsletter
A monthly round-up of blog posts, projects, and internet oddments.