gatsby: Generating SSR bundle failed - Can't resolve 'gatsby-link' in 'xxx/.cache'

Description

When I try to run gatsby build or gatsby develop I keep running into the following issue

Generating SSR bundle failed

Can't resolve 'gatsby-link' in 'xxx/.cache'

If you're trying to use a package make sure that 'gatsby-link' is installed. If you're trying to use a local file make sure that the path is correct.

File: .cache/gatsby-browser-entry.js

Steps to reproduce

This is project that has been working for a few months and I’ve updated yesterday and now I’m getting the above error message

Expected result

Should be able to run gatsby build and gatsby develop with no errors

Actual result

Error mentioned above

Environment

  System:
    OS: macOS 10.15.3
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 14.5.0 - ~/.nvm/versions/node/v14.5.0/bin/node
    Yarn: 1.22.4 - ~/.yvm/shim/yarn
    npm: 6.14.5 - ~/.nvm/versions/node/v14.5.0/bin/npm
  Languages:
    Python: 2.7.16 - /usr/bin/python
  Browsers:
    Chrome: 83.0.4103.116
    Edge: 83.0.478.61
    Firefox: 76.0
    Safari: 13.0.5

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 11
  • Comments: 24 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Have you tried deleting the node-modules folder and the package-lock.json file, running gatsby clean, followed by npm install or yarn? That usually does the trick for me with cache issues, frustrating when they pop up for seemingly no reason out of nowhere!

In my case this problem was caused by the way Yarn hoists dependencies if you’re using its workspaces feature.

The workspace folder structure looks something like this:

package.json  // workspace root
node_modules
project-a
  package.json
  node_modules
  .config
project-b
  package.json
  node_modules
  .config

Because both project-a and project-b depend on the same version of gatsby, Yarn hoists it to the root node_modules. Because nothing else in the workspace depends on gatsby-link, Yarn installs that underneath gatsby. The result looks like this:

package.json
node_modules
  gatsby
    gatsby-link  // underneath `gatsby`
project-a
  .cache
  ...
project-b
  .cache
  ...

When project-a/.cache/whatever.js requires gatsby-link, the module isn’t in scope because it isn’t in the local project-a/node_modules and it’s not visible in the root node_modules. That’s why you can resolve the issue with yarn add gatsby-link in one or both of the projects - doing so will cause Yarn to hoist it into the top level of the root node_modules alongside gatsby, which brings it into scope. In other words, if you can get to this situation, it’ll work:

package.json
node_modules
  gatsby
  gatsby-link  // parallel with `gatsby`
project-a
  .cache
  ...
project-b
  .cache
  ...

I think this could legitimately be called a Gatsby issue, because the files in the .cache folder are making assumptions Yarn doesn’t recognise, though I’m not sure how’d you’d go about solving it.

@domjtalbot on gatsby@2.20.36 had exactly the same issue and yarn adding gatsby-link and gatsby-react-router-scroll as direct dependencies solved the issue. Not sure why this is happening, but glad to see that it’s not just me.

I had this issue when upgrading to gatsby@5.1.0. Seemed to be resolved after removing the @gatsbyjs/reach-router dependency. But now I broke my SEO component which was dependent on that package 😕 Anyone have an idea how to fix this without removing the package?

edit: Gatsby internally uses @gatsbyjs/reach-router@2.0.0-v2.0.3. The latest released version of @gatsbyjs/reach-router is 1.3.9 this is what’s causing the issue. If you update to the same version it works again.

I had this issue when updated to gatsby@5.0.0. The issue was that I had @gatsbyjs/reach-router in my app’s dependencies, after removing it the build went fine

Having the same issue after upgrading to gatsby@2.29.1. As mentioned in https://github.com/gatsbyjs/gatsby/issues/3069, not sure why gatsby-link isn’t just included in the gatsby package?

same with the package gatsby-react-router-scroll.

@blainekasten would it help to have particular dependency version information in the changelog? For example, “as of version gatsby 1.2.3 you will need to have to at least: gatsby-plugin-something-or-other 3.2.1 and gatsby-transformer-something-or-other 4.5.6 installed alongside it”. What do you think?