Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

popper.js in bootstrap 4 gives SyntaxError Unexpected token export

I tried install bootstrap 4, and included following links

<script src="libs/jquery/dist/jquery.min.js"></script>
<script src="libs/tether/dist/js/tether.min.js" ></script>
<script src="libs/popper.js/dist/popper.js"></script>
<script src="libs/bootstrap/dist/js/bootstrap.min.js" ></script>

But The following error occurs :

Uncaught syntaxError: Unexpected token export

enter image description here

Any ideas how to fix it?

like image 724
Павел Шиляев Avatar asked Sep 28 '17 01:09

Павел Шиляев


People also ask

How do I fix SyntaxError unexpected token export?

To solve the "Uncaught SyntaxError Unexpected token 'export'", refactor your code to use the CommonJS syntax, e.g. module. exports = {num}; instead of export num = 10; . Copied!

Why is Popper js use in bootstrap 4?

js for positioning. Dropdowns are built on a third party library, Popper. js, which provides dynamic positioning and viewport detection. So these are the Bootstrap 4 components that need Popper.

Does bootstrap include popper?

You must include popper. min. js before bootstrap. js or use bootstrap.


6 Answers

Just got this too and figured why it really happens. In case others get by here:

Check the readme.md "Usage". The lib is available in three version for three difference module loaders. In short: if you load it with the <script> tag then you must use the UMD version. You can find it in /dist/umd. The default (in /dist) is the ESNext (ECMA-Script) which can not be loaded using the script tag.

like image 70
Marc Avatar answered Sep 29 '22 03:09

Marc


Bootstrap 4 requires the UMD version of popper.js, and make sure the order is as follows:

<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="~/Scripts/jquery-3.0.0.min.js"></script>
<script src="~/Scripts/umd/popper.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
like image 39
Zim Avatar answered Sep 28 '22 03:09

Zim


I encountered the same issue if I use popper.js from CDN network like cdnjs.

If you observe the source code of Bootstrap 4 examples like for example Navbar you can see that popper.min.js is loaded from:

<script src="https://getbootstrap.com/docs/4.1/assets/js/vendor/popper.min.js"></script>

I included that in my project and the error is gone. You can download the source code from

https://getbootstrap.com/docs/4.1/assets/js/vendor/popper.min.js

and include in your project as a local file and it should work.

like image 29
Phani Kumar M Avatar answered Sep 25 '22 03:09

Phani Kumar M


You can also add bootstrap.bundle.min.js and remove popper.js in your html.

Bundled JS files (bootstrap.bundle.js and minified bootstrap.bundle.min.js) include [Popper](https://popper.js.org/), but not jQuery.

like image 34
webdeveloper086 Avatar answered Sep 26 '22 03:09

webdeveloper086


Line 96 in README

Dist targets

Popper.js is currently shipped with 3 targets in mind: UMD, ESM and ESNext.

  • UMD - Universal Module Definition: AMD, RequireJS and globals;
  • ESM - ES Modules: For webpack/Rollup or browser supporting the spec;
  • ESNext: Available in dist/, can be used with webpack and babel-preset-env;

Make sure to use the right one for your needs. If you want to import it with a <script> tag, use UMD.

like image 36
Rob Avatar answered Sep 26 '22 03:09

Rob


You have following code in Bundle Config bundles.Add(new ScriptBundle("~/bundles/jquery").Include( "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));

        // Use the development version of Modernizr to develop with and learn from. Then, when you're
        // ready for production, use the build tool at https://modernizr.com to pick only the tests you need.
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                    "~/Scripts/umd/popper.min.js",
                  "~/Scripts/bootstrap.js",
                  "~/Scripts/respond.js"));

following code in layout html

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")

This worked for me

like image 45
ycs Avatar answered Sep 27 '22 03:09

ycs