i have created the file(index.js) which should connect to the Mongo db , The file code is below
but when when i am trying to run the file by typring node index.js it is giving me the error
C:UsersMOHAS076SBC-trp-web-test-automationnode_modulesmongodblibbson.js:10
catch { } // eslint-disable-line
^
SyntaxError: Unexpected token {
CODE –
// Module calling
MongoClient = require ('mongodb');
// Server path
const url = 'mongodb://10.142.206.22:27017/';
// Name of the database
const dbname = 'snapp_transactions';
MongoClient.connect (url, (err, client)=>{
if (!err) {
log.info ('successful connection with the server');
}
else
{ log.info ('Error in the connectivity'); }
});
The things what i have tried to resolve
- reinstalling my node module.
- reinstalling mongodb after deleting .
3 commenting the line const bson_1 = require("./bson"); which is present in the utils.js , but after that it is giving error ‘mongodb’ not found .
this is below my package.json ( where my "mongodb": "^4.1.0" is that due to compatibility issue , my node version is 8.12 as per the project requirement )
{
"name": "sbcwdwdlr-web-test-automation",
"version": "1.0.0",
"private": true,
"description": "This package contains an example of implementing GQE's Node.js test framework for mobile using Cucumber and Mocha.",
"keywords": [
"gqe",
"mobile",
"node",
"webdriverio"
],
"license": "UNLICENSED",
"author": "James T. Wood",
"main": "wdio.cucumber.conf",
"scripts": {
"build": "npm run clean:dist && npm run lint && npm run build:ts && npm run format",
"build:ts": "tsc -p .tsconfig.json",
"clean:dist": "rm -R ./dist || true",
"format": "prettier --write "dist/**/*.{js,json}" || true",
"lint": "npm run lint:js && npm run lint:ts && npm run lint:gherkin",
"lint:js": "eslint . || true",
"lint:ts": "tslint -c .tslint.json -p .tsconfig.json || true",
"lint:gherkin": "gherkin-lint --rulesdir "src/utilities/build/gherkin-rules" || true",
"test": "npm run test:all",
"test:all": "cross-env TS_NODE_PROJECT=.tsconfig.json mocha -r ts-node/register test/**/*.spec.ts"
},
"dependencies": {
"@gqe/cli": "*",
"@gqe/framework-core": "^2.0.30",
"@gqe/framework-cucumber": "^2.0.43",
"@gqe/framework-entities": "^2.0.44",
"@gqe/framework-services": "^2.0.44",
"@gqe/framework-utilities": "^2.0.28",
"@gqe/framework-web": "^2.0.30",
"@gqe_mslt/common": "git+ssh://[email protected]:MSLT/mslt-common.git#v1.4.3",
"@types/redis": "^2.8.25",
"deepmerge": "^2.2.1",
"dotenv": "^6.2.0",
"dotenv-json": "^1.0.0",
"inquirer": "^6.2.1",
"jimp": "^0.16.1",
"json-stringify-safe": "^5.0.1",
"lodash": "^4.17.20",
"loglevel": "^1.6.1",
"minimist": "^1.2.0",
"moment": "^2.23.0",
"mongodb": "^4.1.0",
"randomstring": "^1.1.5",
"redis": "^2.8.0",
"redis-cli": "^1.3.1",
"unique-names-generator": "^3.0.0",
"uuid": "^8.3.1",
"window": "^4.2.7",
"xml-js": "^1.6.11"
},
"devDependencies": {
"@types/chai": "^4.1.4",
"@types/deepmerge": "^2.1.0",
"@types/dotenv": "^4.0.3",
"@types/inquirer": "0.0.42",
"@types/lodash": "^4.14.119",
"@types/minimist": "^1.2.0",
"@types/mocha": "^5.2.4",
"@types/node": "^10.12.18",
"@types/prettier": "^1.15.2",
"chai": "^4.1.2",
"cross-env": "^7.0.2",
"eslint": "^5.10.0",
"gherkin-lint": "^2.13.2",
"mocha": "^5.2.0",
"node-fetch": "^2.6.1",
"node-key-sender": "^1.0.11",
"prettier": "1.13.7",
"reports": "^0.1.1",
"ts-node": "^7.0.0",
"tslint": "^5.12.0",
"tslint-eslint-rules": "^5.3.1",
"typescript": "^3.6.3",
"wdio-multiple-cucumber-html-reporter": "^1.1.1"
}
}
2
Answers
catch
syntax is wrong in your output error.its written like this.
Summary: You will need a newer version of nodejs to work with that version of mongodb or an older version of mongodb that is certified with your v8.12 version of nodejs.
Details/Explanation:
The offending Mongodb code appears to be this:
Using
catch
without the error as incatch(err)
is known as "optional catch binding". This was not originally a Javascript feature and was added in ES2019 and was originally supported in nodejs 10.That means your nodejs version 8.12 apparently does not support that Javascript construct. So, in other words, that version of Mongodb is not compatible with nodejs v8.x.
If you look in the
engines
section of mongodb’s package.json, you will see that it specifies:To use that version of mongodb, you will need a newer version of nodejs at least v12.9. You could possibly use an older version of mongodb that was compatible with node 8.x, but would need to do without any feature upgrades and/or bug fixes since the older version.
It probably goes without saying, but this is one of the hazards of having a project that specifies an older version of nodejs. Nodejs v8.0.0 was originally released 2017-05-30 and the last revision to it v8.17.0 was 2019-12-17 and it has now reached "end-of-life" per the nodejs project description.
Meanwhile, v14 is the current Long Term Support version. The Nodejs project page explains that you can expect critical bugs will be fixed in a "long-term-support" release for about 30 months from initial release. Both node v8 and v10 have already expired (reached end-of-life) and are not recommended for production applications. v12 is in maintenance until about May 2022 (when it will end-of-life). V14 is the active LTS version and the active LTS version will shift to v16 around the end of this year. So, version 8.x is "old" by nodejs standards. That’s why major modules (like mongodb) are leaving it behind so they can minimize the number of separate nodejs versions they have to test against and so they can advance to use modern features in newer versions of Javascript and nodejs.