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

javascript - How to import bootstrap 5 bundle js with npm? - Stack Overflow

programmeradmin0浏览0评论

I've some problem with bootstrap5 and NPM. The website design use bootstrap and works but not all the js (dropdown, modals etc...), I just would like to learn hot to import the js bs bundle without CDN. Someone can help me, please? thanks for your time

my steps:

npm install [email protected]
npm install popper.js --save
npm intsall node-sass --save-d

Package.json:

{
  "name": "xlf",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "./node_modules/.bin/webpack --watch",
    "sass": "node-sass --watch src/scss -o dist/css --output-style nested"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bootstrap": "^5.0.0-alpha1",
    "popper.js": "^1.16.1"
  },
  "devDependencies": {
    "node-sass": "^5.0.0",
    "webpack": "^5.26.0",
    "webpack-cli": "^4.5.0"
  }
}

webpack.config.js

const path = require('path');

module.exports = {
   entry: './src/js/app.js',
   output: {
      path: path.resolve(__dirname, 'dist','js'),
      filename: 'app.js',
   },
   mode: 'production',
   module: {
      rules:[
       {
          test: /.(scss|css)$/,
          use:
          [
             {
                options: { reloadAll: true },
             },
             'css-loader',
             'sass-loader',
             'node-sass'
          ]
       }
      ]
   }
 };

src/js/app.js

import 'bootstrap';

src/scss/app.scss

@import '../../node_modules/bootstrap/scss/bootstrap.scss';

I've some problem with bootstrap5 and NPM. The website design use bootstrap and works but not all the js (dropdown, modals etc...), I just would like to learn hot to import the js bs bundle without CDN. Someone can help me, please? thanks for your time

my steps:

npm install [email protected]
npm install popper.js --save
npm intsall node-sass --save-d

Package.json:

{
  "name": "xlf",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "./node_modules/.bin/webpack --watch",
    "sass": "node-sass --watch src/scss -o dist/css --output-style nested"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bootstrap": "^5.0.0-alpha1",
    "popper.js": "^1.16.1"
  },
  "devDependencies": {
    "node-sass": "^5.0.0",
    "webpack": "^5.26.0",
    "webpack-cli": "^4.5.0"
  }
}

webpack.config.js

const path = require('path');

module.exports = {
   entry: './src/js/app.js',
   output: {
      path: path.resolve(__dirname, 'dist','js'),
      filename: 'app.js',
   },
   mode: 'production',
   module: {
      rules:[
       {
          test: /.(scss|css)$/,
          use:
          [
             {
                options: { reloadAll: true },
             },
             'css-loader',
             'sass-loader',
             'node-sass'
          ]
       }
      ]
   }
 };

src/js/app.js

import 'bootstrap';

src/scss/app.scss

@import '../../node_modules/bootstrap/scss/bootstrap.scss';
Share Improve this question asked Mar 16, 2021 at 8:31 Lorenzo FranzoneLorenzo Franzone 1431 gold badge1 silver badge11 bronze badges 9
  • You are using a alpha version. That could be the reason. Try npm i -S [email protected]. – user15388024 Commented Mar 16, 2021 at 8:44
  • You can narrow down the JS file in the node_modules import './node_modules/bootstrap/dist/js/bootstrap.min.js' – fatiu Commented Mar 16, 2021 at 8:51
  • @jaba: tried with beta version but same result :( – Lorenzo Franzone Commented Mar 16, 2021 at 9:48
  • What is the result? – user15388024 Commented Mar 16, 2021 at 9:50
  • modal, dropdown ecc not working... with my code JS works only if I also add into HTML popper via CDN – Lorenzo Franzone Commented Mar 16, 2021 at 9:52
 |  Show 4 more ments

1 Answer 1

Reset to default 6

Solved with these steps:

  1. installing beta versione
  2. uninstalling popper.js and installing @popperjs/core
  3. adding import '@popperjs/core'; before import 'bootstrap';

Thanks for help Jabaa.

发布评论

评论列表(0)

  1. 暂无评论