February 25, 2020

Using import/export in node.js with esm

gatsby ES6 issue

Issue (Sep 2018) : gatsby-node.js doesn't allow ES6 javascript.
Error :

Error: <root>/gatsby-node.js:1
SyntaxError: Unexpected token import

Workaround (Dec 2018) :

  1. Install esm
npm install esm    
  1. Create a file called gatsby-node.esm.js in your root folder (the same folder that contains gatsby-node.js)
  2. Move all your code from from gatsby-node.js to gatsby-node.esm.js
  3. Replace all the code in gatsby-node.js with the following:
require = require('esm')(module)
module.exports = require('./gatsby-node.esm.js')
  1. Use import in gatsby-node.esm.js all you want