ESM in Gatsby files #37069
Replies: 10 comments 20 replies
-
I think this long anticipated feature finally arriving introduces an even bigger opportunity for Gatsby. 🙂 I'm probably one of Gatsby's biggest fans, but the honest truth is that when I open an unfamiliar Gatsby codebase for the first time, I feel a bit of apprehension. I just have absolutely no idea what I'm getting myself into. The gap between the developer experience of Gatsby 2 and Gatsby 5 is so incredibly wide that they feel like completely different frameworks. So until I open the But there's an easy way to fix that! 😊 Instead of introducing a clunky new file extension to support ESM, I propose that Gatsby gives the file structure a bit of a refresh for the Gatsby 2022 Glow Up and adopts the file naming conventions of the modern JavaScript ecosystem:
Make this opt-in for ESM support in Gatsby 5 and a requirement for Gatsby 6. Afterwards it will be abundantly clear if a codebase is using "legacy" Gatsby (the static site generator) or modern Gatsby (the full-stack React framework). I know it's a bit silly to have such strong opinions about file names and extensions, but now that Gatsby has caught up to or even surpassed its competitors in many ways, I'm confident that small details like these can make a larger long-term impact on Gatsby's future than even shipping new features. The story of Gatsby's evolution into a full-stack React framework has already been written. Now it's time to get the word out! P.S. Please rename "Valhalla Content Hub" to "Gatsby Content Mesh". I think people are finally ready for the ✨ 𝓬𝓸𝓷𝓽𝓮𝓷𝓽 𝓶𝓮𝓼𝓱 ✨! 😅 |
Beta Was this translation helpful? Give feedback.
-
Thanks for supporting ESM, which I have always been waiting for. I have upgraded my website to gatsby v5 and use the ESM feature. (kxxt/kxxt-website#104)
But it failed to build on vercel because of a GraphQL error. I don't know if it's related to ESM but this is the first time I encounter an error like this. Logs from vercel
|
Beta Was this translation helpful? Give feedback.
-
If you're using VS Code, the files should have the correct icon when using |
Beta Was this translation helpful? Give feedback.
-
Is this being backported to Gatsby v4 or is it only available in v5 on wards? |
Beta Was this translation helpful? Give feedback.
-
I really hope «local» repo Gatsby plugins are supported as well? 🤞🤞🤞 |
Beta Was this translation helpful? Give feedback.
-
Getting this error after I switched to gatsby-node.mjs from gatsby-node.js, how can I make this warning go? ⠴ Building development bundle |
Beta Was this translation helpful? Give feedback.
-
I've added a new page to our docs now to explain ESM and Gatsby: https://www.gatsbyjs.com/docs/how-to/custom-configuration/es-modules/ |
Beta Was this translation helpful? Give feedback.
-
I know it's a bit outside of this discussion's scope, but would .mts file handling (or at least planned and/or possible implementations) resolve TypeScript being unsupported in workspaces? While it can be worked around, I do find this issue a significant and hard-to-diagnose hurdle to an otherwise smooth plugin development experience. |
Beta Was this translation helpful? Give feedback.
-
Any updates on timing of |
Beta Was this translation helpful? Give feedback.
-
Running into this issue a lot in dev mode: #31599 (comment) Where files in |
Beta Was this translation helpful? Give feedback.
-
Welcome to the overarching discussion related to ECMAScript modules (ESM) in Gatsby files!
Here is where you can get the latest info on how to try out the feature while it's in development and share your feedback.
Motivation
The ECMAScript module format is the official TC39 standard for packaging JavaScript. For many years a different module format, CommonJS (CJS) was the de facto standard in Node.js. Today, Gatsby supports CJS in
gatsby-config
andgatsby-node
but not ESM.Support for ESM in
gatsby-config
andgatsby-node
files is a highly requested feature in the community, and ecosystems that Gatsby interacts with like mdx and unified have switched to ESM, dropping CJS in their latest major releases.We intend to enable support for both CommonJS and ESM in Gatsby files.
Direction
There are two main goals we need to meet:
In order to do both, our direction is to adopt the
.mjs
and.mts
file extensions for files written in ESM.This allows us to not change the existing behavior in Gatsby (
.js
and.ts
extensions are CJS) for current users, stay compliant with how Node and TypeScript officially handle ESM, and provide a smooth transition to a future where the default format of Gatsby files is ESM.Current status
gatsby-config.mjs
andgatsby-node.mjs
is available ingatsby@next
and starting withgatsby@5.3.0
gatsby-config.mts
andgatsby-node.mts
is underway, check back again here in the future for more updatesTry it out
To try out ESM in your Gatsby files:
npm install gatsby@latest --legacy-peer-deps
gatsby-config.js
andgatsby-node.js
files togatsby-config.mjs
andgatsby-node.mjs
require
becomesimport
)Limitations
Where can I ask questions and/or give feedback?
Beta Was this translation helpful? Give feedback.
All reactions