Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap: Uncaught TypeError: Cannot read property 'fn' of undefined

I am trying to make an Electron application with Bootstrap. I get this error message:

Uncaught TypeError: Cannot read property 'fn' of undefined
at setTransitionEndSupport (bootstrap.js:122)
at bootstrap.js:199
at bootstrap.js:201
at bootstrap.js:9
at bootstrap.js:10

My dependencies in the package.json are:

"dependencies": {
  "bootstrap": "^4.1.2",
  "electron": "^2.0.5",
  "jquery": "^3.3.1",
  "popper.js": "^1.14.3"
}

My index.html file is:

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Release Management Report</title>

</head>

<body>

  <div class="container">

    <h1>Bootstrap Test</h1>

    <p>
        We are using node
        <script>document.write(process.versions.node)</script>, Chrome
        <script>document.write(process.versions.chrome)</script>, and Electron
        <script>document.write(process.versions.electron)</script>.
    </p>

  </div>

  <script src="node_modules/jquery/dist/jquery.min.js"></script>
  <script src="node_modules/popper.js/dist/umd/popper.min.js"></script>
  <script src="node_modules/bootstrap/dist/js/bootstrap.js"></script>

</body>

</html>

Then the html file is loaded by the main.js from Electron quick start:

const { app, BrowserWindow } = require('electron')

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win

function createWindow() {
  // Create the browser window.
  win = new BrowserWindow({ width: 800, height: 600 })

  // and load the index.html of the app.
  win.loadFile('index.html')

  // Open the DevTools.
  win.webContents.openDevTools()

  // Emitted when the window is closed.
  win.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (win === null) {
    createWindow()
  }
})

Most of the solutions I have found tell me to import jQuery before Bootstrap, but that is already what I am doing.

Thanks for the help!

like image 220
Griford Avatar asked Jul 16 '18 20:07

Griford


2 Answers

Thanks to the answer from Griford I managed to get my Angular&Electron app running with Bootstrap. All I wanted to contribute here is that no external file is needed - you can initialize JQuery and Bootstrap in a script block also:

<script>
  window.$ = window.jQuery = require('jquery');
  window.Bootstrap = require('bootstrap');
</script>

This works for me with the following versions (Electron 2.0.7):

"bootstrap": "^4.1.3",
"jquery": "^3.3.1",
"popper.js": "^1.14.4"

NOTE: no other script integration is needed for using Bootstrap when initialized by the two lines above. The only thing you need in addition is the CSS (in my case added to the bundle via angular.json).

like image 165
Achim Schrepfer Avatar answered Nov 07 '22 15:11

Achim Schrepfer


I found a way to solve the problem.

I added a script in the index.html:

<script src="scripts/script.js"></script>

and this is the content of script.js:

window.$ = window.jQuery = require('jquery'); // not sure if you need this at all
window.Bootstrap = require('bootstrap');

and we can remove these lines from index.html to avoid double import:

<script src="node_modules/jquery/dist/jquery.min.js"></script>
<script src="node_modules/popper.js/dist/umd/popper.min.js"></script>
<script src="node_modules/bootstrap/dist/js/bootstrap.js"></script>

I found this solution at: https://github.com/understrap/understrap/issues/449, under the comment from karo1915.

like image 17
Griford Avatar answered Nov 07 '22 15:11

Griford