question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

gatsby-config/node.js doesn't support ESM import/export

See original GitHub issue

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. 😄

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:37 (23 by maintainers)

github_iconTop GitHub Comments

7reactions
ghostcommented, Apr 30, 2021

@LekoArts @KyleAMathews Node 16 is out, this is no longer experimental and should be reopened.

3reactions
karlhorkycommented, Aug 6, 2021

Yeah, Gatsby not supporting ESM will become a big problem in the next weeks as people upgrade to the latest remark packages, which are full ESM.

@LekoArts would recommend the team reopening this one - it should be an issue, not a feature request.

cc @wooorm

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using ES6 Module Syntax in Gatsby API Files on Gatsby Cloud
gatsby -node.esm.js import { isString } from "lodash" export const onPreInit = gatsbyNodeHelpers => { const { reporter } = gatsbyNodeHelpers ...
Read more >
Importing in Node.js: error "Must use import to load ES Module"
The problem is that Node.js does not currently support import and export natively yet. It is still experimental according to the ...
Read more >
Gatsby Changelog | 5.3.0
Support for ESM in gatsby-config and gatsby-node files has been a highly requested feature in the community, and ecosystems that Gatsby interacts with...
Read more >
Module Methods - webpack
Version 2 of webpack supports ES6 module syntax natively, meaning you can use import and export ... Statically import the export s of...
Read more >
next.config.js: Introduction
js is a regular Node.js module, not a JSON file. It gets used by the Next.js server and build phases, and it's not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found