簡體   English   中英

未定義React

[英]Getting React is not defined

我正在嘗試在下面的快遞服務器上渲染我的React代碼是該代碼

const express  =require('express');
const react  =require('react');
const renderToString  =require('react-dom/server').renderToString;
const HelloWorld=require('./client/components/Home');
const app = express();

app.get('/', (req, res) => {
    const content=renderToString(<HelloWorld/>);
    res.send(content);
});

app.listen(3000, () => {
  console.log('Listening on prot 3000');
});

webpack.server.js

 const path = require('path');

module.exports = {
  // Inform webpack that we're building a bundle
  // for nodeJS, rather than for the browser
  target: 'node',

  // Tell webpack the root file of our
  // server application
  entry: './src/index.js',

  // Tell webpack where to put the output file
  // that is generated
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'build'),

  },
  module:{
      rules:[
          {
              test:/\.js?$/,
              loader:'babel-loader',
              exclude:/node_modules/,
              options:{
                  presets:[
                      'react',
                      'stage-0',//asyc purpose,
                      ['env',{targets:{browsers:['last 2 versions']}}]
                  ]
            }
          }
      ]
  }
};

的package.json

 "dev:build:server": "webpack --config webpack.server.js"

當我使用節點build \\ bundle.js運行我的應用程序時,即使在index.js中導入react,我也沒有定義react

更改

const react=require('react');

const React=require('react'); 解決了我的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM