css-loader: Unknown word error when loading plain CSS (not Sass or Less)
I’m trying out webpack for the first time… Trying to use the style-loader and css-loader.
Plain old css (not sass and not less) could not be loaded.
ERROR in ./~/css-loader!./~/style-loader!./~/css-loader!./src/styles/site.css
Module build failed: CssSyntaxError: C:\css-loader!C:\Users\Foo\Desktop\try-webpack\node_modules\style-loader\index.js!C:\Users\Foo\Desktop\try-webpack\node_modules\css-loader\index.js!C:\Users\Foo\Desktop\try-webpack\src\styles\site.css:5:1: Unknown word
at Input.error (C:\Users\Foo\Desktop\try-webpack\node_modules\postcss\lib\input.js:61:22)
at Parser.unknownWord (C:\Users\Foo\Desktop\try-webpack\node_modules\postcss\lib\parser.js:457:26)
at Parser.word (C:\Users\Foo\Desktop\try-webpack\node_modules\postcss\lib\parser.js:174:14)
at Parser.loop (C:\Users\Foo\Desktop\try-webpack\node_modules\postcss\lib\parser.js:60:26)
at parse (C:\Users\Foo\Desktop\try-webpack\node_modules\postcss\lib\parse.js:26:12)
at new LazyResult (C:\Users\Foo\Desktop\try-webpack\node_modules\postcss\lib\lazy-result.js:61:24)
at Processor.process (C:\Users\Foo\Desktop\try-webpack\node_modules\postcss\lib\processor.js:34:16)
at processCss (C:\Users\Foo\Desktop\try-webpack\node_modules\css-loader\lib\processCss.js:188:11)
at Object.module.exports (C:\Users\Foo\Desktop\try-webpack\node_modules\css-loader\lib\loader.js:24:2)
@ ./~/style-loader!./~/css-loader!./src/styles/site.css 4:14-162
My webpack.config.js file is just this:
var path = require('path');
module.exports = {
entry: "./src/scripts/index.js",
output: {
path: path.join(__dirname, "public", "build"),
filename: "app.js"
},
module: {
loaders: [
{ test: /\.css$/, loader: "style-loader!css-loader" }
]
}
};
If I remove the module part in the above thing, the error is gone.
I also tried { test: /\.css$/, loader: "style!css" }
and
{test: /\.css/, loader: "style!css" }
and various possible permutations with removing characters. There are no comments in the css file or the webpack.config.js file.
Specifying the loaders sequence in the import statement works (shown below):
var css = require("style!css!../styles/site.css");
Am I missing something here? I really can’t see it.
Regards
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 66
- Comments: 40
I seem to figure this out. According to webpack docs there are multiple ways to use loaders.
If use
require
statement to specify the loaders, e.g.require("style!css!../styles/site.css")
, you don’t need to configure css-loader in webpack.config.js.On the contrary, if already configured css-loader in webpack.config.js, you should use
require("../styles/site.css")
orimport "../styles/site.css"
.These problems may caused by specify loaders both in require and webpack.config.js.
I had the same issue too.
bundle.js:15842 Uncaught Error: Cannot find module "!!./../../node_modules/css-loader/index.js!./../../node_modules/style-loader/index.js!./../../node_modules/css-loader/index.js!./home.css"
vendor.js:160 ./~/css-loader!./~/style-loader!./~/css-loader!./css/home/home.css Module build failed: CssSyntaxError: F:\css-loader!F:\work\memberUserCenterFrontend\node_modules\style-loader\index.js!F:\work\memberUserCenterFrontend\node_modules\css-loader\index.js!F:\work\memberUserCenterFrontend\css\home\home.css:5:1: Unknown word at Input.error (F:\work\memberUserCenterFrontend\node_modules\css-loader\node_modules\postcss\lib\input.js:61:22) at Parser.unknownWord (F:\work\memberUserCenterFrontend\node_modules\css-loader\node_modules\postcss\lib\parser.js:457:26) at Parser.word (F:\work\memberUserCenterFrontend\node_modules\css-loader\node_modules\postcss\lib\parser.js:174:14) at Parser.loop (F:\work\memberUserCenterFrontend\node_modules\css-loader\node_modules\postcss\lib\parser.js:60:26) at parse (F:\work\memberUserCenterFrontend\node_modules\css-loader\node_modules\postcss\lib\parse.js:26:12) at new LazyResult (F:\work\memberUserCenterFrontend\node_modules\css-loader\node_modules\postcss\lib\lazy-result.js:61:24) at Processor.process (F:\work\memberUserCenterFrontend\node_modules\css-loader\node_modules\postcss\lib\processor.js:34:16) at processCss (F:\work\memberUserCenterFrontend\node_modules\css-loader\lib\processCss.js:188:11) at Object.module.exports (F:\work\memberUserCenterFrontend\node_modules\css-loader\lib\loader.js:24:2) @ ./~/style-loader!./~/css-loader!./css/home/home.css 4:14-162
But the strange thing is, if I configure loader include a not exist path, It just work fine.
I had the same issue. I think in generally this error happens when the file is compiled by the css loader for more than once. Check your config to see if there are duplicated css loaders.
I switched from
use: [ 'css-loader', 'style-loader' ]
touse: ['style-loader', 'css-loader']
(note the order) and all is well in the world. Weird.I fixed it by remove
{ test: /\.css$/, loader: 'style!css' }
from webpack.config.jsFor whatever reason, I gave myself a couple of hours to continuously uninstall style-loader, css-loader and webpack and trying the versions form Dec 2015 and failing to solve the issue, I also ran npm cache clean, a couple of times in between.
For the 5th time, I just installed webpack, css-loader and style-loader in that order (same as I did the very first time) and voila, it just worked. Absolutely no idea why it failed the first time and succeeded the nth time. (things done in the same). (Throughout this ordeal, I had no other npm packages in my project other than webpack and the two loaders.)
May be someone should close it, or look at it, or whatever…
As of Storybook v5 there is no need to add custom .css loader as it is already defined. If you are getting an error after upgrade from v4 you should most probably review your
.storybook/webpack.config.js
and remove custom .css loader.Yes, tks for pointing to the right direction, reproduce here if anyone encounter it:
In our application using Storybook webpack I have fixed this issue by using this code:
config.module.rules = config.module.rules.filter(rule => rule.test.toString().indexOf('css') == -1);
In order to make it work just add
exclude: /\.css$/,
to the existing rule and then add your own code. You may write something like this (this exact code may not work, but it should be easy to fix if required):Additionally from the docs,
In my case, I accidentally had the same loader twice. For example:
Removing one worked, so it isn’t applied twice.
This issue is the very high in google, so posting here, maybe helps somebody.
Got the same even now by using ‘css-loader’ before ‘style-loader’. E.g.
works, but not
Hey guys, I actually found that if you change the order of your loaders that fix the problem. I used to have css-loader first and style-loader second inside the array but when i switch them it worked.
Spent hours, I just fixed it…shit…when using the vue-cli
webpack
template, the dev webpack config file (build/webpack.dev.conf.js
) line 16-18 :replace with config like:
the whole
build/webpack.dev.conf.js
is:According to the most recent docs on GitHub, the preferred syntax is:
{ test: /\.css$/, use: [ { loader: 'style-loader' }, { loader: 'css-loader' } ], exclude: /node_modules/ }
Please avoid my mistake and take serious note of you potentially having two loaders for the same thing.
I was loading CSS myself, and had a 3rd party plugin that also loaded CSS, so I didn’t immediately think about it.
Removing my loader made this work.
If it might help someone sometime, I was playing with types importing react-scripts. I didnt need that, but the addition to it caused this error for me. Removal, and voila, it worked. Now to tell my PM why it’s taken me so long haha
Vow after hours of struggling with this issue finally writing style-loader above css-loader worked thanks a lot
But what if I want to have “style.module.css” and is not using SASS?
I have to add custom .css loader for Storybook v5 right? Like so (but it is not working):
@splurtcake maybe you should config like this: { test: /.scss$/, loader: ExtractTextPlugin.extract( ‘style-loader’, ‘css-loader!postcss-loader’, ‘sass-loader’ ) }