SyntaxError: Unexpected token ':' in typescript but working in run dev

Viewed 19

Recently, i'm trying to learning about typescript. I have a proble in this part

const connection = mysql.createConnection(config);
//CONNECT TO MYSQL
connection.connect((err: any) => {
    if (err) throw err;
        console.log("Connected!");
    }
);

When i run it with nodemon ./src/index.ts , it is working fine. There is no error, but when i run it with npm run postinstall, i get this error

connection.connect((err: any) => {
                       ^

SyntaxError: Unexpected token ':'

here is my package.json

{
  "name": "database-queue",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node ./dist/index.js",
    "build": "tsc",
    "dev": "nodemon ./src/index.ts",
    "postinstall": "npm run build && npm start"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/node-cron": "^3.0.4",
    "axios": "^0.27.2",
    "body-parser": "^1.20.0",
    "dotenv": "^16.0.2",
    "exceljs": "^4.3.0",
    "express": "^4.18.1",
    "log4js": "^6.6.1",
    "mysql": "^2.18.1",
    "node-cron": "^3.0.2",
    "pg": "^8.8.0",
    "read-excel-file": "^5.5.0",
    "socket.io": "^4.5.2"
  },
  "devDependencies": {
    "@types/express": "^4.17.13",
    "@types/mysql": "^2.15.21",
    "@types/node": "^18.7.14",
    "nodemon": "^2.0.19",
    "typescript": "^4.8.3"
  },
  "engines": {
    "node": "16.16.0"
  }
}

and here is my tsconfig.json

{
  "compileOnSave": true,
  "compilerOptions": {
    "strict": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "noLib": false,
    "outDir": "./dist",
    "removeComments": false,
    "sourceMap": true,
    // "noUnusedLocals": true,
    "target": "es5",
    "typeRoots": ["node_modules/@types"],
    "baseUrl": "./",
    "esModuleInterop":true,

    "paths": {
      "*": ["src/types/*"]
    }
  },
  "include": ["src"],
  "exclude": ["build", "jest", "node_modules", "scripts", "webpack", "types"],
  "filesGlob": ["./**/*.ts", "!./build/**/*.ts", "!./node_modules/**/*.ts"],
  "lib": ["es5"]
}

How can i fix it ?

1 Answers
Related