var express = require('express')-无法读取未定义的属性'prototype' [英] var express = require('express') - Cannot read property 'prototype' of undefined

查看:353
本文介绍了var express = require('express')-无法读取未定义的属性'prototype'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Express的新手,如果这很简单,请原谅我.我有一个React项目,我正在尝试将Express添加到它.我添加了一个以以下行开头的.js文件来导入express:

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');

但是,这会引发错误.

错误

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)

注意:myNewFile.js的第1行是var express = require('express');

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

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

I did try npm install express so I should have access to express and it should be up to date, but it seems that I still get this error.

如何解决此错误以及为什么发生?

谢谢.

推荐答案

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

You need to specify target in webpack config:

target: 'node'

这仅在服务器端作为node.js程序起作用.

And this will work only on server side as node.js program.

这篇关于var express = require('express')-无法读取未定义的属性'prototype'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