简体   繁体   English

为什么 webpack 找不到 scss 文件

[英]Why webpack can't find scss file

I am getting a module not found error when importing scss file into index.js.将 scss 文件导入 index.js 时出现模块未找到错误。

Here is my project directory:这是我的项目目录:

在此处输入图像描述

Here is my index.js (it's in the js folder):这是我的 index.js(它在 js 文件夹中):

import '../sass/main.scss'

Here is my webpack.config.js file:这是我的 webpack.config.js 文件:

const path = require('path');

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

package.json: package.json:

"devDependencies": {
    "css-loader": "^5.0.1",
    "sass": "^1.32.0",
    "sass-loader": "^10.1.0",
    "style-loader": "^2.0.0",
    "webpack": "^5.11.1",
    "webpack-cli": "^4.3.1"
  }

When I am moving the file to the root folder (src) it's working fine.当我将文件移动到根文件夹(src)时,它工作正常。 but when moved back to the folder structure it says module not found.但是当移回文件夹结构时,它说找不到模块。

Please any help would be appreciated.请任何帮助将不胜感激。

Edit:编辑:

I am sorry it was my mistake I should have read the error correctly, it turns out I was using a background-image without giving its src location.对不起,这是我的错误,我应该正确阅读错误,事实证明我使用的是背景图像而没有提供其 src 位置。

Try to set path from root folder: import './src/sass/main.scss' instead import '../sass/main.scss'尝试从根文件夹设置路径: import './src/sass/main.scss'而不是import '../sass/main.scss'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM