gatsby: ENAMETOOLONG: name too long for long headers / slugs

Description

I’m experimenting with Gatsby & Wordpress (really cool stuff), when running gatsby build I hit this error:

error Generating JavaScript bundles failed

Error: ENAMETOOLONG: name too long, open '/home/user/applications/wpgatsby/public/path---crazy-insane-long-slug-why-on-earth-did-anyone-ever-choose-to-create-a-header-this-long-its-completely-ridiculous-please-make-it-stop-48ae67005d81988049b2.js'

What drives the file naming here?

Environment

Gatsby version: 1.1.40 Node.js version: 8.9.4 Operating System: Linux Mint 18.3

File contents (if changed):

gatsby-config.js:

module.exports = {
  siteMetadata: {
    title: 'Gatsby Default Starter',
  },
  plugins: [
    'gatsby-plugin-react-helmet',
    {
      resolve: `gatsby-source-wordpress`,
      options: {
        /*
        * The base URL of the Wordpress site without the trailingslash and the protocol. This is required.
        * Example : 'gatsbyjswpexample.wordpress.com' or 'www.example-site.com'
        */
        baseUrl: `sitewithlongnames.com`,
        // The protocol. This can be http or https.
        protocol: `http`,
        // Indicates whether the site is hosted on wordpress.com.
        // If false, then the asumption is made that the site is self hosted.
        // If true, then the plugin will source its content on wordpress.com using the JSON REST API V2.
        // If your site is hosted on wordpress.org, then set this to false.
        hostingWPCOM: false,
        // If useACF is true, then the source plugin will try to import the Wordpress ACF Plugin contents.
        // This feature is untested for sites hosted on Wordpress.com
        useACF: false,
        verboseOutput: true,
      },
    }, 
    'gatsby-plugin-netlify', // make sure to put last in the array
  ],
};

package.json:

...
"dependencies": {
    "gatsby": "^1.9.188",
    "gatsby-image": "^1.0.35",
    "gatsby-link": "^1.6.34",
    "gatsby-plugin-netlify": "^1.0.18",
    "gatsby-plugin-react-helmet": "^2.0.3",
    "gatsby-source-wordpress": "^2.0.54",
    "react-helmet": "^5.2.0"
  },
...

gatsby-node.js:

const _ = require(`lodash`)
const path = require(`path`)
const Promise = require(`bluebird`)
const slash = require(`slash`)

// Implement the Gatsby API “createPages”. This is
// called after the Gatsby bootstrap is finished so you have
// access to any information necessary to programmatically
// create pages.
// Will create pages for Wordpress pages (route : /{slug})
// Will create pages for Wordpress posts (route : /post/{slug})
exports.createPages = ({ graphql, boundActionCreators }) => {
  const { createPage } = boundActionCreators
  return new Promise((resolve, reject) => {
    // The “graphql” function allows us to run arbitrary
    // queries against the local Wordpress graphql schema. Think of
    // it like the site has a built-in database constructed
    // from the fetched data that you can run queries against.

    // ==== PAGES (WORDPRESS NATIVE) ====
    graphql(
      `
        {
          allWordpressPage {
            edges {
              node {
                id
                slug
                status
                template
              }
            }
          }
        }
      `
    )
      .then(result => {
        if (result.errors) {
          console.log(result.errors)
          reject(result.errors)
        }
        // Create Page pages.
        const pageTemplate = path.resolve(`./src/templates/page.js`)
        // We want to create a detailed page for each
        // page node. We'll just use the Wordpress Slug for the slug.
        // The Page ID is prefixed with 'PAGE_'
        _.each(result.data.allWordpressPage.edges, edge => {
          // Gatsby uses Redux to manage its internal state.
          // Plugins and sites can use functions like "createPage"
          // to interact with Gatsby.
          createPage({
            // Each page is required to have a `path` as well
            // as a template component. The `context` is
            // optional but is often necessary so the template
            // can query data specific to each page.
            path: `/${edge.node.slug}/`,
            component: slash(pageTemplate),
            context: {
              id: edge.node.id,
            },
          })
        })
      })
      // ==== END PAGES ====

      // ==== POSTS (WORDPRESS NATIVE AND ACF) ====
      .then(() => {
        graphql(
          `
            {
              allWordpressPost {
                edges {
                  node {
                    id
                    slug
                    status
                    template
                    format
                  }
                }
              }
            }
          `
        ).then(result => {
          if (result.errors) {
            console.log(result.errors)
            reject(result.errors)
          }
          const postTemplate = path.resolve(`./src/templates/post.js`)
          // We want to create a detailed page for each
          // post node. We'll just use the Wordpress Slug for the slug.
          // The Post ID is prefixed with 'POST_'
          _.each(result.data.allWordpressPost.edges, edge => {
            createPage({
              path: edge.node.slug,
              component: slash(postTemplate),
              context: {
                id: edge.node.id,
              },
            })
          })
          resolve()
        })
      })
    // ==== END POSTS ====
  })
}

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 19 (15 by maintainers)

