我收到此错误:
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
尝试使用ReactDOMServer.renderToStaticMarkup
时。
这是我的反应:
"use strict";
import React from 'react'
module.exports = () => {
return (
<div></div>
);
};
这是我的节点服务器呈现代码:
"use strict";
const path = require('path');
const webpack = require('webpack');
const React = require('react'), ReactDOMServer = require('react-dom/server'),
DOM = React.DOM, body = DOM.body, div = DOM.div, script = DOM.script;
webpack({
target: "node",
entry: [
path.resolve(__dirname, '../js', 'app.js'),
],
module: {
loaders: [
{
exclude: /node_modules/,
loader: 'babel',
test: /\.js$/,
},
]
},
output: {filename: 'app.bundle.js', path: __dirname},
},() => {
const App = React.createFactory(require('./app.bundle.js'));
let html = ReactDOMServer.renderToStaticMarkup(body(null,
div({
id: 'root', dangerouslySetInnerHTML: {
__html: ReactDOMServer.renderToString(App())
}
})
));
});
有没有人知道导致此错误的原因以及如何解决此问题?
提前致谢。
答案 0 :(得分:0)
默认情况下,捆绑包与commonjs不兼容。当我在webpack docs中阅读时,您需要将libraryTarget:'commonjs2'
添加到输出对象中才能执行此操作。
像这样:
output: {filename: 'app.bundle.js', path: __dirname,libraryTarget:'commonjs2'}