最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Issue Loading PopperJS and Bootstrap via RequireJS, Even After Using Recommended PopperJS Version - Stack Overflow

programmeradmin1浏览0评论

I'm attempting to load jquery, popperjs, and bootstrap (v4-beta) via requirejs and in the console I keep getting:

Uncaught Error: Bootstrap dropdown require Popper.js ()
    at bootstrap.js:6
    at bootstrap.js:6
    at bootstrap.js:6

Here's my code in main:

requirejs.config({

  paths: {
    'jquery': 'lib/jquery',
    'popper': 'lib/popper',
    'bootstrap': 'lib/bootstrap'
  },

  shim: {
    'bootstrap': ['jquery', 'popper']
  }

});

requirejs(['jquery', 'popper', 'bootstrap'], function(jquery, popper, bootstrap) {});

This has already been asked a few times regarding issues with loading popper.js and bootstrap with requirejs. Yes, I'm using the umd version referenced here.

Everything is loading properly into the page:

<script data-main="js/main.js" src="js/require.js"></script>
<script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="main" src="js/main.js"></script>
<script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="jquery" src="js/lib/jquery.js"></script>
<script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="popper" src="js/lib/popper.js"></script>
<script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="bootstrap" src="js/lib/bootstrap.js"></script>

I'm confused as to why I'm still getting this error and am starting to think it's something with my require config. Any ideas?

I'm attempting to load jquery, popperjs, and bootstrap (v4-beta) via requirejs and in the console I keep getting:

Uncaught Error: Bootstrap dropdown require Popper.js (https://popper.js)
    at bootstrap.js:6
    at bootstrap.js:6
    at bootstrap.js:6

Here's my code in main:

requirejs.config({

  paths: {
    'jquery': 'lib/jquery',
    'popper': 'lib/popper',
    'bootstrap': 'lib/bootstrap'
  },

  shim: {
    'bootstrap': ['jquery', 'popper']
  }

});

requirejs(['jquery', 'popper', 'bootstrap'], function(jquery, popper, bootstrap) {});

This has already been asked a few times regarding issues with loading popper.js and bootstrap with requirejs. Yes, I'm using the umd version referenced here.

Everything is loading properly into the page:

<script data-main="js/main.js" src="js/require.js"></script>
<script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="main" src="js/main.js"></script>
<script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="jquery" src="js/lib/jquery.js"></script>
<script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="popper" src="js/lib/popper.js"></script>
<script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="bootstrap" src="js/lib/bootstrap.js"></script>

I'm confused as to why I'm still getting this error and am starting to think it's something with my require config. Any ideas?

Share Improve this question asked Sep 1, 2017 at 16:02 MikeMike 831 silver badge6 bronze badges 5
  • I use what Bootstrap gives <script src="cdnjs.cloudflare./ajax/libs/popper.js/1.11.0/umd/…" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script> and it's loaded before my </body> tag – mlegg Commented Sep 1, 2017 at 16:37
  • also this goes in the <head> section <script src="cdnjs.cloudflare./ajax/libs/popper.js/1.11.0/umd/…" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script> <script src="maxcdn.bootstrapcdn./bootstrap/4.0.0-beta/js/…" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script> – mlegg Commented Sep 1, 2017 at 16:39
  • @mlegg I don't have any issues with either of those methods. My issue arises when I utilize requirejs to load everything. – Mike Commented Sep 1, 2017 at 16:47
  • The requirejs build of Bootstrap beta.1 is kind of buggy, it expects Popper.js to be available in the global scope. They should fix this problem in the next release – Fez Vrasta Commented Sep 1, 2017 at 17:54
  • The library is popper.js, not popper – Fez Vrasta Commented Sep 15, 2017 at 10:01
Add a ment  | 

3 Answers 3

Reset to default 8

Alternatively- "bootstrap./contents": Bundled JS files (bootstrap.bundle.js and minified bootstrap.bundle.min.js) include Popper, but not jQuery.

So I added it to my library. Changed the name ( "bootstrap.bundle.min.js" -> "bootstrap.min" ), created a path for bootstrap and a shim for jquery. Seems to have worked for me.

Until Bootstrap PR #22888 is released, this is how I've fixed the Bootstrap dropdown require Popper.js (https://popper.js) issue...

Similar to what's mentioned in other blog posts the idea is to create your own module that wraps Bootstrap. This module then loads and sets popper.js in the prescribed manner before loading bootstrap:

requirejs.config({
    "paths" : {
        "jquery"        : "https://code.jquery./jquery-3.2.1.slim.min",
        "popper"        : "https://cdnjs.cloudflare./ajax/libs/popper.js/1.12.3/umd/popper.min",
        "bootstrap"     : "https://maxcdn.bootstrapcdn./bootstrap/4.0.0-beta/js/bootstrap.min"
        "initBootstrap" : "...wotever..."
    },
    "shim" : {
        "bootstrap" : ["jquery"]
    }
});

...

define("initBootstrap", ["popper"], function(popper) {
    // set popper as required by Bootstrap
    window.Popper = popper;
    require(["bootstrap"], function(bootstrap) {
        // do nothing - just let Bootstrap initialise itself
    });
});

Now let your code / website have a dependency on initBootstrap and not bootstrap.

Or as chasepeeler mentions, if you don't want to create a new module, you can just add a require statement:

require(["popper"], function(popper) {
    window.Popper = popper;
    require(["bootstrap"]);
});

Note this is also posted on the Bootstrap Popper Issue on GitHub.

This works for me;

require.config({
    
    shim: {
        'bootstrap' : ["jquery"]
      },
      paths: {
        'jquery': '../jsLibrary/jquery',
        'popper': "https://cdnjs.cloudflare./ajax/libs/popper.js/1.12.3/umd/popper.min",
        'bootstrap': "https://maxcdn.bootstrapcdn./bootstrap/4.0.0-beta/js/bootstrap.min",
        'tatMusic': '../music/tatMusic'
      }
});

require(["tatMusic","popper"], function(Music,popper){
    window.Popper = popper;
    require(["bootstrap"], function(bootstrap) {
    });
});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论