Error importing superagent in React code
Asked Answered
E

1

5

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?

Engage answered 8/1, 2016 at 4:30 Comment(2)
Can superagent be imported like that with a space D:?Referent
Sorry that was autocorrect at play when I typed the question :PEngage
E
5

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.

Engage answered 8/1, 2016 at 7:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.