Debugging Common Errors | Part 1

 Importing Unknown Modules

When we try to import unknown modules

root@123:/.../myapp# nodemon index.js
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
internal/modules/cjs/loader.js:968
throw err;
^
Error: Cannot find module 'expresses'
Require stack:
- /home/workspace/nodejs/sessions/Introduction-to-Express-JS-Part-2/myapp/index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
at Function.Module._load (internal/modules/cjs/loader.js:841:27)
at Module.require (internal/modules/cjs/loader.js:1025:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/home/workspace/nodejs/sessions/Introduction-to-Express-JS-Part-2/myapp/index.js:1:17)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/home/workspace/nodejs/sessions/Introduction-to-Express-JS-Part-2/myapp/index.js'
]
}
[nodemon] app crashed - waiting for file changes before starting...
 
Collapse

2. Starting Server in Multiple Terminals

When we try to start the server in multiple terminals

root@123:/.../myapp# nodemon index.js
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
events.js:292
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1313:16)
at listenInCluster (net.js:1361:12)
at Server.listen (net.js:1447:7)
at Function.listen (/home/workspace/nodejs/sessions/Introduction-to-Express-JS-Part-2/myapp/node_modules/.pnpm/express@4.17.1/node_modules/express/lib/application.js:618:24)
at initializeDBAndServer (/home/workspace/nodejs/sessions/Introduction-to-Express-JS-Part-2/myapp/index.js:16:9)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1340:8)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: 'EADDRINUSE',
errno: 'EADDRINUSE',
syscall: 'listen',
address: '::',
port: 3000
}
[nodemon] app crashed - waiting for file changes before starting...
 
Collapse

When you get address already in use :::3000 error

Step 1: Kill the currently running process in the terminal with Ctrl + C

Step 2: Run

lsof -i :port_number
in your CCBP IDE Terminal

Step 3: Run

kill -9 process_id
in your CCBP IDE Terminal

Example :

root@123:/.../...part-3/myapp# nodemon index.js
[nodemon] starting `node index.js`
events.js:292
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
.
.
.
^C
root@123:/.../...part-3# lsof -i :3000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 9841 root 20u IPv6 345981 0t0 TCP *:3000 (LISTEN)
root@123:/.../...part-3# kill -9 9841
root@123:/.../...part-3#
Collapse

3. Starting Server outside the myapp

When we try to start the server outside myapp using node

root@123:/.../...part-3# node index.js
internal/modules/cjs/loader.js:968
throw err;
^
Error: Cannot find module '/.../...Part-3/index.js'
root@123:/.../...part-3#

When we try to start the server outside myapp using nodemon

root@123:/.../...part-3# nodemon index.js
Usage: nodemon [nodemon options] [script.js] [args]
See "nodemon --help" for more.
root@123:/.../...part-3#

4. Accessing Wrong URL

When we try to Send Request for wrong URLs

HTTP/1.1 404 Not Found
X-Powered-By: Express
Content-Security-Policy: default-src 'none'
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=utf-8
Content-Length: 148
Date: Sun, 04 Apr 2021 11:50:56 GMT
Connection: close
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /bookss/1/</pre>
</body>
</html
Collapse

5. Missing Function Call

When we miss calling the function express and trying to access methods returned from the function express

