Formatting MacOS Disk on Command Line

For reasons beyond my comprehension, macOS's Disk Utility randomly won't format external drives.

For example, here's what disk utility shows when I first plug in the flash drive.

Here's the error when I try to format it as ExFAT.

Instead, use the diskutil command to first list the disks to find out which one is the offending disk disk4 in this case) and then reformat it with something like diskutil eraseDisk [format] [name] [disk].