3

The most frustrating part is that I had this working earlier then somehow broke it, but I'm using gatsby-plugin-sharp and gatsby-plugin-image to add a photo to my homepage and seeing this error:

Gatsby-plugin-sharp wasn't setup correctly in gatsby-config.js. Make sure you add it to the plugins array.

I have no idea why... I've tried deleting node_modules, package_lock.json, reinstalling... beyond that I can also tell that the plugin is certainly in my array, and if I add options to the plugin-sharp that are unresolved, it asks me about them when the plugin loads.

I'm not sure if I need to order them in a different way and am kicking myself for not backing up.

gatsby-config.js:

module.exports = {
  siteMetadata: {
    siteUrl: 'myinfo-redacted',
    name: "myinfo-redacted",
    defaultTitle: "Bmyinfo-redacted",
    titleTemplate: "%s • myinfo-redacted",
    description: "myinfo-redacted",
    author: "myinfo-redacted",
    hero: {
      heading: "myinfo-redacted",
      subHeading: "myinfo-redacted",
    },
    social: [
      {
        name: "twitter",
        url: "myinfo-redacted",
      },
      {
        name: "linkedin",
        url: "myinfo-redacted",
      },
      {
        name: "devto",
        url: "myinfo-redacted",
      },
      {
        name: "youtube",
        url: "myinfo-redacted",
      },
    ],
  },
  plugins: [
    `gatsby-plugin-image`,
    `gatsby-plugin-sharp`,
    `gatsby-transformer-sharp`,
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `images`,
        path: `${__dirname}/src/gatsby-theme-purist/images`
      },
    },
    {
      resolve: 'gatsby-theme-purist',
      options: {
        contentBase: 'content',
        basePath: '/',
        featuredArticleLimit: 2,
      },
    },
    {
      resolve: `gatsby-plugin-manifest`,
      options: {
        name: `Bryan Barrows`,
        short_name: `Bryan Barrows`,
        start_url: `/`,
        icon: 'static/favicon.png'
      },
    },
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `markdown-pages`,
        path: `${__dirname}/content/articles`,
        ignore: [`**/drafts`]
      },
    },
    {
      resolve: `gatsby-plugin-feed`,
      options: {
        query: `
          {
            site {
              siteMetadata {
                title
                description
                siteUrl
                site_url: siteUrl
              }
            }
          }
        `,
        feeds: [
          {
            serialize: ({ query: { site, allMdx } }) => {
              return allMdx.edges.map(edge => {
                return Object.assign({}, edge.node.frontmatter, {
                  description: edge.node.frontmatter.description,
                  date: edge.node.frontmatter.date,
                  author: site.siteMetadata.name,
                  url: site.siteMetadata.siteUrl + edge.node.fields.slug,
                  guid: site.siteMetadata.siteUrl + edge.node.fields.slug,
                  custom_elements: [{ "content:encoded": edge.node.html }]
                });
              });
            },
            query: `
              {
                allMdx(
                  sort: { order: DESC, fields: [frontmatter___date] }, filter: {frontmatter: { draft: {eq: false} }}
                ) {
                  edges {
                    node {
                      excerpt
                      html
                      fields { slug }
                      frontmatter {
                        title
                        date
                        description
                        author
                      }
                    }
                  }
                }
              }
            `,
            output: "/rss.xml",
            title: "Bryan Barrows RSS Feed",
            // optional configuration to insert feed reference in pages:
            // if `string` is used, it will be used to create RegExp and then test if pathname of
            // current page satisfied this regular expression;
            // if not provided or `undefined`, all pages will have feed reference inserted
            match: "^/articles/"
          }
        ]
      }
    },
    {
      resolve: "gatsby-plugin-social9-socialshare",
      options: {
        content:  "ca24ce819d944595a0a4c3f53125d1a8",
        async: true,
        defer: true
      }
    },
    {
      resolve: 'gatsby-plugin-offline',
      options: {}
    },
  ],
}

package.json:

