ES Modules (ESM) and Gatsby
gatsby-node in ESM syntax.
This feature was added in
- A Gatsby project set up with
firstname.lastname@example.org later. (Need help creating one? Follow the Quick Start)
Usage in Gatsby
Generally speaking you need to follow the official standard as explained in the Node.js documentation.
gatsby-config.mjs file. Here’s an example
gatsby-config using ESM syntax:
gatsby-node.mjs file and use any of the Node APIs as usual. Here’s an example
gatsby-node using ESM syntax:
Migrating from CommonJS to ES Modules
exportsyntax instead of
File extensions in imports are mandatory
You can replicate the
You can replicate
The documents Interopability with CommonJS and Differences between ES Modules and CommonJS also apply to ESM in Gatsby.
Here’s how you’d migrate a
gatsby-config.js file to
The TypeScript variants of
gatsby-nodedo not support ESM yet. We plan on adding support in a future minor release by using the
.mtsextension. If you have questions or suggestions about this, please go to our ESM in Gatsby files umbrella discussion.
However, you can use Type Hinting in the meantime.
The ESM in Gatsby files umbrella discussion is also the right place for any questions about the