简体   繁体   English

尝试加载 Bootstrap js 文件时出现 webpack 错误

[英]webpack error when trying to load a Bootstrap js file

I have a gridsome project (v0.7.23).我有一个网格项目 (v0.7.23)。 I load the Bootstrap framework via npm. Im using node v14.18.0 via nvm.我通过 npm 加载 Bootstrap 框架。我通过 nvm 使用节点 v14.18.0。

When I import a Bootstrap JS component (collapse):当我导入 Bootstrap JS 组件时(折叠):

import Collapse from 'bootstrap/js/src/collapse'

webpack fails to build and returns this error: webpack 构建失败并返回此错误:

Module parse failed: Identifier directly after number (8:17)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|  */
| 
> const MAX_UID = 1_000_000
| const MILLISECONDS_MULTIPLIER = 1000
| const TRANSITION_END = 'transitionend'

 @ ./node_modules/bootstrap/js/src/collapse.js 8:0-14:21 71:23-45 182:4-10 188:22-44 216:20-30 232:23-45 288:19-41 300:0-18
 @ ./node_modules/cache-loader/dist/cjs.js??ref--1-0!./node_modules/babel-loader/lib??ref--1-1!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/gridsome/node_modules/vue-loader/lib??vue-loader-options!./src/components/Nav.vue?vue&type=script&lang=js&
 @ ./src/components/Nav.vue?vue&type=script&lang=js&

It's like webpack was not able to parse/read the const keyword.就像 webpack 无法解析/读取 const 关键字。

Here is my package.json dependencies (up to date):这是我的 package.json 依赖项(最新):

"dependencies": {
    "@gridsome/plugin-sitemap": "^0.4.0",
    "@popperjs/core": "^2.9.2",
    "bootstrap": "^5.0.1",
    "gridsome": "^0.7.0",
    "gridsome-plugin-gtm": "^0.1.1",
    "gsap": "^3.6.1"
  },
  "devDependencies": {
    "node-sass": "^5.0.0",
    "sass-loader": "^13.2.0",
    "css-loader": "^6.7.2",
    "postcss-loader": "^7.0.2",
    "sass": "^1.56.1",
    "style-loader": "^3.3.1"
  }

Any idea?任何的想法? thanks谢谢

you just need to add following code in your webpack.config.js file in rules array.您只需要在rules数组中的webpack.config.js文件中添加以下代码。

first add babel-loader and @babel/preset-react in your devDepencies首先在你的 devDepencies 中添加babel-loader@babel/preset-react

 {
    test: /\.jsx?$/,
    exclude: /node_modules/,
    use: {
      loader: "babel-loader",
      options: {
        presets: ['@babel/preset-react']
      }
    }
  }

hope this will help you...希望对你有帮助...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在 js 中加载图像文件时出现此错误 - I got this error when trying to load a image file in js 使用Webpack加载引导时出现“ jquery未定义” - 'jquery is not defined' when using webpack to load bootstrap 尝试运行“ webpack --config ./webpack.config.js”时出错 - Getting error when trying to run my “webpack --config ./webpack.config.js” 尝试在index.html中加载外部js文件时出错(Vue) - Error when trying to load external js file in index.html (Vue) 尝试在浏览器中的 html 文件中加载 functions.js 文件时出现 GET 404 错误(*使用节点 js 作为服务器) - GET 404 Error When Trying to Load a functions.js File Inside an html File in Browser (*using node js as server) Webpack bootstrap-loader在引导程序之后加载自定义文件 - Webpack bootstrap-loader load custom file after bootstrap 尝试通过vue和Webpack安装keycloak-js时出现未捕获的类型错误:__WEBPACK_IMPORTED_MODULE_3_keycloak_js__不是函数 - Uncaught type error when trying to install keycloak-js with vue and webpack: __WEBPACK_IMPORTED_MODULE_3_keycloak_js__ is not a function Webpack 5 Receiving a Polyfill Error??.. 我的 JavaScript React 项目在尝试编译我的 webpack.config.js 文件时收到一个 polyfill 错误 - Webpack 5 Receiving a Polyfill Error?!?! My JavaScript React project is receiving a polyfill error while trying to compile my webpack.config.js file 尝试加载外部js文件时,ie8和7权限被拒绝 - ie8 and 7 Permission Denied when trying to load an external js file 使用 require.js 加载时,在 bootstrap 4 中加载 popper.js 时出错 - Error load popper.js in bootstrap 4 when use require.js to load
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM