Search code examples
node.jsmongodbmongoosesyntax-errormern

Getting Syntax error Unexpected token '.' on executing npm run app command


I am novice in MERN app development was attending tutorial on the same. I have to create basic MERN based application to demonstrate the fundamental working of MERN backend with nodeJS, mongoose, mongoDB, express.

Following are the content of the project folder.

When I try executing npm run app I am getting following syntax error.

$ npm run app

> [email protected] app
> nodemon app.js

[nodemon] 3.0.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node app.js`
/home/harsh/Documents/GDSCMernClass/node_modules/mongodb/lib/error.js:714
        return RETRYABLE_WRITE_ERROR_CODES.has(error.result?.code ?? error.code ?? 0);
                                                            ^

SyntaxError: Unexpected token '.'
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/harsh/Documents/GDSCMernClass/node_modules/mongodb/lib/operations/add_user.js:5:17)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
[nodemon] app crashed - waiting for file changes before starting...

content of ./config/db.js

const mongoose = require('mongoose');
const config = require('config')
const db = config.get('mongoURI')

const connectDB = async () => {
    try {
        mongoose.set('strictQuery', true);
        await mongoose.connect(db, {
            useNewUrlParser: true,
        });

        console.log('MongoDB Connected');
    }
    catch (err){
        console.error(err.message);
        process.exit(1);
    }
};

module.exports = connectDB;

content of App.js

const express = require('express');
const connectDB = require('./config/db')

const app = express();

connectDB();

app.get('/', (req, res) => res.send("Hello world"))

const port = process.env.PORT || 8082

app.listen(port, () => console.log(`Server running on port ${port}`));

content of package.json

{
  "name": "gdscmernclass",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js",
    "app": "nodemon app.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/Harsh-raj/GDSCMernclass.git"
  },
  "author": "Harsh Raj",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/Harsh-raj/GDSCMernclass/issues"
  },
  "homepage": "https://github.com/Harsh-raj/GDSCMernclass#readme",
  "dependencies": {
    "body-parser": "^1.20.2",
    "config": "^3.3.9",
    "express": "^4.18.2",
    "mongoose": "^7.5.0",
    "n": "^9.1.0"
  },
  "devDependencies": {
    "nodemon": "^3.0.1"
  }
}

The ./config/default.json contains mongoURI which i correctly put the password.

Please Help me rectify this error. Much appreciated.

I was trying to run the Node App and everything was set fine but I am getting syntax error instead for the command npm run app


Solution

  • It looks like the error is caused by optional chaining being unsupported. This feature was supported from Node.js v14. You must have a really old version of Node running.

    For a whole host of reasons, this none the least, you need to upgrade your Node.js version.

    Run node -v to see which version you have on your system.