npm install bootstrap 不能直接使用 JS 组件,因缺 peer dependency @popperjs/core,导致 Uncaught ReferenceError: Popper is not defined;必须显式执行 npm install bootstrap @popperjs/core 才能正常使用 Dropdown、Tooltip、Modal 等依赖定位计算的组件。

npm install bootstrap 能装上文件,但大概率 JS 组件直接报错——因为缺 @popperjs/core,不是命令写错了,是依赖链没补全。
为什么 npm install bootstrap 后 Dropdown/Tooltip 不工作
Bootstrap 5+ 的 Dropdown、Tooltip、Modal 等组件底层调用 Popper 做定位计算,而 bootstrap 包本身只声明 @popperjs/core 为 peerDependency,npm 不会自动安装它。
常见错误现象:
Uncaught ReferenceError: Popper is not defined- 控制台提示
Missing required dependency: popper.js - 点击下拉菜单无反应,Tooltip 鼠标悬停不弹出
正确做法必须显式安装两个包:
npm install bootstrap @popperjs/core
注意:不要用 bootstrap@4 或带 jquery 的旧组合,Angular 16/Vue 3/Vite 项目默认应选 bootstrap@5.3+。
要不要加 --save?npm 版本决定行为
现代 npm(≥ v7)默认把包写入 package.json 的 dependencies,但仍有例外:
- 如果执行后
package.json没新增"bootstrap": "^5.3.3",说明安装被跳过或缓存异常 - 显式加
--save(或-S)能强制写入,尤其在 CI 环境或老旧 npm 配置下更可靠 - 推荐统一用:
npm install bootstrap @popperjs/core --save
验证是否生效:检查 node_modules/bootstrap/dist/ 下是否存在 bootstrap.min.css 和 bootstrap.bundle.min.js;再确认 node_modules/@popperjs/core 目录存在。
安装后 JS 还是报错?检查构建工具导入方式
即使包装好了,Webpack/Vite/Angular CLI 仍可能因导入路径或模块解析失败导致 Cannot find module 'bootstrap':
- 在
main.ts(Angular)或main.js(Vue/Vanilla)中,用 ES import 方式引入 JS:import { Modal } from 'bootstrap',而不是require('bootstrap') - 若用 Sass 定制,需在构建配置里加
includePaths: ['node_modules'],否则@import 'bootstrap/scss/functions'会报路径找不到 - Angular 项目必须在
angular.json的styles和scripts字段里显式添加node_modules/bootstrap/dist/css/bootstrap.min.css和node_modules/bootstrap/dist/js/bootstrap.min.js(或用 bundle 版)
别直接在 HTML 里 script 引 node_modules/ 下的文件——构建工具不会处理这类路径,本地开发可能侥幸跑通,打包后必挂。
真正容易被忽略的是:装完只是第一步,@popperjs/core 必须和 bootstrap 版本对齐(例如 bootstrap@5.3.3 对应 @popperjs/core@2.11.8),手动升级其中一个却忘了另一个,就可能触发 TypeError: Popper.createPopper is not a function 这类隐性兼容问题。


















