skip to Main Content

When I import the bootstrap in react project like this:

import bootstrap from "bootstrap";

shows error:

Uncaught SyntaxError: The requested module '/node_modules/.vite/deps/bootstrap.js?t=1693123714754&v=6a448f48' does not provide an export named 'default'

this is the demo code:

import React from 'react';
import bootstrap from "bootstrap";

const App: React.FC = () => {

  React.useEffect(() => {
    let modal = document.getElementById('exampleModal');
    if (modal) {
      var myModal = new bootstrap.Modal(modal);
      myModal.show();
    }
  }, []);

  return (
    <div>
      <div className="modal fade" id="exampleModal" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div className="modal-dialog">
          <div className="modal-content">
            <div className="modal-header">
              <h5 className="modal-title" id="exampleModalLabel">Modal title</h5>
              <button type="button" className="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div className="modal-body">
              ...
            </div>
            <div className="modal-footer">
              <button type="button" className="btn btn-secondary" data-bs-dismiss="modal">Close</button>
              <button type="button" className="btn btn-primary">Save changes</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}

export default App;

Am I missing something? I am using this command to install the bootstrap:

pnpm install [email protected]

2

Answers


  1. try this
    import * as bootstrap from 'bootstrap'

    Login or Signup to reply.
  2. Bootstrap css does not have a default export so you will need to import it as normal css

    import 'bootstrap/dist/css/bootstrap.min.css';

    but bootstrap js will not fully work on react the docs does not recommend it instead you need to install react-bootstrap

    pnpm install react-bootstrap bootstrap
    

    importing individual components from their own files is recommended

    import Button from 'react-bootstrap/Button';
    

    instead of

    import { Button } from 'react-bootstrap';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search