Bootstrap: Uncaught TypeError: Cannot read propert

2020-05-27 03:18发布

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!

4条回答
Deceive 欺骗
2楼-- · 2020-05-27 03:40

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).

查看更多
唯我独甜
3楼-- · 2020-05-27 03:41

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.

查看更多
姐就是有狂的资本
4楼-- · 2020-05-27 03:52

I get same error with bootstrap 4.1.3, and I am sure script order is correct, jquery is placed before bootstrap.

Because I load these scripts in tampermonkey scripts( a browser add-on to manage third party scripts which can modify current web page ) by the @require attribute, it's hard to inject codes between jquery loading and bootstrap loading.

Changing to a previous version 3.3.7 of bootstrap solves the error. Waiting for next version of bootstrap would solve this.

查看更多
趁早两清
5楼-- · 2020-05-27 03:54

In my case, I had the ordering of the imports of scripts wrong. The bootstrap import should come after the popper and jquery imports.

Since Bootstrap v4.+ depends on popper.js and jquery

"scripts": [
          "./node_modules/jquery/dist/jquery.slim.min.js",
          "./node_modules/popper.js/dist/umd/popper.min.js",
          "./node_modules/bootstrap/dist/js/bootstrap.min.js"
        ]
查看更多
登录 后发表回答