Issue in configure the scss with bootstrap in react project - reactjs

I am learning react and currently facing the issue in configuring the scss with bootstrap in a project, that was initially built by my friend and now I want to work on it. It works fine when I configure it for bootstrap. But when I try to configure the scss I have the following type error..
1)Here is the terminal error
ERROR in ./client/styles/styles.scss
Module parse failed: Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
| #import '~bootstrap/scss/bootstrap.scss';
| $btn-font-weight:bold;
|
# ./client/App.js 17:0-31
# ./client/main.js
# multi react-hot-loader/patch webpack-hot-middleware/client?reload=true ./client/main.js
2/ Here is my webpack.config file:
const path = require('path')
const webpack = require('webpack')
const CURRENT_WORKING_DIR = process.cwd()
const config = {
name: "browser",
mode: "development",
devtool: 'eval-source-map',
entry: [
'react-hot-loader/patch',
'webpack-hot-middleware/client?reload=true',
path.join(CURRENT_WORKING_DIR, 'client/main.js')
],
output: {
path: path.join(CURRENT_WORKING_DIR , '/dist'),
filename: 'bundle.js',
publicPath: '/dist/'
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: [
'babel-loader'
]
}, {
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
},
{
test: /\.scss$/,
loaders: [ 'style-loader', 'css-loader', 'sass-loader' ]
},
{
test: /\.(ttf|eot|svg|gif|jpg|png)(\?[\s\S]+)?$/,
use: 'file-loader'
}
]
}, plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin()
]
}
module.exports = config
3)Here is my app.js file
import React from 'react'
import MainRouter from './MainRouter'
import {BrowserRouter} from 'react-router-dom'
import './styles/styles.scss';
import { hot } from 'react-hot-loader';
const App = () => (
<BrowserRouter>
<MuiThemeProvider>
<MainRouter/>
</MuiThemeProvider>
</BrowserRouter>
)
export default hot(module)(App)

To achieve expected result, use below option of using loaders in below format
use: [
{
loader: 'style-loader'
},
{
loader: 'css-loader'
},
{
loader: 'sass-loader'
}
]

Related

importing katex.min.css into my React file and webpack.config.js just doesn't work

