为什么 bootstrap4 和 font-awesome 模块不能被 webpack 识别?

Why are bootstrap4 and font-awesome modules not recognized by webpack?

我正在试验 webpack 并想使用 bootstrap4font-awesome。除了这两个模块外,所有模块都被识别。

npm install <module>之后创建的package.json

{
  "name": "blog",
  "version": "1.0.0",
  "description": "my site",
  "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"
  }
}

我使用的webpack.config.js文件:

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']
            }
        ]

    }
}

运行 webpackbootstrap4font-awesome:

生成错误
D:\Dropbox\dev\jekyll\blog\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]
 [117] ./~/style-loader/lib/addStyles.js 8.7 kB {0} [built]
 [118] (webpack)/buildin/global.js 509 bytes {0} [built]
 [119] (webpack)/buildin/module.js 517 bytes {0} [built]
 [120] ./js/explorer.js 1.35 kB {0} [built]
 [121] ./~/lodash/lodash.js 540 kB {0} [built]
 [122] ./less/navbar.less 1.13 kB {0} [built]
 [123] ./less/style.less 1.12 kB {0} [built]
 [124] ./~/vue/dist/vue.runtime.esm.js 195 kB {0} [built]
 [125] ./entry.js 572 bytes {0} [built]
 [126] ./~/css-loader!./~/less-loader/dist/cjs.js!./less/navbar.less 2.8 kB {0} [built]
 [127] ./~/css-loader!./~/less-loader/dist/cjs.js!./less/style.less 7.67 kB {0} [built]
 [128] ./~/moment/locale ^\.\/.*$ 2.79 kB {0} [optional] [built]
 [129] ./~/process/browser.js 5.42 kB {0} [built]
    + 116 hidden modules

ERROR in ./entry.js
Module not found: Error: Can't resolve 'font-awesome' in 'D:\Dropbox\dev\jekyll\blog\webpack'
 @ ./entry.js 16:0-39

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

引用的entry.js:

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')

为什么这两个模块有问题?


来自 webpack 的完整错误日志,为了完整性:

D:\Dropbox\dev\jekyll\blog\webpack>webpack --display-error-details
Hash: 2501a45f8eacb5b3fdac
Version: webpack 2.7.0
Time: 2952ms
   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]
 [117] ./~/style-loader/lib/addStyles.js 8.7 kB {0} [built]
 [118] (webpack)/buildin/global.js 509 bytes {0} [built]
 [119] (webpack)/buildin/module.js 517 bytes {0} [built]
 [120] ./js/explorer.js 1.35 kB {0} [built]
 [121] ./~/lodash/lodash.js 540 kB {0} [built]
 [122] ./less/navbar.less 1.13 kB {0} [built]
 [123] ./less/style.less 1.12 kB {0} [built]
 [124] ./~/vue/dist/vue.runtime.esm.js 195 kB {0} [built]
 [125] ./entry.js 572 bytes {0} [built]
 [126] ./~/css-loader!./~/less-loader/dist/cjs.js!./less/navbar.less 2.8 kB {0} [built]
 [127] ./~/css-loader!./~/less-loader/dist/cjs.js!./less/style.less 7.67 kB {0} [built]
 [128] ./~/moment/locale ^\.\/.*$ 2.79 kB {0} [optional] [built]
 [129] ./~/process/browser.js 5.42 kB {0} [built]
    + 116 hidden modules

ERROR in ./entry.js
Module not found: Error: Can't resolve 'font-awesome' in 'D:\Dropbox\dev\jekyll\blog\webpack'
resolve 'font-awesome' in 'D:\Dropbox\dev\jekyll\blog\webpack'
  Parsed request is a module
  using description file: D:\Dropbox\dev\jekyll\blog\webpack\package.json (relative path: .)
    Field 'browser' doesn't contain a valid alias configuration
  after using description file: D:\Dropbox\dev\jekyll\blog\webpack\package.json (relative path: .)
    resolve as module
      D:\Dropbox\dev\jekyll\blog\node_modules doesn't exist or is not a directory
      D:\Dropbox\dev\jekyll\node_modules doesn't exist or is not a directory
      D:\Dropbox\dev\node_modules doesn't exist or is not a directory
      D:\Dropbox\node_modules doesn't exist or is not a directory
      D:\node_modules doesn't exist or is not a directory
      looking for modules in D:\Dropbox\dev\jekyll\blog\webpack\node_modules
        using description file: D:\Dropbox\dev\jekyll\blog\webpack\package.json (relative path: ./node_modules)
          Field 'browser' doesn't contain a valid alias configuration
        after using description file: D:\Dropbox\dev\jekyll\blog\webpack\package.json (relative path: ./node_modules)
          using description file: D:\Dropbox\dev\jekyll\blog\webpack\node_modules\font-awesome\package.json (relative path: .)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              D:\Dropbox\dev\jekyll\blog\webpack\node_modules\font-awesome is not a file
            .js
              Field 'browser' doesn't contain a valid alias configuration
              D:\Dropbox\dev\jekyll\blog\webpack\node_modules\font-awesome.js doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              D:\Dropbox\dev\jekyll\blog\webpack\node_modules\font-awesome.json doesn't exist
            as directory
              existing directory
                using path: D:\Dropbox\dev\jekyll\blog\webpack\node_modules\font-awesome\index
                  using description file: D:\Dropbox\dev\jekyll\blog\webpack\node_modules\font-awesome\package.json (relative path: ./index)
                    no extension
                      Field 'browser' doesn't contain a valid alias configuration
                      D:\Dropbox\dev\jekyll\blog\webpack\node_modules\font-awesome\index doesn't exist
                    .js
                      Field 'browser' doesn't contain a valid alias configuration
                      D:\Dropbox\dev\jekyll\blog\webpack\node_modules\font-awesome\index.js doesn't exist
                    .json
                      Field 'browser' doesn't contain a valid alias configuration
                      D:\Dropbox\dev\jekyll\blog\webpack\node_modules\font-awesome\index.json doesn't exist
