Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

287
Vistas
Load bootstrap JS with esbuild

I'm trying to import the bootstrap JS in my esbuild build:

// index.js
import 'jquery-import'
import 'bootstrap'

// jquery-import.js
import jquery from 'jquery'
window.jQuery = window.$ = jquery;

The build target is esm. The problem is that the following bootstrap code is run:

factory(exports, require('jquery'), require('popper.js'));

This does not use my global window.jQuery but rather requires its own, which means that my global window.$.fn doesn't include the Bootstrap extensions. Also, I think that this makes jQuery load twice.

Is there a way of fixing this without adapting the bootstrap source code?


A little more background on this:

The bootstrap source code begins with the following statements (lightly adapted for better readability):

(function (global, factory) {
  if (typeof exports === 'object' && typeof module !== 'undefined') {
    // Option 1: The route taken by esbuild
    factory(exports, require('jquery'), require('popper.js'));
  } else if (typeof define === 'function' && define.amd) {
    // Option 2
    define(['exports', 'jquery', 'popper.js'], factory);
  } else {
    // Option 2
    (global = global || self, factory(global.bootstrap = {}, global.jQuery, global.Popper));
  }
}(this, function (exports, $, Popper) { 'use strict';

I have tried commenting out individual options, and neither 1, 2 or 3 work by just taking the already globally defined window.jQuery.

Many thanks for your help!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The reason it's not working, is for some reason your esbuild is trying to import a UMD file (typically used for old Node and browser hybrid code).

Having looked at bootstrap's package.json, it seems that "module": "dist/js/bootstrap.js", which exports bootstrap as modules, which is probably what esbuild should be referencing.

So to fix this without fixing the esbuild config, I'd conjecture you could try:

import 'bootstrap/dist/js/bootstrap'

...or to fix it with esbuild config, add this:

const buildOpts = {
  mainFields: ['module', 'main'],
}

This setting tells the bundler to prioritises module, then main entrypoints.

Normally this is module before main, but it may be the other way round because you set platform to "node".

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda