var express = require('express') - Cannot read property 'prototype' of undefined

Rba*_*bar 5 node.js express reactjs

I am new to Express, so forgive me if this is a simple one. I have a React project and I am trying to add Express to it. I have added a .js file that begins with the following line to import express:

var express = require('express');
Run Code Online (Sandbox Code Playgroud)

However, this throws an error.

Error

request.js:31 Uncaught TypeError: Cannot read property 'prototype' of undefined
    at Object.<anonymous> (request.js:31)
    at __webpack_require__ (bootstrap 2c79a52…:555)
    at fn (bootstrap 2c79a52…:86)
    at Object.<anonymous> (express.js:20)
    at __webpack_require__ (bootstrap 2c79a52…:555)
    at fn (bootstrap 2c79a52…:86)
    at Object.<anonymous> (index.js:11)
    at __webpack_require__ (bootstrap 2c79a52…:555)
    at fn (bootstrap 2c79a52…:86)
    at Object.<anonymous> (myNewFile.js:1)
Run Code Online (Sandbox Code Playgroud)

Note: line 1 of myNewFile.js is var express = require('express');

我确实尝试过,npm install express所以我应该可以访问快递,并且应该是最新的,但看来我仍然收到此错误。

如何解决此错误,为什么会发生?

谢谢。

okl*_*las 5

您需要target在 webpack 配置中指定:

target: 'node'
Run Code Online (Sandbox Code Playgroud)

这只能在服务器端作为 node.js 程序运行。