Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't export anything but string or react component w/in pages directory #2787

Closed
benjaminhoffman opened this issue Nov 4, 2017 · 6 comments

Comments

@benjaminhoffman
Copy link
Contributor

Here is my setup

src
   pages
      services
         index.js
         data.js

data.js:

export const someArr = []
export const someObj = {}

index.js: import { someArr, someObj } from './data'

throws this error:
screen shot 2017-11-04 at 10 03 19 am

which links to this: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: [missing argument].[missing argument]

Observations:

  1. when I delete the import {} ... line, the error is still thrown. It appears the mere presence of the data.js file is breaking things

  2. when I change the exports to strings, everything works fine.

  3. when I move the someArr and someObj into the index.js file, everything works fine.

Thoughts?

@benjaminhoffman benjaminhoffman changed the title Can't export anything but string or react component Can't export anything but string or react component w/in pages directory Nov 4, 2017
@KyleAMathews
Copy link
Contributor

Ah yeah — any JS file under pages, Gatsby treats as a page component so it must have a default export that's a component.

Try moving data.js to src/data.js or something equivalent.

@benjaminhoffman
Copy link
Contributor Author

benjaminhoffman commented Nov 4, 2017

hmm that's what I figured immediately after you tweeted back! Thank you!

Can I open a PR to update this page to add a third list item: https://www.gatsbyjs.org/docs/debugging-html-builds/

something like:

2. Check that any js files in your pages directory are exporting either React components or strings.  

Any JS file under pages, Gatsby treats as a page component so it must have a default export that's a component.

No worries if you don't it's necessary but would've saved me a half day of debugging.

@KyleAMathews
Copy link
Contributor

That'd be super helpful! Sorry about the debugging pain :-(

@benjaminhoffman
Copy link
Contributor Author

#2788

Let me know if this is good or I should make additional notes/changes. Feel free to close out this issue and/or merge the PR if everything looks good!

@KyleAMathews
Copy link
Contributor

🥇

@JoshElias
Copy link

I like keeping files local to the components that use them. I have test files and CSS-In-JS files with some of my pages. Adding the following ignore to the gatsby-plugin-page-creator does nothing.

    {
      resolve: `gatsby-plugin-page-creator`,
      options: {
        path: `${__dirname}/src/pages`,
        ignore: [`**/*.emotion.*`, `**/*.stories.js`, `**/*.test.*`],
        // See pattern syntax recognized by micromatch
        // https://www.npmjs.com/package/micromatch#matching-features
      }
    },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants