我第一次尝试使用webpack,并使用this tutorial入门并包含react.js。

完成步骤并安装stylecss模块后,我仍然收到错误消息,称CSS模块未返回函数。

这是我的index.jsx:

/** @jsx React.DOM */

'use strict';

require('../css/normalize.css');

var React = require('react');
var Hello = require('./Test/Hello');

React.render(<Hello />, document.getElementById('content'));

和我的webpack配置文件:
module.exports = {
    entry: './ui/src/index.jsx',
    output: {
        path: __dirname + '/build-ui',
        filename: 'app.js', //this is the default name, so you can skip it
        //at this directory our bundle file will be available
        //make sure port 8090 is used when launching webpack-dev-server
        publicPath: 'http://localhost:8090/assets'
    },
    module: {
        loaders: [
            {
                //tell webpack to use jsx-loader for all *.jsx files
                test: /\.jsx$/,
                loader: 'jsx-loader?insertPragma=React.DOM&harmony'
            },
            {
                test: /\.css$/,
                loader: "style!css"
            },
            {
                test: /\.scss$/,
                loader: "style!css!sass"
            }
        ]
    },
    externals: {
        //don't bundle the 'react' npm package with our bundle.js
        //but get it from a global 'React' variable
        'react': 'React'
    },
    resolve: {
        extensions: ['', '.js', '.jsx']
    }
};

当webpack尝试 bundle 项目时,总是会出现以下错误:
ERROR in Loader /Users/Johannes/Documents/Development/holmes/node_modules/css/index.js didn't return a function
 @ ./ui/src/index.jsx 5:0-31

我不知道该怎么办。有人遇到过这个问题吗?我该如何解决呢?

编辑:我的目录如下:
holmes/
  ui/
    css/
      normalize.css
    src/
      Test/
        Hello.jsx
      index.jsx
    index.html
  package.json
  webpack.config.js

最佳答案

此错误是由css中的node_modules模块引起的。由于您已经在配置中指定了css -loader,因此webpack会尝试在node_modules中查找该加载器,并找到另一个名为css的模块,该模块看起来不像加载器(因此出现错误消息)。

为避免混淆,您只需将-loader后缀添加到每个加载器。省略-loader后缀只是webpack的一项便利功能,但不幸的是,这是该情况下该错误的罪魁祸首。

    loaders: [
        {
            //tell webpack to use jsx-loader for all *.jsx files
            test: /\.jsx$/,
            loader: 'jsx-loader?insertPragma=React.DOM&harmony'
        },
        {
            test: /\.css$/,
            loader: "style-loader!css-loader"
        },
        {
            test: /\.scss$/,
            loader: "style-loader!css-loader!sass-loader"
        }

更新:从webpack 2开始,您不能再省略-loader后缀。我们决定这样做是为了防止出现此类错误。

10-05 20:39
查看更多