skip to Main Content

ive been searching this for hours
getting series of errors and exceptions for even executing the hello world program for express in node js
error i have encountered include :

  1. SyntaxError: Invalid or unexpected token
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1153:20)
    at Module._compile (node:internal/modules/cjs/loader:1197:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
    at node:internal/main/run_main_module:23:47

  2. MODULE NOT FOUND [[enter image description here](https://phpout.com/wp-content/uploads/2023/07/q0Xfl.png)

i have tried deleting all the modules and reinstalling them and adding rules into my firewall and changing the port number of code at : app.listen(3000);

2

Answers


  1. Chosen as BEST ANSWER

    What helped me was to place the .js file that I was working with in a new folder, drag and drop that folder into VS Code (to open the directory directly in VS Code), open the terminal in VS Code, and then simply type node .js (or in my case node index.js).

    I had already installed node on my system, but for whatever reason, I was still getting the error that you've mentioned, even when I typed the direct path to the file i.e. node /desktop/index.js.

    So, creating a new folder on my desktop, placing the .js file inside that folder, opening that folder within VS Code, and then typing node index.js in the terminal solved my issue.


  2. i can’t figure out whats the real problema, but i usually use arrow functions as callback in the second parameter of the HTTP methods.

    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      res.send('Hello World!')
    })
    
    app.listen(port, () => {
      console.log(`Example app listening on port ${port}`)
    })
    

    I recommend you check your Node and Express versions too.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search