Post

Why do people use the "prepublishOnly" and "prepare" npm script? I feel like they serve no real purpose, but people use them because they’re used and written about a lot.

0 reposts0 likes

Yeah, "prepack" is the way to go. I just see it relatively little in practice. And sometimes people actually meant to use "dependencies" instead of "prepare".

I feel like what people should be using keeps changing. prepublishOnly makes sure my library does its thing before publishing it so I use it. On CI I skip it. Scripts like “prepare”, “prepack” and previously unknown “dependencies” have vague names which I can’t for the life of me memorize.

"prepack" happens before packing. This is where your build script should go. "prepublishOnly" happens before publishing. I can’t think of any use case for that. "dependencies" runs if `npm install` changes your node_modules. This is the right place for something like "husky". It’s a bit more niche

prepublishOnly is then fine for tests, linting, type checking, checking formatting etc., is it not? Or is the implication that pnpm publish would ensure the main branch, and the code that ends there is already checked?

pnpm publish verifying the branch is new, I think. Before that you could run from anywhere, that’s one of the reasons why I think it’s good to perform checks as part of local publish. The other assumption, I assume, is that people would not publish locally, only on CI?

And yeah, this could be true as well, in the early days different approaches were circulating, people weren’t quite sure what was the “correct” one. (And still aren’t.) A good topic for a blog post if you were thinking of writing something.