Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jQfCF-0008G6-7U for pgsql-general@arkaria.postgresql.org; Mon, 20 Apr 2020 22:47:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jQfCE-00028L-3Y for pgsql-general@arkaria.postgresql.org; Mon, 20 Apr 2020 22:47:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jQfCD-000287-Of for pgsql-general@lists.postgresql.org; Mon, 20 Apr 2020 22:47:21 +0000 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jQfCA-0003Cg-Hs for pgsql-general@lists.postgresql.org; Mon, 20 Apr 2020 22:47:21 +0000 Received: by mail-pl1-x630.google.com with SMTP id g2so4515736plo.3 for ; Mon, 20 Apr 2020 15:47:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:cc:subject:in-reply-to:message-id :date:mime-version; bh=PWdcDAQug9fCW0IgbCW0ddR6O4fNKs8onlqc7x9hZq0=; b=FhEyS3Dswg0hKZCO31V9JLaBYNd4zaotHGSCg4onytkksJLJRbcNmcgvPBAEr3tqVa /AalKIP+1tevszvtxENm8HvpgyHYs6lOceP7CNWAri8aak6PvPyY6a6ObbTOkERBCaOq BmT+pKzGR+LhTjFAzMoxgPISRvQzXkyYi94ToAyMvGo8hTmyXCTS3J02VURB3igKvfVz 2keLmXwXwYr7c0FXtj2Wa4zfyvZ7QU/aTACl4sUKCPyMYTppdeiOiL2vvPy8Jn8R12cO +0c9fEFD5iHEEOmlVxk0A3IBAs5DSdBFy6QEGCGWLpLtOOtuvku/gm+JcoGD1LMAtPpT TUCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:cc:subject :in-reply-to:message-id:date:mime-version; bh=PWdcDAQug9fCW0IgbCW0ddR6O4fNKs8onlqc7x9hZq0=; b=cHBFQ5V2OzKyjrbNdfhVoqvy+pu0ImzCLTGEXkK7HziSuvSWdCZWPRLwJpIoaFivRW qrl2imv8awzKN/YOmuWFKIXk2dMy5l8mA14fIFaOiH0U6b6iudYMJ5QRKZD9FJcHrWUS 3w0+QNsRnWmLaMy7W24c9fqYU7UnuRXd+3uwUg3793zB8V/sHLoBd3y9WbzY7yAzBdfv Q48Oz9zmSKPPyIERVYZA9YZYuXQbRJ4GpGJuh0ZZo+HhzIYbwRFPtXuKxZ2ez2qloRF/ qmfcIs3dZzN13KSiudjJ6E63+5XGru1ldoo0nXqlWPJ2ljmXl9illr2AXYllyoTBy6oi DTlw== X-Gm-Message-State: AGi0PuZ8GLI1fCD3z1LZ1WwbjeXH3aG5KOeP0fVvdziC4sQ/ZsshnhaY lPK8GqSKnI0De5D3rPRVHEtiXlP/5uF0Lg== X-Google-Smtp-Source: APiQypJbYgsawooZ9peYOI77T+5MGG0vAs3pevJJ3rXsdmtCGt4JUFp2VDAvNowy/SFKUyKwAg+T5g== X-Received: by 2002:a17:90a:df88:: with SMTP id p8mr1992096pjv.119.1587422836114; Mon, 20 Apr 2020 15:47:16 -0700 (PDT) Received: from tim-desktop (203-173-27-59.dyn.iinet.net.au. [203.173.27.59]) by smtp.gmail.com with ESMTPSA id u3sm540744pfb.36.2020.04.20.15.47.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Apr 2020 15:47:15 -0700 (PDT) References: User-agent: mu4e 1.4.1; emacs 27.0.91 From: Tim Cross To: Marco Ippolito Cc: pgsql-general@postgresql.org, pgsql-general@lists.postgresql.org Subject: Re: Unable to connect to the database: TypeError: net.Socket is not a constructor In-reply-to: Message-ID: <87h7xd4vww.fsf@gmail.com> Date: Tue, 21 Apr 2020 08:47:11 +1000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Marco Ippolito 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 () > 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