简体   繁体   English

使用Webpack和Babel加载CSS时出错

[英]Error Loading CSS with Webpack and Babel

I'm building a SSR React App with Webpack3 and Babel6 but when I run the server, it cannot find any css file (Error: Cannot find module './file.css'). 我正在使用Webpack3和Babel6构建SSR React App,但是当我运行服务器时,它找不到任何CSS文件(错误:找不到模块'./file.css')。

Here is my folder structure so far: 到目前为止,这是我的文件夹结构:

src/
  |-components/
     |-App/
        |-App.css
        |-App.jsx

App.jsx App.jsx

....
import './App.css';
....

webpack.config.js webpack.config.js

....
module.exports: { loaders: [
  ....
  {
    test: /\.css$/,
    loader: ['style-loader', 'css-loader'], // I have loaders installed and included in package.json devDependencies
    include: path.join(__dirname, 'src'),
    exclude: /node_modules/
  }
  ....
]}

Any ideas? 有任何想法吗?

By default when you install packages locally, they go into node_modules folder in your project directory, therefore style-loader and css-loader are also included there. 默认情况下,在本地安装软件包时,它们会进入项目目录中的node_modules文件夹,因此样式加载器css-loader也包含在其中。 From your webpack.config.js file you are excluding this packages, which are needed in compiling your css assets. webpack.config.js文件中,您将排除此软件包,这是编译CSS资源所需的。 just remove the exclude line and every thing would be fine. 只需删除排除行,一切都会好起来的。

your webpack.config.js file would look like this. 您的webpack.config.js文件将如下所示。

webpack.config

....
module.exports: { loaders: [
....
{
  test: /\.css$/,
  loader: ['style-loader', 'css-loader'],
  include: path.join(__dirname, 'src'),
}
....
]}

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

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