What could be causing webpack to fail to recognize the bootstrap4 and font-awesome modules?

Recently, I've been delving into webpack and exploring how to integrate various modules. I encountered an issue with two specific modules - bootstrap4 and font-awesome, as they were not getting recognized.

After running npm install <module>, here's a snippet of the updated package.json:

{
  "name": "website",
  "version": "1.0.0",
  "description": "personal blog",
  "main": "index.html",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.25.0",
    "babel-loader": "^7.0.0",
    "bootstrap4": "0.0.1-security",
    "css-loader": "^0.28.4",
    "font-awesome": "^4.7.0",
    "less": "^2.7.2",
    "less-loader": "^4.0.4",
    "lodash": "^4.17.4",
    "moment": "^2.18.1",
    "style-loader": "^0.18.2",
    "vue": "^2.3.4"
  }
}

The configuration in my webpack.config.js file includes:

const path = require('path');

module.exports = {
    entry: path.join(__dirname, 'entry.js'),
    output: {
        path: path.join(__dirname, 'src'),
        filename: 'build.js'
    },
    module: {
        rules: [{
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            },
            {
                test: /\.less$/,
                use: ['style-loader', 'css-loader', 'less-loader']
            }
        ]

    }
}

However, upon executing webpack, errors relating to the bootstrap4 and font-awesome modules arised:

D:\Projects\website\webpack>webpack
Hash: 2501a45f8eacb5b3fdac
Version: webpack 2.7.0
Time: 3028ms
   Asset     Size  Chunks                    Chunk Names
build.js  1.24 MB       0  [emitted]  [big]  main
   [0] ./~/moment/moment.js 129 kB {0} [built]
  [14] ./~/moment/locale/bo.js 3.47 kB {0} [optional] [built]
 ...
 ... Other built assets are listed here ...
 ...
ERROR in ./entry.js
Module not found: Error: Can't resolve 'font-awesome' in 'D:\Projects\website\webpack'
 @ ./entry.js 16:0-39

ERROR in ./entry.js
Module not found: Error: Can't resolve 'bootstrap4' in 'D:\Projects\website\webpack'
 @ ./entry.js 15:0-36

This led me to investigate the entry.js file where I had included both modules:

import Vue from 'vue'
import bootstrap4 from 'bootstrap4'
import fontawesome from 'font-awesome'
import _ from 'lodash'
import moment from 'moment'
require('./less/style.less')
require('./less/navbar.less')
//require('./js/posts.js')
require('./js/explorer.js')
console.log('hello')

Why am I facing difficulties with these two modules?


For a detailed error log generated by webpack, see below:

D:\Projects\website\webpack>webpack --display-error-details
Hash: 2501a45f8eacb5b3fdac
Version: webpack 2.7.0
Time: 2952ms
 ...
 ... Remaining webpack build details are displayed here...
 ...

Answer №1

font-awesome does not have the necessary fields in its package.json that are supported by webpack. Additionally, there is no index.js file in the package, making it challenging to import conventionally. If you intend to use the CSS file, consider importing it like this:

import 'font-awesome/css/font-awesome.css';

bootstrap4 is not a legitimate package. As stated on the npm registry for bootstrap4:

Security holding package

This package name has been inactive and may be available for use in the future. For inquiries about adopting this package, please contact [email protected].

To utilize Bootstrap, obtain the actual bootstrap package. The current stable version is 3.3.7, while the v4 alpha can be installed via npm using the following command:

npm install --save bootstrap@next

Answer №2

Did you know that font-awesome can be integrated seamlessly with webpack? All you need to do is include the "style" element in the resolve.mainFields section:

module.exports = {

  ...

  resolve: {
    ...
    mainFields: ["browser", "module", "main", "style"]
    ...
  }

  ...
}

The first three values are specified in the official documentation, while the final value can be found in the font-awesome package.json file:

{
  ...
  "style": "css/font-awesome.css",
  ...
}

With this setup, webpack will now recognize where font-awesome stores its CSS files! Just note that webpack automatically sets a default value for resolve.mainFields based on the project's target configuration; This adjustment overrides that default behavior, so make sure to refer to the documentation for the appropriate resolve.mainField settings.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are the steps for distributing a styled React component?

My goal is to release a UI library using React, but I am struggling with how to handle styles. When I write code in a project, I typically use webpack and babel to build my code, resulting in the creation of a *.css file. import React from 'react&ap ...

Is it necessary to convert SCSS into CSS in React?

I have a query about whether it is necessary to first compile our scss files into css and then import the css files into our react components, or if we can simply import the scss directly into the components? I've successfully imported scss directly ...

How Webpack 4 Utilizes splitChunks to Create Numerous CSS Files

I need to create multiple CSS files using webpack 4 with the min-css-extract-plugin and splitChunks plugin. Here is my webpack configuration. const MiniCssExtractPlugin = require("mini-css-extract-plugin"); const path = require("path"); module.exports = ...

Guide to navigating to a different webpage with Node.js

I am a beginner user of NodeJS and I have a specific request. I need assistance with setting up a page redirect from one page to another upon clicking on a link. Any guidance provided will be greatly appreciated. Here is the code snippet: app.js var expr ...

Toggle css comment line using gulp

Is it possible to temporarily deactivate a particular CSS comment on a specific line using Gulp and then reactivate it after completing a build task? As an illustration, I am interested in deactivating the @import 'compass/reset'; statement loca ...

The system encountered an issue with the CSS code, indicating "Invalid CSS after "...inear-gradient(": expected selector, was "{"

While attempting to compile my sass code, I encountered the following error message regarding a syntax issue: /* { "status": 1, "file": "C:/Users/faido/Desktop/Productivity/sass css/project/sass/main.sass", "line": 36, "column": 9, "mes ...

Unable to display imported image in React application

I have a component where I want to display an image: import React from 'react' import background from '../media/content-background.jpg' function ContentPage(){ return( <div id="content-page"> < ...

Integrating foundation-sites with webpack, unable to apply styles

Delving into the world of webpack for the first time has been quite a daunting experience! I'm attempting to set up the foundation for sites, but I feel completely lost when it comes to configuring it properly. Here is my Webpack configuration: var ...

An error occurred when attempting to run the command npm run compile:sass, displaying the message: npm ERR! missing script:

Everything seems to be in place with the sass folders and files, so what could be the issue? I have my package.json file set up correctly with the following code: { "name": "starter", "version": "1.0.0", " ...

A combination of Webpack CSS modules with Angular templates

I am currently trying to integrate webpack CSS modules into my angular/ionic project. I'm wondering if it's possible for the CSS module class definitions to be correctly appended to an external template instead of an inline template. When I embe ...

What is the best way to link CSS files from libraries that are downloaded using npm?

For instance, let's say I installed a package: npm install --save package and it gets saved in ./node_modules/package/ Inside that folder, there might be a directory named styles and within that directory, you could find style.css. How can I link ...

How can I properly integrate jQuery libraries, along with CSS, in a create-react-app project?

I'm currently working on incorporating the select2 jquery library into my React application, which was created using create-react-app. After adding jquery and select2 to my package.json file, I was able to get the javascript functionality to work pro ...

Webpack Encore: SCSS import order causing unexpected issues

When developing with Symfony + Webpack Encore, I aim to split styles into "layout" and "page-based" categories for convenience. However, despite this organization, I still prefer to compile all styles into a single CSS file. To achieve this, I structure my ...

Node-modules CSS

In my React application, I am utilizing CSS modules. By configuring modules:true in my webpack.config.js, I can successfully apply styles from files within my src folder. However, when importing components from node-modules, the corresponding CSS is not be ...

How come the changes in my react component's CSS are not updating in real-time?

After integrating redux into my create-react-app project, I've been struggling to implement a navigator that highlights the active "page link." To achieve this functionality, I am using a combination of react hooks (to maintain the current page state) ...

Unable to locate a resolution for the error message "Warning: Task "uglify" not found"

Below is the content of my Gruntfile.js: module.exports = function(grunt) { require('load-grunt-tasks')(grunt); grunt.initConfig({ uglify: { start: { files: { 'js/script.min.js': ['js/script.js&a ...

What could be causing html-webpack-inline-source-plugin to prevent the page from refreshing after editing CSS files?

Currently, I am utilizing a plugin that embeds all CSS within the final HTML file. This method prevents the application from refreshing in development mode. Whenever I make edits to the CSS, the build process completes normally, but the styles do not updat ...

How are the plugins configured for `postcss-import` implemented?

Recently, I've transitioned to using PostCSS exclusively with Webpack. As I explore the functionalities of using postcss-import to inline external stylesheets, I'm noticing that its options offer the ability to configure plugins and transformers ...

The issue I'm facing is that the style loader is failing to load the CSS within the <head

I am currently facing an issue with importing my CSS into my webpack bundle for our Angular 1 application. Initially, everything was working fine as we bundled our application using Webpack. The HTML included the bundle and vendor scripts, additional Java ...

What is the best way to include CSS styles in a React app with Webpack?

Having an issue with my React app. Upon successfully compiling Webpack and serving the content, no styles are displayed within the app. This is the current content of my webpack.config.js file: const path = require('path'); const BUILD_DIR = pat ...