Most upvoted comments

We’ve been experiencing this problem too.

Hi all, I wasn’t sure if I should open a new issue or not, but I’m having a similar problem with the slug length being too long.

Recently I was trying to clone and build the new freeCodeCamp learn project and ran into an error when running yarn develop, which in turn runs yarn build:frame-runner && gatsby develop:

ERROR  Failed to compile with 3 errors                               16:22:30

These dependencies were not found:

* /home/kris/Documents/GitHub/forks/learn/.cache/json/javascript-algorithms-and-data-structures-object-oriented-programming-use-closure-to-protect-properties-within-an-object-from-being-modified-externally.json in ./.cache/sync-requires.js
* /home/kris/Documents/GitHub/forks/learn/.cache/json/javascript-algorithms-and-data-structures-functional-programming-use-the-every-method-to-check-that-every-element-in-an-array-meets-a-criteria.json in ./.cache/sync-requires.js
* /home/kris/Documents/GitHub/forks/learn/.cache/json/javascript-algorithms-and-data-structures-functional-programming-use-the-some-method-to-check-that-any-elements-in-an-array-meet-a-criteria.json in ./.cache/sync-requires.js

To install them, you can run: npm install --save /home/kris/Documents/GitHub/forks/learn/.cache/json/javascript-algorithms-and-data-structures-object-oriented-programming-use-closure-to-protect-properties-within-an-object-from-being-modified-externally.json /home/kris/Documents/GitHub/forks/learn/.cache/json/javascript-algorithms-and-data-structures-functional-programming-use-the-every-method-to-check-that-every-element-in-an-array-meets-a-criteria.json /home/kris/Documents/GitHub/forks/learn/.cache/json/javascript-algorithms-and-data-structures-functional-programming-use-the-some-method-to-check-that-any-elements-in-an-array-meet-a-criteria.json

When I try to run npm install --save … from the error message, I get an error from npm saying that the filename is too long. I believe it’s because Linux Mint 18.3 home directory is encrypted, so filenames can only be ~143 characters.

Is there an easy way to work around this limitation? Maybe change the way that the pages are saved in .cache/json to create a file structure that mimics that of the source data? For example, saving a page as javascript-algorithms-and-data-structures/object-oriented-programming/verify-an-objects-constructor-with-instanceof.json rather than javascript-algorithms-and-data-structures-object-oriented-programming-verify-an-objects-constructor-with-instanceof.json.

When I updated to "node --max_old_space_size=1200000 --stack-size=65000 ./node_modules/.bin/gatsby build --prefix-paths", I got a passing build! 🚀 But these parameters were a total stab in the dark…is there any general guidance you recommend @KyleAMathews regarding larger site builds? Happy to dig in and try to write some docs if it would be helpful.