root@123:/.../...part-3/myapp# nodemon index.js
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
/home/workspace/nodejs/sessions/Introduction-to-Express-JS-Part-2/myapp/index.js:26
app.get("/books/", async (request, response) => {
^
TypeError: app.get is not a function
at Object.<anonymous> (/home/workspace/nodejs/sessions/Introduction-to-Express-JS-Part-2/myapp/index.js:26:5)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
[nodemon] app crashed - waiting for file changes before starting...
 
Collapse

6. Importing Unknown File

When we try to import an unknown file

root@123:/.../...part-3/myapp# nodemon index.js
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
Server Running at http://localhost:3000/
(node:668) UnhandledPromiseRejectionWarning: Error: SQLITE_ERROR: no such table: book
(node:668) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:668) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
 

1. Request Methods

When we try to request with a wrong method

HTTP/1.1 404 Not Found
X-Powered-By: Express
Content-Security-Policy: default-src 'none'
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=utf-8
Content-Length: 148
Date: Sun, 04 Apr 2021 11:50:56 GMT
Connection: close
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /books/</pre>
</body>
</html
Collapse

2. Missing colon(:)

When we miss semicolon while setting Path Parameters

HTTP/1.1 404 Not Found
X-Powered-By: Express
Content-Security-Policy: default-src 'none'
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=utf-8
Content-Length: 148
Date: Sun, 04 Apr 2021 11:50:56 GMT
Connection: close
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /books/</pre>
</body>
</html
Collapse

3. Accessing Path Parameters

When we are trying to access path parameters with the wrong name

root@123:/.../...part-3/myapp# nodemon index.js
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
Server Running at http://localhost:3000/
(node:905) UnhandledPromiseRejectionWarning: ReferenceError: bookId is not defined
at /home/workspace/nodejs/sessions/Introduction-to-Express-JS-Part-2/myapp/index.js:46:19
at Layer.handle [as handle_request] (/home/workspace/nodejs/sessions/Introduction-to-Express-JS-Part-2/myapp/node_modules/.pnpm/express@4.17.1/node_modules/express/lib/router/layer.js:95:5)
at next (/home/workspace/nodejs/sessions/Introduction-to-Express-JS-Part-2/myapp/node_modules/.pnpm/express@4.17.1/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/workspace/nodejs/sessions/Introduction-to-Express-JS-Part-2/myapp/node_modules/.pnpm/express@4.17.1/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/workspace/nodejs/sessions/Introduction-to-Express-JS-Part-2/myapp/node_modules/.pnpm/express@4.17.1/node_modules/express/lib/router/layer.js:95:5)
at /home/workspace/nodejs/sessions/Introduction-to-Express-JS-Part-2/myapp/node_modules/.pnpm/express@4.17.1/node_modules/express/lib/router/index.js:281:22
at param (/home/workspace/nodejs/sessions/Introduction-to-Express-JS-Part-2/myapp/node_modules/.pnpm/express@4.17.1/node_modules/express/lib/router/index.js:354:14)
at param (/home/workspace/nodejs/sessions/Introduction-to-Express-JS-Part-2/myapp/node_modules/.pnpm/express@4.17.1/node_modules/express/lib/router/index.js:365:14)
at Function.process_params (/home/workspace/nodejs/sessions/Introduction-to-Express-JS-Part-2/myapp/node_modules/.pnpm/express@4.17.1/node_modules/express/lib/router/index.js:410:3)
at next (/home/workspace/nodejs/sessions/Introduction-to-Express-JS-Part-2/myapp/node_modules/.pnpm/express@4.17.1/node_modules/express/lib/router/index.js:275:10)
(node:905) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:905) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
 
Collapse

4. Query Formatting

When we miss embedding expression into string literal properly in the query

root@123:/.../...part-3/myapp# nodemon index.js
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
Server Running at http://localhost:3000/
(node:1023) UnhandledPromiseRejectionWarning: Error: SQLITE_ERROR: no such table: book
(node:1023) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1023) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
 

5. Replacing SQLite Methods

When we use one SQLite method instead of another SQLite method we may get an unexpected response

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 34
ETag: W/"22-fiAy4921oZAEOAA97wnoLQyL2Dw"
Date: Sun, 04 Apr 2021 12:46:07 GMT
Connection: close
{
"stmt":{},
"lastID":0,
"changes":0
}
Collapse

6. HTTP Request URL

6.1 Missing 
?

When we miss

?
before starting query parameters

HTTP/1.1 404 Not Found
X-Powered-By: Express
Content-Security-Policy: default-src 'none'
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=utf-8
Content-Length: 148
Date: Sun, 04 Apr 2021 11:50:56 GMT
Connection: close
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET http://localhost:3000/books/offset=2&limit=3&search_q=potter&order_by=price&order=DESC</pre>
</body>
</html
 
Collapse

6.2 Missing 
&
 between Query Parameters

When we miss

&
between query parameters

root@123:/.../...part-3/myapp# nodemon index.js
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
Server Running at http://localhost:3000/
string
(node:1408) UnhandledPromiseRejectionWarning: Error: SQLITE_ERROR: unrecognized token: "3offset"
(node:1408) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1408) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
 
Collapse

6.3 Replacing 
&
 with 
,
 in Query Parameters

When we replace

&
with
,
in Query Parameters

root@123:/.../...part-3/myapp# nodemon index.js
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
Server Running at http://localhost:3000/
string
(node:1452) UnhandledPromiseRejectionWarning: Error: SQLITE_ERROR: near ",": syntax error
(node:1452) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1452) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
 
Collapse

7. Accessing Unknown Database

When we are trying to access an unknown Database it will not show any error instead it creates a new database with the given name

root@123:/.../myapp# sqlite3 goodread.db
SQLite version 3.16.2 2017-01-06 16:32:41
Enter ".help" for usage hints.
sqlite>.tables
sqlite>

8. Accessing Wrong Port Number

When we are trying to access the wrong port number, we may get the below notification

The connection was rejected. Either the requested service isn’t running on the requested server/port, the proxy settings in vscode are misconfigured, or a firewall is blocking requests. Details: RequestError: connect ECONNREFUSED 127.0.0.1:4000.

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form