Search code examples
javascriptwebpackcss-loaderwebpack-3extracttextwebpackplugin

Webpack throws TypeError: Super expression must either be null or a function, not undefined when importing LESS file


I have the following in a file initialize.js:

import App from './components/App';

import './styles/application.less';

document.addEventListener('DOMContentLoaded', () => {
    const app = new App();

    app.start();
});

In webpack.config.js I have:

'use strict';

const path = require('path');

const webpack = require('webpack');
const merge = require('webpack-merge');

const ProvidePlugin = webpack.ProvidePlugin;
const ModuleConcatenationPlugin = webpack.optimize.ModuleConcatenationPlugin;
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

const extractLess = new ExtractTextPlugin({
    filename: 'app.css',
});

const webpackCommon = {
    entry: {
        app: ['./app/initialize']
    },
    module: {
        rules: [{
            test: /\.js$/,
            exclude: /node_modules/,
            use: [{
                loader: 'babel-loader?presets[]=es2015'
            }]
        }, {
            test: /\.hbs$/,
            use: {
                loader: 'handlebars-loader'
            }
        }, {
            test: /\.less$/,
            exclude: /node_modules/,
            use: extractLess.extract({
                use: [{
                    loader: 'css-loader'
                }, {
                    loader: 'less-loader'
                }],
                // use style-loader in development
                fallback: 'style-loader'
            }),
        }]
    },
    output: {
        filename: 'app.js',
        path: path.join(__dirname, './public'),
        publicPath: '/'
    },
    plugins: [
        extractLess,
        new CopyWebpackPlugin([{
            from: './app/assets/index.html',
            to: './index.html'
        }]),
        new ProvidePlugin({
            $: 'jquery',
            _: 'underscore'
        }),
        new ModuleConcatenationPlugin(),
    ],
};

module.exports = merge(webpackCommon, {
    devtool: '#inline-source-map',
    devServer: {
        contentBase: path.join(__dirname, 'public'),
        compress: true,
        port: 9000
    }
});

I tried removing the the plugins and the contents of application.less, but I keep getting this error:

ERROR in ./node_modules/css-loader!./node_modules/less-loader/dist/cjs.js!./app/styles/application.less

Module build failed: TypeError: Super expression must either be null or a function, not undefined
at ...
@ ./app/styles/application.less 4:14-127
@ ./app/initialize.js

If I replace that LESS file with a CSS one and update the config it works fine, so I guess the problem has to do with less-loader.

I'm using Webpack 3.4.1, Style Loader 0.18.2, LESS Loader 4.0.5, Extract Text Webpack Plugin 3.0.0 and CSS Loader css-loader.


Solution

  • My bad, I didn't notice I was using an old less version. That was the culprit. Just updated it to 2.7.2 and the problem is gone.