尝试服务器端渲染反应时"Element type is invalid: expected a string"



我得到这个错误:

Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

尝试使用CCD_ 1时。

这是我的react应用程序:

"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())
            }
        })
    ));
});

有人知道是什么原因导致了这个错误,以及如何解决这个问题吗?

提前谢谢。

捆绑包默认情况下似乎与commonjs不兼容。正如我在webpack文档中所读到的,您需要将libraryTarget:'commonjs2'添加到您的输出对象中才能执行此操作

像这样:

output: {filename: 'app.bundle.js', path: __dirname,libraryTarget:'commonjs2'}

相关内容

  • 没有找到相关文章

最新更新