Published at
Updated at
Reading time
2min
This post is part of my Today I learned series in which I share all my web development learnings.

If you maintain a Node.js library or framework that's consumed by people downstream, you know the pain of introducing breaking changes and deprecating existing functionality. People get mad very quickly and the only solution is to communicate very early that things might change or will disappear in a future version.

I've thought that the only way to communicate future deprecations is to mark methods as deprecated via TSDoc / JSDoc.

/**
 * Returns a string value.
 * @deprecated This function is deprecated and will be removed in a future version.
 * @returns {string} The string 'something'
 */
export const getSomething = () => {
  return 'something'
}

In this case, if someone imports the deprecated getSomething method, the editor (in my case it's VS Code) will make it very clear that they should look into not using the method.

VS Code interface showing a deprecation warning and strike-through text.

VS Code's crossed out text is very convincing if you ask me but I don't know if every editor displays deprecation warnings as prominently.

If you really want to make sure people see your Node.js deprecation notice, today, I learned that Node.js provides a native deprecate method.

import { deprecate } from "node:util";

/**
 * Returns a string value.
 * @deprecated This function is deprecated and will be removed in a future version.
 * @returns {string} The string 'something'
 */
export const getSomething = deprecate(() => {
  return 'something'
}, 'watch out! something is deprecated!')

And if someone now uses deprecated methods, they will also get notified when they run the code via Node.js.

$ node index.ts

something
(node:21682) DeprecationWarning: watch out! something is deprecated!
(Use `node --trace-deprecation ...` to show where the warning was created)

Sweet!

If you enjoyed this article...

Join 6.1k readers and learn something new every week with Web Weekly.

Web Weekly — Your friendly Web Dev newsletter
Reply to this post and share your thoughts via good old email.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles