TIL: Deno's stdlib is Available Outside Deno

by Graham Marlow

Out of the craziness that has been the last couple weeks of NPM supply-chain drama we're reminded that Deno at least had a few things right when it pushed for a secure-by-default stance towards third-party code. In the same general space we've seen Pnpm disable postinstall scripts by default and more recently release some minor mitigating configuration settings. It is certainly an interesting time to be a JavaScript developer, not that this kind of issue is unique to NPM.

There are, however, many reasons why NPM is the perfect target for this kind of attack. JavaScript is a language with a small standard library, an enormous user base, and a general social acceptance of insane node_modules folders. We all remember the left-pad incident, a mere 17 lines of code that sent the JavaScript ecosystem into a spiral.

Anyway, this TIL isn't about all of that. It's about one of the nice things I stumbled upon while reading the aforementioned Hacker News thread: Deno's standard library is widely available even for non-Deno users. You can find it on Deno's independent module registry, JSR.

When I work in JavaScript or Rust I'm always envious of Go. I may hate the syntax and find the code lacking expressivity, but it undeniably has a rich and simple standard library. I've often seen an ethos in Go to stick to "pure Go" and avoid pulling in third-party implementations for items already covered by the stdlib. It's a stacked comparison because JavaScript was never meant to be a language running on the server (that's really a Node problem, after all), but times have changed and JavaScript is no longer just a browser runtime.

The ability to use Deno's standard library for non-Deno projects offers a nice default for JavaScript developers. We might just be trading one dependency for another, but I have a high degree of trust in the Deno folks to make good on their architectural goals and offer a widely useful, yet dependency-free set of functions.

On a quick scan of Deno's @std modules, there are quite a few goodies:

  • @std/collections which covers utility functions a la lodash
  • @std/uuid for UUID generation/validation
  • @std/async for quite a few commonly-implemented async handlers (debounce, retry, exponential back-off)
  • @std/csv and @std/yaml for common file formats

The retry function from @std/async looks especially appealing:

import { retry } from "@std/async/retry";
const req = async () => {
 // some function that throws sometimes
};

// Below resolves to the first non-error result of `req`
const retryPromise = await retry(req, {
 multiplier: 2,
 maxTimeout: 60000,
 maxAttempts: 5,
 minTimeout: 100,
 jitter: 1,
});

Something that's definitely worth exploring for new projects. The source code for the entirety of Deno's standard library is available here: denoland/std.