css-loader icon indicating copy to clipboard operation
css-loader copied to clipboard

Module build failed: Unknown word (5:1)

Open metabacalhau opened this issue 10 years ago • 29 comments

When I try to load CSS like so require('css!./file.css'); I receive an error:

ERROR in ./~/css-loader!./file.css
Module build failed: Unknown word (5:1)
  3 | // load the styles
  4 | var content = require("!!./../../../node_modules/css-loader/index.js!./file.css");
> 5 | if(typeof content === 'string') content = [[module.id, content, '']];
    | ^
  6 | // add the styles to the DOM
  7 | var update = require("!./../../../node_modules/style-loader/addStyles.js")(content, {});
  8 | if(content.locals) module.exports = content.locals;

Content of the CSS file:

body {
}

Here is a complete sample: https://gist.github.com/desperate-man/50e522139734934b287382cf63e534af

However if I load the CSS file like this require('style!./file.css'); or this require('./file.css'); I have no errors. Is require('css!'); supposed to work or not? I use version 0.25.0.

metabacalhau avatar Oct 23 '16 13:10 metabacalhau

+1

dphov avatar Oct 26 '16 07:10 dphov

I had the same error, "style!" didn't help though. I'm using Angular2 Webpack Starter, turned out the issue was caused by unmatching include/exclude directories in webpack.common.js (css to-string-loader) and webpack.dev.js (css style-loader). So the erroneous config was:

{
test: /\.css$/,
use: ['to-string-loader', 'css-loader'],
exclude: [helpers.root('src', 'styles')]
}

{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
include: [helpers.root('src', 'assets')]
}

Exchanged that by:

{
test: /\.css$/,
use: ['to-string-loader', 'css-loader'],
exclude: [helpers.root('src', 'assets')]
}

{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
include: [helpers.root('src', 'assets')]
}

uhinze avatar Feb 06 '17 13:02 uhinze

I have this exact same error in webpack2 + css-loader, adding !css-loader or !style-loader or not does not help at all. Does anyone know why it might be happening or what can cause this kind of error?

EDIT: Now I know, I was trying to use it with vue the wrong way: http://stackoverflow.com/questions/37031123/why-am-i-not-being-able-to-compile-sass-with-webpack

chanon avatar Feb 17 '17 16:02 chanon

@desperate-man @uhinze @chanon Please provide a CSS file aswell, maybe related to #355

michael-ciniawsky avatar Mar 07 '17 00:03 michael-ciniawsky

@michael-ciniawsky https://gist.github.com/desperate-man/50e522139734934b287382cf63e534af

metabacalhau avatar Mar 07 '17 21:03 metabacalhau

I can confirm the bug, happening on both webpack 1 and 2. Going to investigate.

ekulabuhov avatar Mar 08 '17 13:03 ekulabuhov

Actually here's the problem, loaders are applied twice: ERROR in ./~/css-loader!./~/style-loader!./~/css-loader!./index.css

Both webpack config and require string is applied when resolving. I think this is as designed.

Solution: either remove the loader from require or from the webpack.config.js.

ekulabuhov avatar Mar 08 '17 13:03 ekulabuhov

@michael-ciniawsky don't know about support in webpack 1. But in webpack 2 you can use

require('./index.css'); // add `link`
var exportedStyles = require('!!css-loader!./index.css'); // just export

console.log(exportedStyles.toString()); // Here we have string with all styles from `index.css`

or with import

import './index.css';
import exportedStyles from '!!css-loader!./index.css';

console.log(exportedStyles.toString()); // Here we have string with all styles from `index.css`

/cc @michael-ciniawsky we can close this issue Also i am bad in documentation and very bad in documentation in english, maybe you can do PR with example in README.md about this :smile:

alexander-akait avatar Apr 20 '17 13:04 alexander-akait

/cc @desperate-man @dphov @uhinze @chanon @desperate-man @ekulabuhov please see comment above :star2:

alexander-akait avatar Apr 20 '17 13:04 alexander-akait

@evilebottnawi I in a overhaul of the README and will address this 👍

michael-ciniawsky avatar Apr 20 '17 14:04 michael-ciniawsky

In my case, the problem was the order of the loaders. I changed this:

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

