Search code examples
reactjsgatsbyyarnpkgcontact-formnetlify

Installing 'react-bootstrap' on a Gatsby Starter deployed on Netlify


I'm very new to both gatsby and react. I'm making a Contact form and wanted to do it based on a tutorial that used components from react-bootstrap, however I don't know how to install it on my the gatsby starter I've deployed through my github repo.

My code to give an idea:

import React from 'react';

import Layout from '@common/Layout';
import Navbar from '@common/Navbar';

import Header from '@sections/Header';
import About from '@sections/About'; 
import Footer from '@sections/Footer';

import {Row, Col, Container, Form, Button} from "react-bootstrap" 

const IndexPage = () => (
  <Layout>
    <Navbar />
    <Header />
    <About>
      <Container>
        <Form name= "contact v1" method="post" data-netlify="true" onSubmit="submit">
         <input type="hidden" name="form-name" value="contact v1" />
          <Row>
            <Col>
              <Form.Group>
                <Form.Label>First Name </Form.Label>
                <Form.Control required size="lg" type="text"/>
              </Form.Group>
            </Col>
            <Col>
              <Form.Group>
                <Form.Label>Last Name </Form.Label>
                <Form.Control required size="lg" type="text"/>
              </Form.Group>
            </Col>
          </Row>
          <Form.Group>
                <Form.Label>How can we help? </Form.Label>
                <Form.Control required as="textarea" rows="3" placeholder="What do you do?"/>
          </Form.Group>
          <Button type="submit"> Submit </Button>
        </Form>
      </Container>
    </About>
                
    <Footer />
  </Layout>
);

export default IndexPage; 

And this is the error when I deploy on Netlify

3:08:08 PM: failed Building production JavaScript and CSS bundles - 15.912s
3:08:08 PM: error Generating JavaScript bundles failed
3:08:08 PM: Can't resolve 'react-bootstrap' in '/opt/build/repo/src/pages'
3:08:08 PM: If you're trying to use a package make sure that 'react-bootstrap' is installed. If you're trying to use a local file make sure that the path is correct.
3:08:08 PM: not finished Generating image thumbnails - 20.910s
3:08:08 PM: error Command failed with exit code 1.
3:08:08 PM: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Solution

  • As you can see in the docs, just run:

    npm install react-bootstrap bootstrap
    

    Keep in mind that when you do:

    import {Row, Col, Container, Form, Button} from "react-bootstrap" 
    

    You are importing the components from the node_modules directory, specifically from react-bootstrap folder, so you need to install the dependency first.