gatsby: gatsby-config/node.js doesn't support ESM import/export
Summary
Node v14 is out and with it the removal of the Experimental Modules warning! š
I installed it, added "type": "module" to package.json, used export default in gatsby-config.js and took the default starter for a spin. This is what I got:
Error: [ERR_REQUIRE_ESM]: Must use import to load ES Module: gatsby-config.js require() of ES modules is not supported. require() of gatsby-config.js from node_modules/gatsby/dist/bootstrap/get-config-file.js is an ES module file as it is a .js file whose nearest parent package.json contains ātypeā: āmoduleā which defines all .js files in that package scope as ES modules. Instead rename gatsby-config.js to end in .cjs, change the requiring code to use import(), or remove ātypeā: āmoduleā from package.json.
Sounds like Gatsby, specifically gatsby/dist/bootstrap/get-config-file.js, stands in the way of using ESM import/export syntax. I think itās time to make gatsbyās config and node APIs ESM-ready. š
Basic example
export default {
siteMetadata: {
title: `Gatsby Default Starter`,
author: `@gatsbyjs`,
},
plugins: [
`gatsby-plugin-react-helmet`,
// ...
],
}
Motivation
Would be really cool if we could finally use a single import/export system all throughout our Gatsby projects! As the release announcement states:
It is our belief that the current implementation offers a future proof model to authoring ESM modules that paves the path to Universal JavaScript.
If that doesnāt get you excited, you must be in the wrong line of work. š
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 6
- Comments: 37 (23 by maintainers)
@LekoArts @KyleAMathews Node 16 is out, this is no longer experimental and should be reopened.
Yeah, Gatsby not supporting ESM will become a big problem in the next weeks as people upgrade to the latest
remarkpackages, which are full ESM.@LekoArts would recommend the team reopening this one - it should be an issue, not a feature request.
cc @wooorm
@tujoworker thx, but that method doesnt work with remark ecosystem. iāve tried that before.
@Ir1d - I havenāt found a solution to using import/export in gatsby-config/node.js. The closest I got to using import/export in gatsby-config/node.js is by using the
esmnpm package torequireserializable js files and convert to es modules to use in gatsby-config/node.js. For example, I hadconfig/site.js, which contained serializable javascript like so:and I use
esmnpm package to convert site.js from esm to cjs:this way, I can use
site(which is esm) in gatsby-config.js:However, this no longer works if you try to convert .js/jsx to .ts/tsx files. I know you can convert .jsx files to .tsx in Gatsby, but
esmnpm package cannot read .ts files so I am unable to update my Gatsby sites to use TypeScript at the moment. Iām probably better off using Next.js at this point if I want to continue using my config filesPlease open a feature request for ESM support here: https://github.com/gatsbyjs/gatsby/discussions/categories/ideas-feature-requests
Same problem here. Several gatsby-remark plugins are created inside the
./pluginsfolder of my project. They fail to require the latestunist-util-visit. I tried to convert them to.mjsand.cjsfiles without much success. The code work withunist-util-visitversion^2.0.3and fail with^3.0.1. I believe the problem is in how gatsby load local plugins more than in theunist-util-visit. This is an extract of the error:Let us know if we need to open a new issue.
Iām facing a similar issue as @thecodingwizard where I want to use the latest
unist-util-visitv3.0.0 (which is ESM only) in Gatsby. Iāve also tried using theesmpackage and also adding"type": "modules"into my Gatsby package.json, but it doesnāt work.@bytrangle No known way currently. Gatsby is only compatible with packages that are NOT pure ESM. Downgrade to older versions for use with Gatsby.
The Gatsby team is apparently ānot againstā moving to ESM (tweet from @wardpeet), but Iām guessing it will still be some time before you can use this in your projects.
If you absolutely need to use the new versions now, you may want to try experimenting with bundling / precompiling the dependency to CommonJS, although this may be complex.
Ah, the reason I canāt find it in the docs is because returning a function in
gatsby-config.jsis only for themes:https://github.com/gatsbyjs/gatsby/issues/16013#issuecomment-514235110
And yeah, that doesnāt support promises / async functions:
https://github.com/gatsbyjs/gatsby/issues/19644#issuecomment-558073951
This isnāt a ānice to haveā or āhas a workaroundā - Gatsby just doesnāt work with new versions of many packages.
Not «that» complex, see the this doc: https://support.gatsbyjs.com/hc/en-us/articles/1500000294121-Using-ES6-Module-Syntax-in-Gatsby-API-Files-on-Gatsby-Cloud
Sure, I agree, having that build in, would be very nice!
indeed. afaik, no one has ever been able to make gatsby work with esm module unist-util-visit
For xdm specifically, Iāve tried a variety of techniques to import it into Gatsby (including using the
esmpackage) and none of them worked. Did anybody have better success importing esm libraries into gatsby-node.js?Fair enough. Although itās probably a good idea to start looking into this now.