question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Errors when importing antd.less using less-loader

See original GitHub issue

Version

antd @2.13.4 webpack @3.6.0 less @3.0.0 less-loader @4.0.5

Describe

I’m importing antd.less into my antd project built with wepack. Follow the direction of official doc in the Customize Theme. I create a standalone less file and import the antd.less as follows:

@import "~antd/dist/antd.less";

But when I run npm start, an error occurys:

ERROR in ./node_modules/css-loader!./node_modules/less-loader/dist/cjs.js!./src/style/main.less
Module build failed:

@import "./themes/default";
^

The first thought in my head is there must be something goes wrong with webpack or less-loader, so I have tried a lot in configuring my webpack.config.js. But that seems doesn’t work.

I try to compile the antd.less directly in the CLI:

lessc ./node_modules/antd/dist/antd.less

Still goes wrong. So I guess it may be an antd issues:

FileError: './themes/default' wasn't found. Tried - D:\Projects\ncslab\node_modules\antd\lib\style\themes\default,D:\Projects\ncslab\node_modules\antd\dist\themes\default,D:\Projects\ncslab\node_modules\themes\default,themes\default.less in D:\Projects\ncslab\node_modules\antd\lib\style\index.less on line 1, column 1:
1 @import "./themes/default";
2 @import "./core/index";

Solution

The way on my searching a solution for the issue, I found another workaround to import antd style in my project, which using babel-plugin-import and add such a config items in the .babelrc file:

  "plugins": [[
    "import",
    [{
      "libraryName": "antd",
      "style": "css"
    }]
  ]]

Thongh it works, I still wonder why the official scheme doesn’t go as expected.

Here is my webpack configuration.

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

module.exports = {

  entry: './src/app.jsx',

  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },

  devtool: 'eval-source-map',

  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        include: [path.resolve(__dirname, "src")],
        use: 'babel-loader'
      },
      {
        test: /\.css$/,
        use: ['style-loader','css-loader']
      },
      {
        test: /\.less$/,
        use: ['style-loader','css-loader',"less-loader"]
      },
      {
        test: /\.(png|jpg|gif)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 8192
            }
          }
        ]
      }
    ]
  }
};

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:14
  • Comments:28 (6 by maintainers)

github_iconTop GitHub Comments

29reactions
afc163commented, Oct 10, 2017

You mean less@3.0.0-alpha.3? Could you try less@2.7.2?

12reactions
klhcommented, Feb 27, 2018

add .less to extensions in your webpack definition config file: extensions: ['.web.js', '.mjs', '.js', '.json', '.web.jsx', '.jsx', '.less'],

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to load antd less styles - css - Stack Overflow
you have to use less loader and config it in next.config.js of your project. it's not supported to use less in next app...
Read more >
Customize Theme - Ant Design
We are using Less as the development language for styling. A set of less variables are defined for each design aspect that can...
Read more >
ant-design/ant-design-english - Gitter
https://github.com/ant-design/babel-plugin-import ... Anyone with experience integrating Antd in meteor ? ... and are you using less loader ?
Read more >
less loader inline javascript is not enabled. is it set in your ...
I had same problem, I use webpack with less loader, I needed to add ... Gatsby run develop runs into error due to...
Read more >
gatsby-plugin-less
Write your stylesheets in Less and require or import them as normal. ... If you need to pass options to the Less loader...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found