gatsby: Develop works, build fails: Cannot read property 'initial' of undefined

My develop version runs fine, with no issues. However, I get the following error(s) when I run gatsby build or any sort of production script:

/home/coder/Developer/portfolio/node_modules/extract-text-webpack-plugin/index.js:22
			if(c.initial) return;
			    ^

TypeError: Cannot read property 'initial' of undefined
    at ExtractTextPlugin.<anonymous> (/home/coder/Developer/portfolio/node_modules/extract-text-webpack-plugin/index.js:22:8)
    at Array.forEach (native)
    at ExtractTextPlugin.mergeNonInitialChunks (/home/coder/Developer/portfolio/node_modules/extract-text-webpack-plugin/index.js:21:16)
    at ExtractTextPlugin.<anonymous> (/home/coder/Developer/portfolio/node_modules/extract-text-webpack-plugin/index.js:275:12)
    at Array.forEach (native)
    at ExtractTextPlugin.<anonymous> (/home/coder/Developer/portfolio/node_modules/extract-text-webpack-plugin/index.js:273:21)
    at /home/coder/Developer/portfolio/node_modules/extract-text-webpack-plugin/node_modules/async/lib/async.js:52:16
    at Object.async.forEachOf.async.eachOf (/home/coder/Developer/portfolio/node_modules/extract-text-webpack-plugin/node_modules/async/lib/async.js:236:30)
    at Object.async.forEach.async.each (/home/coder/Developer/portfolio/node_modules/extract-text-webpack-plugin/node_modules/async/lib/async.js:209:22)
    at ExtractTextPlugin.<anonymous> (/home/coder/Developer/portfolio/node_modules/extract-text-webpack-plugin/index.js:237:10)
    at Compilation.applyPluginsAsync (/home/coder/Developer/portfolio/node_modules/tapable/lib/Tapable.js:71:13)
    at Compilation.seal (/home/coder/Developer/portfolio/node_modules/webpack/lib/Compilation.js:525:7)
    at Compiler.<anonymous> (/home/coder/Developer/portfolio/node_modules/webpack/lib/Compiler.js:397:15)
    at /home/coder/Developer/portfolio/node_modules/tapable/lib/Tapable.js:103:11
    at Compilation.<anonymous> (/home/coder/Developer/portfolio/node_modules/webpack/lib/Compilation.js:445:10)
    at /home/coder/Developer/portfolio/node_modules/webpack/lib/Compilation.js:417:12

I have absolutely no idea what the root of this problem is. Any help would be appreciated.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 9
  • Comments: 28 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Yes @revolunet you should not have pages or components that are called within other pages within the folder of pages, that is, inside the folder of pages you must only have the pages that are functional for gatsby, if any of those pages needs some components you must position them outside of the page folder, this resolve the issue for me

@rshah03 could you please re-open the issue? The solution of editing files in node_modules is not acceptable, because people usually do not modify third-party-dependencies directly.

Fix: Add if (typeof c === 'undefined') return; right below line 21 in node_modules/extract-text-webpack-plugin/index.js

To me, this issue occurred when I imported constant in one page from another page. Fixed by moving constant to utilities folder and importing from there.

v2 is out which upgrades webpack and plugins + removes the confusing layouts component so closing. See the v2 migration guide to upgrade! https://next.gatsbyjs.org/docs/migrating-from-v1-to-v2/

My workaround, fwiw, in my package.json file:

{ ...
    "scripts": {
        "patch": "sed -i -e 's/if(c\\.initial) return;/if(!c||c.initial) return;/g' ./node_modules/extract-text-webpack-plugin/index.js",
        "build": "npm run patch && gatsby build",
    }
...
}

I guess this bug will have to wait for Gatsby 2 (#1824), since I don’t think Webpack devs will be interested in bug reports for Webpack 1. I am pretty sure that the bug still exists in Webpack 3, but I currently don’t have the time to create a minimal example. Maybe someone else could take a Webpack 3 config and try to replicate as follows:

  1. Create a file we will call shared, tell Webpack to make a chunk for it (or however that works, I am not sure).
  2. Create a lot of files that import shared and have Webpack make chunks for them.
  3. Create a chunk that depends on all chunks.
  4. Make sure that the shared chunk is being deleted by RemoveEmptyChunksPlugin.
  5. Check if the chunk that depends on all chunks still references the shared chunk. If it does, then the bug is not fixed in Webpack 3.

I worked around this bug by eliminating inter-page imports. I had a lot of pages import stuff from an index page and that was causing the index page to become an empty chunk (my guess is that it was being inlined into other chunks).

I hit this issue too - I added the workaround to node_modules/gatsby-1-config-extract-plugin/node_modules/extract-text-webpack-plugin, and then when I re-ran gatsby build the actual error showed up. There was a syntax error in my page template - I was assigning to a const. Just a heads up - for me this error was masking the syntax error, so when I fixed the syntax error I could remove the workaround and the build succeeded.

Did anyone find the fix of this issue?? Other than modifying the node modules files .