JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > React >

Solve the Module not found: Can't resolve 'react-bootstrap' error

Author:JIYIK Last Updated:2025/03/15 Views:

npm install react-bootstrap bootstrapTo resolve the error “Module not found: Error: Can't resolve 'react-bootstrap'”, make sure to install the react-bootstrap package and restart our development server by opening a terminal in the root directory of our project and running the command

Open a terminal in the root directory of your project (where your package.json file is located) and run the following command:

#  for NPM
$ npm install react-bootstrap bootstrap

#  仅当我们使用 TypeScript
$ npm install --save-dev @types/react-bootstrap @types/bootstrap

# ----------------------------------------------

#  for YARN
$ yarn add react-bootstrap bootstrap

#  仅当我们使用 TypeScript
$ yarn add @types/react-bootstrap @types/bootstrap

This command will add the react-bootstrap package to your project's dependencies.

Be sure to restart your development server and IDE if necessary. Our development server will not pick up changes until you stop and re-run the npm start command.

You should now be able to import and use the react-bootstrap package in your React.js application.

App.js

import {Button, Badge} from 'react-bootstrap';
import 'bootstrap/dist/css/bootstrap.min.css';

// or
// import Button from 'react-bootstrap/Button';
// import Badge from 'react-bootstrap/Badge';

function App() {
  return (
    <div>
      <p>
        <Button variant="primary">
          Profile <Badge bg="secondary">9</Badge>
          <span className="visually-hidden">unread messages</span>
        </Button>
      </p>
    </div>
  );
}

export default App;

If the error is not resolved, try deleting our node_modules and package-lock.json (not package.json) files, re-run npm install and restart the IDE.

# 删除 node_modules 和 package-lock.json
$ rm -rf node_modules
$ rm -f package-lock.json

#  清除 npm 缓存
$ npm cache clean --force

$ npm install

If the error persists, make sure to restart your IDE and development server. VSCode often glitches and sometimes restarting can fix the problem.

If you still get the "Module not found: Error: Can't resolve 'react-bootstrap'" error, open your package.json file and make sure it includes the react-bootstrap package in the dependencies object.

{
  // ... rest
  "dependencies": {
    "bootstrap": "^5.1.3",
    "react-bootstrap": "^2.2.2",
  },

  "devDependencies": {
    "@types/bootstrap": "^5.1.9",
    "@types/react-bootstrap": "^0.32.29",
  }
}

The react-bootstrap module should not be installed globally or in your project's devDependencies, it should be in the dependencies object of your package.json file.

We can try adding these lines manually and re-running npm install.

$ npm install

Or install the latest version of the package:

# for NPM
$ npm install react-bootstrap@latest bootstrap@latest

# 仅当使用 TypeScript
$ npm install --save-dev @types/react-bootstrap@latest @types/bootstrap@latest

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

React Tutorial - Transferring Props

Publish Date:2025/03/16 Views:185 Category:React

React transfers Props. Props are generated when components are encapsulated. Components expose some properties (Props) to the outside world to complete some functions.

React Tutorial: Props Anti-Pattern

Publish Date:2025/03/16 Views:183 Category:React

React's Props anti-pattern, using Props to generate state in getInitialState is an anti-pattern - Anti-Pattern.

React Tutorial - Props Validation

Publish Date:2025/03/16 Views:99 Category:React

Props validation is a very useful way to use components correctly. It can avoid many bugs and problems as your application becomes more and more complex. In addition, it can make your program more readable.

Why do you need to bind event handlers in React Class Components?

Publish Date:2025/03/16 Views:58 Category:React

When using React, we must have come across control components and event handlers. We need to use `.bind()` in the constructor of the custom component to bind these methods to the component instance. As shown in the following code:

Solution to the error "does not contain a default export" in React

Publish Date:2025/03/16 Views:187 Category:React

When we try to use `default import` to import from a module that does not have a `default export`, we get a "does not contain a default export" error. To fix the error, make sure the module has named exports and wrap the import in curly braces, e.g.

Rendered fewer hooks than expected error in React

Publish Date:2025/03/16 Views:50 Category:React

When we use a hook after a condition that may return a value, we get the error "Rendered fewer hooks than expected. This may be caused by an accidental early return statement". To fix this error, you need to move all React hooks to any condition that

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial