Unable to connect to the database: TypeError: net.Socket is not a constructor - Mailing list pgsql-general

From Marco Ippolito
Subject Unable to connect to the database: TypeError: net.Socket is not a constructor
Date
Msg-id CAFegzBQVR723sRfSR_x9=iVuuTV+dtEK5C=kNuhyJCoZxdaZUg@mail.gmail.com
Whole thread Raw
Responses Re: Unable to connect to the database: TypeError: net.Socket is not aconstructor
Re: Unable to connect to the database: TypeError: net.Socket is nota constructor
List pgsql-general
I'm trying to connect to a postgres database (Postgresql-11) within my nodejs-vue.js app, but in console I'm getting this error:

    [HMR] Waiting for update signal from WDS...
    pg.js?c8c2:27 Unable to connect to the database: TypeError: net.Socket is not a constructor
        at new Connection (webpack-internal:///./node_modules/pg/lib/connection.js:22:34)
        at new Client (webpack-internal:///./node_modules/pg/lib/client.js:55:37)
        at Promise.tap.query (webpack-internal:///./node_modules/sequelize/lib/dialects/postgres
    /connection-manager.js:124:26)
        at ConnectionManager.connect (webpack-internal:///./node_modules/sequelize/lib/dialects
    /postgres/connection-manager.js:121:12)
        at eval (webpack-internal:///./node_modules/sequelize/lib/dialects/abstract/connection-
    manager.js:318:50)
    From previous event:
        at ConnectionManager._connect (webpack-internal:///./node_modules/sequelize/lib/dialects
    /abstract/connection-manager.js:318:8)
        at ConnectionManager.getConnection (webpack-internal:///./node_modules/sequelize/lib/dialects
    /abstract/connection-manager.js:254:46)
        at eval (webpack-internal:///./node_modules/sequelize/lib/sequelize.js:640:36)
    From previous event:
        at eval (webpack-internal:///./node_modules/sequelize/lib/sequelize.js:631:53)
        at eval (webpack-internal:///./node_modules/retry-as-promised/index.js:70:21)
        at new Promise (<anonymous>)
        at retryAsPromised (webpack-internal:///./node_modules/retry-as-promised/index.js:60:10)
        at eval (webpack-internal:///./node_modules/sequelize/lib/sequelize.js:631:30)
    From previous event:
        at Sequelize.query (webpack-internal:///./node_modules/sequelize/lib/sequelize.js:580:23)
        at Sequelize.authenticate (webpack-internal:///./node_modules/sequelize/lib/sequelize.js:892:17)
        at eval (webpack-internal:///./src/plugins/db/pg.js:23:11)
        at Object../src/plugins/db/pg.js (https://ggc.world/js/app.js:1128:1)
        at __webpack_require__ (https://ggc.world/js/app.js:785:30)
        at fn (https://ggc.world/js/app.js:151:20)
        at eval (webpack-internal:///./src/main.js:16:72)
        at Module../src/main.js (https://ggc.world/js/app.js:1083:1)
        at __webpack_require__ (https://ggc.world/js/app.js:785:30)
        at fn (https://ggc.world/js/app.js:151:20)
        at Object.1 (https://ggc.world/js/app.js:1141:18)
        at __webpack_require__ (https://ggc.world/js/app.js:785:30)
        at checkDeferredModules (https://ggc.world/js/app.js:46:23)
        at https://ggc.world/js/app.js:861:18
        at https://ggc.world/js/app.js:864:10

 In /src/main.js :  

    import '@/plugins/db/pg';

    (base) /src/plugins/db$ ls -lah
    total 28K
    drwxr-xr-x 6 marco marco 4,0K apr 20 15:42 .
    drwxr-xr-x 3 marco marco 4,0K apr 20 15:41 ..
    drwxr-xr-x 2 marco marco 4,0K apr 20 17:20 config
    drwxr-xr-x 2 marco marco 4,0K apr 20 15:48 migrations
    drwxr-xr-x 2 marco marco 4,0K apr 20 15:48 models
    -rw-r--r-- 1 marco marco  819 apr 20 08:53 pg.js
    drwxr-xr-x 2 marco marco 4,0K apr 20 17:21 seeders


nano pg.js

    const { Pool } = require('pg');
    const { Sequelize } = require('sequelize');
    const pool = new Pool();
    const sequelize = new Sequelize('pusers', 'postgres', 'pwd', {
      host: 'localhost',
      dialect: 'postgres',
      pool: {
        max: 5,
        min: 0,
        acquire: 30000,
        idle: 10000
      }
    });
    sequelize
      .authenticate()
      .then(() => {
        console.log('Connection has been established successfully.');
      })
      .catch(err => {
        console.log('Unable to connect to the database:', err);
      });

    module.exports = {
      query: (text, params, callback) => {
        return pool.query(text, params, callback);
      },
    }

Populated the sample database with a row:

    pusers=# SELECT schemaname,relname,n_live_tup
    pusers-#   FROM pg_stat_user_tables
    pusers-#   ORDER BY n_live_tup DESC;
     schemaname |    relname    | n_live_tup
    ------------+---------------+------------
     public     | pusers        |          1
     public     | SequelizeMeta |          1
    (2 rows)

I read here: https://stackoverflow.com/questions/40599069/node-js-net-socket-is-not-a-constructor that
"there are no plain TCP sockets in the browser, so that is why trying to use `net.Socket` in the browser (via webpack, browserify, etc.) won't work"

    Environment Info:

      System:
        OS: Linux 5.3 Ubuntu 18.04.4 LTS (Bionic Beaver)
        CPU: (8) x64 Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz
      Binaries:
        Node: 12.15.0 - ~/.nvm/versions/node/v12.15.0/bin/node
        Yarn: 1.22.4 - ~/.nvm/versions/node/v12.15.0/bin/yarn
        npm: 6.14.4 - ~/.nvm/versions/node/v12.15.0/bin/npm
      Browsers:
        Chrome: 81.0.4044.92
        Firefox: 75.0
      npmGlobalPackages:
        @vue/cli: 4.2.3

So... how to solve the problem?
Looking forward to your kind help.
Marco

pgsql-general by date:

Previous
From: Saurav Sarkar
Date:
Subject: Usage reporting of Cloud provided PostgreSQL service
Next
From: Adrian Klaver
Date:
Subject: Re: Unable to connect to the database: TypeError: net.Socket is not aconstructor