I am incorporating the antd React component library in my project and I'm using ts-loader to efficiently load the components. Currently, I am facing an issue while trying to configure webpack to transpile less files. The error I am encountering is as follows:
ERROR in ./node_modules/antd/lib/button/style/index.less
Module build failed: Error: Cannot find module 'less'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:594:15)
at Function.Module._load (internal/modules/cjs/loader.js:520:25)
...
It seems like there might be an issue with the less-loader's installation or configuration, even though it is installed and should be configured correctly. Below is a snippet of my webpack configuration:
webpack.dev.js
module: {
rules: [
{
test: /\.scss$/,
loaders: ['style-loader', 'css-loader', 'postcss-loader', 'sass-loader']
},
{
test: /\.css$/,
loaders: ['style-loader', 'css-loader']
},
{
test: /\.less$/,
loader: 'less-loader' // compiles Less to CSS
},
]
},
common.webpack.js
const getTsLoaderRule = env => {
const rules = [
{ loader: 'cache-loader' },
{
loader: 'ts-loader',
options: {
transpileOnly: true,
getCustomTransformers: () => ({
before: [ tsImportPluginFactory({
libraryDirectory: 'es',
libraryName: 'antd',
style: 'css',
})]
}),
compilerOptions: {
module: 'es2015'
}
},
}
];
if (env === 'development') {
rules.unshift({
loader: 'react-hot-loader/webpack'
});
}
return rules;
};
...
module: {
rules: [
{
test: /\.tsx?$/,
use: getTsLoaderRule(options.env),
include: [utils.root('./src/main/webapp/app')],
exclude: ['node_modules']
},
{
test: /\.(jpe?g|png|gif|svg|woff2?|ttf|eot)$/i,
loaders: ['file-loader?hash=sha512&digest=hex&name=content/[hash].[ext]']
},
{
enforce: 'pre',
test: /\.jsx?$/,
loader: 'source-map-loader'
},
{
test: /\.tsx?$/,
enforce: 'pre',
loaders: 'tslint-loader',
exclude: ['node_modules']
}
]
},
Any suggestions on how to resolve this issue?