TypeError: Object(...) is not a function for http(s) createServer

In my App.tsx, I have an import:

import {generateCertAndKey} from './server.tsx';

In server.tsx, I have:

export {generateCertAndKey}

import forge from 'node-forge';
forge.options.usePureJavaScript = true;
import { WebSocketServer, WebSocket } from 'ws';
import {createServer} from 'http';

const sslCertAndKey = generateCertAndKey();
const wssServer = createServer();

…and the generateCertAndKey function.

Everything is fine until the createServer() line which gives:

TypeError: Object(...) is not a function

    8 | const sslCertAndKey = generateCertAndKey();
>  9 | const wssServer = createServer();

Why is the app not seeing the the http or https libraries?


When I console.log(http), there is no Server or createServer like there is on regular node.js.

Is there any way to get the complete http(s) library or an alternative?


I doubt there’s a way to create arbitrary TCP sockets in the web browser. What are you trying to accomplish?

If you were doing this on Android/iOS you could try react-native-tcp-socket, but you’d need EAS Build.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.