Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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)

Note: line 1 of myNewFile.js is var express = require('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.

How can I resolve this error and why is it occurring?

Thanks.

like image 272
Rbar Avatar asked Jun 01 '17 20:06

Rbar


1 Answers

You need to specify target in webpack config:

target: 'node'

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

like image 95
oklas Avatar answered Nov 12 '22 18:11

oklas