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.

rewrite gatsby-config & gatsby-node with node 11's experimental ECMAScript modules

See original GitHub issue

Has anyone tried replacing module.exports/require() in gatsby-config.js and/or gatsby-node.js with the experimental import/export syntax introduced in node v11.10.1? I’m interested in advice on how to make the transition, whether or not I should wait and if there are any limitations.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
harrysolovaycommented, Apr 1, 2019

Okay, I’ll take a shot at it.

From what I can tell. Here are the changes that need to take place:

  1. Creating a helper package that differentiates between ESMs & CJS:

const interopRequire = path => {
  const required = require(path)
  return required.__esModule ? required.default : required
}
  1. In packages/Gatsby/src/internal-plugins/internal-data-bridge/gatsby-node, add the require highjack:
require = require(‘esm’)(module)

…Along with––at line 121––swapping out the require with interopRequire

One question: where are the other user-defined config resources being required? I searched for a while but couldn’t find exactly where it happens. If anyone could let me know, that would be so so nice. Thanks 💯

2reactions
harrysolovaycommented, Mar 31, 2019

@wardpeet I like that solution––esm is a great project. Could you or someone who has familiarity with the Gatsby codebase speak to whether the current caching would play nice with esm’s? If so, supporting ESModules would be as easy as adding the require highjack. Would love to be able to use this syntax.

Read more comments on GitHub >

github_iconTop Results From Across the Web

gatsby-config/node.js doesn't support ESM import/export #23705
Instead rename gatsby-config.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from package.json.
Read more >
Gatsby Config API
Plugins are Node.js packages that implement Gatsby APIs. The config file accepts an array of plugins. Some plugins may need only to be...
Read more >
Gatsby Changelog | 5.3.0
The ECMAScript module (ESM) format is the official TC39 standard for packaging ... Before now, gatsby-config and gatsby-node had to be written in...
Read more >
JavaScript Import vs Require Statements in Gatsby, Next.js ...
Why there is a difference of usage between the import statement and the require statement in different parts of the project directories ...
Read more >
Gatsby config with TypeScript - miyauci.me
It mentions the different execution environments and module systems for each Config ... As for features, gatsby-config and gatsby-node assume CommonJS .
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