Skip to content

💡 Handy Tips

A collection of 124 tips that come in real handy when you need them. Originally posted as tweets on my Twitter account, and now gathered here so you can browse them easily.

Quickly Navigate to Folder in Shell

💡 Are you often navigating to the same folders in the terminal?

👉🏼 Add the folders as variables in your source file and type:

➡️ cd $myproject

instead of:

➡️ cd /Users/maxim/Code/myproject

Enable using variables with cd in Z Shell with: setopt cdablevars and in Bash with: shopt -s cdable_vars. Then run export yourapp="path/to/yourapp" after which you can use cd $yourapp.

Use Native Promises With Node.js fs Module

💡 Have you been promisifying the fs module in Node.js all this time like I did?

👉🏼 Not anymore! Node.js >= 10 ships with a native promise implementation of the fs module. #latetotheparty 😅

➡️ require('fs/promises')

Use the built-in promisified version of fs module in Node.js by importing fs/promises.

Relinquish Process Privileges in Node.js

💡 Running Node.js as root so you can bind to ports 80/443?

👉🏼 You can relinquish privileges with "process.setuid()" & "process.setguid()" to reduce the damage potential in case of a breach. Do this right after the app starts.

Inside the callback function for server.listen, change process user with process.setuid('nodejs') and process.setguid('nodejs') to relinquish privileges and reduce damage potential in case of a breach.

Measure Execution Time With console.time

💡 Need to find out how long a section of code or external request takes to complete?

👉🏼 A way to quickly measure this is to use console.time & console.timeEnd.

To start the timer, use console.time('fetch'). console.timeEnd('fetch') ends the timer and outputs the time in milliseconds.