简体   繁体   中英

Getting React is not defined

i am trying render my React code on express server below is the code for that

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"

when i run my application using node build\\bundle.js i am getting react is not defined even i am importing react in index.js

Changing

const react=require('react');

to

const React=require('react'); solved my problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM