Software engineer, musician, content creator
Sym-Linked Dependencies: A TypeScript Tale (or Yarn)
March 31st, 2023

This morning I stumbled to my desk into a TypeScript codebase that was riddled with sym-linked dependencies from a debugging session the night before (created using yarn link). Now was the time to undo the tangle, and to put things back as they were.

And that’s when I faced the first challenge of the day - how can I see a list of all my sym-linked dependencies? yarn link provides no such option. Then I remembered a stack overflow article that I copied and pasted from the other day for this exact problem.

Too lazy to open a browser, I hit Ctrl + R and started typing a search query from what I vaguely remembered of the command. It had an ls in there … And also a ^l I remember, and lo and behold, I found it:

$ ( ls -l node_modules ; ls -l node_modules/@* ) | grep ^l

Great - and it worked as expected. But what is it doing?

Nothing special, but worth remembering:

  1. First, it’s listing (ls) all files inside node_modules. Additionally, it accounts for scoped packages (prepended with @).
  2. The  -l flag is provided to produce a longer output format, effectively showing file permissions, owners, groups, size, etc.
  3. And the most important part - we pipe the output into grep to filter results with regex. It’s a simple expression: ^l. Basically, only return lines that start with l.

And why is that? Because the first character of a file’s permissions denote what type of file it is:

  • - is a regular file
  • d is a directory
  • l is a link

By listing all directories inside node_modules and node_modules/@ and filtering those that start with l, we effectively get a list of all sym-linked packages!

© 2023 Adnan Chowdhury