0

I have a webpack build which is serving me fine, the only problem is, I am running Bootstrap for styling, but I have brought that in through a CDN link in my index.ejs file.

This is fine when I build my application as this is my template file, but it doesn't load Bootstrap when I am running webpack-dev-server.

I have read around and tried a few things, including Boostrap Loader - but that just made my dev-server freak out with multiple errors.

Is there a reasonably simple fix for this?

Any help appreciated.

UPDATE (@Kesh Shan)

Ok, as suggested I have tried to go down the npm route. Here is my process:

I installed bootstrap and jquery npm modules. Apparently these needed the peer dependencies file-loader and [email protected], both of which I have now installed. Do I need to reference these loaders in my webpack.config.js? Here it is as it stands:

webpack.config.js

"use strict";

const debug = process.env.NODE_ENV !== "production";

const webpack = require('webpack');
const path = require('path');

module.exports = {
  devtool: debug ? 'inline-sourcemap' : null,
  entry: path.join(__dirname, 'src', 'app-client.js'),
  devServer: {
    inline: true,
    port: 3333,
    contentBase: "src/static/",
    historyApiFallback: {
      index: '/index-static.html'
    }
  },
  output: {
    path: path.join(__dirname, 'src', 'static', 'js'),
    publicPath: "/js/",
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      {
         test: path.join(__dirname, 'src'),
         loader: ['babel-loader'],
         query: {
            cacheDirectory: 'babel_cache',
            presets: debug ? ['react', 'es2015', 'react-hmre'] : ['react', 'es2015']
         }
      },
      { test: /\.css$/, loader: "style-loader!css-loader" },
      { test: /\.png$/, loader: "url-loader?limit=100000" },
      { test: /\.jpg$/, loader: "file-loader" }
   ]
  },
  plugins: debug ? [
     new webpack.ProvidePlugin({
         $: "jquery",
         jQuery: "jquery"
         })
  ] : [
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
    }),
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.UglifyJsPlugin({
      compress: { warnings: false },
      mangle: true,
      sourcemap: false,
      beautify: false,
      dead_code: true
    }),
    // as suggested at http://stackoverflow.com/questions/37651015/webpack-using-bootstrap-jquery-is-not-defined
    new webpack.ProvidePlugin({
           $: "jquery",
           jQuery: "jquery"
   })
  ]
};

Finally, here is my entry js file:

app-client.js

'use strict';

import React from 'react';
import ReactDOM from 'react-dom';
import AppRoutes from './components/AppRoutes';

global.jQuery = require('jquery');

// import bootstrap from 'bootstrap'
require('bootstrap');

// import css
import css from './static/css/style.css';

window.onload = () => {
   ReactDOM.render(<AppRoutes/>,
      document.getElementById('main')
   );
};

Now I have switched import bootstrap from 'bootstrap' for require('bootstrap') (the page renders ok - the console message saying transition.js:59Uncaught ReferenceError: jQuery is not defined(…) no longer shows in the console).

Still no Bootstrap styling though :(

6
  • Try installing bootstrap through npm, remember also jquery, its dependency, if you provide your error log and webpack.config it would be helpful Commented Nov 1, 2016 at 19:22
  • Hi Kesh - please see update Commented Nov 1, 2016 at 23:09
  • Have tried that (see update) but it's made no difference. I also switched import bootstrap from 'bootstrap' (which caused the transition.js:59Uncaught ReferenceError: jQuery is not defined(…) console message) for require('bootstrap') - now the page renders, but still has no bootstrap styling... Commented Nov 2, 2016 at 6:47
  • Oops, I added the webpack plugin after dubug to make the plugin run in dev environment (see update) and now it renders even with the ES6 import on 'bootstrap'. But still no styling. Commented Nov 2, 2016 at 7:09
  • hope your using react specific attributes for e.g. : the attribute is className, not class Commented Nov 2, 2016 at 8:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.