to this:

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

and it worked.

wolmir avatar May 13 '17 13:05 wolmir

@wolmir this seems to be the actual fix for this! thanks!

mihaiOD avatar Nov 30 '17 15:11 mihaiOD

So it seems the readme is all up to date, and by following it I implemented all these fixes, but I'm still seeing this issue:

ERROR  Failed to compile with 3 errors23:03:46             
[1] 
[1]  error  in ./node_modules/bootstrap/dist/css/bootstrap.css
[1] 
[1] Module build failed: Unknown word (5:1)
[1] 
[1]   3 | // load the styles
[1]   4 | var content = require("!!../../../css-loader/index.js?{\"sourceMap\":true}!../../../postcss-loader/lib/index.js?{\"sourceMap\":true}!./bootstrap.css");
[1] > 5 | if(typeof content === 'string') content = [[module.id, content, '']];
[1]     | ^
[1]   6 | if(content.locals) module.exports = content.locals;
[1]   7 | // add the styles to the DOM
[1]   8 | var update = require("!../../../vue-style-loader/lib/addStylesClient.js")("0831cbd4", content, false);
[1] 
[1] 
[1]  @ ./node_modules/bootstrap/dist/css/bootstrap.css 4:14-192 18:2-22:4 19:20-198
[1]  @ ./client/src/main.js
[1]  @ multi (webpack)-dev-server/client?http://localhost:5000 webpack/hot/dev-server ./client/src/main.js
[1] 
[1]  error  in ./node_modules/bootstrap-vue/dist/bootstrap-vue.css
[1] 
[1] Module build failed: Unknown word (5:1)
[1] 
[1]   3 | // load the styles
[1]   4 | var content = require("!!../../css-loader/index.js?{\"sourceMap\":true}!../../postcss-loader/lib/index.js?{\"sourceMap\":true}!./bootstrap-vue.css");
[1] > 5 | if(typeof content === 'string') content = [[module.id, content, '']];
[1]     | ^
[1]   6 | if(content.locals) module.exports = content.locals;
[1]   7 | // add the styles to the DOM
[1]   8 | var update = require("!../../vue-style-loader/lib/addStylesClient.js")("f6bbb946", content, false);
[1] 
[1] 
[1]  @ ./node_modules/bootstrap-vue/dist/bootstrap-vue.css 4:14-184 18:2-22:4 19:20-190
[1]  @ ./client/src/main.js
[1]  @ multi (webpack)-dev-server/client?http://localhost:5000 webpack/hot/dev-server ./client/src/main.js
[1] 
[1]  error  

My main.js looks like this:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import BootstrapVue from 'bootstrap-vue'
import App from './App'
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'

Vue.config.productionTip = false

Vue.use(BootstrapVue);

/* eslint-disable no-new */
new Vue({
  el: '#app',
  components: { App },
  template: '<App/>'
})

claysquareninety avatar Jan 23 '18 05:01 claysquareninety

I had to do this instead:

{
        test: /\.css$/,
        loader: 'style-loader'
}

claysquareninety avatar Jan 23 '18 22:01 claysquareninety

this worked for me
{ test: /\.scss$/, use: [ { loader: "style-loader" }, { loader: "css-loader", }, ] }

hitolv4 avatar Jan 26 '18 16:01 hitolv4

I've tried several combinations, but the only combo that worked for me, was the same as for @claysquareninety

{
  test: /\.css$/,
  loader: 'style-loader'
}

All other combinations with css-loader, always gave me an error while compiling.

itsAnuga avatar Feb 05 '18 21:02 itsAnuga

{ test: /.scss$/, use: [ "style-loader" , "css-loader" ] } It worked for me

niennguyen84 avatar Mar 01 '18 01:03 niennguyen84

I had the same promlem. I have looked through my package.json. The 'babel-cli' was installed. I uninstalled it. It helped.

yenin-dev avatar Jun 27 '18 15:06 yenin-dev

i also have but i use all kinds of methods is not use please help.... image image i not know why just update my webpack

yehoan avatar Aug 03 '18 03:08 yehoan

I m also facing the same problem. Please provide me the solutions.

 const path = require('path');
 const webpack = require('webpack');
 const autoprefixer = require('autoprefixer');
 const precss = require('precss');

