Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  146] [ 2]  / answers: 1 / hits: 45286  / 6 Years ago, thu, may 31, 2018, 12:00:00

I recently upgraded from Webpack 3 to 4. It's now throwing an error:




Module parse failed: Unexpected character '@' You may need an
appropriate loader to handle this file type. | @import
'./scss/variables.scss'; | | * { @ ./src/index.js 1:0-22




In my styles.scss file, I am doing the following:



@import 'scss/variables.scss';

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

body {
font-family: Arial, Helvetica, sans-serif;
}


In my index.js file, I am only doing the following:



import './style.scss';


In my webpack.dev.js, all I changed was an addition of mode: 'development':



const StyleLintPlugin = require('stylelint-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const Dotenv = require('dotenv-webpack');

module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
filename: 'public/bundle.js'
},
module: {
rules: [
{
test: /.js$/,
exclude: /node_modules/,
loader: ['babel-loader', 'eslint-loader']
},
{
test: /.scss$/,
loader: ExtractTextPlugin.extract(['css-loader', 'sass-loader'])
}
]
},
plugins: [
new StyleLintPlugin({
configFile: '.stylelintrc',
context: 'src',
files: '**/*.scss',
failOnError: false,
quiet: false,
syntax: 'scss'
}),
new ExtractTextPlugin('public/style.css'),
new Dotenv()
]
};


I don't know what change from Webpack 3 to 4 has caused this error.



The issue I'm having is very similar to the issue posted here: Webpack 4 Module parse failed: Unexpected character '@' (1:0)



I have been through all related stackoverflow questions and none of them helped.



Here are the relevant dependencies in my package.json:



babel-loader: ^7.1.4,
css-loader: ^0.28.11,
eslint-loader: ^1.9.0,
extract-text-webpack-plugin: ^4.0.0-beta.0,
node-sass: ^4.9.0,
sass-loader: ^6.0.7,
style-loader: ^0.20.3,
stylelint-webpack-plugin: ^0.10.5,
uglifyjs-webpack-plugin: ^1.2.5,
webpack: ^4.8.3,
webpack-cli: ^2.1.4


Here are the relevant scripts in my package.json file, for reference in the comments:



scripts: {
watch: ./node_modules/.bin/webpack --mode development --watch --progress,
build: ./node_modules/.bin/webpack --mode production
},

More From » webpack

 Answers
42

The problem was the script I was using to run Webpack did not specify the config file. This is what it should look like:



scripts: {
watch: ./node_modules/.bin/webpack --watch --config webpack.dev.js,
},


I believe this was generating the @import problem because it was not loading the css-loader as without specifying the config file like above, it uses a default Webpack development config which does not include the css-loader.


[#54305] Sunday, May 27, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sidneyh

Total Points: 118
Total Questions: 108
Total Answers: 105

Location: Mali
Member since Fri, Jun 18, 2021
3 Years ago
sidneyh questions
Tue, Jun 7, 22, 00:00, 2 Years ago
Wed, Apr 13, 22, 00:00, 2 Years ago
Wed, Aug 12, 20, 00:00, 4 Years ago
Wed, Jun 3, 20, 00:00, 4 Years ago
Fri, Apr 24, 20, 00:00, 4 Years ago
;