模块构建失败:ReferenceError:未定义窗口

时间:2018-05-18 22:25:06

标签: javascript reactjs webpack

我使用 Webpack4 构建我的反应应用,webpack.config.js看起来如下:

const HtmlWebPackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const path = require('path');

module.exports = {
  entry: ['@babel/polyfill', './src/index.js'],
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
    rules: [{
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env", "@babel/preset-react"]
          }
        }
      },
      {
        test: /\.html$/,
        use: [{
          loader: "html-loader",
          options: {
            minimize: true
          }
        }]
      },
      {
        test: /\.scss$/,
        use: [{
          loader: MiniCssExtractPlugin.loader
        }, {
          loader: "style-loader"
        }, {
          loader: "css-loader"
        }, {
          loader: "sass-loader",
          options: {
            includePaths: ["./node_modules"]
          }
        }]
      }
    ]
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: "./public/index.html",
      filename: "./index.html"
    }),
    new MiniCssExtractPlugin({
      filename: "[name].css",
      chunkFilename: "[id].css"
    })
  ]
}; 

尝试编译应用程序,编译器抱怨:

ERROR in ./src/index.scss
Module build failed: ReferenceError: window is not defined
    at eval (webpack:///./node_modules/style-loader/lib/addStyles.js?:23:2)
    at eval (webpack:///./node_modules/style-loader/lib/addStyles.js?:12:46)
    at module.exports (webpack:///./node_modules/style-loader/lib/addStyles.js?:77:88)
    at eval (webpack:///./src/index.scss?./node_modules/style-loader!./node_modules/css-loader!./node_modules/sass-loader/lib/loader.js??ref--6-3:16:134)
    at Object../node_modules/style-loader/index.js!./node_modules/css-loader/index.js!./node_modules/sass-loader/lib/loader.js??ref--6-3!./src/index.scss (/Volumes/Develop/react-reason/cockpit/node_modules/style-loader/index.js!/Volumes/Develop/react-reason/cockpit/node_modules/css-loader/index.js!/Volumes/Develop/react-reason/cockpit/node_modules/sass-loader/lib/loader.js??ref--6-3!/Volumes/Develop/react-reason/cockpit/src/index.scss:104:1)
    at __webpack_require__ (/Volumes/Develop/react-reason/cockpit/node_modules/style-loader/index.js!/Volumes/Develop/react-reason/cockpit/node_modules/css-loader/index.js!/Volumes/Develop/react-reason/cockpit/node_modules/sass-loader/lib/loader.js??ref--6-3!/Volumes/Develop/react-reason/cockpit/src/index.scss:21:30)
    at /Volumes/Develop/react-reason/cockpit/node_modules/style-loader/index.js!/Volumes/Develop/react-reason/cockpit/node_modules/css-loader/index.js!/Volumes/Develop/react-reason/cockpit/node_modules/sass-loader/lib/loader.js??ref--6-3!/Volumes/Develop/react-reason/cockpit/src/index.scss:70:18
    at Object.<anonymous> (/Volumes/Develop/react-reason/cockpit/node_modules/style-loader/index.js!/Volumes/Develop/react-reason/cockpit/node_modules/css-loader/index.js!/Volumes/Develop/react-reason/cockpit/node_modules/sass-loader/lib/loader.js??ref--6-3!/Volumes/Develop/react-reason/cockpit/src/index.scss:73:10)
    at Module._compile (internal/modules/cjs/loader.js:678:30)
    at exec (/Volumes/Develop/react-reason/cockpit/node_modules/mini-css-extract-plugin/dist/loader.js:54:10)
 @ ./src/index.js 9:0-23
 @ multi (webpack)-dev-server/client?http://localhost:8080 @babel/polyfill ./src/index.js . 

index.scss文件的内容如下所示:

@import './font-awesome/scss/fontawesome.scss';
@import 'bootstrap/scss/bootstrap';  

窗口缺少什么?

2 个答案:

答案 0 :(得分:1)

我删除了样式加载器并解决了问题

 rules: [
            {
                test: /\.(s*)css$/,
                use: [
                    MiniCssExtractPlugin.loader,
                     //'style-loader',
                    'css-loader',
                    'sass-loader'
                ]
            }]

答案 1 :(得分:0)

根据此issue

  

不要在node_modules内使用includePaths,使用~已经意味着您希望从node_modules加载的内容。

所以index.scss应该是:

@import '~bootstrap/scss/bootstrap' 
@import 'path/to/other/file'