Eleven lines that broke half the JavaScript world
On Tuesday the left-pad package vanished from npm and thousands of builds fell over, ours included. What happened, and a slightly grumpy rant about how we got here.
Tuesday afternoon, around half past four, our CI went red. Not one build, all of them. The error was something I’d never seen before:
npm ERR! 404 'left-pad' is not in the npm registry.
My first thought was that npm was down. My second thought was “what on earth is left-pad and why do we need it?”. It turns out a lot of the JavaScript world was asking that exact same question at that exact same moment.
What happened#
Short version, as far as I can piece it together from the npm blog and a lot of GitHub threads:
- A developer had published a package called
kikon npm. A company with the same name wanted that package name and asked him to hand it over. He refused. - The company went to npm, and npm transferred the package name to them.
- The developer disagreed with that decision so strongly that he unpublished all of his packages from npm. Around 250 of them.
- One of those was
left-pad. Eleven lines of code that pad a string on the left. It turned out to be a dependency of a dependency of a dependency of a huge number of projects, including big names in the build tooling world. Babel, for one. - For a couple of hours, every
npm installthat neededleft-padanywhere in the tree failed. Then npm took the unusual step of restoring the package themselves.
I’m not going to take sides on the name dispute. Both sides have a point, and I don’t know all the details. What interests me is step 4.
The eleven lines#
This is roughly what the package did:
module.exports = leftpad;
function leftpad (str, len, ch) {
str = String(str);
var i = -1;
if (!ch && ch !== 0) ch = ' ';
len = len - str.length;
while (++i < len) {
str = ch + str;
}
return str;
}
That’s it. That’s what stopped a few thousand companies from deploying on Tuesday afternoon. Not a cryptography library, not a database driver. A while loop.
How did we get here?#
Out of curiosity I counted how many packages our frontend build pulls in:
$ npm ls --parseable | wc -l
1143
One thousand one hundred and forty-three. For an AngularJS app that we’re slowly porting to React. I know maybe twenty of those by name. The rest arrived as a dependency of something we did choose.
This is the same thing I’ve been feeling for months, and that people are starting to call “JavaScript fatigue”. To write a modern React component in 2016 you need Babel, and since Babel 6 you need a .babelrc with the right presets, otherwise it does nothing at all:
{
"presets": ["es2015", "react"]
}
Then webpack to bundle it, with loaders for Babel, CSS, images. Then a linter with a plugin for JSX. Then a test runner, an assertion library, a mocking library, something for hot reloading. Every one of those tools is reasonable on its own. Together they form a pile of hundreds of packages where nobody knows exactly what’s inside.
And I do like ES2015, for the record. This is so much nicer than what we wrote two years ago:
const pad = (n) => (n < 10 ? `0${n}` : `${n}`);
const stamp = (d) => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
The language is getting better. The ecosystem around it is getting heavier.
The tiny-module philosophy#
There’s a school of thought in the Node world that says: small modules that do one thing are good. Reuse instead of rewriting. I understand the idea and in principle I agree with it. But there’s a difference between “don’t rewrite a CSV parser” and “don’t write an eleven-line function”. Every dependency is a small bet that someone else will keep maintaining it, won’t break it and won’t delete it. For a CSV parser, that bet is worth it. For left-pad, I’d say no.
There’s even a proposal on the table to add a padStart method to strings in the language itself. Fine by me.
What we’re changing at the startup#
Sarah and I sat down on Wednesday and made a short list:
npm shrinkwrapeverywhere. We were already using it on the backend, not on the frontend. That’s changing. It doesn’t help against a package disappearing, but it does help against surprises in the version that gets installed.- A local registry mirror. We’re setting up
sinopiaas a cache in front of the npm registry, so we have our own copy of everything we’ve ever installed. If a package disappears upstream, our builds keep working. - A dependency budget. Before someone adds a new package, the question is: how big is it, how many dependencies does it pull in, who maintains it? For something smaller than a screen of code, we write it ourselves.
- No more “it’ll be fine”. Our deploy depends on an external service we don’t control. That’s a choice, and we should make it consciously.
npm has said they’re going to look at making it harder to unpublish packages that others depend on. Good. But the real lesson isn’t for npm, it’s for us: everything in your node_modules is code you ship. Whether you’ve read it or not.
Jef from the agency used to say: “Every line of code you didn’t write is a line you can’t fix at three in the morning.” I used to think that was old-fashioned. I’m starting to understand him.