Unable to connect to the database: TypeError: net.Socket is not a constructor

Started by Marco Ippolitoalmost 6 years ago6 messagesgeneral
Jump to latest
#1Marco Ippolito
ippolito.marco@gmail.com

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

#2Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Marco Ippolito (#1)
Re: Unable to connect to the database: TypeError: net.Socket is not a constructor

On 4/20/20 10:50 AM, Marco Ippolito wrote:

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

    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?

From what I can gather the issue is that the code is running in the
context of the browser and net.Socket is not supported there. It seems
that the fix is to run the database code in node.js and pass the data
from there to vue.js. Honestly, this whole reactive code thing is still
something I'm wrapping my head around. I did find some tutorials
searching on 'node.js vue.js Sequelize postgresql'. Most seemed to use
express.js as the glue between node and vue.

Looking forward to your kind help.
Marco

--
Adrian Klaver
adrian.klaver@aklaver.com

#3Marco Ippolito
ippolito.marco@gmail.com
In reply to: Adrian Klaver (#2)
Re: Unable to connect to the database: TypeError: net.Socket is not a constructor

Il giorno lun 20 apr 2020 alle ore 20:11 Adrian Klaver <
adrian.klaver@aklaver.com> ha scritto:

On 4/20/20 10:50 AM, Marco Ippolito wrote:

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

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?

From what I can gather the issue is that the code is running in the
context of the browser and net.Socket is not supported there. It seems
that the fix is to run the database code in node.js and pass the data
from there to vue.js. Honestly, this whole reactive code thing is still
something I'm wrapping my head around. I did find some tutorials
searching on 'node.js vue.js Sequelize postgresql'. Most seemed to use
express.js as the glue between node and vue.

Thank you Adrian for your very kind response.
express.js is the server framework for node.js
So...two questions come in mind:
am I obliged to use express.js as glue between node and vue, if I already
use nginx as web server?
And if I'm obliged to use another web server apart from nginx to glue
node and vue, would it be possible to use a golang web-server,
which could be a further complication but would have its own positive
sides?

Marco

Show quoted text

Looking forward to your kind help.
Marco

--
Adrian Klaver
adrian.klaver@aklaver.com

#4Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Marco Ippolito (#3)
Re: Unable to connect to the database: TypeError: net.Socket is not a constructor

On 4/20/20 11:29 AM, Marco Ippolito wrote:

Il giorno lun 20 apr 2020 alle ore 20:11 Adrian Klaver
<adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>> ha scritto:

On 4/20/20 10:50 AM, Marco Ippolito wrote:

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

      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?

 From what I can gather the issue is that the code is running in the
context of the browser and net.Socket is not supported there. It seems
that the fix is to run the database code in node.js and pass the data
from there to vue.js. Honestly, this whole reactive code thing is still
something I'm wrapping my head around. I did find some tutorials
searching on 'node.js vue.js Sequelize postgresql'. Most seemed to use
express.js as the glue between node and vue.

Thank you Adrian for your very kind response.
express.js is the server framework for node.js
So...two questions come in mind:
  am I obliged to use express.js as glue between node and vue, if I
already use nginx as web server?
  And if I'm obliged to use another web server apart from nginx to glue
node and vue, would it be possible to use a golang web-server,
  which could be a further complication but would have its own positive
sides?

I have no idea, this is not something I normally deal with. I would
guess you will get better answers from a node.js forum.

Marco

Looking forward to your kind help.
Marco

--
Adrian Klaver
adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>

--
Adrian Klaver
adrian.klaver@aklaver.com

#5Tim Cross
theophilusx@gmail.com
In reply to: Marco Ippolito (#1)
Re: Unable to connect to the database: TypeError: net.Socket is not a constructor

Marco Ippolito <ippolito.marco@gmail.com> writes:

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

This has nothing to do with postgres. This is a purely Javascript issue
combined with differences between the browser Javascript API and node
javascript API.

Bottom line - if your trying to connect directly to a PG database from
within Javascript code running inside the browser, it won't work. The
browser does not have the 'net' library. You can only do this from
within node.js. Likewise, references to express are also red hearings -
you don't have a web server inside the browser either. If your not
trying to develop inside the browser but are in fact developing at the
node.js level, then you don't need webpack.

I think what you really need to do is step back and look closer at your
architecture. Typically, you would put all your database interaction
stuff in the web server using node.js. A common design pattern would be
to use one of the node.js web servers, like express (but there are
others) and have something like nginx as a proxy server in front of it.
You would then wrap your database interaction in a node.js API which you
would then call from your client browser using http/https or web
sockets.

--
Tim Cross

#6Marco Ippolito
ippolito.marco@gmail.com
In reply to: Tim Cross (#5)
Re: Unable to connect to the database: TypeError: net.Socket is not a constructor

Thank you very much Tim and Adrian for your very kind and valuable
information and suggestions.

Marco

Il giorno mar 21 apr 2020 alle ore 00:47 Tim Cross <theophilusx@gmail.com>
ha scritto:

Show quoted text

Marco Ippolito <ippolito.marco@gmail.com> writes:

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

This has nothing to do with postgres. This is a purely Javascript issue
combined with differences between the browser Javascript API and node
javascript API.

Bottom line - if your trying to connect directly to a PG database from
within Javascript code running inside the browser, it won't work. The
browser does not have the 'net' library. You can only do this from
within node.js. Likewise, references to express are also red hearings -
you don't have a web server inside the browser either. If your not
trying to develop inside the browser but are in fact developing at the
node.js level, then you don't need webpack.

I think what you really need to do is step back and look closer at your
architecture. Typically, you would put all your database interaction
stuff in the web server using node.js. A common design pattern would be
to use one of the node.js web servers, like express (but there are
others) and have something like nginx as a proxy server in front of it.
You would then wrap your database interaction in a node.js API which you
would then call from your client browser using http/https or web
sockets.

--
Tim Cross