admin管理员组

文章数量:1221299

In our original setup of react + webpack, when running npm start we are facing "Can't find stylesheet to import" issue. But this happens only in Windows and not in MacOS.

To replicate this issue on a small codebase, I have created following setup.

In this setup you will get the error when running npm run dev:

ERROR in ./src/main.scss (./node_modules/css-loader/dist/cjs.js!./node_modules/sass-loader/dist/cjs.js??ruleSet[1].rules[1].use[2]!./src/main.scss) Module build failed (from ./node_modules/sass-loader/dist/cjs.js): Can't find stylesheet to import. ╷ 1 │ @use "D:\Work\Playground\webpack-sass\stylesheets\_utils.scss" as *; │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

package.json:

    {
  "name": "webpack-sass",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "dev": "webpack-dev-server --open",
    "build": "webpack"
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "@babel/preset-env": "^7.26.0",
    "babel-loader": "^9.2.1",
    "css-loader": "^7.1.2",
    "html-webpack-plugin": "^5.6.3",
    "sass": "^1.80.7",
    "sass-loader": "^16.0.3",
    "style-loader": "^4.0.0",
    "webpack": "^5.96.1",
    "webpack-cli": "^6.0.1",
    "webpack-dev-server": "^5.2.0"
  }
}

webpack.config.js

    const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

const sassAdditionalData = `@use "${path.join(__dirname, "./stylesheets/_utils.scss")}" as *;`;
const stylesPath = path.resolve(__dirname, "stylesheets");
module.exports = {
  mode: "development",
  entry: "./src/index.js",
  devServer: {
    static: "./dist",
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: "babel-loader",
      },
      {
        test: /\.scss$/,
        use: [
          "style-loader",
          "css-loader",
          {
            loader: "sass-loader",
            options: {
              sassOptions: {
                includePaths: [stylesPath],
              },
              additionalData: sassAdditionalData,
            },
          },
        ],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: "Development",
    }),
  ],
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "dist"),
  },
};

.babelrc

    {
  "presets": ["@babel/preset-env"]
}

src/index.js

import "./main.scss";

const foo = (name) => {
  console.log(name);
};

foo("Nimish");

main.scss

$body-color: red;

body {
  background-color: $body-color;
}

stylesheets/_utils.scss

$body-color-new: green;

I have tried upgrading webpack, sass-loader, adding include paths and referencing with just file name but nothing seems to be working.

Also when I control+click the exact filepath shown in error, it opens the actual file.

So I am not really sure why sass-loader does not find it.

Any help is highly appreciated.

本文标签: reactjsFacing issue of Can39t find stylesheet to import when using webpack and sass loaderStack Overflow