const TransferWebpackPlugin = require('transfer-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");


module.exports = {
entry: {
    'bundle.css': [
        path.resolve(__dirname, './src/assets/css/demo.css'),
        path.resolve(__dirname, './src/assets/css/theme.css'),
        path.resolve(__dirname, './src/assets/scss/demo.scss'),
        path.resolve(__dirname, './src/assets/scss/theme.scss')
    ],
    'bundle.js': [
        path.resolve(__dirname, './src/index.js')
    ],
    'common.js': [
        path.resolve(__dirname, './src/assets/js/bootstrap/bootstrap.bundle.js'),
        path.resolve(__dirname, './src/assets/js/bootstrap/bootstrap.js'),
        path.resolve(__dirname, './src/assets/js/demo.js'),
        path.resolve(__dirname, './src/assets/js/theme.js')
    ],
},
output: {
    filename: '[name]',
    path: path.resolve(__dirname, 'dist')
},
module: {
    rules: [
        {
            test: /\.scss$/,
            use: [
                {
                    loader: "css-loader",
                },
                {
                    loader: "style-loader",
                },
                {
                    loader: "postcss-loader",
                    options: {
                        sourceMap: true
                    }
                },
                {
                    loader: "sass-loader",
                    options: {
                        sourceMap: true
                    }
                }
            ]
        },
        {
            // test: /\.css$/,use:[{loader : 'css-loader'}],
            test: /\.css$/, use: [{loader: 'css-loader'}],
        }, {
            test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
            loader: "url?limit=10000&mimetype=application/font-woff"
        }, {
            test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
            loader: "url?limit=10000&mimetype=application/font-woff"
        }, {
            test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
            loader: "url?limit=10000&mimetype=application/octet-stream"
        }, {
            test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
            loader: "file"
        }, {
            test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
            loader: "url?limit=10000&mimetype=image/svg+xml"
        },
        {
            test: /\.js$/,
            use: {
                loader: 'babel-loader',
                options: {
                    presets: ['@babel/preset-env']
                }
            }
        },
        {
            test: /\.(png|jp?g|gif|svg)$/,
            use: [
                'file-loader?name=images/[name].[ext]',
                'image-webpack-loader?bypassOnDebug'
            ]
        },
        {
            test: /bootstrap\/dist\/js\/umd\//, use: 'imports-loader?jQuery=jquery'
        },
        {
            test: /font-awesome\.config\.js/,
            use: [
                {loader: 'style-loader'},
                {loader: 'font-awesome-loader'}
            ]
        },

    ]
},
plugins: [
    new webpack.ProvidePlugin({
        $: "jquery",
        jQuery: "jquery",
        "window.jQuery": "jquery",
        "jQuery.tagsinput": "bootstrap-tagsinput"
    }),
    new ExtractTextPlugin("bundle.css"),
    new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        'window.jQuery': 'jquery',
        tether: 'tether',
        Tether: 'tether',
        'window.Tether': 'tether',
        Popper: ['popper.js', 'default'],
        'window.Tether': 'tether',
        Alert: 'exports-loader?Alert!bootstrap/js/dist/alert',
        Button: 'exports-loader?Button!bootstrap/js/dist/button',
        Carousel: 'exports-loader?Carousel!bootstrap/js/dist/carousel',
        Collapse: 'exports-loader?Collapse!bootstrap/js/dist/collapse',
        Dropdown: 'exports-loader?Dropdown!bootstrap/js/dist/dropdown',
        Modal: 'exports-loader?Modal!bootstrap/js/dist/modal',
        Popover: 'exports-loader?Popover!bootstrap/js/dist/popover',
        Scrollspy: 'exports-loader?Scrollspy!bootstrap/js/dist/scrollspy',
        Tab: 'exports-loader?Tab!bootstrap/js/dist/tab',
        Tooltip: "exports-loader?Tooltip!bootstrap/js/dist/tooltip",
        Util: 'exports-loader?Util!bootstrap/js/dist/util'
    }),
],
resolve: {
    alias: {
        jquery: "jquery/src/jquery"
    }
}

};

ng2695 avatar Oct 25 '18 10:10 ng2695