代码之家  ›  专栏  ›  技术社区  ›  Luma

无法读取未定义的属性“jquery”

  •  0
  • Luma  · 技术社区  · 3 年前

    我有一个带有Webpacker的Rails6应用程序。节点包已成功安装了纱线。

    该应用程序在开发和生产模式下与彪马服务器一起在本地工作。但当在我们的虚拟主机上调用网站时,Phusion Passenger无法加载Bootstrap 4 JavaScript部分:

    /var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:21178
              var e = i.default.fn.jquery.split(' ')[0].split('.');
                                   ^
    
    TypeError: Cannot read property 'jquery' of undefined
        at Object.jQueryDetection (/var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:21178:32)
        at a (/var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:21183:9)
        at Object.<anonymous> (/var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:22435:56)
        at n (/var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:8:17)
        at Module.<anonymous> (/var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:18176:7)
        at Module.<anonymous> (/var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:18179:7)
        at n (/var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:8:17)
        at /var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:26:98
        at Object.<anonymous> (/var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:27:2)
        at Module._compile (internal/modules/cjs/loader.js:1085:14)
    

    enter image description here

    app/javascript/packs/application.js:

    // This file is automatically compiled by Webpack, along with any other files
    // present in this directory. You're encouraged to place your actual application logic in
    // a relevant structure within app/javascript and only use these pack files to reference
    // that code so it'll be compiled.
    
    console.log('Webpacker: START application.js');
    
    require('@rails/ujs').start();
    require('turbolinks').start();
    require('@rails/activestorage').start();
    require('channels');
    require('jquery');
    
    // Uncomment to copy all static images under ../images to the output folder and reference
    // them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>)
    // or the `imagePath` JavaScript helper below.
    //
    // const images = require.context('../images', true)
    // const imagePath = (name) => images(name, true)
    
    // #################### Proprietary configuration ####################
    
    // JAVASCRIPT LIBRARIES
    import 'popper.js';
    import 'bootstrap';
    import '@fortawesome/fontawesome-free';
    
    // CSS OF USED LIBRARIES
    import 'stylesheets/application';
    
    // datetime picker
    // load "moment" globally to avoid:
    // "Error: Tempus Dominus Bootstrap4's requires moment.js. Moment.js must be included before Tempus Dominus Bootstrap4's JavaScript."
    // See https://stackoverflow.com/questions/49580266/tempus-dominus-bootstrap4-requires-moment-js-datetime-picker
    global.moment = require('moment');
    require('moment/locale/de');
    // If you require timezone data (see moment-timezone-rails for additional file options)
    // import "moment-timezone-with-data"
    require('tempusdominus-bootstrap-4');
    
    console.log('Webpacker: END application.js');
    

    config/webpack/enenvironments.js(不知道这里的正确之处):

    const { environment } = require('@rails/webpacker')
    
    const webpack = require('webpack');
    environment.plugins.prepend('Provide', // append
        new webpack.ProvidePlugin({
          $: 'jquery/src/jquery',
          jQuery: 'jquery/src/jquery' // ,
          // Popper: ['popper.js', 'defaults']
        })
    );
    
    module.exports = environment
    

    package.json:

    {
      "name": "myapp",
      "private": true,
      "dependencies": {
        "@fortawesome/fontawesome-free": "^5.15.4",
        "@popperjs/core": "^2.10.1",
        "@rails/activestorage": "^6.1.4-1",
        "@rails/ujs": "^6.1.4-1",
        "@rails/webpacker": "5.4.3",
        "bootstrap": "4.6.0",
        "jquery": "^3.6.0",
        "moment": "^2.29.1",
        "moment-timezone": "^0.5.33",
        "popper.js": "^1.16.1",
        "tempusdominus-bootstrap-4": "^5.39.0",
        "tempusdominus-core": "^5.19.0",
        "turbolinks": "^5.2.0",
        "webpack": "^4.46.0",
        "webpack-cli": "^3.3.12"
      },
      "devDependencies": {
        "webpack-dev-server": "^3"
      }
    }
    

    当我使用

    if (typeof window !== "undefined") {
      require('popper.js');
      require('bootstrap');
      require('@fortawesome/fontawesome-free');
    }
    

    喜欢在 Trying to import Bootstrap on Next.JS but says "TypeError: Cannot read property 'jquery' of undefined" ,我得到另一个错误:

    /var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:1785
          return window.document;
          ^
    
    ReferenceError: window is not defined
        at Object.<anonymous> (/var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:1785:7)
        at Object.<anonymous> (/var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:1786:7)
        at n (/var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:8:17)
        at Object.<anonymous> (/var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:6524:10)
        at n (/var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:8:17)
        at Object.<anonymous> (/var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:1682:110)
        at Object.<anonymous> (/var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:1772:8)
        at n (/var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:8:17)
        at Object.<anonymous> (/var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:1860:7)
        at n (/var/www/vhosts/mydomain.com/httpdocs/myapp/public/packs/js/application-2432a12b5b63094eb637.js:8:17)
    

    我错过了什么?

    0 回复  |  直到 3 年前
        1
  •  0
  •   Luma    3 年前

    另外,带有Webpacker的Rails应用程序不能作为Node.js应用程序运行,尽管有一些Node包与Webpacker一起使用。它们仅用于客户端。

    虚拟主机上的Phusion Passenger被配置为不加载频繁的应用程序更改,我的许多试用修复程序都是如此。这就是为什么当我几周前尝试上述解决方案时,错误没有消失;-)


    https://stackoverflow.com/questions/60198141/trying-to-import-bootstrap-on-next-js-but-says-typeerror-cannot-read-property是正确的。只需将application.js中的所有Javascript库加载打包为条件:
    if (typeof window !== "undefined")
    
    推荐文章