<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/surviving-the-shift-how-to-transition-from-commonjs-to-esm-fxncppkjm" -->

---
title: Surviving The Shift: How to Transition from CommonJS To ESM
description: A practical guide to migrating from CommonJS to ES Modules in Node.js. Covers common pitfalls like the loss of __dirname and __filename in ESM, and offers...
canonical: https://daily.dev/posts/surviving-the-shift-how-to-transition-from-commonjs-to-esm-fxncppkjm
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Surviving The Shift: How to Transition from CommonJS To ESM | daily.dev
og:description: A practical guide to migrating from CommonJS to ES Modules in Node.js. Covers common pitfalls like the loss of __dirname and __filename in ESM, and offers...
og:url: https://daily.dev/posts/surviving-the-shift-how-to-transition-from-commonjs-to-esm-fxncppkjm
og:image: https://api.daily.dev/og/posts/FXnCPPkjm.png
og:image:alt: Surviving The Shift: How to Transition from CommonJS To ESM
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Surviving The Shift: How to Transition from CommonJS To ESM

**[Medium](https://daily.dev/sources/medium_js)** · 4 min read · 0 upvotes · 0 comments

## Summary

A practical guide to migrating from CommonJS to ES Modules in Node.js. Covers common pitfalls like the loss of __dirname and __filename in ESM, and offers three migration strategies: using dynamic import() to load ESM-only packages in legacy CommonJS apps, starting new projects with ESM from day one, and using TypeScript or alternative runtimes like Bun to sidestep Node's strict module division.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://blog.stackademic.com/surviving-the-shift-how-to-transition-from-commonjs-to-esm-part-2-9c169ea39209>

## Similar posts on daily.dev

- [The Module System: ES Modules vs CommonJS](https://daily.dev/posts/the-module-system-es-modules-vs-commonjs-g7g50hx0k) · Medium · 2 upvotes · 0 comments
- [Ways to Improve Node.js Loader Performance](https://daily.dev/posts/ways-to-improve-node-js-loader-performance-mo7ywupjt) · AppSignal · 6 upvotes · 0 comments
- [A Well-Designed JavaScript Module System is Your First Architecture Decision](https://daily.dev/posts/a-well-designed-javascript-module-system-is-your-first-architecture-decision-uldejtx1m) · CSS-Tricks · 3 upvotes · 0 comments
- [Medium](https://daily.dev/posts/medium-gf0pfdb7d) · Medium · 0 upvotes · 0 comments
- [How To Use \_\_dirname in Node.js](https://daily.dev/posts/how-to-use-dirname-in-node-js-cfniqvm2w) · DigitalOcean Community · 23 upvotes · 2 comments

---

Tags: [#javascript](https://daily.dev/tags/javascript), [#nodejs](https://daily.dev/tags/nodejs)

[View this post on daily.dev](https://daily.dev/posts/surviving-the-shift-how-to-transition-from-commonjs-to-esm-fxncppkjm)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Surviving The Shift: How to Transition from CommonJS To ESM","url":"https://daily.dev/posts/surviving-the-shift-how-to-transition-from-commonjs-to-esm-fxncppkjm","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/surviving-the-shift-how-to-transition-from-commonjs-to-esm-fxncppkjm"},"datePublished":"2026-07-21T14:05:59.904Z","dateModified":"2026-07-21T14:07:14.455Z","description":"A practical guide to migrating from CommonJS to ES Modules in Node.js. Covers common pitfalls like the loss of __dirname and __filename in ESM, and offers...","image":"https://media.daily.dev/image/upload/s--58gMhC4P--/f_auto/v1722860399/public/Placeholder%2012","thumbnailUrl":"https://media.daily.dev/image/upload/s--58gMhC4P--/f_auto/v1722860399/public/Placeholder%2012","isAccessibleForFree":true,"articleSection":"Medium","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Medium","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/medium","url":"https://daily.dev/sources/medium_js"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/surviving-the-shift-how-to-transition-from-commonjs-to-esm-fxncppkjm","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"javascript,nodejs","timeRequired":"PT4M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Medium","item":"https://daily.dev/sources/medium_js"},{"@type":"ListItem","position":3,"name":"Surviving The Shift: How to Transition from CommonJS To ESM"}]}
```

