gatsby-node.esm.js
February 25, 2020
Using import/export in node.js with esm
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) :
- Install esm
npm install esm
- Create a file called gatsby-node.esm.js in your root folder (the same folder that contains gatsby-node.js)
- Move all your code from from gatsby-node.js to gatsby-node.esm.js
- Replace all the code in gatsby-node.js with the following:
require = require('esm')(module)
module.exports = require('./gatsby-node.esm.js')
- Use import in gatsby-node.esm.js all you want