简体   繁体   English

React 中的 Bootstrap — 无法解析 jQuery 模块?

[英]Bootstrap in React — can't resolve jQuery Module?

I'm working on a project using React.我正在使用 React 进行一个项目。 I've tried to install Bootstrap, but my project won't compile.我尝试安装 Bootstrap,但我的项目无法编译。 I've installed jquery@1.9.1 using npm.我已经使用 npm 安装了 jquery@1.9.1。 But I keep on getting the following error message:但我不断收到以下错误消息:

./node_modules/bootstrap/dist/js/bootstrap.js
Module not found: Can't resolve 'jquery' in '/Users/my_name/React_Projects/my_react_project/node_modules/bootstrap/dist/js'

This is a project that I was given to work on, so I haven't made it from scratch -- so I'm just piecing things together as I go.这是一个我被赋予工作的项目,所以我没有从头开始——所以我只是在做的时候把事情拼凑在一起。 I thought it may be an error with Webpack, so one of the things I tried to do was add jquery as an external resource in my webpack.config.js file -- but there is no such file in the project at this point.我认为这可能是 Webpack 的错误,所以我尝试做的一件事是在我的 webpack.config.js 文件中添加 jquery 作为外部资源——但此时项目中没有这样的文件。

This project was created using react-scripts, which I am told is a wrapper around Webpack.这个项目是使用 react-scripts 创建的,我听说它是​​ Webpack 的包装器。 Anyone have any idea how to resolve this issue?任何人都知道如何解决这个问题? How can I get jQuery to work so I can start using Bootstrap?如何让 jQuery 工作以便我可以开始使用 Bootstrap?

You will need to install the jquery and popper.js packages from npm.你需要从 npm 安装 jquery 和 popper.js 包。 In your terminal, run below command:在您的终端中,运行以下命令:

npm install jquery popper.js

or

yarn add jquery popper.js

Next, go to the src/index.js file and add the following imports:接下来,转到 src/index.js 文件并添加以下导入:

import $ from 'jquery';
import Popper from 'popper.js';
import 'bootstrap/dist/js/bootstrap.bundle.min';

There you have it.你有它。 You can now use the full power of Bootstrap and jQuery in your React application.您现在可以在 React 应用程序中使用 Bootstrap 和 jQuery 的全部功能。

This post solved my issues, maybe it can help you guys out too.这篇文章解决了我的问题,也许它也可以帮助你们。 Just run these two commands.只需运行这两个命令。

npm install --save resolve-url-loader --only=dev
npm i --save-dev bootstrap@next bootstrap-loader tether jquery

Look here link看这里链接

You will need to install jquery package from npm.你需要从 npm 安装 jquery 包。 In your terminal, run below command:在您的终端中,运行以下命令:

npm install jquery --save npm 安装 jquery --save

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM