Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not resolve @import. Module parse failed: Unexpected character '@' #344

Closed
wzup opened this issue Jan 5, 2017 · 9 comments
Closed

Comments

@wzup
Copy link

wzup commented Jan 5, 2017

The loader can't resolve @imports, throws error. Config file is just fine, but sass-loader throws errors anyhow:

ERROR in ./assets/sass/index.scss
Module parse failed: C:\Users\user\app\assets\sass\index.scss Unexpected character '@' (2:0)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected character '@' (2:0)

The config:

module: {
    loaders: [
        {
            test: /\.scss$/i,
            include: [
                path.resolve(__dirname, '..', 'node_modules', 'bootstrap/scss')
            ],
            loaders: [
                'css-loader?minimize',
                'sass-loader'
            ]
        },
        {
            test: /\.css$/i,
            loader: 'css-loader?-minimize',

        },
        {
            test: /\.js$/i,
            exclude: /(node_modules|bower_components)/,
            loader: 'babel-loader',
            query: {
                presets: ['es2015', "stage-0", "react"]
            }
        }
    ]
},
sassLoader: {
    indentWidth: 4,
    outputStyle: 'expanded',
    sourceComments: true,
},
target: 'node'

Two .scss files.

index.scss:

@import "_one";

.text {
    color: red;
    font-style: italic;
}

one.scss:

.key {
    background-color: #ccc;
}

App file:

let css = require('index.scss');

Why error?

PS:

However, it works all right if I call loaders inline:

let css = require('css-loader!sass-loader!index.scss');

Why? I need a config, not inline

@wzup wzup changed the title Does not resolve @import. Unexpected character '@' Does not resolve @import. Module parse failed: Unexpected character '@' Jan 5, 2017
@wzup
Copy link
Author

wzup commented Jan 5, 2017

Loader's include option solved the issue.

module: {
    loaders: [
        {
            test: /\.(scss|sass)$/i,
            include: [
                path.resolve(__dirname, 'node_modules'),
                path.resolve(__dirname, 'path/to/imported/file/dir'), <== This solved the issue
            ],
            loaders: ["css", "sass"]
        },
    ]
},

@leongaban
Copy link

Hey I'm running into this same problem, however I added the include folder like you did above, yet still running into that error :( mind a look see here? #364

@yunda
Copy link

yunda commented Apr 11, 2017

@wzup thanks a lot. That helped me too

@gbhipolitoglyph
Copy link

yeah i also encounter the same issue, the include doesn't solve mine

@thismax
Copy link

thismax commented Feb 20, 2018

Here's what worked for me:

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

@nodediggity
Copy link

@thismax Thanks! This worked for me :)

@JeanHules
Copy link

JeanHules commented Sep 26, 2018

This worked for me

module: {
		rules: [
			{
				enforce: 'pre',
				test: /\.jsx?$/,
				loader: 'eslint-loader',
				exclude: /node_modules/
			},
			{ test: /\.(js|jsx)$/, exclude: /node_modules/, loader: 'babel-loader' },
			{ test: /\.(png|woff|woff2|eot|ttf|svg)$/, loader: 'url-loader?limit=100000' },
			{
				test: /\.scss$/,
				use: [
					{
						loader: 'style-loader'
					},
					{
						loader: 'css-loader'
					},
					{
						loader: 'sass-loader'
					}
				]
			}
		]
	}

@vladyn
Copy link

vladyn commented Feb 24, 2019

@thismax - thanks a lot - it solved the same issue for me!

@sudharsteyn
Copy link

Where should I add this above-mentioned module object?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants