aboutusesTILblog

Today I learned about...
node

Back to all tags

node11.10.2024

Better node objects logging!

Sick of seeing truncated Objects in Node? I got you bro (well Matt Pocock has got you).

let deepNesting = {
  level1: {
    level2: {
      level3: {
        level4: {
          level5: {
            name: 'Moderately Nested Object',
            value: 42,
            attributes: {
              created: '2023-10-01',
              modified: '2023-10-15',
              tags: ['demo', 'test', 'nested'],
              metadata: {
                author: 'Jane Doe',
                version: '1.0',
                license: 'MIT',
              },
            },
            items: [
              {
                id: 1,
                description: 'First item',
                status: 'active',
              },
              {
                id: 2,
                description: 'Second item',
                status: 'inactive',
              },
            ],
          },
        },
      },
    },
  },
}

// logs: { level1: { level2: { level3: [Object] } } }
console.log(deepNesting)

// You get the whole object!
console.dir(deepNesting, {depth: Infinity})

Matt is the man!

node27.07.2023

node require cache

TIL that commonjs has a require cache. Basically, if you require a module once in the file, it gets cached and if you attempt to require the module a second time, EVEN IF THE CONTENT HAS CHANGED, you’ll get the cached version.

Usually this isn’t a problem but it bit me in the butt when I moved my remix app over to be using express. I had to call this function inside of my server code when remix finished rebundling.

function clearRequireCache() {
  Object.keys(require.cache).forEach(function (key) {
    delete require.cache[key]
  })
}
node04.05.2020

Promise Based Node Functions

TIL during a basic async node training video, that the node core api exposes promisified (I think that’s a word) functions. For example fs provides a promise based API.

Check it out