我是第一次尝试使用webpack,并使用this tutorial开始并包含了react.js。
在完成这些步骤并安装了css和css
模块之后,我一直收到一个错误,指出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尝试捆绑项目时,它总是会显示以下错误:
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
发布于 2015-04-27 16:23:51
此错误是由node_modules
中的css
模块引起的。由于您已经在配置中指定了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
后缀了。我们决定这样做是为了防止出现这样的错误。
发布于 2016-01-05 01:06:19
我在使用react-flexbox-grid
时也遇到了类似的问题。在我的例子中,解决方案是安装css-loader
和style-loader
npm模块:
npm install css-loader style-loader --save-dev
发布于 2016-07-14 00:46:46
幸运的是,当我将enzyme
和react-addons-test-utils
添加到项目中时,使用null
作为替代是可行的。
https://stackoverflow.com/questions/29883534
复制相似问题