gpt4 book ai didi

reactjs - 使用 Webpack、SASS 和 React 在 'cannot find module' 上出现错误 'main.scss'

转载 作者:行者123 更新时间:2023-12-03 23:38:10 26 4
gpt4 key购买 nike

当尝试设置 SCSS 以使用 Webpack 在我的 React 应用程序上运行样式时,我遇到了错误:

Module not found: Error: Can't resolve 'style' in '/Users/sachinkaria/Workspace/GC' @ ./app/index.js 4:0-29 @ multi ./app/index.js'

在浏览器中:
Uncaught Error: Cannot find module "/styles/main.scss"

我的 webpack.config.js 配置如下:
var HtmlWebpackPlugin = require('html-webpack-plugin');-
var HTMLWebpackPluginConfig = new HtmlWebpackPlugin({
template: __dirname + '/app/index.html',
filename: 'index.html',
inject: 'body'
});

module.exports = {
entry: [
'./app/index.js'
],
output: {
path: __dirname + '/dist',
filename: "index_bundle.js"
},
module: {
loaders: [
{test: /\.js$/, exclude: /node_modules/, loader: "babel-loader"},{
test: /\.scss$/,
loaders: ['style', 'css', 'sass']
}
]
},
plugins: [HTMLWebpackPluginConfig]
};

我的 package.json:
    {
"name": "get-cooked",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server",
"prod": "webpack -p"
},
"author": "Sachin Karia",
"license": "ISC",
"dependencies": {
"classnames": "^2.2.5",
"react": "^0.14.6",
"react-bootstrap": "^0.30.7",
"react-dom": "^0.14.6",
"react-router": "^2.0.0-rc5"
},
"devDependencies": {
"babel-core": "^6.4.5",
"babel-loader": "^6.2.1",
"babel-preset-react": "^6.3.13",
"html-webpack-plugin": "^2.7.1",
"node-sass": "^4.5.0",
"sass-loader": "^6.0.2",
"webpack": "2.2.1",
"webpack-dev-server": "^1.14.1"
}
}

我要导入 main.scss 的 index.js (产生错误):
var React = require('react');
var ReactDOM = require('react-dom');
var routes = require('./config/routes');
require('./styles/main.scss');

ReactDOM.render(routes, document.getElementById('app'));

我所有的 scss 文件都在我的样式文件夹中,但是,我似乎无法将它们导入我的 index.js 并返回“找不到模块”错误。

这是我的文件夹结构:
- app
- components
- Home.js
- config
- routes.js
- containers
- styles
- components
- main.scss
- index.html
- index.js
- nodemodules
webpack.config.js
package.json

任何帮助表示赞赏!

最佳答案

原来这是一个加载程序问题,并且没有正确的节点模块。只需运行以下脚本即可解决问题:

npm install style-loader css-loader --save-dev 

以及使用将“style-loader”、“css-loader”和“sass-loader”添加到 Webpack 而不是“style”、“css”、“sass”。

关于reactjs - 使用 Webpack、SASS 和 React 在 'cannot find module' 上出现错误 'main.scss',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42475046/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com