【问题标题】:Netlify build fail for Gatsby site盖茨比网站的 Netlify 构建失败
【发布时间】:2021-04-08 21:29:12
【问题描述】:

我正在尝试使用博客部署我的第一个 gatsby 站点,但它一直失败。我已经更新了所有依赖项,但现在我无法弄清楚到底是什么问题。

我真的很感激任何建议,它已经困扰了我好几天了。在此之前,我遇到了一堆 npm 错误,因此我更新了所有我认为可以解决问题的依赖项。

仅供参考,我在博客中使用内容丰富的 cms

这里的构建似乎失败了:

12:33:55 PM: error A page component must export a React component for it to be valid. Please make sure this file exports a React component:
12:33:55 PM: /opt/build/repo/src/pages/BlogElements.js
12:33:55 PM: not finished createPagesStatefully - 0.064s
12:33:55 PM: npm ERR! code ELIFECYCLE
12:33:55 PM: npm ERR! errno 1
12:33:55 PM: npm ERR! gatsby-starter-hello-world@0.1.0 build: `gatsby build`
12:33:55 PM: npm ERR! Exit status 1
12:33:55 PM: npm ERR!
12:33:55 PM: npm ERR! Failed at the gatsby-starter-hello-world@0.1.0 build script.
12:33:55 PM: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
12:33:55 PM: npm ERR! A complete log of this run can be found in:
12:33:55 PM: npm ERR!     /opt/buildhome/.npm/_logs/2021-01-02T12_33_55_072Z-debug.log

BlogElements.js 文件只是一个样式化组件:

import styled from 'styled-components';



export const Styledh2 = styled.h2`
  color: purple;
`;

export const StyledBox = styled.li`
 
    width: 500px;
    background-color: lightblue;
    float: left

`

export const StyledLink = styled.div`
    background-color: yellow;
    position: relative;
    left: 3rem;
    justify-content: space-around
`

【问题讨论】:

    标签: deployment build graphql gatsby netlify


    【解决方案1】:

    您的问题出现是因为 BlogElements 存储在 /pages 文件夹下,因此 Gatsby 正在尝试基于该文件夹结构创建 URL 路径。由于您没有返回有效的 React 组件,因此您的代码正在破坏。

    如果您仅将BlogElements 用于样式设置,请将其移动到另一个位置(例如,在/src/components/styles 下),然后将其导入到您需要的任何位置。

    /pages 下的有效样式组件应如下所示:

    import React from "react"
    import styled from "styled-components"
    
    const Container = styled.div`
      margin: 3rem auto;
      max-width: 600px;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
    `
    
    const UserWrapper = styled.div`
      display: flex;
      align-items: center;
      margin: 0 auto 12px auto;
      &:last-child {
        margin-bottom: 0;
      }
    `
    
    const Avatar = styled.img`
      flex: 0 0 96px;
      width: 96px;
      height: 96px;
      margin: 0;
    `
    
    const Description = styled.div`
      flex: 1;
      margin-left: 18px;
      padding: 12px;
    `
    
    const Username = styled.h2`
      margin: 0 0 12px 0;
      padding: 0;
    `
    
    const Excerpt = styled.p`
      margin: 0;
    `
    
    const User = props => (
      <UserWrapper>
        <Avatar src={props.avatar} alt="" />
        <Description>
          <Username>{props.username}</Username>
          <Excerpt>{props.excerpt}</Excerpt>
        </Description>
      </UserWrapper>
    )
    
    export default function UsersList() {
      return (
        <Container>
          <h1>About Styled Components</h1>
          <p>Styled Components is cool</p>
          <User
            username="Jane Doe"
            avatar="https://s3.amazonaws.com/uifaces/faces/twitter/adellecharles/128.jpg"
            excerpt="I'm Jane Doe. Lorem ipsum dolor sit amet, consectetur adipisicing elit."
          />
          <User
            username="Bob Smith"
            avatar="https://s3.amazonaws.com/uifaces/faces/twitter/vladarbatov/128.jpg"
            excerpt="I'm Bob smith, a vertically aligned type of guy. Lorem ipsum dolor sit amet, consectetur adipisicing elit."
          />
        </Container>
      )
    }
    

    请注意,上面的示例 sn-p 将 UsersList 导出为 React 组件,这与您的 BlogElements 不同。

    在将项目推送到 Netlify 之前尝试在本地构建您的项目以避免该错误。

    【讨论】:

      猜你喜欢
      • 2020-11-12
      • 2021-04-05
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      • 2019-09-29
      • 2020-02-02
      • 2023-03-26
      • 2020-05-23
      相关资源
      最近更新 更多