question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: net.Socket is not a constructor

See original GitHub issue

The task: Connect the Postgresql 11 database to the project on React to get data from there.

What I did to connect to the database: Created a new React project:

create-react-app PGTestProject

Then I tried to connect to the database. The actions listed in the “Getting started” section are not enough to get started with the package. For example, it does not indicate how to change the connection parameters to the database. Without this, it is impossible to connect to the created database.

The actions listed in the “Connecting” section are also not enough. The error “net.Socket is not a constructor”

App.js:

import React from 'react';

import logo from './logo.svg';
import './App.css';

async function getDatafromDB(){
    const { Pool, Client } = require('pg')

       const pool = new Pool({
        user: 'postgres',
        host: 'server_ip',
        database: 'mydb',
        password: 'password',
        port: 5432,
    })

    pool.query('SELECT NOW()', (err, res) => {
        console.log(err, res)
        pool.end()
    })

    const pool = new Pool({
        user: 'postgres',
        host: 'server_ip',
        database: 'mydb',
        password: 'password',
        port: 5432,
    })
    client.connect()

    client.query('SELECT NOW()', (err, res) => {
        console.log(err, res)
        client.end()
    })
}

function App() {
    getDatafromDB();
    return (
        <div className="App">
            <header className="App-header">
                <img src={logo} className="App-logo" alt="logo"/>
                <p>
                    Edit <code>src/App.js</code> and save to reload.
                </p>
                <a
                    className="App-link"
                    href="https://reactjs.org"
                    target="_blank"
                    rel="noopener noreferrer"
                >
                    Learn React
                </a>
            </header>
        </div>
    );
}

export default App;

An error occurs in the browser console: TypeError: net.Socket is not a constructor

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

4reactions
charmandercommented, Jun 26, 2019

You can’t run pg in the browser. It runs in Node.

1reaction
charmandercommented, Jan 20, 2020

@micimize Cloudflare workers don’t run in the browser, as far as I know.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node JS: net.Socket is not a constructor - Stack Overflow
I've tried making the below code an object and importing / requiring it into my project and I still get that error. var...
Read more >
Unable to connect to the database: TypeError: net.Socket is ...
i'm trying to connect to a postgres database within my node.js-vue.js app , but in console I'm getting this error:
Read more >
TypeError: net.Socket is not a constructor - PostgreSQL
Re: Unable to connect to the database: TypeError: net.Socket is not a constructor. From: Marco Ippolito <ippolito(dot)marco(at)gmail(dot) ...
Read more >
TypeError: "x" is not a constructor - JavaScript - MDN Web Docs
The JavaScript exception "is not a constructor" occurs when there was an attempt to use an object or a variable as a constructor,...
Read more >
Errors | Node.js v19.3.0 Documentation
Usually encountered by http or net . Often a sign that a socket.end() was not properly called. Class: TypeError #.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found