I am getting below error when I’m start server npm start.
Error: try {
^
SyntaxError: Unexpected token ‘{‘
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1178:20)
at Module._compile (node:internal/modules/cjs/loader:1220:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47
Node.js v18.17.1
[nodemon] app crashed – waiting for file changes before starting…
Code:
index.js
const express = require('express');
const Razorpay = require( 'razorpay' );
const cors = require( "cors" );
require('dotenv').config();
const PORT = process.env.PORT;
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors());
app.post('/order', async (res, req) = {
try {
const razorpay = new Razorpay({
key_id: process.env.RAZORPAY_KEY_ID,
key_secret: process.env.RAZORPAY_KEY_SECRET,
});
const options = req.body;
const order = await razorpay.orders.create(options);
if(!order){
return res.status(500).send("Error");
}
res.json(order);
} catch (err) {
console.log(err);
res.status(500).send("Error");
}
});
app.listen(PORT, () =>{
console.log('Listening on the port',PORT);
});
Package.json:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"start": "nodemon index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"nodemon": "^3.1.0",
"razorpay": "^2.9.3"
}
}
I don’t know why this code is showing typescript error even it’s in javascript.
Can someone help on this.
Tried renaming the file but it’s not working.


