CSS 的 Webpack 包 returns 'Cannot find module'

Webpack bundle returns 'Cannot find module' for CSS

我有一个文件夹结构如下的应用程序:

 /app/
    |- /assets/
    |   |- /css/
    |       |- myapp.css
    |- index.html
    |- index.js
 /dist/
 /node_modules/
 .babelrc
 package.json
 webpack.config.js

myapp.css 是一个简单、普通的 css,没有什么特别之处。只是 body 上的样式,看看它是否有效。 我的 webpack.config.js 文件有这个:

// In 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: /\.css$/, loaders: ['style', 'css']}
    ]
  },
  plugins: [HTMLWebpackPluginConfig]
};

我已将所需的 css 文件放在 index.js 上,如下所示:

var React = require('react');
var ReactDOM = require('react-dom');

require('assets/css/myapp.css');

var HelloWorld = React.createClass({
    render: function () {
        return (
            <div> Hello ReactJs</div>
        )
    }
});

ReactDOM.render(
    <HelloWorld />,
    document.getElementById('app')
)

我已经尝试了所有方法来加载它,在 assets word 之前添加 ../ 和 everythig,但我一直在控制台上收到此错误: Error: Cannot find module "assets/css/myapp.css"

我仔细检查了一下,css-loaderstyle-loader 都在 /node_modules 文件夹中。

我做错了什么?我坚持了3个多小时,检查了数十个教程,并且我已经完成了所有指定的。感谢您的帮助!

嗯...我只需要将 ./ 添加到 require 行,如下所示:

require('./assets/css/myapp.css');

希望这对其他人有帮助。谢谢,@QoP