{
  "name": "gatsby-starter-purist",
  "version": "1.0.0",
  "main": "index.js",
  "repository": "",
  "author": "Sebastian Ojeda <hello@sebastianojeda.com> (@sebsojeda)",
  "license": "0BSD",
  "private": true,
  "scripts": {
    "start": "yarn dev",
    "dev": "GATSBY_GRAPHQL_IDE=playground gatsby develop",
    "build": "gatsby build",
    "clean": "gatsby clean"
  },
  "dependencies": {
    "gatsby": "^4.0.0-next",
    "gatsby-image": "^3.11.0",
    "gatsby-plugin-feed": "^4.4.0",
    "gatsby-plugin-image": "^2.4.0",
    "gatsby-plugin-manifest": "^4.3.0",
    "gatsby-plugin-mdx": "^3.4.0",
    "gatsby-plugin-offline": "^5.3.0",
    "gatsby-plugin-sharp": "^3.8.0",
    "gatsby-plugin-social9-socialshare": "^1.0.5",
    "gatsby-remark-images": "^6.4.0",
    "gatsby-source-filesystem": "^4.4.0",
    "gatsby-theme-purist": "^1.3.0",
    "gatsby-transformer-remark": "^5.3.0",
    "gatsby-transformer-sharp": "^4.4.0",
    "markdown-to-jsx": "^7.1.5",
    "react": "^16.14.0",
    "react-dom": "^16.14.0"
  }
}

The fact that it was working as intended earlier today leads me to believe that it should be possible and that something got funky with the gatsby-config.js - particularly becuase of the error about misconfiguration or it missing from plugin array.

3 Answers3

5

Got the same error message but from Netlify build log. Was able to resolve by updating to latest version: npm install gatsby-plugin-sharp@latest or yarn add gatsby-plugin-sharp@latest.

For more context, this was also after updating from Gatsby v3 to v4. Below was the before(not working) and after(working) of affected dependencies:

not working:

 "gatsby": "^4.14.1"  
 "gatsby-plugin-sharp": "^3.5.0"

working:

"gatsby": "^4.14.1"
"gatsby-plugin-sharp": "^4.15.0"
simonxcode
  • 143
  • 1
  • 3
  • 13
2

A local gatsby-plugin-sharp install inside the node_modules dir of a plugin/theme can be the cause of this issue.

Ensure that gatsby-plugin-sharp is being loaded from the root node_modules by all plugins. Look for a gatsby-plugin-sharp folder inside node_modules of each installed plugin. If you find one, you should need to configure the gatsby-plugin-sharp version of this package to match the one you're using on your website. Don't forget to remove package-lock.json and node_modules from both the plugin and your Gatsby installation, then run npm install again. You can try to match the gatsby-plugin-sharp version of your installation with the plugin version.

I'm not sure if this is your case, but I'm posting it here anyway to serve as a reference for others. In your package.json, gatsby-theme-purist version of gatsby-plugin-sharp is ^2.14.4. Your package requires ^3.8.0, maybe this theme is causing your issue.

stldo
  • 31
  • 4
0

I fixed this by messing around with versions. To be honest, I have been fighting for hours with this so I forgot exactly what I did. That said, I do have it working now with the following combination:

"@babel/preset-env": "7.9.0",
        "@chakra-ui/core": "^0.6.1",
        "@emotion/core": "^10.0.28",
        "@emotion/styled": "^10.0.27",
        "emotion-theming": "^10.0.27",
        "gatsby": "^4.4.0",
        "gatsby-image": "^2.3.1",
        "gatsby-plugin-chakra-ui": "^0.1.4",
        "gatsby-plugin-google-gtag": "^4.4.0",
        "gatsby-plugin-react-helmet": "^3.2.0",
        "gatsby-plugin-sharp": "^4.4.0",
        "gatsby-source-contentful": "^2.2.3",
        "gatsby-source-filesystem": "^2.2.2",
        "gatsby-transformer-sharp": "^2.4.2",
        "react": "^16.12.0",
        "react-dom": "^16.12.0",
        "react-helmet": "^5.2.1",
        "react-icons": "^3.9.0"

On Netlify, I used environment variable NODE_VERSION 14.17.5.

I am sorry I couldn't be more specific what I did.

Edit: You might want to try transformer-sharp: ^4.4.0 instead. I think I messed up above.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Victor
  • 1
  • 1
  • I'm running into similar problems. Sudden. In a build that was working until 2 days ago. I'll post more if I learn anything useful. But while I'm here, let me say: the gatsby + sharp is extremely brittle. When I have a problem with a build, it's often because imageSharp and gatsby aren't getting along. – dns Jan 19 '22 at 00:32
  • Follow-up to my earlier comment: In my case, I have a Gatsby site that consumes a Gatsby theme. The two package.json files both defined a devDependency of "gatsby-plugin-image" — but the versions were different. So, a deeper error was happening and showing up with this more cryptic "gatsby-plugin-sharp" error that you posted about. – dns Jan 19 '22 at 04:24