Error importing superagent in React code - node.js

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?

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.

Related

How can I load three/examples/jsm/loaders/GLTFLoader.js in nodejs server side

I am currently using threejs lib in nodejs server side and the following statement works well:
const THREE = require('three')
const OrbitControls = require('three-orbitcontrols')
Now, I need also to use three/examples/jsm/loaders/GLTFLoader.js but same require line does not work. It works in client side using import statement, but unfortunaly I cannot use it outside of module.
Is there a way to use this js in nodejs server side ? The reason is because I would load and build my scene in backend then pass it to the client only for rendering.
Looking forward to get some help
Yes, finally I was bale to add the jsm : import three/examples/jsm/loaders/GLTFLoader.js on nodejs

Node Crypto missing a lot of methods when running in browser

I have a nextjs app and I'm trying to use some of node crypto methods like hkdf, scrypt, generateKeyPairs... The problem is I get an error saying there's no such functions. I import crypto from 'crypto' then console.log(crypto) and I notice those functions were missing from the crypto object Image of this. I create a new javascript file, initialize node, does the same thing and all the functions are there. Image of this I'm really confused on why this happen... Please help.

Streaming response body to file in typescript: Property 'pipe' does not exist on type 'ReadableStream<Uint8Array>'

I am able to fetch a binary body from an API to write it to a file in node.
const fileStream = fs.createWriteStream(filePath);
fetch(apiURL).then((downloadResponse) => {
downloadResponse.body.pipe(fileStream);
});
However, when doing so I get a linting error of:
Property 'pipe' does not exist on type
'ReadableStream'
It seems weird to me that the call would work when the linter gives an error.
I even initially thought my logic was wrong and wasted time debugging this working call...
Is my typescript version misidentifying the type for some reason or should I not be able to perform this call ?
I am barely beginning with typescript but on occasion I run into such idiosyncrasies that slow me down when I am doing something that would seem perfectly valid.
The answer provided in the comments was indeed correct but the context in which this wasn't straight-forward was in a nextjs app using fetch from a server side rendering function such as getServerSideProps.
On the client side it is pretty straight-forward that the standard fetch api is used however on SSR it wasn't evident that one had to additionnally install the types for node-fetch using npm i #types/node-fetch.

Remove warning on Node.js WebStorm

I am learning backend with Node.js and using middleware to catch for errors and send a different response code and body.
The warning is similar to err.message.
How do I remove these warnings?
Are you using express? Its methods are generated dynamically in runtime, so they can't be resolved during static code analysis. Installing TypeScript stubs should help to get methods work: put cursor on 'express' in const express = require('express'); , hit Alt+Enter and choose Install TypeScript definitions for better type information to install typings - see https://www.jetbrains.com/help/webstorm/configuring-javascript-libraries.html#ws_jsconfigure_libraries_ts_definition_files

Browserify the node.js http server

We created a simple js file, intending to find out if http.createServer works on client browser or not:
var http = require("http")
var server = http.createServer()
server.listen(9024, function () {
console.log("demo server listening on port 9024")
})
and embedded it into a html after browserify.
Display the html in chrome, unfortunately, it always fails on line 2 on http.createServer():
"Uncaught Type Error: undefined is not a function"
We also played around with "serve-browserify" a bit without success.
We have attempted the same thing on both chrome and firefox, and on Linux and Windows. All failed.
Searching through the web, there are quite a few examples for browserify http into the browser.
They all appear to be simple invocation of browserify. However we don't seem to be able to get the same good result.
Your help will be greately appreciated.
You can't use Node.js modules in the browser. All Browserify does is bundling CommonJS modules, it does not allow you to run server side code in the browser.

Resources