You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails 6 webpacker报错Cannot find module 'jquery'问题求助

Rails 6 webpacker报Cannot find module 'jquery' 解决方案

问题根因

你的config/webpack/environment.js中配置了resolve.alias将jquery指向了jquery-ui-dist包内的jquery文件,但你的package.json中并未安装jquery-ui-dist依赖,同时ProvidePlugin中的jquery路径和别名配置冲突,导致webpack解析jquery模块时无法找到对应文件。

解决步骤

  • 修正config/webpack/environment.js配置,删除冲突的别名设置,调整ProvidePlugin的jquery引用路径:
const { environment } = require('@rails/webpacker')

const webpack = require('webpack');
environment.plugins.prepend('Provide',
    new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        Popper: ['popper.js', 'default']
    })
);

module.exports = environment
  • 重新安装依赖确保所有包安装完整,执行命令:
    yarn install
    如果使用npm则执行:
    npm install
  • 清理webpacker缓存避免旧缓存影响:
rails tmp:cache:clear
bin/webpacker clobber
  • 重启Rails服务以及webpack-dev-server即可生效。

补充说明

如果你确实需要使用jquery-ui-dist包来引入jquery和jquery-ui,只需额外执行安装命令yarn add jquery-ui-dist,保留原别名配置即可,无需修改上述environment.js内容。

内容的提问来源于stack exchange,提问作者chmich

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 16:15:01