Alexander
Alexander

Reputation: 65

CSS ReactJs - You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file

I already know that this problem has been asked many times. I looked over all the questions, but it doesn't work. I converted typescript to javascript, everything is going very well until I get to implement css. After importing my css, I get this error.

ERROR in ./src/components/Navbar.css 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> .NavbarContainer {
|     width: 100%;
|     height: 100%;
 @ ./src/components/Nav.jsx 15:0-22
 @ ./src/components/index.js
 @ ./src/app.jsx
 @ ./src/index.jsx

The CSS I want to import is for Navbar only.

This is my webpack:

var HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');

module.exports = {
    mode: 'development',
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                loader: 'babel-loader'
            },
            {
                test: /\.css$/,
                use: [
                    { loader: 'style-loader' },
                    { loader: 'css-loader' },
                    { loader: 'less-loader' }
                ]
            }
        ]
    },
    resolve: {
        mainFiles: ['index', 'Index'],
        extensions: ['.js', '.jsx'],
        alias: {
            '@': path.resolve(__dirname, 'src/'),
        }
    },
    plugins: [new HtmlWebpackPlugin({
        template: './src/index.html'
    })],
    devServer: {
        historyApiFallback: true
    },
    externals: {
        // global app config object
        config: JSON.stringify({
            apiUrl: 'http://localhost:4000'
        })
    }
}

and this is my package.json

{
    "name": "glitcher",
    "version": "1.0.0",
    "license": "MIT",
    "scripts": {
        "build": "webpack --mode production",
        "start": "webpack-dev-server --open"
    },
    "dependencies": {
        "formik": "^2.1.4",
        "history": "^4.10.1",
        "prop-types": "^15.7.2",
        "query-string": "^6.11.0",
        "react": "^16.8.6",
        "react-dom": "^16.8.6",
        "react-router-dom": "^5.0.0",
        "rxjs": "^6.3.3",
        "yup": "^0.28.1"
    },
    "devDependencies": {
        "@babel/core": "^7.4.3",
        "@babel/preset-env": "^7.4.3",
        "@babel/preset-react": "^7.0.0",
        "babel-loader": "^8.0.5",
        "css-loader": "^3.6.0",
        "html-webpack-plugin": "^3.2.0",
        "less": "^3.11.0",
        "less-loader": "^5.0.0",
        "path": "^0.12.7",
        "style-loader": "^1.1.3",
        "webpack": "^4.46.0",
        "webpack-cli": "^3.3.0",
        "webpack-dev-server": "^3.2.1"
    }
}

The code works properly. I tried to uninstall everything and do another project but I think the error does not come from the npm packages. Something escapes me in webpack.config and I would need a little help.

Thank you!

Upvotes: 4

Views: 3480

Answers (1)

lawrence-witt
lawrence-witt

Reputation: 9354

The less-loader plugin converts Less files to CSS. The rule should be:

{
    test: /\.less$/,
    use: [
        { loader: 'style-loader' },
        { loader: 'css-loader' },
        { loader: 'less-loader' }
    ]
}

The compiler is throwing an error because you are trying to pass a CSS file to the Less loader - it doesn't know what to do with it. If you haven't actually got any Less files in your project, just get rid of the less-loader and change the property test to /\.css$/:

{
    test: /\.css$/,
    use: [
        { loader: 'style-loader' },
        { loader: 'css-loader' }
    ]
}

Otherwise, keep both rules in to cover both Less and CSS files.

Upvotes: 1

Related Questions