Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error importing superagent in React code

I have a NodeJS server code written in JSX where I'm importing superagent like so:

import request from 'superagent';

When server starts after build, I get the following error: TypeError: require is not a function. And this happens at the following line in my compiled code:

var crypto = require('crypto');

On tracing back I realized that crypto is required by 'formidable', which is required by 'superagent'.

I have the same superagent import in my client side javascript code but that works fine. I diffed the compile JS code for node(server), and client, and both are the same. So it doesn't seem like an issue with the way its build.

Has anyone seen this error before and would you know what needs to be done?

like image 432
SherinThomas Avatar asked Jan 08 '16 04:01

SherinThomas


1 Answers

Found a solution to this here:

https://github.com/visionmedia/superagent/wiki/Superagent-for-Webpack

Adding the said plugin to web pack solved the issue.

like image 84
SherinThomas Avatar answered Oct 31 '22 10:10

SherinThomas