When I try to run my compiled typescript code I get a syntax error:
\entity\Config.ts:1
(function (exports, require, module, __filename, __dirname) { import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm";
^
SyntaxError: Unexpected token {
but when I run the typescript code with ts-node
and nodemon
code runs just fine.
So I've worked on some logging to figure out where the problem is occurring and it seems to happen when I hit createConnection()
method on TypeORM. I'm new to Typescript and the TypeORM library.
entity/config.ts
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm";
@Entity()
export class Config extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column()
app: String;
@Column()
endpoint: String;
@Column()
token: String;
}
server.ts
import { createConnection } from "typeorm";
// Database connected
createConnection()
.then(() => {
console.log("Test");
})
.catch(err => {
console.log(err);
});
index.ts
require("reflect-metadata");
require("dotenv/config");
require("./server");
package.json dependancies
"scripts": {
"dev:server": "ts-node src",
"dev": "nodemon -e ts -w src -x npm run dev:server",
"build:server": "tsc",
"start:server": "node build/index.js",
"start": "npm run build:server && npm run start:server"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"@types/axios": "^0.14.0",
"@types/graphql": "^14.0.3",
"@types/node": "^10.12.18",
"@types/winston": "^2.4.4",
"nodemon": "^1.18.9",
"ts-node": "^7.0.1",
"typescript": "^3.2.2"
},
"dependencies": {
"apollo-server-express": "^2.3.1",
"axios": "^0.18.0",
"dotenv": "^6.2.0",
"express": "^4.16.4",
"graphql": "^14.0.2",
"pg": "^7.7.1",
"reflect-metadata": "^0.1.12",
"sequelize": "^4.42.0",
"typeorm": "^0.2.9",
"winston": "^3.1.0"
}
}
So a member on the TypeORM Slack (going by uladzimir as of answering this question) solved the problem. The issue was with my ormconfig file.
Problem:
"entities": ["src/database/entity/**/*.ts", "build/database/entity/**/*.js"],
"migrations": [
"src/database/migration/**/*.ts",
"build/database/migration/**/*.js"
],
"subscribers": [
"src/database/subscriber/**/*.ts",
"build/database/subscriber/**/*.js"
],
"cli": {
"entitiesDir": "src/entity",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
}
Solution:
"entities": ["build/database/entity/**/*.js"],
"migrations": ["build/database/migration/**/*.js"],
"subscribers": ["build/database/subscriber/**/*.js"],
"cli": {
"entitiesDir": "src/entity",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
}
For some reason I thought I had to add the *ts
files for testing/developing purposes but that is not the case and what was causing the issue. I'm not exactly sure why it would be causing this problem but if I find out I'll post that as a comment to this answer.
Thank you everyone for the help on this issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With