Currently, I am in the process of developing a newsletter subscription API using node.js and typescript. This project involves my first experience with typeorm and PostgreSQL. Following several tutorials, I configured typeorm and created the entity types as instructed.
Despite this, I have encountered a persistent error related to semicolons placed after each column/attribute within every entity. Surprisingly, the error originates from the compiled JavaScript code within the dist directory, rather than the original TypeScript source code.
Error Log
(node:14564) UnhandledPromiseRejectionWarning: FILEPATH\fatura-backend-newsletter\dist\entity\BaseEntityAttributes.js:12
createdAt;
^
SyntaxError: Unexpected token ;
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at C:\Users\Seif_A\Desktop\Fatura hackathon\fatura-backend-newsletter\node_modules\typeorm\util\DirectoryExportedClassesLoader.js:42:39
at Array.map (<anonymous>)
at Object.importClassesFromDirectories (C:\Users\Seif_A\Desktop\Fatura hackathon\fatura-backend-newsletter\node_modules\typeorm\util\DirectoryExportedClassesLoader.js:42:10)
(node:14564) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:14564) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
[nodemon] clean exit - waiting for changes before restart
ormconfig.json
{
"name": "mainConnect",
"type": "postgres",
"host": "localhost",
"port": "5432",
"username": "****",
"password": "****",
"database": "NewsletterDB",
"synchronize": true,
"logging": false,
"entities": [
"./dist/entity/*.js"
],
"migrations": [
"./dist/migration/*.js"
],
"subscribers": [
"src/subscriber/**/*.ts"
],
"cli": {
"entitiesDir": "src/entity", "migrationsDir": "src/migration"
}
}
src/entity/BaseEntityAttributes.ts
import { BaseEntity, CreateDateColumn, UpdateDateColumn } from "typeorm";
export abstract class BaseEntityAttributes extends BaseEntity {
@CreateDateColumn({
name: "created_at",
type: "datetime",
})
public createdAt!: number;
@UpdateDateColumn({
name: "updated_at",
type: "datetime",
})
public updatedAt!: number;
}
dist\entity\BaseEntityAttributes.js
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseEntityAttributes = void 0;
const typeorm_1 = require("typeorm");
class BaseEntityAttributes extends typeorm_1.BaseEntity {
createdAt; // <<< Error here
updatedAt; // <<< Also here
}
__decorate([
typeorm_1.CreateDateColumn({
name: "created_at",
type: "datetime",
})
], BaseEntityAttributes.prototype, "createdAt", void 0);
__decorate([
typeorm_1.UpdateDateColumn({
name: "updated_at",
type: "datetime",
})
], BaseEntityAttributes.prototype, "updatedAt", void 0);
exports.BaseEntityAttributes = BaseEntityAttributes;