【问题标题】:Cannot Require Popper with Webpack不能使用 Webpack 要求 Popper
【发布时间】:2019-10-18 15:10:47
【问题描述】:

我正在尝试将我的 javascript 文件移动到 Webpack。我对 Webpack 不是很熟悉,所以我不确定我是否正确地编写了这些代码。我正在尝试加载 jquery-ui、popper 和 bootstrap 4。但是,在需要 Popper 时出现错误。请注意,我正在为 Ruby on Rails 使用 Wepacker gem。

我在 environment.js 文件中包含以下代码,以便在每个文件中自动包含 jQuery。

const { environment } = require('@rails/webpacker')

const webpack = require('webpack')
environment.plugins.append('Provide', new webpack.ProvidePlugin({
  $: 'jquery',
  jQuery: 'jquery'
}))

module.exports = environment

这部分有效。所以,我从那里跑了yarn add jquery-ui

然后在我的 /pack/application.js 文件中包含require('jquery-ui')

从我的 js 文件中,以下代码打印到我的控制台:

$(document).ready(function(){
    if (jQuery.ui) {
      console.log("loaded");
    }
});

在此之后,我尝试安装并要求 popperyarn add popper

然后从我的 document.ready 函数内部调用 popper 我得到一个错误:

$(document).ready(function(){

    console.log(window.Popper)

});

错误:

Uncaught Error: Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /home/ubuntu/environment/node_modules/path-platform/path.js: 'return' outside of function (32:2)

  30 | if (_path.posix) {
  31 |   module.exports = _path;
  32 |   return;
     |   ^
  33 | }
  34 | 
  35 | // resolves . and .. elements in a path array with directory names there
    at Parser.raise (home/ubuntu/environment/node_modules/@babel/parser/lib/index.js:6420)
    at Parser.parseReturnStatement 
(home/ubuntu/environment/node_modules/@babel/parser/lib/index.js:10370)
    at Parser.parseStatementContent 
(home/ubuntu/environment/node_modules/@babel/parser/lib/index.js:10057)
    at Parser.parseStatement (home/ubuntu/environment/node_modules/@babel/parser/lib/index.js:10009)
    at Parser.parseBlockOrModuleBlockBody 
(home/ubuntu/environment/node_modules/@babel/parser/lib/index.js:10585)
    at Parser.parseBlockBody (home/ubuntu/environment/node_modules/@babel/parser/lib/index.js:10572)
    at Parser.parseBlock (home/ubuntu/environment/node_modules/@babel/parser/lib/index.js:10556)
    at Parser.parseStatementContent 
    (home/ubuntu/environment/node_modules/@babel/parser/lib/index.js:10085)
    at Parser.parseStatement (home/ubuntu/environment/node_modules/@babel/parser/lib/index.js:10009)
    at Parser.parseIfStatement 
    (home/ubuntu/environment/node_modules/@babel/parser/lib/index.js:10363)
    at Parser.parseStatementContent 
    (home/ubuntu/environment/node_modules/@babel/parser/lib/index.js:10054)
    at Parser.parseStatement (home/ubuntu/environment/node_modules/@babel/parser/lib/index.js:10009)
    at Parser.parseBlockOrModuleBlockBody 
    (home/ubuntu/environment/node_modules/@babel/parser/lib/index.js:10585)
at Parser.parseBlockBody (home/ubuntu/environment/node_modules/@babel/parser/lib/index.js:10572)
at Parser.parseTopLevel (home/ubuntu/environment/node_modules/@babel/parser/lib/index.js:9940)
at Parser.parse (home/ubuntu/environment/node_modules/@babel/parser/lib/index.js:11447)
at parse (home/ubuntu/environment/node_modules/@babel/parser/lib/index.js:11483)
at parser (home/ubuntu/environment/node_modules/@babel/core/lib/transformation/normalize-file.js:168)
at normalizeFile (home/ubuntu/environment/node_modules/@babel/core/lib/transformation/normalize-file.js:102)
at runSync (home/ubuntu/environment/node_modules/@babel/core/lib/transformation/index.js:44)
at runAsync (home/ubuntu/environment/node_modules/@babel/core/lib/transformation/index.js:35)
at process.nextTick (home/ubuntu/environment/node_modules/@babel/core/lib/transform.js:34)
at process._tickCallback (internal/process/next_tick.js:61)
at Object../node_modules/path-platform/path.js (index.js:82)
at __webpack_require__ (bootstrap:19)
at Object.<anonymous> (index.js:1)
at Object../node_modules/parents/index.js (index.js:39)
at __webpack_require__ (bootstrap:19)
at Object.<anonymous> (index.js:19)
at Object../node_modules/module-deps/index.js (index.js:623)
at __webpack_require__ (bootstrap:19)
at Object.<anonymous> (index.js:3)
at Object../node_modules/browserify/index.js (index.js:846)

这是我的 pack/application.js 文件

require("@rails/ujs").start()
require("@rails/activestorage").start()

require('jquery-ui')
require('popper')

【问题讨论】:

    标签: javascript ruby-on-rails webpack webpacker


    【解决方案1】:

    我只是做了同样的事情,然后才意识到问题所在。 popper 是 Node.js 中的某种高级浏览器测试库。 Bootstrap 依赖于 popper.js ,这是一个浏览器的工具提示弹出库。

    因此,要解决此问题,您需要:

    yarn remove popper
    yarn add popper.js
    

    【讨论】:

      猜你喜欢
      • 2018-09-09
      • 2018-02-16
      • 2015-11-01
      • 2017-04-29
      • 1970-01-01
      • 2021-02-23
      • 2022-12-03
      • 1970-01-01
      • 2016-07-05
      相关资源
      最近更新 更多