Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module not found: Error: Can't resolve './style.css' in Directory?

I am trying to run the command npm run build but it is not working. and I am getting the error below:

> [email protected] build /Users/Prashant/Code/typescript
> webpack

Hash: c6dbd1eb3357da70ca81
Version: webpack 3.2.0
Time: 477ms
    Asset     Size  Chunks             Chunk Names
bundle.js  2.89 kB       0  [emitted]  main
   [0] ./src/index.js 51 bytes {0} [built]
   [1] ./src/index.css 290 bytes {0} [built] [failed] [1 error]

ERROR in ./src/index.css
Module build failed: Unknown word (5:1)

  3 | // load the styles
  4 | var content = require("!!./index.css");
> 5 | if(typeof content === 'string') content = [[module.id, content, '']];
    | ^
  6 | // Prepare cssTransformation
  7 | var transform;
  8 |


@ ./src/index.js 1:0-22

My web pack config file(webpack.config.js) is:

var path = require('path');

module.exports = {
    entry: './src/index.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    'css-loader',
                    'style-loader'
                ]
            }
        ]
    }
};

And my CSS file(index.css) is

body {
    color:red;
}

and my js file index.js is below:

require("./index.css");
alert('this is my alert');

I am trying to run the file but it is not working I have checked all the spelling also try to add a lot of other CSS but it is not working, can you please help me how can I solve this issue?

like image 731
Prashant Barve Avatar asked Jul 22 '17 12:07

Prashant Barve


People also ask

How do I fix module not found?

Make sure that your file's letter-casing correctly matches with its respective static imports. Also make sure that your letter-casing in your filenames and imports are identical between your repository and local machine. If you are using git , make sure git config core. ignorecase false is set in your environment.

How to resolve sass error?

To solve the error "Module not found: Error: Can't resolve 'sass-loader'", make sure to install the package by opening your terminal in your project's root directory and running the command npm install --save-dev sass-loader sass webpack and restart your development server.

What is CSS modules?

A CSS Module is a CSS file that defines class and animation names that are scoped locally by default.


1 Answers

The loaders are applied in reverse order. That means you're applying style-loader first and then pass its result to css-loader. The output of style-loader is JavaScript, which will insert the styles into a <style> tag, but css-loader expects CSS and fails to parse JavaScript as it is not valid CSS.

The correct rule is:

{
    test: /\.css$/,
    use: [
        'style-loader',
        'css-loader'
    ]
}
like image 58
Michael Jungo Avatar answered Sep 29 '22 03:09

Michael Jungo