Error importing superagent in React code

会有一股神秘感。 提交于 2019-12-22 07:08:31

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/34669533/error-importing-superagent-in-react-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!