After an epic fight with my webpack, I need to go to bed. I honestly don't know wtf the issue is.
I tried now for hours and copied some webpack configs from the web together.
This is my webpack.config.js
const path = require('path');
const MiniCssExtractPlugin = require('css-minimizer-webpack-plugin');
const nodeModulesPath = path.resolve(__dirname, 'node_modules');
module.exports = {
entry: './src/index.tsx',
module: {
rules: [
{
test: /\.json$/,
use: 'json-loader',
},
{
test: /\.(js)x?$/,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: /\.(ts)x?$/,
exclude: /node_modules|\.d\.ts$/,
use: {
loader: 'ts-loader',
options: {
compilerOptions: {
noEmit: false,
},
},
},
},
{
test: /\.(scss|css)$/,
// exclude: /node_modules/,
use: [
MiniCssExtractPlugin.loader,
{ loader: 'style-loader' },
{ loader: 'css-loader' },
// This is needed to help find the KaTeX fonts.
// https://github.com/bholloway/resolve-url-loader/issues/107
// https://github.com/bholloway/resolve-url-loader/issues/212
// https://stackoverflow.com/questions/54042335/webpack-and-fonts-with-relative-paths
// https://stackoverflow.com/questions/68366936/how-to-bundle-katex-css-from-node-modules-to-a-single-output-css-from-a-sass-us
'resolve-url-loader',
{
loader: "sass-loader",
options: {
// This is needed for resolve-url-loader to work!
// https://github.com/bholloway/resolve-url-loader/issues/212#issuecomment-1011630220
sourceMap: true,
sassOptions: {
includePaths: [nodeModulesPath],
},
},
},
],
},
],
},
plugins : [new MiniCssExtractPlugin()],
resolve : {
extensions: ['.css', '.tsx', '.ts', '.js'],
},
output : {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
}
};
I get the error message:
["..." | object { assert?, compiler?, dependency?, descriptionData?, enforce?, exclude?, generator?, include?, issuer?, issuerLayer?, layer?, loader?, mimetype?, oneOf?, options?, parser?, realResource?, resolve?, resource?, resourceFragment?, resourceQuery?, rules?, scheme?, sideEffects?, test?, type?, use? }, ...]
-> A rule.
Details:
* configuration.module.rules[3].use[0] should be one of these:
object { ident?, loader?, options? } | function | non-empty string
-> A description of an applied loader.
Details:
* configuration.module.rules[3].use[0] should be an object:
object { ident?, loader?, options? }
* configuration.module.rules[3].use[0] should be an instance of function.
* configuration.module.rules[3].use[0] should be a non-empty string.
-> A loader request.
* configuration.module.rules[3].use[0] should be one of these:
object { ident?, loader?, options? } | function | non-empty string
-> An use item.
Error: Process completed with exit code 2.
However, what I want is simply to import Katex, like
import 'katex/dist/katex.min.css'
into my table component.
Maybe someone has an idea?
Ok, I found it. And just because it was so hard for me to find the right answer online (that might be just me) - here is my solution.
I wanted to use katex in typescript React - to show a nice table here
https://0xpolygonmiden.github.io/examples/.
For that I imported into my table component
import { Fragment } from 'react';
import ReactMarkdown from 'react-markdown';
import gfm from 'remark-gfm';
import math from 'remark-math';
import katex from 'rehype-katex'
import { assemblerInstructions } from '../data/instructions';
import 'katex/dist/katex.min.css'
So katex comes with its own CSS file. Locally it runs fine and renders beautifully. However, when I tried to deploy on GitHub pages, webpack compliant that there is a loader missing for that import.
Ok. So I tried to find the right loader for that import for webpack 5.
It is simply the sass-loader and by default node-modules are excluded so I had to remove that exclude: /node_modules/, when I defined the sass-loader for CSS files. Easy, ha?
const path = require('path');
module.exports = {
entry: './src/index.tsx',
module: {
rules: [
{
test: /\.json$/,
use: 'json-loader',
},
{
test: /\.(js)x?$/,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: /\.(ts)x?$/,
exclude: /node_modules|\.d\.ts$/, // this line as well
use: {
loader: 'ts-loader',
options: {
compilerOptions: {
noEmit: false, // this option will solve the issue
},
},
},
},
{
test: /\.css$/,
use:['style-loader', 'css-loader', 'sass-loader'],
},
],
},
resolve: {
extensions: ['.css', '.tsx', '.ts', '.js'],
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
};
Here is the full code for those who are interested.
https://github.com/0xPolygonMiden/examples/tree/main/playground

Webpack error "Module build failed: ERR_INVALID_ARG_TYPE"

I'm pretty new to react and am having some issues with web pack. All I'm trying to do is import a SVG file into my react project. I'm lost as to what is causing this as it doesn't give me much info about it. I would be grateful if anyone knows what's going on, thank you in advance lol.
Error code in console:
ERROR in ./src/img/flag/united-kingdom.svg
Module build failed: TypeError [ERR_INVALID_ARG_TYPE]: The "from" argument must be of type string. Received undefined
at validateString (internal/validators.js:120:11)
at Object.relative (path.js:437:5)
at Object.loader (C:\Users\Callum\Desktop\sites\csgo\mern\client\node_modules\file-loader\dist\index.js:78:72)
# ./src/components/header.js 12:0-74
# ./src/components/app.js
# ./src/app.js
My webpack file (webpack.common.js)
const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
entry: {
app: './src/app.js',
vender: [
'react', 'react-dom', 'redux',
'react-redux', 'react-router-dom',
'axios', 'prop-types']
},
output: {
path: path.resolve(__dirname, '../docs/'),
filename: "js/[name].[chunkhash].js"
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: path.resolve(__dirname, 'node_modules'),
loader: 'babel-loader'
},
{
test: /\.s?css$/,
exclude: path.resolve(__dirname, "node_modules"),
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [{
loader: 'css-loader',
options: {
sourceMap: true
}
},'sass-loader'],
})
},
{
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'file-loader',
options: {
outputPath: 'images/',
name: '[name][hash].[ext]',
},
},
],
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
loader: 'file-loader?name=/fonts/[name].[ext]'
},
{
test: /\.(svg)$/,
exclude: /fonts/, /* dont want svg fonts from fonts folder to be included */
use: [
{
loader: 'svg-url-loader',
options: {
noquotes: true,
},
},
],
},
]
},
plugins: [
new HtmlWebpackPlugin({template: path.resolve(__dirname, 'src/index.html')}),
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
filename: "manifest.js",
chunks: ['vender']
}),
new ExtractTextPlugin({
filename: 'styles/style.css'
}),
]
}
Import code (literally all that references it):
import { ReactComponent as UKFlag } from '../img/flag/united-kingdom.svg'
Just had this problem, changed my file-loader in package.json from 6.2.0 to 6.1.1 and it fixed it

Error while importing font or images file inside scss using Webpack 4 and react js

I am using webpack and react js.
I am getting this error when i try to import image or font file inside my scss file.
I have tried many solutions but none of them solved my problem,
webpack.common.js
enter image description here
const path = require("path");
var HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: {
main: "./src/index.js",
vendor: "./src/vendor.js"
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.html$/,
use: ["html-loader"]
},
{
test: /\.(ttf|svg|png|jpg|gif)$/,
use: {
loader: "file-loader",
options: {
name: "[name].[hash].[ext]",
outputPath: "imgs"
}
}
}
]
}
};
Here is another webpack.dev.js
module.exports = merge(common, {
mode: "development",
output: {
filename: "[name].bundle.js",
path: path.resolve(__dirname, "dist")
},
plugins: [
new HtmlWebpackPlugin({
template: "./src/template.html"
})
],
module: {
rules: [
{
test: /\.scss$/,
use: [
"style-loader", //3. Inject styles into DOM
"css-loader", //2. Turns css into commonjs
"sass-loader" //1. Turns sass into css
]
}
]
}
});
ERROR in ./src/assets/index.scss (./node_modules/css-loader/dist/cjs.js!./node_modules/sass-loader/lib/loader.js!./src/assets/index.scss)
Module not found: Error: Can't resolve '../../../src/assets/fonts/icomoon.ttf' in 'C:\Users\jamal\Documents\webpack-demo-app\src\assets'
# ./src/index.js
# multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.js
You need to remember that the import actually takes "place" from the root index.scss file (the one that loads all the other partials). So the path you are using to fetch the asset is not accurate.
You need to use ./fonts/icomoon.ttf instead of ../fonts/icomoon.ttf
your file structure:
assets/
------/fonts
------/images
------/sass
------/---/partial1.scss // while the reference to the image is here
------/index.scss // the actual root of the reference call is here

webpack 3.8.1 facebook react import node module css not working

I am using Facebook react with Webpack and I import node module CSS in my component. It's not working.
webpack.config.js
const path = require("path");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
entry: "./src/index.js",
output: {
path: path.resolve("dist"),
filename: "index_bundle.js"
},
module: {
loaders: [
{
test: /\.jsx?$/,
loader: "babel-loader",
exclude: /node_modules/,
query: {
presets: ["react", "es2015", "stage-0"],
plugins: ["transform-class-properties"]
}
},
{
test: /\.css?$/,
loaders: ExtractTextPlugin.extract(
"style-loader!css-loader?modules=true&localIdentName=[name]_[local]_[hash:base64:5]"
)
}
]
},
plugins: [new ExtractTextPlugin("./public/styles.css")]
};
React component file
I import react-times css file
import React from "react";
import TimePicker from "react-times";
import "react-times/css/material/default.css";
export default class Book extends React.Component {
render() {
return (
<TimePicker />
);
}
}
you can try
// module
{
test : /\.css$/,
loader: ExtractTextPlugin.extract({
fallback: "style-loader",
use: ['css-loader']
})
}
// plugins
new ExtractTextPlugin("styles.css")

