From 93b1caf248ffa76bd3b5c3c83f741565fd79ae95 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Wed, 14 Feb 2018 10:22:01 -0800 Subject: [PATCH] Catch require error for hot-reloading gatsby-config.js fixes #3990 --- .../internal-data-bridge/gatsby-node.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/gatsby/src/internal-plugins/internal-data-bridge/gatsby-node.js b/packages/gatsby/src/internal-plugins/internal-data-bridge/gatsby-node.js index c481e9ef9fd67..8d3b614147105 100644 --- a/packages/gatsby/src/internal-plugins/internal-data-bridge/gatsby-node.js +++ b/packages/gatsby/src/internal-plugins/internal-data-bridge/gatsby-node.js @@ -122,10 +122,18 @@ exports.sourceNodes = ({ boundActionCreators, store }) => { `gatsby-config.js` ) chokidar.watch(pathToGatsbyConfig).on(`change`, () => { - // Delete require cache so we can reload the module. - delete require.cache[require.resolve(pathToGatsbyConfig)] - const config = require(pathToGatsbyConfig) - createGatsbyConfigNode(config) + const oldCache = require.cache[require.resolve(pathToGatsbyConfig)] + try { + // Delete require cache so we can reload the module. + delete require.cache[require.resolve(pathToGatsbyConfig)] + const config = require(pathToGatsbyConfig) + createGatsbyConfigNode(config) + } catch (e) { + // Restore the old cache since requiring the new gatsby-config.js failed. + if (oldCache !== undefined) { + require.cache[require.resolve(pathToGatsbyConfig)] = oldCache + } + } }) }