Cli: netlify-lambda shows connect ECONNREFUSED error after I hit an api endpoint

Created on 19 Aug 2020  路  9Comments  路  Source: netlify/cli

Describe the bug

I am using netlify functions using serverless-http to translate the express router routes to netlify functions and netlify-lambda for serving it locally. The server runs okay and the first api hit is a success. I get back a response but after this I get a connect ECONNREFUSED error and the server stops.

To Reproduce

Steps to reproduce the behavior:

  1. Run git clone https://github.com/usmanabdurrehman/socialserver.git
  2. Navigate to the cloned repository
  3. Run netlify-lambda
  4. See that the CLI exits with an error

Configuration
netlify.toml config:

[build]
  functions = "functions"

System config:

    OS: Windows 7 6.1.7601
    CPU: (4) x64 Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
    Memory: 905.19 MB / 3.90 GB
  Binaries:
    Node: 10.15.3 - C:\Program Files\nodejs\node.EXE
    npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD

CLI Output

Request from ::1: GET /.netlify/functions/api/hmn
Warning: connect.session() MemoryStore is not
designed for a production environment, as it will leak
memory, and will not scale past a single process.
(node:7464) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
Your lambda function didn't return a body, which may be a mistake. Check our Usage docs for examples (https://github.com/netlify/netlify-lambda#usage).
          If this is intentional, you can silence this warning by setting process.env.SILENCE_EMPTY_LAMBDA_WARNING to a truthy value or process.env.CONTEXT to 'production'
Response with status 304 in 629 ms.
C:\Users\Spinnin\desktop\socialserver2\functions\api.js:50
 */e.exports=function(e,a){if(!1!==t(e))return o(a,null,e),e;return function(e,a){var n=e.__onFinished;n&&n.queue||(n=e.__onFinished=function(e){function a(n){if(e.__onFinished===a&&(e.__onFinished=null),a.queue){var i=a.queue;a.queue=null;for(var o=0;o<i.length;o++)i[o](n,e)}}return a.queue=[],a}(e),function(e,a){var n,o,t=!1;function r(e){n.cancel(),o.cancel(),t=!0,a(e)}function s(a){e.removeListener("socket",s),t||n===o&&(o=i([[a,"error","close"]],r))}if(n=o=i([[e,"end","finish"]],r),e.socket)return void s(e.socket);e.on("socket",s),void 0===e.socket&&function(e,a){var n=e.assignSocket;if("function"!=typeof n)return;e.assignSocket=function(e){n.call(this,e),a(e)}}(e,s)}(e,n));n.queue.push(a)}(e,a),e},e.exports.isFinished=t;var i=n(150),o="function"==typeof setImmediate?setImmediate:function(e){process.nextTick(e.bind.apply(e,arguments))};function t(e){var a=e.socket;return"boolean"==typeof e.finished?Boolean(e.finished||a&&!a.writable):"boolean"=

Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
    --------------------
    at u._enqueue (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:389:67404)
    at u.handshake (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:389:66149)
    at d.connect (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:78:6497)
    at Object.<anonymous> (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:50:1180)
    at n (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:1:220)
    at Object.<anonymous> (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:78:1075)
    at n (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:1:220)
    at Object.<anonymous> (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:205:89180)
    at Object.<anonymous> (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:205:89788)
    at n (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:1:220)
    at C:\Users\Spinnin\desktop\socialserver2\functions\api.js:1:1019
    at Object.<anonymous> (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:1:1030)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `netlify-lambda serve src`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Spinnin\AppData\Roaming\npm-cache\_logs\2020-08-19T13_28_42_699Z-debug.log


**error log**
0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'start' ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle [email protected]~prestart: [email protected]
6 info lifecycle [email protected]~start: [email protected]
7 verbose lifecycle [email protected]~start: unsafe-perm in lifecycle true
8 verbose lifecycle [email protected]~start: PATH: C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\Users\Spinnin\desktop\socialserver2\node_modules\.bin;C:\Users\Spinnin\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\local\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Users\Spinnin\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Program Files\nodejs;C:\PHP;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\PROGRA~2\ThriXXX\3D SexVilla;C:\PROGRA~2\PICC;C:\Program Files (x86)\Smart Projects\IsoBuster;C:\Users\Spinnin\AppData\Roaming\Sublime Text 3\Packages\User;C:\Users\Spinnin\AppData\Local\Programs\Python\Python35\Scripts;C:\Users\Spinnin\AppData\Local\Programs\Python\Python35;C:\Modeltech_5.7g\win32;C:\Python34\Scripts;C:\Program Files\heroku\bin;C:\flutter\flutter\bin;C:\Users\Spinnin\AppData\Roaming\npm;C:\MinGW\bin;C:\Program Files\Git\usr\bin\vendor_perl;C:\Program Files\Git\usr\bin\core_perl
9 verbose lifecycle [email protected]~start: CWD: C:\Users\Spinnin\desktop\socialserver2
10 silly lifecycle [email protected]~start: Args: [ '/d /s /c', 'netlify-lambda serve src' ]
11 silly lifecycle [email protected]~start: Returned: code: 1  signal: null
12 info lifecycle [email protected]~start: Failed to exec start script
13 verbose stack Error: [email protected] start: `netlify-lambda serve src`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:301:16)
13 verbose stack     at EventEmitter.emit (events.js:189:13)
13 verbose stack     at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:189:13)
13 verbose stack     at maybeClose (internal/child_process.js:970:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
14 verbose pkgid [email protected]
15 verbose cwd C:\Users\Spinnin\desktop\socialserver2
16 verbose Windows_NT 6.1.7601
17 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
18 verbose node v10.15.3
19 verbose npm  v6.4.1
20 error code ELIFECYCLE
21 error errno 1
22 error [email protected] start: `netlify-lambda serve src`
22 error Exit status 1
23 error Failed at the [email protected] start script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]
bug

