Node.js CommonJS → ESM refactoring cheat sheet.

Update requires to imports:

Find: const (.*?)\s*?=\s*?require\(('.*?')\)

Replace: import $1 from $2

(If you don’t use consts, update search expression to use var, etc.)

Update default exports:

Find: module.exports =
Replace: export default

PS. If you use dynamic requires, remember that dynamic imports return promises / are asynchronous.

· · Web · 1 · 2 · 3
Sign in to participate in the conversation
Aral’s Mastodon

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!