[D:\Dropbox\dev\jekyll\blog\node_modules]
[D:\Dropbox\dev\jekyll\node_modules]
[D:\Dropbox\dev\node_modules]
[D:\Dropbox\node_modules]
[D:\node_modules]
[D:\Dropbox\dev\jekyll\blog\webpack\node_modules\font-awesome]
[D:\Dropbox\dev\jekyll\blog\webpack\node_modules\font-awesome.js]
[D:\Dropbox\dev\jekyll\blog\webpack\node_modules\font-awesome.json]
[D:\Dropbox\dev\jekyll\blog\webpack\node_modules\font-awesome\index]
[D:\Dropbox\dev\jekyll\blog\webpack\node_modules\font-awesome\index.js]
[D:\Dropbox\dev\jekyll\blog\webpack\node_modules\font-awesome\index.json]
 @ ./entry.js 16:0-39

ERROR in ./entry.js
Module not found: Error: Can't resolve 'bootstrap4' in 'D:\Dropbox\dev\jekyll\blog\webpack'
resolve 'bootstrap4' in 'D:\Dropbox\dev\jekyll\blog\webpack'
  Parsed request is a module
  using description file: D:\Dropbox\dev\jekyll\blog\webpack\package.json (relative path: .)
    Field 'browser' doesn't contain a valid alias configuration
  after using description file: D:\Dropbox\dev\jekyll\blog\webpack\package.json (relative path: .)
    resolve as module
      D:\Dropbox\dev\jekyll\blog\node_modules doesn't exist or is not a directory
      D:\Dropbox\dev\jekyll\node_modules doesn't exist or is not a directory
      D:\Dropbox\dev\node_modules doesn't exist or is not a directory
      D:\Dropbox\node_modules doesn't exist or is not a directory
      D:\node_modules doesn't exist or is not a directory
      looking for modules in D:\Dropbox\dev\jekyll\blog\webpack\node_modules
        using description file: D:\Dropbox\dev\jekyll\blog\webpack\package.json (relative path: ./node_modules)
          Field 'browser' doesn't contain a valid alias configuration
        after using description file: D:\Dropbox\dev\jekyll\blog\webpack\package.json (relative path: ./node_modules)
          using description file: D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\package.json (relative path: .)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4 is not a file
            .js
              Field 'browser' doesn't contain a valid alias configuration
              D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4.js doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4.json doesn't exist
            as directory
              existing directory
                use ./index.js from main in package.json
                  using description file: D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\package.json (relative path: .)
                    Field 'browser' doesn't contain a valid alias configuration
                  after using description file: D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\package.json (relative path: .)
                    using description file: D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\package.json (relative path: ./index.js)
                      no extension
                        Field 'browser' doesn't contain a valid alias configuration
                        D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\index.js doesn't exist
                      .js
                        Field 'browser' doesn't contain a valid alias configuration
                        D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\index.js.js doesn't exist
                      .json
                        Field 'browser' doesn't contain a valid alias configuration
                        D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\index.js.json doesn't exist
                      as directory
                        D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\index.js doesn't exist
                using path: D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\index
                  using description file: D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\package.json (relative path: ./index)
                    no extension
                      Field 'browser' doesn't contain a valid alias configuration
                      D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\index doesn't exist
                    .js
                      Field 'browser' doesn't contain a valid alias configuration
                      D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\index.js doesn't exist
                    .json
                      Field 'browser' doesn't contain a valid alias configuration
                      D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\index.json doesn't exist
[D:\Dropbox\dev\jekyll\blog\node_modules]
[D:\Dropbox\dev\jekyll\node_modules]
[D:\Dropbox\dev\node_modules]
[D:\Dropbox\node_modules]
[D:\node_modules]
[D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4]
[D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4.js]
[D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4.json]
[D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\index.js]
[D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\index.js.js]
[D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\index.js.json]
[D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\index.js]
[D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\index]
[D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\index.js]
[D:\Dropbox\dev\jekyll\blog\webpack\node_modules\bootstrap4\index.json]
 @ ./entry.js 15:0-36

font-awesome没有任何fields in package.json that are recognised by webpack and there is also no index.js in the package,因此无法照常导入。大概你想使用 CSS 文件,所以你可以像这样导入它。

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

bootstrap4 不是真正的包。来自 npm registry - bootstrap4:

Security holding package

This package name is not currently in use, but was formerly occupied by another package. To avoid malicious use, npm is hanging on to the package name, but loosely, and we'll probably give it to you if you want it.

You may adopt this package by contacting support@npmjs.com and requesting the name.

您需要实际的 boostrap package。稳定版本目前是 3.3.7,但 v4 alpha 可在 npm 上获得。你可以安装它:

npm install --save bootstrap@next

你确实可以让 font-awesome 与 webpack 一起很好地工作。添加元素 "style"resolve.mainFields:

module.exports = {

  ...

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

  ...
}

前三个值来自 docs, and the last one comes from the font-awesome package.json 字段:

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

现在 webpack 知道 font-awesome 保存它的位置 CSS!请记住,webpack 将根据 webpack 配置字段 targetresolve.mainFields 设置默认值;此更改否定了此行为,因此您必须阅读 docs 以获得正确的 resolve.mainField 值。