Most helpful comment

@erezrokah now surprisingly, the server works hmnnn

Connected! .
Waiting for localhost:34567..................................... ................Request from ::1: GET /.netlify/functions/api/hmn
.Response with status 200 in 10974 ms.
Connected to database successfully
...Request from ::1: GET /.netlify/functions/api/hmn
Response with status 304 in 12 ms.
....Request from ::1: GET /.netlify/functions/api/authCheck
Response with status 404 in 20 ms.
.........Request from ::1: GET /.netlify/functions/api/checkAuth
Response with status 200 in 8 ms.

All 9 comments

Hi @usmanabdurrehman, see https://github.com/usmanabdurrehman/socialserver/pull/1.
No need to netlify-lambda if your functions don't require a build step.

I dropped netlify-lambda and was able to get a better error message:

Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
    --------------------
    at Protocol._enqueue (/Users/erezrokah/Code/github/socialserver/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/Users/erezrokah/Code/github/socialserver/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at Connection.connect (/Users/erezrokah/Code/github/socialserver/node_modules/mysql/lib/Connection.js:119:18)
    at Object.<anonymous> (/Users/erezrokah/Code/github/socialserver/src/config/dbconfig.js:18:4)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Module.require (internal/modules/cjs/loader.js:1026:19)
    at require (internal/modules/cjs/helpers.js:72:18) {
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 3306,
  fatal: true
}

Looks like it's crashing due to trying to connect to a non running database instance

@erezrokah I forgot to start the database. After running the database. The error changed

Request from ::1: GET /.netlify/functions/api/hmn
Warning: connect.session() MemoryStore is not
designed for a production environment, as it will leak
memory, and will not scale past a single process.
(node:8032) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
Your lambda function didn't return a body, which may be a mistake. Check our Usage docs for examples (https://github.com/netlify/netlify-lambda#usage).
          If this is intentional, you can silence this warning by setting process.env.SILENCE_EMPTY_LAMBDA_WARNING to a truthy value or process.env.CONTEXT to 'production'
Response with status 304 in 748 ms.
C:\Users\Spinnin\desktop\socialserver2\functions\api.js:50
 */e.exports=function(e,a){if(!1!==t(e))return o(a,null,e),e;return function(e,a){var n=e.__onFinished;n&&n.queue||(n=e.__onFinished=function(e){function a(n){if(e.__onFinished===a&&(e.__onFinished=null),a.queue){var i=a.queue;a.queue=null;for(var o=0;o<i.length;o++)i[o](n,e)}}return a.queue=[],a}(e),function(e,a){var n,o,t=!1;function r(e){n.cancel(),o.cancel(),t=!0,a(e)}function s(a){e.removeListener("socket",s),t||n===o&&(o=i([[a,"error","close"]],r))}if(n=o=i([[e,"end","finish"]],r),e.socket)return void s(e.socket);e.on("socket",s),void 0===e.socket&&function(e,a){var n=e.assignSocket;if("function"!=typeof n)return;e.assignSocket=function(e){n.call(this,e),a(e)}}(e,s)}(e,n));n.queue.push(a)}(e,a),e},e.exports.isFinished=t;var i=n(150),o="function"==typeof setImmediate?setImmediate:function(e){process.nextTick(e.bind.apply(e,arguments))};function t(e){var a=e.socket;return"boolean"==typeof e.finished?Boolean(e.finished||a&&!a.writable):"boolean"=

Error: Received packet in the wrong sequence.
    at u._parsePacket (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:389:69558)
    at u._parsePacket (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:389:78287)
    at u.write (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:389:72515)
    at u.write (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:389:66026)
    at Socket.<anonymous> (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:78:5625)
    at Socket.<anonymous> (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:78:4689)
    at Socket.emit (events.js:189:13)
    at addChunk (_stream_readable.js:284:12)
    at readableAddChunk (_stream_readable.js:265:11)
    at Socket.Readable.push (_stream_readable.js:220:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
    --------------------
    at u._enqueue (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:389:67404)
    at u.handshake (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:389:66149)
    at d.connect (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:78:6497)
    at Object.<anonymous> (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:50:1180)
    at n (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:1:220)
    at Object.<anonymous> (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:78:1075)
    at n (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:1:220)
    at Object.<anonymous> (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:205:89180)
    at Object.<anonymous> (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:205:89788)
    at n (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:1:220)
    at C:\Users\Spinnin\desktop\socialserver2\functions\api.js:1:1019
    at Object.<anonymous> (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:1:1030)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `netlify-lambda serve src`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Spinnin\AppData\Roaming\npm-cache\_logs\2020-08-19T14_52_22_921Z-debug.log

Hi @usmanabdurrehman, can you try running without netlify-lambda (see instructions in my PR)? That will give us the unminified error

@erezrokah Run it only using node and got this error

Error: Received packet in the wrong sequence.
    at u._parsePacket (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:3
89:69558)
    at u._parsePacket (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:3
89:78287)
    at u.write (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:389:7251
5)
    at u.write (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:389:6602
6)
    at Socket.<anonymous> (C:\Users\Spinnin\desktop\socialserver2\functions\api.
js:78:5625)
    at Socket.<anonymous> (C:\Users\Spinnin\desktop\socialserver2\functions\api.
js:78:4689)
    at Socket.emit (events.js:189:13)
    at addChunk (_stream_readable.js:284:12)
    at readableAddChunk (_stream_readable.js:265:11)
    at Socket.Readable.push (_stream_readable.js:220:10)
    --------------------
    at u._enqueue (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:389:6
7404)
    at u.handshake (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:389:
66149)
    at d.connect (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:78:649
7)
    at Object.<anonymous> (C:\Users\Spinnin\desktop\socialserver2\functions\api.
js:50:1180)
    at n (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:1:220)
    at Object.<anonymous> (C:\Users\Spinnin\desktop\socialserver2\functions\api.
js:78:1075)
    at n (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:1:220)
    at Object.<anonymous> (C:\Users\Spinnin\desktop\socialserver2\functions\api.
js:205:89180)
    at Object.<anonymous> (C:\Users\Spinnin\desktop\socialserver2\functions\api.
js:205:89788)
    at n (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:1:220)

Sorry for not being clear with my instructions.
Netlify lambda takes your code from src/ and bundles all of it under the same file functions/api.js.
That makes it very hard to understand where the error is coming from.
Removing the netlify-lambda dependency from your package json and running netlify dev will show us where the error is coming from in original source files.

@erezrokah Here it is. Did what you told. Seems like the exact same error as with node

Error: Received packet in the wrong sequence.
at u._parsePacket (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:389:69558)
at u._parsePacket (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:389:78287)
at u.write (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:389:72515)
at u.write (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:389:66026)
at Socket. (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:78:5625)
at Socket. (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:78:4689)
at Socket.emit (events.js:189:13)
at addChunk (_stream_readable.js:284:12)
at readableAddChunk (_stream_readable.js:265:11)
at Socket.Readable.push (_stream_readable.js:220:10)
--------------------
at u._enqueue (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:389:67404)
at u.handshake (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:389:66149)
at d.connect (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:78:6497)
at Object. (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:50:1180)
at n (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:1:220)
at Object. (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:78:1075)
at n (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:1:220)
at Object. (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:205:89180)
at Object. (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:205:89788)
at n (C:\Users\Spinnin\desktop\socialserver2\functions\api.js:1:220)

Sorry, forgot that you should also change your netlify.toml to:

[build]
  functions = "src" 

See https://github.com/usmanabdurrehman/socialserver/pull/1/files#diff-5db06fd2327543bbb72119cd1e5761cfR2

@erezrokah now surprisingly, the server works hmnnn

Connected! .
Waiting for localhost:34567..................................... ................Request from ::1: GET /.netlify/functions/api/hmn
.Response with status 200 in 10974 ms.
Connected to database successfully
...Request from ::1: GET /.netlify/functions/api/hmn
Response with status 304 in 12 ms.
....Request from ::1: GET /.netlify/functions/api/authCheck
Response with status 404 in 20 ms.
.........Request from ::1: GET /.netlify/functions/api/checkAuth
Response with status 200 in 8 ms.

Cool, closing the issue

Was this page helpful?
0 / 5 - 0 ratings