Using Grommet with ReactJS webpack and scss import Issue

I am trying to use grommet in my React Project and i am using webpack. I have added the scss loader and when i build my app i am getting the following error:
ERROR in ./~/css-loader!./~/sass-loader!./scss/index.scss
Module build failed:
#import "inuit-defaults/settings.defaults";
^
File to import not found or unreadable: inuit-defaults/settings.defaults
Parent style sheet: /Users/hduser/sample-app/node_modules/grommet/scss/grommet-core/_settings.scss
in /Users/hduser/sample-app/node_modules/grommet/scss/grommet-core/_settings.scss (line 2, column 1)
# ./scss/index.scss 4:14-116 13:2-17:4 14:20-122
Not sure what i am doing wrong..
Here is my webpack.config.js
var webpack = require('webpack');
var path = require('path');
module.exports ={
devtool :'cheap-module-eval-source-map',
entry:[
'webpack/hot/only-dev-server',
'./index.jsx'
],
module:{
loaders:[
{
test: /\.jsx?$/,
exclude :/node_modules/,
include: __dirname,
loader:'react-hot'
},
{
test: /\.jsx?$/,
exclude :/node_modules/,
include: __dirname,
loader:'babel',
query:{
"plugins":["transform-decorators-legacy"],
"presets":["es2015","react"]
}
},
{
test :/\.css?$/,
include: __dirname,
loaders :['style','css']
},
{
test: /\.less$/,
loader: "style!css!less"
},
{
test: /\.json$/,
loader: "json"
},
{
test: /\.scss$/,
loaders: ["style", "css", "sass"]
}
]
},
resolve:{
extensions:['','.js','.jsx']
},
output:{
path: __dirname+'/',
publicPath:'/',
filename:'bundle.js'
},
devServer:{
contentBase: './',
hot:true
},
plugins: [
new webpack.HotModuleReplacementPlugin()
,new webpack.NoErrorsPlugin()
]
}
Update
if u use npm3+ you can simply do this(see alan's explanation here):
{
test: /\.scss$/,
loader: 'style!css!sass?outputStyle=expanded&' +
'includePaths[]=' +
(encodeURIComponent(path.resolve('./node_modules')))
},
or write scss loader like this:
{
test: /\.scss$/,
loader: "style!css!sass?OutputStyle=expaned&" +
'includePaths[]=' +
(encodeURIComponent(
path.resolve(process.cwd(), './node_modules')
)) +
'&includePaths[]=' +
(encodeURIComponent(
path.resolve(process.cwd(),
'./node_modules/grommet/node_modules'))
)
}
I had the same issue, and got this working by this way:
var ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
// ...
module: {
loaders: [
// ...
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('css!sass')
}
]
},
sassLoader: {
includePaths: [ './node_modules' ]
},
plugins: [
new ExtractTextPlugin('public/grommet.css', {
allChunks: true
})
]
}
But remember to import the generated css file into your index.html
I fixed it by following Webpack 2 conventions.
{
test: /(\.scss$)/,
loaders: [{
loader: 'style-loader'
}, {
loader: 'css-loader'
}, {
loader: 'sass-loader',
options: {
outputStyle: 'compressed',
includePaths: ['./node_modules']
}
}]
}
Working quite well on my side!
Use the following scss loader in webpack config (thanks to https://github.com/primozs)
{
test: /\.scss$/,
loader: 'style!css!sass?outputStyle=expanded&' +
'includePaths[]=' +
(encodeURIComponent(
path.resolve(process.cwd(), './node_modules')
)) +
'&includePaths[]=' +
(encodeURIComponent(
path.resolve( process.cwd(),
'./node_modules/grommet/node_modules'))
)
}

Resources