I'm fairly new to React, Vite and Bootstrap, so I'm trying to get a basic app set up.
I essentially followed a mix of these tutorials:
- https://www.traversymedia.com/blog/vite-crash-course
- https://getbootstrap.com/docs/5.2/getting-started/vite/.
I was able to succesfully make some of my own components and import react-bootstrap components into them. The Header component I made (see Header.jsx below) works great.
However, whenever I try to import other react-bootstrap components to use, for example:
import Form from 'react-bootstrap/Accordion';
I get this error:
[plugin:vite:import-analysis] Failed to resolve import "react-bootstrap/Accordion" from "src/components/Header.jsx". Does the file exist?
I'm confused because I don't know what could be different about the syntax, and I can find a package.json file under the node_modules/react-bootstrap/Accordion folder (see below).
Below is also my package.json file.
Header.jsx file
import Container from 'react-bootstrap/Container';
import Nav from 'react-bootstrap/Nav';
import Navbar from 'react-bootstrap/Navbar';
import Image from 'react-bootstrap/Image';
function Header() {
return (
<Navbar fixed ="top" expand="lg" className="navbar-lightnavbar-custom bg-body-tertiary">
<Container>
<Navbar.Brand href="#home">
<Image className ="logo d-inline-block align-top" alt="my logo" src="./src/assets/logo.png" thumbnail />
</Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="me-auto header-text-color">
<Nav.Link href="#home">Home</Nav.Link>
<Nav.Link href="#link">Email</Nav.Link>
</Nav>
</Navbar.Collapse>
</Container>
</Navbar>
);
}
export default Header;
package.json
{
"name": "energy-data-test-app",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint src --ext js,jsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.0.37",
"@types/react-dom": "^18.0.11",
"@vitejs/plugin-react": "^4.0.0",
"bootstrap": "^5.3.0",
"eslint": "^8.38.0",
"eslint-plugin-react": "^7.25.3",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.3.4",
"react-bootstrap": "^2.8.0",
"sass": "^1.63.6",
"vite": "^4.3.9"
}
}
After some searching around for the error online, I think my issue has something to do with my dependencies and compatibility. Specifically, I saw this question which took me to the bootstrap/react-bootstrap compatibility table. It seems like my bootstrap v3.x and react-bootstrap v2.x versions should work together?
I tried uninstalling and reinstalling the npm packages (bootstrap and react-bootstrap), and also saving them with the --save-dev
flag but that didn't seem to change anything. I also tried clearing my browser's cache, but again, no issue. It's weird to me because the components I already imported seem to continue to have no issue.
To be clear, I am not experiencing the issue described in this post-- instead, some components do import, and others do not.
Why are some components importing correctly and others are not? Could this be an issue with some of my other dependencies (e.g. eslint)?
Thank you in advance for your help!