0

I get an "Uncaught SyntaxError: Unexpected token ." message when I try to use my express app in debug mode in VSCode (F5) if my code includes optional chaining (?.). I do not have this problem when I use the app with the command "npm start" which is working properly. I do not have experience on building projects with very specific configuration (compilers, lighters...), this one was created just with npm init.

Below you can see the details that I consider useful for solving the issue. Let me know if you need anything else. Is there anyway I can debug the app if it includes optional chaining syntax? Should I update any version? Should I change any config file?

Details:
node v16.10.0
npm v7.24.0

package.json

  "main": "server/server.js",
  "scripts": {
    "start": "nodemon -e js,json,html,yml,css server/server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
  },
  "license": "ISC",
  "dependencies": {
    "@marsaud/smb2": "^0.17.2",
    "bcrypt": "^5.0.1",
    "body-parser": "^1.19.0",
    "compression": "^1.7.4",
    "cookie-parser": "^1.4.5",
    "cors": "^2.8.5",
    "equals": "^1.0.5",
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "moment": "^2.24.0",
    "mongodb": "^3.6.5",
    "mssql": "^7.2.1",
    "nodemailer": "^6.4.10",
    "nodemon": "^2.0.7",
    "redoc": "^2.0.0-rc.24",
    "swagger-jsdoc": "^4.0.0",
    "tedious": "^14.0.0",
    "underscore": "^1.9.1"
  }
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Iniciar el programa",

            "program": "${workspaceFolder}/backEnd/server/server.js"
          }
    ],
    
}
Balaclava
  • 109
  • 2
  • 7

3 Answers3

0

Did you try to switch to different node version? I use node v14 and don't have the problem, but I read on other websites mentioned that they are using v16 and also have problem with optional chaining.

GoldenArcher
  • 812
  • 1
  • 9
  • 12
  • I tried with node v14 and it does not work. The same behavior as before, it works with npm start (which executes the main file with nodemon). But when I try it in debug mode, it crashes automatically as soon as it finds a optional chaining (?.) – Balaclava Jan 19 '22 at 07:55
0

I had this same issue and downgrading npm to version 6.14.16 is what worked for me. Another work around that I was running while searching for a solution was to remove nodemon from the package.json scripts commands and replacing them with just node

"scripts": {
    "start": "node js,json,html,yml,css server/server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
},

and running

nodemon --exec npm run start

Not sure what the exact issue is though, but it seems to be related to running the nodemon command from within the scripts in package.json file. I was able to verify this by simply running

npm run start

with the package.json changes mentioned above perfectly fine, but once it was changed to run with nodemon, I run into issues with optional chaining.

0

I finally found the solution, I was not using the right debugger so it was mainly a Visual Studio Code wrong configuration setup issue. I recently installed some other debuggers so I had to set the node.js debugger again.

Balaclava
  • 109
  • 2
  • 7